-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtransport.go
88 lines (76 loc) · 1.52 KB
/
transport.go
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
package lua_debugger
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"github.com/edolphin-ydf/gopherlua-debugger/proto"
"io"
"log"
"net"
"strconv"
)
type Transport struct {
c net.Conn
Handler func(int, interface{})
}
func (t *Transport) Connect(host string, port int) error {
var err error
t.c, err = net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return err
}
go t.parseMsg()
return nil
}
func (t *Transport) parseMsg() {
var lineBuf bytes.Buffer
r := bufio.NewReader(t.c)
readHead := true
cmd := 0
for {
tmpLine, isPrefix, err := r.ReadLine()
if err != nil {
t.Handler(proto.MsgIdActionReq, &proto.ActionReq{Action: proto.Stop})
break
}
lineBuf.Write(tmpLine)
if isPrefix {
continue
}
if readHead {
cmd, err = strconv.Atoi(lineBuf.String())
if err != nil {
log.Println("parse cmd error:", err)
_ = t.c.Close()
break
}
lineBuf = bytes.Buffer{}
readHead = false
continue
}
msg := proto.GetMsg(cmd)
if err = json.Unmarshal(lineBuf.Bytes(), msg); err != nil {
log.Println("unmarshal json fail", err)
break
}
if t.Handler != nil {
t.Handler(cmd, msg)
}
lineBuf = bytes.Buffer{}
readHead = true
}
}
func (t *Transport) Send(cmd int, msg interface{}) {
if t.c == nil {
return
}
buf := bytes.Buffer{}
buf.WriteString(fmt.Sprintf("%d\n", cmd))
data, _ := json.Marshal(msg)
buf.Write(data)
buf.WriteString("\n")
if _, err := io.Copy(t.c, &buf); err != nil {
log.Println("send msg fail:", string(data), err)
}
}