forked from go-chassis/go-chassis
/
client_conn.go
executable file
·159 lines (141 loc) · 3.65 KB
/
client_conn.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package highway
import (
"bufio"
"fmt"
"github.com/go-chassis/go-chassis/client/highway/pb"
"github.com/go-chassis/go-chassis/core/lager"
"net"
"sync"
)
// constant for buffer size
const (
DefaultReadBufferSize = 0
DefaultWriteBufferSize = 1024
)
//ClientConnection Highway client connection
type ClientConnection struct {
remoteAddr string
baseConn net.Conn
client *BaseClient
mtx sync.Mutex
closed bool
}
//NewHighwayClientConnection creat Highway client connection
func NewHighwayClientConnection(conn net.Conn, client *BaseClient) *ClientConnection {
tmp := new(ClientConnection)
//conn.SetKeepAlive(true)
tmp.baseConn = conn
tmp.client = client
tmp.closed = false
return tmp
}
//Open Init Highway client connection
func (hwClientConn *ClientConnection) Open() error {
err := hwClientConn.Hello()
if err != nil {
hwClientConn.Close()
return err
}
go hwClientConn.msgRecvLoop()
return nil
}
//Hello Highway handshake
func (hwClientConn *ClientConnection) Hello() error {
wBuf := bufio.NewWriterSize(hwClientConn.baseConn, DefaultWriteBufferSize)
protoObj := &ProtocolObject{}
protoObj.SerializeHelloReq(wBuf)
err := wBuf.Flush()
if err != nil {
return err
}
rdBuf := bufio.NewReaderSize(hwClientConn.baseConn, DefaultReadBufferSize)
rsp := &Response{}
rsp.Result = &highway.LoginResponse{}
protoObj.DeSerializeFrame(rdBuf)
if err != nil {
return err
}
err = protoObj.DeSerializeRsp(rsp)
if err != nil && rsp.Status != Ok {
return err
}
return nil
}
//Close the connection
func (hwClientConn *ClientConnection) Close() {
hwClientConn.mtx.Lock()
defer hwClientConn.mtx.Unlock()
if hwClientConn.closed {
return
}
hwClientConn.closed = true
hwClientConn.baseConn.Close()
}
func (hwClientConn *ClientConnection) msgRecvLoop() {
rdBuf := bufio.NewReaderSize(hwClientConn.baseConn, DefaultReadBufferSize)
for {
protoObj := &ProtocolObject{}
err := protoObj.DeSerializeFrame(rdBuf)
if err != nil {
break
}
hwClientConn.processMsg(protoObj)
}
hwClientConn.Close()
}
func (hwClientConn *ClientConnection) processMsg(protoObj *ProtocolObject) {
ctx := hwClientConn.client.GetWaitMsg(protoObj.FrHead.MsgID)
if ctx != nil {
protoObj.DeSerializeRsp(ctx.Rsp)
ctx.Done()
} else {
lager.Logger.Info(fmt.Sprintf("Cann't find the msg, perhaps it's timeout:%d", protoObj.FrHead.MsgID))
}
}
//AsyncSendMsg Highway send message
func (hwClientConn *ClientConnection) AsyncSendMsg(ctx *InvocationContext) error {
wBuf := bufio.NewWriterSize(hwClientConn.baseConn, DefaultWriteBufferSize)
protoObj := &ProtocolObject{}
protoObj.SerializeReq(ctx.Req, wBuf)
err := wBuf.Flush()
if err != nil {
ctx.Rsp.Err = err.Error()
ctx.Done()
}
return err
}
//PostMsg Highway post message, Respond is needless
func (hwClientConn *ClientConnection) PostMsg(req *Request) error {
wBuf := bufio.NewWriterSize(hwClientConn.baseConn, DefaultWriteBufferSize)
protoObj := &ProtocolObject{}
protoObj.SerializeReq(req, wBuf)
return wBuf.Flush()
}
/*
func (this *ClientConnection) SyncSendMsg(req *client.Request, rsp *client.Response) error {
wBuf := bufio.NewWriterSize(this.baseConn, 1024)
protoObj := &HighWayProtocalObject_10{}
protoObj.SerializeReq(req, wBuf)
err := wBuf.Flush()
if err != nil {
//this.client.RspCallBack(nil)
return err
}
rdBuf := bufio.NewReaderSize(this.baseConn, 1024)
err = protoObj.DeSerializeFrame(rdBuf)
if err != nil {
//this.client.RspCallBack(nil)
return err
}
protoObj.DeSerializeRsp(rsp)
if err != nil {
//this.client.RspCallBack(nil)
return err
}
return err
}
*/
//Closed Highway connection status
func (hwClientConn *ClientConnection) Closed() bool {
return hwClientConn.closed
}