forked from cryptogarageinc/quickfix-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_session.go
293 lines (239 loc) · 8.7 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
package quickfix
import (
"time"
"github.com/quickfixgo/quickfix/enum"
)
type inSession struct {
}
func (state inSession) String() string { return "In Session" }
func (state inSession) IsLoggedOn() bool { return true }
func (state inSession) FixMsgIn(session *session, msg Message) (nextState sessionState) {
var msgType FIXString
if err := msg.Header.GetField(tagMsgType, &msgType); err == nil {
switch string(msgType) {
//logon
case "A":
return state.handleLogon(session, msg)
//logout
case "5":
return state.handleLogout(session, msg)
//test request
case "1":
return state.handleTestRequest(session, msg)
//resend request
case "2":
return state.handleResendRequest(session, msg)
//sequence reset
case "4":
return state.handleSequenceReset(session, msg)
default:
if err := session.verify(msg); err != nil {
return state.processReject(session, msg, err)
}
}
}
session.store.IncrNextTargetMsgSeqNum()
return state
}
func (state inSession) Timeout(session *session, event event) (nextState sessionState) {
switch event {
case needHeartbeat:
heartBt := NewMessage()
heartBt.Header.SetField(tagMsgType, FIXString("0"))
session.send(heartBt)
case peerTimeout:
testReq := NewMessage()
testReq.Header.SetField(tagMsgType, FIXString("1"))
testReq.Body.SetField(tagTestReqID, FIXString("TEST"))
session.send(testReq)
session.peerTimer.Reset(time.Duration(int64(1.2 * float64(session.heartBeatTimeout))))
return pendingTimeout{}
}
return state
}
func (state inSession) handleLogon(session *session, msg Message) (nextState sessionState) {
if err := session.handleLogon(msg); err != nil {
return state.initiateLogout(session, "")
}
return state
}
func (state inSession) handleLogout(session *session, msg Message) (nextState sessionState) {
session.log.OnEvent("Received logout request")
state.generateLogout(session)
session.application.OnLogout(session.sessionID)
return latentState{}
}
func (state inSession) handleSequenceReset(session *session, msg Message) (nextState sessionState) {
var gapFillFlag FIXBoolean
msg.Body.GetField(tagGapFillFlag, &gapFillFlag)
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:
session.store.SetNextTargetMsgSeqNum(int(newSeqNo))
case newSeqNo < expectedSeqNum:
//FIXME: to be compliant with legacy tests, do not include tag in reftagid? (11c_NewSeqNoLess)
session.doReject(msg, valueIsIncorrectNoTag())
}
}
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.NextTargetMsgSeqNum()
if (session.sessionID.BeginString >= enum.BeginStringFIX42 && endSeqNo == 0) ||
(session.sessionID.BeginString <= enum.BeginStringFIX42 && endSeqNo == 999999) ||
(endSeqNo >= expectedSeqNum) {
endSeqNo = expectedSeqNum - 1
}
state.resendMessages(session, int(beginSeqNo), endSeqNo)
session.store.IncrNextTargetMsgSeqNum()
return state
}
func (state inSession) resendMessages(session *session, beginSeqNo, endSeqNo int) {
msgs, err := session.store.GetMessages(beginSeqNo, endSeqNo)
if err != nil {
session.log.OnEventf("error retrieving messages from store: %s", err.Error())
panic(err)
}
seqNum := beginSeqNo
nextSeqNum := seqNum
for _, msgBytes := range msgs {
msg, _ := ParseMessage(msgBytes)
msgType, _ := msg.Header.GetString(tagMsgType)
sentMessageSeqNum, _ := msg.Header.GetInt(tagMsgSeqNum)
if isAdminMessageType(msgType) {
nextSeqNum = sentMessageSeqNum + 1
continue
}
if seqNum != sentMessageSeqNum {
state.generateSequenceReset(session, seqNum, sentMessageSeqNum)
}
session.resend(msg)
seqNum = sentMessageSeqNum + 1
nextSeqNum = seqNum
}
if seqNum != nextSeqNum { // gapfill for catch-up
state.generateSequenceReset(session, seqNum, nextSeqNum)
}
}
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)
session.send(heartBt)
}
session.store.IncrNextTargetMsgSeqNum()
return state
}
func (state inSession) processReject(session *session, msg Message, rej MessageRejectError) (nextState sessionState) {
switch TypedError := rej.(type) {
case targetTooHigh:
switch session.sessionState.(type) {
default:
session.doTargetTooHigh(TypedError)
case resendState:
//assumes target too high reject already sent
}
session.messageStash[TypedError.ReceivedTarget] = msg
return resendState{}
case targetTooLow:
return state.doTargetTooLow(session, msg, TypedError)
case incorrectBeginString:
return state.initiateLogout(session, rej.Error())
}
switch rej.RejectReason() {
case rejectReasonCompIDProblem, rejectReasonSendingTimeAccuracyProblem:
session.doReject(msg, rej)
return state.initiateLogout(session, "")
default:
session.doReject(msg, rej)
session.store.IncrNextTargetMsgSeqNum()
return state
}
}
func (state inSession) doTargetTooLow(session *session, msg Message, rej targetTooLow) (nextState sessionState) {
var posDupFlag FIXBoolean
if err := msg.Header.GetField(tagPossDupFlag, &posDupFlag); err == nil && posDupFlag {
origSendingTime := new(FIXUTCTimestamp)
if err = msg.Header.GetField(tagOrigSendingTime, origSendingTime); err != nil {
session.doReject(msg, RequiredTagMissing(tagOrigSendingTime))
return state
}
sendingTime := new(FIXUTCTimestamp)
msg.Header.GetField(tagSendingTime, sendingTime)
if sendingTime.Value.Before(origSendingTime.Value) {
session.doReject(msg, sendingTimeAccuracyProblem())
return state.initiateLogout(session, "")
}
if appReject := session.fromCallback(msg); appReject != nil {
session.doReject(msg, appReject)
return state.initiateLogout(session, "")
}
} else {
return state.initiateLogout(session, rej.Error())
}
return state
}
func (state *inSession) initiateLogout(session *session, reason string) (nextState logoutState) {
state.generateLogoutWithReason(session, reason)
time.AfterFunc(time.Duration(2)*time.Second, func() { session.sessionEvent <- logoutTimeout })
return
}
func (state *inSession) generateSequenceReset(session *session, beginSeqNo int, endSeqNo int) {
sequenceReset := NewMessage()
session.fillDefaultHeader(sequenceReset)
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)
}
//FIXME error check?
msgBytes, _ := sequenceReset.Build()
session.sendBytes(msgBytes)
}
func (state *inSession) generateLogout(session *session) {
state.generateLogoutWithReason(session, "")
}
func (state *inSession) generateLogoutWithReason(session *session, reason string) {
reply := NewMessage()
reply.Header.SetField(tagMsgType, FIXString("5"))
reply.Header.SetField(tagBeginString, FIXString(session.sessionID.BeginString))
reply.Header.SetField(tagTargetCompID, FIXString(session.sessionID.TargetCompID))
reply.Header.SetField(tagSenderCompID, FIXString(session.sessionID.SenderCompID))
if reason != "" {
reply.Body.SetField(tagText, FIXString(reason))
}
session.send(reply)
session.log.OnEvent("Sending logout response")
}