-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
74 lines (65 loc) · 1.78 KB
/
message.go
File metadata and controls
74 lines (65 loc) · 1.78 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
package main
import (
"encoding/binary"
)
func getPingMsg() []byte {
pingMsg := make([]byte, 5)
copy(pingMsg[0:4], []byte{0, 0, 0, 1})
copy(pingMsg[4:5], []byte{0})
return pingMsg
}
func getPongMsg() []byte {
pingMsg := make([]byte, 5)
copy(pingMsg[0:4], []byte{0, 0, 0, 1})
copy(pingMsg[4:5], []byte{1})
return pingMsg
}
func getChatMsg(msgContent string) []byte {
chatMsg := make([]byte, len(msgContent)+5)
getBytesFromUint32(chatMsg[0:4], uint32(len(msgContent)+1))
copy(chatMsg[4:5], []byte{2})
copy(chatMsg[5:], msgContent)
return chatMsg
}
func extractChatMsg(chatMsg []byte) []byte {
return chatMsg[1:]
}
func getFileInfoMsg(fileLen uint64, fileName string, md5 string, uniqueID uint32) []byte {
fileNameLen := uint8(len(fileName))
fileMsgLen := 10 + fileNameLen + 32 + 4
fileMsg := make([]byte, fileMsgLen+4)
getBytesFromUint32(fileMsg[0:4], uint32(fileMsgLen))
fileMsg[4] = 3
fileMsg[5] = fileNameLen
getBytesFromUint64(fileMsg[6:], fileLen)
copy(fileMsg[14:], md5)
getBytesFromUint32(fileMsg[46:50], uniqueID)
copy(fileMsg[50:], fileName)
return fileMsg
}
func getFileDataMsg(fileData []byte, uniqueID uint32) []byte {
fileDataMsg := make([]byte, 5+len(fileData)+32)
msgLen := len(fileData) + 32
getBytesFromUint32(fileDataMsg[0:4], uint32(msgLen)+1)
fileDataMsg[4] = 5
getBytesFromUint32(fileDataMsg[5:37], uniqueID)
copy(fileDataMsg[37:], fileData)
return fileDataMsg
}
func extractFileDataFromMsg(fileMsg []byte) (uint32, []byte) {
uniqueID := binary.BigEndian.Uint32(fileMsg[1:33])
fileData := fileMsg[33:]
return uniqueID, fileData
}
func getMsgType(msg []byte) string {
availableMsgTypes := map[byte]string{
0: "ping",
1: "pong",
2: "chat",
3: "file_info",
4: "file_accept",
5: "file_data",
}
msgType := availableMsgTypes[msg[0]]
return msgType
}