forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_session.go
389 lines (321 loc) · 11.1 KB
/
in_session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package quickfix
import (
"bytes"
"time"
"github.com/lldld/quickfix/internal"
)
type inSession struct{ loggedOn }
func (state inSession) String() string { return "In Session" }
func (state inSession) FixMsgIn(session *session, msg *Message) sessionState {
msgType, err := msg.Header.GetBytes(tagMsgType)
if err != nil {
return handleStateError(session, err)
}
switch {
case bytes.Equal(msgTypeLogon, msgType):
if err := session.handleLogon(msg); err != nil {
if err := session.initiateLogoutInReplyTo("", msg); err != nil {
return handleStateError(session, err)
}
return logoutState{}
}
return state
case bytes.Equal(msgTypeLogout, msgType):
return state.handleLogout(session, msg)
case bytes.Equal(msgTypeResendRequest, msgType):
return state.handleResendRequest(session, msg)
case bytes.Equal(msgTypeSequenceReset, msgType):
return state.handleSequenceReset(session, msg)
case bytes.Equal(msgTypeTestRequest, msgType):
return state.handleTestRequest(session, msg)
default:
if err := session.verify(msg); err != nil {
return state.processReject(session, msg, err)
}
}
if err := session.store.IncrNextTargetMsgSeqNum(); err != nil {
return handleStateError(session, err)
}
return state
}
func (state inSession) Timeout(session *session, event internal.Event) (nextState sessionState) {
switch event {
case internal.NeedHeartbeat:
heartBt := NewMessage()
heartBt.Header.SetField(tagMsgType, FIXString("0"))
if err := session.send(heartBt); err != nil {
return handleStateError(session, err)
}
case internal.PeerTimeout:
testReq := NewMessage()
testReq.Header.SetField(tagMsgType, FIXString("1"))
testReq.Body.SetField(tagTestReqID, FIXString("TEST"))
if err := session.send(testReq); err != nil {
return handleStateError(session, err)
}
session.log.OnEvent("Sent test request TEST")
session.peerTimer.Reset(time.Duration(float64(1.2) * float64(session.HeartBtInt)))
return pendingTimeout{state}
}
return state
}
func (state inSession) handleLogout(session *session, msg *Message) (nextState sessionState) {
if err := session.verifySelect(msg, false, false); err != nil {
return state.processReject(session, msg, err)
}
if session.IsLoggedOn() {
session.log.OnEvent("Received logout request")
session.log.OnEvent("Sending logout response")
if err := session.sendLogoutInReplyTo("", msg); err != nil {
session.logError(err)
}
} else {
session.log.OnEvent("Received logout response")
}
if err := session.store.IncrNextTargetMsgSeqNum(); err != nil {
session.logError(err)
}
if session.ResetOnLogout {
if err := session.dropAndReset(); err != nil {
session.logError(err)
}
}
return latentState{}
}
func (state inSession) handleTestRequest(session *session, msg *Message) (nextState sessionState) {
if err := session.verify(msg); err != nil {
return state.processReject(session, msg, err)
}
var testReq FIXString
if err := msg.Body.GetField(tagTestReqID, &testReq); err != nil {
session.log.OnEvent("Test Request with no testRequestID")
} else {
heartBt := NewMessage()
heartBt.Header.SetField(tagMsgType, FIXString("0"))
heartBt.Body.SetField(tagTestReqID, testReq)
if err := session.sendInReplyTo(heartBt, msg); err != nil {
return handleStateError(session, err)
}
}
if err := session.store.IncrNextTargetMsgSeqNum(); err != nil {
return handleStateError(session, err)
}
return state
}
func (state inSession) handleSequenceReset(session *session, msg *Message) (nextState sessionState) {
var gapFillFlag FIXBoolean
if msg.Body.Has(tagGapFillFlag) {
if err := msg.Body.GetField(tagGapFillFlag, &gapFillFlag); err != nil {
return state.processReject(session, msg, err)
}
}
if err := session.verifySelect(msg, bool(gapFillFlag), bool(gapFillFlag)); err != nil {
return state.processReject(session, msg, err)
}
var newSeqNo FIXInt
if err := msg.Body.GetField(tagNewSeqNo, &newSeqNo); err == nil {
expectedSeqNum := FIXInt(session.store.NextTargetMsgSeqNum())
session.log.OnEventf("Received SequenceReset FROM: %v TO: %v", expectedSeqNum, newSeqNo)
switch {
case newSeqNo > expectedSeqNum:
if err := session.store.SetNextTargetMsgSeqNum(int(newSeqNo)); err != nil {
return handleStateError(session, err)
}
case newSeqNo < expectedSeqNum:
//FIXME: to be compliant with legacy tests, do not include tag in reftagid? (11c_NewSeqNoLess)
if err := session.doReject(msg, valueIsIncorrectNoTag()); err != nil {
return handleStateError(session, err)
}
}
}
return state
}
func (state inSession) handleResendRequest(session *session, msg *Message) (nextState sessionState) {
if err := session.verifyIgnoreSeqNumTooHighOrLow(msg); err != nil {
return state.processReject(session, msg, err)
}
var err error
var beginSeqNoField FIXInt
if err = msg.Body.GetField(tagBeginSeqNo, &beginSeqNoField); err != nil {
return state.processReject(session, msg, RequiredTagMissing(tagBeginSeqNo))
}
beginSeqNo := beginSeqNoField
var endSeqNoField FIXInt
if err = msg.Body.GetField(tagEndSeqNo, &endSeqNoField); err != nil {
return state.processReject(session, msg, RequiredTagMissing(tagEndSeqNo))
}
endSeqNo := int(endSeqNoField)
session.log.OnEventf("Received ResendRequest FROM: %d TO: %d", beginSeqNo, endSeqNo)
expectedSeqNum := session.store.NextSenderMsgSeqNum()
if (session.sessionID.BeginString >= BeginStringFIX42 && endSeqNo == 0) ||
(session.sessionID.BeginString <= BeginStringFIX42 && endSeqNo == 999999) ||
(endSeqNo >= expectedSeqNum) {
endSeqNo = expectedSeqNum - 1
}
if err := state.resendMessages(session, int(beginSeqNo), endSeqNo, *msg); err != nil {
return handleStateError(session, err)
}
if err := session.checkTargetTooLow(msg); err != nil {
return state
}
if err := session.checkTargetTooHigh(msg); err != nil {
return state
}
if err := session.store.IncrNextTargetMsgSeqNum(); err != nil {
return handleStateError(session, err)
}
return state
}
func (state inSession) resendMessages(session *session, beginSeqNo, endSeqNo int, inReplyTo Message) (err error) {
if session.DisableMessagePersist {
err = state.generateSequenceReset(session, beginSeqNo, endSeqNo+1, inReplyTo)
return
}
msgs, err := session.store.GetMessages(beginSeqNo, endSeqNo)
if err != nil {
session.log.OnEventf("error retrieving messages from store: %s", err.Error())
return
}
seqNum := beginSeqNo
nextSeqNum := seqNum
msg := NewMessage()
for _, msgBytes := range msgs {
_ = ParseMessageWithDataDictionary(msg, bytes.NewBuffer(msgBytes), session.transportDataDictionary, session.appDataDictionary)
msgType, _ := msg.Header.GetBytes(tagMsgType)
sentMessageSeqNum, _ := msg.Header.GetInt(tagMsgSeqNum)
if isAdminMessageType(msgType) {
nextSeqNum = sentMessageSeqNum + 1
continue
}
if !session.resend(msg) {
nextSeqNum = sentMessageSeqNum + 1
continue
}
if seqNum != sentMessageSeqNum {
if err = state.generateSequenceReset(session, seqNum, sentMessageSeqNum, inReplyTo); err != nil {
return err
}
}
session.log.OnEventf("Resending Message: %v", sentMessageSeqNum)
msgBytes = msg.build()
session.sendBytes(msgBytes)
seqNum = sentMessageSeqNum + 1
nextSeqNum = seqNum
}
if seqNum != nextSeqNum { // gapfill for catch-up
if err = state.generateSequenceReset(session, seqNum, nextSeqNum, inReplyTo); err != nil {
return err
}
}
return
}
func (state inSession) processReject(session *session, msg *Message, rej MessageRejectError) sessionState {
switch TypedError := rej.(type) {
case targetTooHigh:
var nextState resendState
switch currentState := session.State.(type) {
case resendState:
//assumes target too high reject already sent
nextState = currentState
default:
var err error
if nextState, err = session.doTargetTooHigh(TypedError); err != nil {
return handleStateError(session, err)
}
}
if nextState.messageStash == nil {
nextState.messageStash = make(map[int]*Message)
}
nextState.messageStash[TypedError.ReceivedTarget] = msg
//do not reclaim stashed message
msg.keepMessage = true
return nextState
case targetTooLow:
return state.doTargetTooLow(session, msg, TypedError)
case incorrectBeginString:
if err := session.initiateLogout(rej.Error()); err != nil {
return handleStateError(session, err)
}
return logoutState{}
}
switch rej.RejectReason() {
case rejectReasonCompIDProblem, rejectReasonSendingTimeAccuracyProblem:
if err := session.doReject(msg, rej); err != nil {
return handleStateError(session, err)
}
if err := session.initiateLogout(""); err != nil {
return handleStateError(session, err)
}
return logoutState{}
default:
if err := session.doReject(msg, rej); err != nil {
return handleStateError(session, err)
}
if err := session.store.IncrNextTargetMsgSeqNum(); err != nil {
return handleStateError(session, err)
}
return state
}
}
func (state inSession) doTargetTooLow(session *session, msg *Message, rej targetTooLow) (nextState sessionState) {
var posDupFlag FIXBoolean
if msg.Header.Has(tagPossDupFlag) {
if err := msg.Header.GetField(tagPossDupFlag, &posDupFlag); err != nil {
if rejErr := session.doReject(msg, err); rejErr != nil {
return handleStateError(session, rejErr)
}
return state
}
}
if !posDupFlag.Bool() {
if err := session.initiateLogout(rej.Error()); err != nil {
return handleStateError(session, err)
}
return logoutState{}
}
if !msg.Header.Has(tagOrigSendingTime) {
if err := session.doReject(msg, RequiredTagMissing(tagOrigSendingTime)); err != nil {
return handleStateError(session, err)
}
return state
}
var origSendingTime FIXUTCTimestamp
if err := msg.Header.GetField(tagOrigSendingTime, &origSendingTime); err != nil {
if rejErr := session.doReject(msg, err); rejErr != nil {
return handleStateError(session, rejErr)
}
return state
}
sendingTime := new(FIXUTCTimestamp)
if err := msg.Header.GetField(tagSendingTime, sendingTime); err != nil {
return state.processReject(session, msg, err)
}
if sendingTime.Before(origSendingTime.Time) {
if err := session.doReject(msg, sendingTimeAccuracyProblem()); err != nil {
return handleStateError(session, err)
}
if err := session.initiateLogout(""); err != nil {
return handleStateError(session, err)
}
return logoutState{}
}
return state
}
func (state *inSession) generateSequenceReset(session *session, beginSeqNo int, endSeqNo int, inReplyTo Message) (err error) {
sequenceReset := NewMessage()
session.fillDefaultHeader(sequenceReset, &inReplyTo)
sequenceReset.Header.SetField(tagMsgType, FIXString("4"))
sequenceReset.Header.SetField(tagMsgSeqNum, FIXInt(beginSeqNo))
sequenceReset.Header.SetField(tagPossDupFlag, FIXBoolean(true))
sequenceReset.Body.SetField(tagNewSeqNo, FIXInt(endSeqNo))
sequenceReset.Body.SetField(tagGapFillFlag, FIXBoolean(true))
var origSendingTime FIXString
if err := sequenceReset.Header.GetField(tagSendingTime, &origSendingTime); err == nil {
sequenceReset.Header.SetField(tagOrigSendingTime, origSendingTime)
}
session.application.ToAdmin(sequenceReset, session.sessionID)
msgBytes := sequenceReset.build()
session.sendBytes(msgBytes)
session.log.OnEventf("Sent SequenceReset TO: %v", endSeqNo)
return
}