forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
message.go
365 lines (292 loc) · 8.6 KB
/
message.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
package quickfix
import (
"bytes"
"fmt"
"math"
"time"
"github.com/alpacahq/quickfix/datadictionary"
)
// Header is first section of a FIX Message
type Header struct{ FieldMap }
// in the message header, the first 3 tags in the message header must be 8,9,35
func headerFieldOrdering(i, j Tag) bool {
var ordering = func(t Tag) uint32 {
switch t {
case tagBeginString:
return 1
case tagBodyLength:
return 2
case tagMsgType:
return 3
}
return math.MaxUint32
}
orderi := ordering(i)
orderj := ordering(j)
switch {
case orderi < orderj:
return true
case orderi > orderj:
return false
}
return i < j
}
// Init initializes the Header instance
func (h *Header) Init() {
h.initWithOrdering(headerFieldOrdering)
}
// Body is the primary application section of a FIX message
type Body struct{ FieldMap }
// Init initializes the FIX message
func (b *Body) Init() {
b.init()
}
// Trailer is the last section of a FIX message
type Trailer struct{ FieldMap }
// In the trailer, CheckSum (tag 10) must be last
func trailerFieldOrdering(i, j Tag) bool {
switch {
case i == tagCheckSum:
return false
case j == tagCheckSum:
return true
}
return i < j
}
// Init initializes the FIX message
func (t *Trailer) Init() {
t.initWithOrdering(trailerFieldOrdering)
}
// Message is a FIX Message abstraction.
type Message struct {
Header Header
Trailer Trailer
Body Body
//ReceiveTime is the time that this message was read from the socket connection
ReceiveTime time.Time
rawMessage *bytes.Buffer
//slice of Bytes corresponding to the message body
bodyBytes []byte
//field bytes as they appear in the raw message
fields []TagValue
//flag is true if this message should not be returned to pool after use
keepMessage bool
}
// ToMessage returns the message itself
func (m *Message) ToMessage() *Message { return m }
// parseError is returned when bytes cannot be parsed as a FIX message.
type parseError struct {
OrigError string
}
func (e parseError) Error() string { return fmt.Sprintf("error parsing message: %s", e.OrigError) }
// NewMessage returns a newly initialized Message instance
func NewMessage() *Message {
m := new(Message)
m.Header.Init()
m.Body.Init()
m.Trailer.Init()
return m
}
// ParseMessage constructs a Message from a byte slice wrapping a FIX message.
func ParseMessage(msg *Message, rawMessage *bytes.Buffer) (err error) {
return ParseMessageWithDataDictionary(msg, rawMessage, nil, nil)
}
// ParseMessageWithDataDictionary constructs a Message from a byte slice wrapping a FIX message using an optional session and application DataDictionary for reference.
func ParseMessageWithDataDictionary(
msg *Message,
rawMessage *bytes.Buffer,
transportDataDictionary *datadictionary.DataDictionary,
applicationDataDictionary *datadictionary.DataDictionary,
) (err error) {
msg.Header.Clear()
msg.Body.Clear()
msg.Trailer.Clear()
msg.rawMessage = rawMessage
rawBytes := rawMessage.Bytes()
//allocate fields in one chunk
fieldCount := 0
for _, b := range rawBytes {
if b == '\001' {
fieldCount++
}
}
if fieldCount == 0 {
return parseError{OrigError: fmt.Sprintf("No Fields detected in %s", string(rawBytes))}
}
if cap(msg.fields) < fieldCount {
msg.fields = make([]TagValue, fieldCount)
} else {
msg.fields = msg.fields[0:fieldCount]
}
fieldIndex := 0
//message must start with begin string, body length, msg type
if rawBytes, err = extractSpecificField(&msg.fields[fieldIndex], tagBeginString, rawBytes); err != nil {
return
}
msg.Header.add(msg.fields[fieldIndex : fieldIndex+1])
fieldIndex++
parsedFieldBytes := &msg.fields[fieldIndex]
if rawBytes, err = extractSpecificField(parsedFieldBytes, tagBodyLength, rawBytes); err != nil {
return
}
msg.Header.add(msg.fields[fieldIndex : fieldIndex+1])
fieldIndex++
parsedFieldBytes = &msg.fields[fieldIndex]
if rawBytes, err = extractSpecificField(parsedFieldBytes, tagMsgType, rawBytes); err != nil {
return
}
msg.Header.add(msg.fields[fieldIndex : fieldIndex+1])
fieldIndex++
trailerBytes := []byte{}
foundBody := false
for {
parsedFieldBytes = &msg.fields[fieldIndex]
rawBytes, err = extractField(parsedFieldBytes, rawBytes)
if err != nil {
return
}
switch {
case isHeaderField(parsedFieldBytes.tag, transportDataDictionary):
msg.Header.add(msg.fields[fieldIndex : fieldIndex+1])
case isTrailerField(parsedFieldBytes.tag, transportDataDictionary):
msg.Trailer.add(msg.fields[fieldIndex : fieldIndex+1])
default:
foundBody = true
trailerBytes = rawBytes
msg.Body.add(msg.fields[fieldIndex : fieldIndex+1])
}
if parsedFieldBytes.tag == tagCheckSum {
break
}
if !foundBody {
msg.bodyBytes = rawBytes
}
fieldIndex++
}
//body length would only be larger than trailer if fields out of order
if len(msg.bodyBytes) > len(trailerBytes) {
msg.bodyBytes = msg.bodyBytes[:len(msg.bodyBytes)-len(trailerBytes)]
}
length := 0
for _, field := range msg.fields {
switch field.tag {
case tagBeginString, tagBodyLength, tagCheckSum: //tags do not contribute to length
default:
length += field.length()
}
}
bodyLength, err := msg.Header.GetInt(tagBodyLength)
if err != nil {
err = parseError{OrigError: err.Error()}
} else if length != bodyLength {
err = parseError{OrigError: fmt.Sprintf("Incorrect Message Length, expected %d, got %d", bodyLength, length)}
}
return
}
func isHeaderField(tag Tag, dataDict *datadictionary.DataDictionary) bool {
if tag.IsHeader() {
return true
}
if dataDict == nil {
return false
}
_, ok := dataDict.Header.Fields[int(tag)]
return ok
}
func isTrailerField(tag Tag, dataDict *datadictionary.DataDictionary) bool {
if tag.IsTrailer() {
return true
}
if dataDict == nil {
return false
}
_, ok := dataDict.Trailer.Fields[int(tag)]
return ok
}
// MsgType returns MsgType (tag 35) field's value
func (m *Message) MsgType() (string, MessageRejectError) {
return m.Header.GetString(tagMsgType)
}
// IsMsgTypeOf returns true if the Header contains MsgType (tag 35) field and its value is the specified one.
func (m *Message) IsMsgTypeOf(msgType string) bool {
if v, err := m.MsgType(); err == nil {
return v == msgType
}
return false
}
// reverseRoute returns a message builder with routing header fields initialized as the reverse of this message.
func (m *Message) reverseRoute() *Message {
reverseMsg := NewMessage()
copy := func(src Tag, dest Tag) {
var field FIXString
if m.Header.GetField(src, &field) == nil {
if len(field) != 0 {
reverseMsg.Header.SetField(dest, field)
}
}
}
copy(tagSenderCompID, tagTargetCompID)
copy(tagSenderSubID, tagTargetSubID)
copy(tagSenderLocationID, tagTargetLocationID)
copy(tagTargetCompID, tagSenderCompID)
copy(tagTargetSubID, tagSenderSubID)
copy(tagTargetLocationID, tagSenderLocationID)
copy(tagOnBehalfOfCompID, tagDeliverToCompID)
copy(tagOnBehalfOfSubID, tagDeliverToSubID)
copy(tagDeliverToCompID, tagOnBehalfOfCompID)
copy(tagDeliverToSubID, tagOnBehalfOfSubID)
//tags added in 4.1
var beginString FIXString
if m.Header.GetField(tagBeginString, &beginString) == nil {
if string(beginString) != BeginStringFIX40 {
copy(tagOnBehalfOfLocationID, tagDeliverToLocationID)
copy(tagDeliverToLocationID, tagOnBehalfOfLocationID)
}
}
return reverseMsg
}
func extractSpecificField(field *TagValue, expectedTag Tag, buffer []byte) (remBuffer []byte, err error) {
remBuffer, err = extractField(field, buffer)
switch {
case err != nil:
return
case field.tag != expectedTag:
err = parseError{OrigError: fmt.Sprintf("extractSpecificField: Fields out of order, expected %d, got %d", expectedTag, field.tag)}
return
}
return
}
func extractField(parsedFieldBytes *TagValue, buffer []byte) (remBytes []byte, err error) {
endIndex := bytes.IndexByte(buffer, '\001')
if endIndex == -1 {
err = parseError{OrigError: "extractField: No Trailing Delim in " + string(buffer)}
remBytes = buffer
return
}
err = parsedFieldBytes.parse(buffer[:endIndex+1])
return buffer[(endIndex + 1):], err
}
func (m *Message) String() string {
if m.rawMessage != nil {
return m.rawMessage.String()
}
return string(m.build())
}
func formatCheckSum(value int) string {
return fmt.Sprintf("%03d", value)
}
// Build constructs a []byte from a Message instance
func (m *Message) build() []byte {
m.cook()
var b bytes.Buffer
m.Header.write(&b)
m.Body.write(&b)
m.Trailer.write(&b)
return b.Bytes()
}
func (m *Message) cook() {
bodyLength := m.Header.length() + m.Body.length() + m.Trailer.length()
m.Header.SetInt(tagBodyLength, bodyLength)
checkSum := (m.Header.total() + m.Body.total() + m.Trailer.total()) % 256
m.Trailer.SetString(tagCheckSum, formatCheckSum(checkSum))
}