-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.go
More file actions
69 lines (53 loc) · 1.2 KB
/
net.go
File metadata and controls
69 lines (53 loc) · 1.2 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
package main
import (
"bytes"
"encoding/binary"
"net"
"os"
log "github.com/sirupsen/logrus"
)
const douyuserver = "openbarrage.douyutv.com:8601"
var conn net.Conn
func connect() {
var err error
conn, err = net.Dial("tcp", douyuserver)
if err != nil {
log.Errorf("Error connecting: %s", err)
os.Exit(1)
}
log.Infof("Connecting to %s", douyuserver)
}
func shutdown() {
log.Info("Close Connect")
conn.Close()
}
func send(data string) {
msglen := uint32(len(data) + 8)
b := new(bytes.Buffer)
binary.Write(b, binary.LittleEndian, msglen)
binary.Write(b, binary.LittleEndian, msglen)
sendType := int16(689)
binary.Write(b, binary.LittleEndian, sendType)
zeroPad := uint8(0)
binary.Write(b, binary.LittleEndian, zeroPad)
binary.Write(b, binary.LittleEndian, zeroPad)
binary.Write(b, binary.LittleEndian, []byte(data))
_, err := conn.Write(b.Bytes())
if err != nil {
log.Warnf("send err %s", err)
return
}
}
func recv() []byte {
buf := make([]byte, 1024*10)
reqLen, err := conn.Read(buf)
if err != nil {
log.Errorf("Error to read message because of %s", err)
return buf
}
if reqLen < 18 {
log.Errorf("Not Valid Package len %d", reqLen)
return buf
}
return buf[12:reqLen]
}