7 "github.com/jhalter/mobius/concat"
20 tranSendInstantMsg = 108
21 tranShowAgreement = 109
22 tranDisconnectUser = 110
23 // tranDisconnectMsg = 111 TODO: implement friendly disconnect
24 tranInviteNewChat = 112
25 tranInviteToChat = 113
26 tranRejectChatInvite = 114
29 tranNotifyChatChangeUser = 117
30 tranNotifyChatDeleteUser = 118
31 tranNotifyChatSubject = 119
32 tranSetChatSubject = 120
34 tranServerBanner = 122
35 tranGetFileNameList = 200
36 tranDownloadFile = 202
43 tranMakeFileAlias = 209
44 tranDownloadFldr = 210
45 // tranDownloadInfo = 211 TODO: implement file transfer queue
46 tranDownloadBanner = 212
48 tranGetUserNameList = 300
49 tranNotifyChangeUser = 301
50 tranNotifyDeleteUser = 302
51 tranGetClientInfoText = 303
52 tranSetClientUserInfo = 304
60 tranUserBroadcast = 355
61 tranGetNewsCatNameList = 370
62 tranGetNewsArtNameList = 371
66 tranGetNewsArtData = 400
72 type Transaction struct {
75 Flags byte // Reserved (should be 0)
76 IsReply byte // Request (0) or reply (1)
77 Type []byte // Requested operation (user defined)
78 ID []byte // Unique transaction ID (must be != 0)
79 ErrorCode []byte // Used in the reply (user defined, 0 = no error)
80 TotalSize []byte // Total data size for the transaction (all parts)
81 DataSize []byte // Size of data in this transaction part. This allows splitting large transactions into smaller parts.
82 ParamCount []byte // Number of the parameters for this transaction
86 func NewTransaction(t int, clientID *[]byte, fields ...Field) *Transaction {
87 typeSlice := make([]byte, 2)
88 binary.BigEndian.PutUint16(typeSlice, uint16(t))
90 idSlice := make([]byte, 4)
91 binary.BigEndian.PutUint32(idSlice, rand.Uint32())
99 ErrorCode: []byte{0, 0, 0, 0},
104 // ReadTransaction parses a byte slice into a struct. The input slice may be shorter or longer
105 // that the transaction size depending on what was read from the network connection.
106 func ReadTransaction(buf []byte) (*Transaction, int, error) {
107 totalSize := binary.BigEndian.Uint32(buf[12:16])
109 // the buf may include extra bytes that are not part of the transaction
110 // tranLen represents the length of bytes that are part of the transaction
111 tranLen := int(20 + totalSize)
113 if tranLen > len(buf) {
114 return nil, 0, errors.New("buflen too small for tranLen")
116 fields, err := ReadFields(buf[20:22], buf[22:tranLen])
126 ErrorCode: buf[8:12],
127 TotalSize: buf[12:16],
128 DataSize: buf[16:20],
129 ParamCount: buf[20:22],
134 func readTransactions(buf []byte) ([]Transaction, int, error) {
135 var transactions []Transaction
140 for bytesRead < bufLen {
141 t, tReadLen, err := ReadTransaction(buf[bytesRead:])
143 return transactions, bytesRead, err
145 bytesRead += tReadLen
147 transactions = append(transactions, *t)
150 return transactions, bytesRead, nil
153 const minFieldLen = 4
155 func ReadFields(paramCount []byte, buf []byte) ([]Field, error) {
156 paramCountInt := int(binary.BigEndian.Uint16(paramCount))
157 if paramCountInt > 0 && len(buf) < minFieldLen {
158 return []Field{}, fmt.Errorf("invalid field length %v", len(buf))
161 // A Field consists of:
164 // Data: FieldSize number of bytes
166 for i := 0; i < paramCountInt; i++ {
167 if len(buf) < minFieldLen {
168 return []Field{}, fmt.Errorf("invalid field length %v", len(buf))
171 fieldSize := buf[2:4]
172 fieldSizeInt := int(binary.BigEndian.Uint16(buf[2:4]))
173 expectedLen := minFieldLen + fieldSizeInt
174 if len(buf) < expectedLen {
175 return []Field{}, fmt.Errorf("field length too short")
178 fields = append(fields, Field{
180 FieldSize: fieldSize,
181 Data: buf[4 : 4+fieldSizeInt],
184 buf = buf[fieldSizeInt+4:]
188 return []Field{}, fmt.Errorf("extra field bytes")
194 func (t *Transaction) MarshalBinary() (data []byte, err error) {
195 payloadSize := t.Size()
197 fieldCount := make([]byte, 2)
198 binary.BigEndian.PutUint16(fieldCount, uint16(len(t.Fields)))
200 var fieldPayload []byte
201 for _, field := range t.Fields {
202 fieldPayload = append(fieldPayload, field.Payload()...)
205 return concat.Slices(
206 []byte{t.Flags, t.IsReply},
211 payloadSize, // this is the dataSize field, but seeming the same as totalSize
217 // Size returns the total size of the transaction payload
218 func (t *Transaction) Size() []byte {
219 bs := make([]byte, 4)
222 for _, field := range t.Fields {
223 fieldSize += len(field.Data) + 4
226 binary.BigEndian.PutUint32(bs, uint32(fieldSize+2))
231 func (t *Transaction) GetField(id int) Field {
232 for _, field := range t.Fields {
233 if id == int(binary.BigEndian.Uint16(field.ID)) {