8 "github.com/jhalter/mobius/concat"
21 tranSendInstantMsg = 108
22 tranShowAgreement = 109
23 tranDisconnectUser = 110
24 // tranDisconnectMsg = 111 TODO: implement friendly disconnect
25 tranInviteNewChat = 112
26 tranInviteToChat = 113
27 tranRejectChatInvite = 114
30 tranNotifyChatChangeUser = 117
31 tranNotifyChatDeleteUser = 118
32 tranNotifyChatSubject = 119
33 tranSetChatSubject = 120
35 tranServerBanner = 122
36 tranGetFileNameList = 200
37 tranDownloadFile = 202
44 tranMakeFileAlias = 209
45 tranDownloadFldr = 210
46 // tranDownloadInfo = 211 TODO: implement file transfer queue
47 tranDownloadBanner = 212
49 tranGetUserNameList = 300
50 tranNotifyChangeUser = 301
51 tranNotifyDeleteUser = 302
52 tranGetClientInfoText = 303
53 tranSetClientUserInfo = 304
61 tranUserBroadcast = 355
62 tranGetNewsCatNameList = 370
63 tranGetNewsArtNameList = 371
67 tranGetNewsArtData = 400
73 type Transaction struct {
76 Flags byte // Reserved (should be 0)
77 IsReply byte // Request (0) or reply (1)
78 Type []byte // Requested operation (user defined)
79 ID []byte // Unique transaction ID (must be != 0)
80 ErrorCode []byte // Used in the reply (user defined, 0 = no error)
81 TotalSize []byte // Total data size for the transaction (all parts)
82 DataSize []byte // Size of data in this transaction part. This allows splitting large transactions into smaller parts.
83 ParamCount []byte // Number of the parameters for this transaction
87 func NewTransaction(t int, clientID *[]byte, fields ...Field) *Transaction {
88 typeSlice := make([]byte, 2)
89 binary.BigEndian.PutUint16(typeSlice, uint16(t))
91 idSlice := make([]byte, 4)
92 binary.BigEndian.PutUint32(idSlice, rand.Uint32())
100 ErrorCode: []byte{0, 0, 0, 0},
105 // ReadTransaction parses a byte slice into a struct. The input slice may be shorter or longer
106 // that the transaction size depending on what was read from the network connection.
107 func ReadTransaction(buf []byte) (*Transaction, int, error) {
108 totalSize := binary.BigEndian.Uint32(buf[12:16])
110 // the buf may include extra bytes that are not part of the transaction
111 // tranLen represents the length of bytes that are part of the transaction
112 tranLen := int(20 + totalSize)
114 if tranLen > len(buf) {
115 return nil, 0, errors.New("buflen too small for tranLen")
117 fields, err := ReadFields(buf[20:22], buf[22:tranLen])
127 ErrorCode: buf[8:12],
128 TotalSize: buf[12:16],
129 DataSize: buf[16:20],
130 ParamCount: buf[20:22],
135 const tranHeaderLen = 20 // fixed length of transaction fields before the variable length fields
137 // transactionScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens
138 func transactionScanner(data []byte, _ bool) (advance int, token []byte, err error) {
139 // The bytes that contain the size of a transaction are from 12:16, so we need at least 16 bytes
144 totalSize := binary.BigEndian.Uint32(data[12:16])
146 // tranLen represents the length of bytes that are part of the transaction
147 tranLen := int(tranHeaderLen + totalSize)
148 if tranLen > len(data) {
152 return tranLen, data[0:tranLen], nil
155 const minFieldLen = 4
157 func ReadFields(paramCount []byte, buf []byte) ([]Field, error) {
158 paramCountInt := int(binary.BigEndian.Uint16(paramCount))
159 if paramCountInt > 0 && len(buf) < minFieldLen {
160 return []Field{}, fmt.Errorf("invalid field length %v", len(buf))
163 // A Field consists of:
166 // Data: FieldSize number of bytes
168 for i := 0; i < paramCountInt; i++ {
169 if len(buf) < minFieldLen {
170 return []Field{}, fmt.Errorf("invalid field length %v", len(buf))
173 fieldSize := buf[2:4]
174 fieldSizeInt := int(binary.BigEndian.Uint16(buf[2:4]))
175 expectedLen := minFieldLen + fieldSizeInt
176 if len(buf) < expectedLen {
177 return []Field{}, fmt.Errorf("field length too short")
180 fields = append(fields, Field{
182 FieldSize: fieldSize,
183 Data: buf[4 : 4+fieldSizeInt],
186 buf = buf[fieldSizeInt+4:]
190 return []Field{}, fmt.Errorf("extra field bytes")
196 func (t *Transaction) MarshalBinary() (data []byte, err error) {
197 payloadSize := t.Size()
199 fieldCount := make([]byte, 2)
200 binary.BigEndian.PutUint16(fieldCount, uint16(len(t.Fields)))
202 var fieldPayload []byte
203 for _, field := range t.Fields {
204 fieldPayload = append(fieldPayload, field.Payload()...)
207 return concat.Slices(
208 []byte{t.Flags, t.IsReply},
213 payloadSize, // this is the dataSize field, but seeming the same as totalSize
219 // Size returns the total size of the transaction payload
220 func (t *Transaction) Size() []byte {
221 bs := make([]byte, 4)
224 for _, field := range t.Fields {
225 fieldSize += len(field.Data) + 4
228 binary.BigEndian.PutUint32(bs, uint32(fieldSize+2))
233 func (t *Transaction) GetField(id int) Field {
234 for _, field := range t.Fields {
235 if id == int(binary.BigEndian.Uint16(field.ID)) {
243 func (t *Transaction) IsError() bool {
244 return bytes.Compare(t.ErrorCode, []byte{0, 0, 0, 1}) == 0