-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
115 lines (87 loc) · 1.86 KB
/
protocol.go
File metadata and controls
115 lines (87 loc) · 1.86 KB
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
package protocol
import (
"encoding/binary"
"math"
)
type (
// OrderRequest ...
OrderRequest struct {
ClientID uint32 // 4
ID uint32 // 4
ReqType uint8 // 1
OrderKind uint8 // 1
Volume float64 // 8
Instrument string // variadic
}
// OrderResponse ...
OrderResponse struct {
ID uint32 // 4
Code uint16 // 2
}
)
var (
bo = binary.LittleEndian
reqFixLen = 18
resFixLen = 6
)
// EncodeOrderRequest ...
func EncodeOrderRequest(req OrderRequest) []byte {
res := make([]byte, reqFixLen+len(req.Instrument))
c := 0
bo.PutUint32(res[c:c+4], req.ClientID)
c += 4
bo.PutUint32(res[c:c+4], req.ID)
c += 4
res[c] = req.ReqType
c++
res[c] = req.OrderKind
c++
PutFloat64(res[c:c+8], req.Volume)
c += 8
copy(res[c:], []byte(req.Instrument))
return res
}
// DecodeOrderRequest decodes request
func DecodeOrderRequest(body []byte) OrderRequest {
res := OrderRequest{}
c := 0
res.ClientID = bo.Uint32(body[c : c+4])
c += 4
res.ID = bo.Uint32(body[c : c+4])
c += 4
res.ReqType = body[c]
c++
res.OrderKind = body[c]
c++
res.Volume = Float64FromBytes(body[c : c+8])
c += 8
res.Instrument = string(body[c:])
return res
}
// DecodeOrderResponse decodes request
func DecodeOrderResponse(body []byte) OrderResponse {
res := OrderResponse{}
c := 0
res.ID = bo.Uint32(body[c : c+4])
c += 4
res.Code = bo.Uint16(body[c:])
return res
}
func EncodeOrderResponse(resp OrderResponse) []byte {
res := make([]byte, resFixLen, resFixLen)
c := 0
bo.PutUint32(res[c:c+4], resp.ID)
c += 4
bo.PutUint16(res[c:c+2], resp.Code)
c += 2
return res
}
func Float64FromBytes(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}
func PutFloat64(bytes []byte, float float64) {
bits := math.Float64bits(float)
binary.LittleEndian.PutUint64(bytes, bits)
}