forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_socket.go
executable file
·58 lines (48 loc) · 1.25 KB
/
tcp_socket.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
// Package tcp provides a TCP transport
package tcp
import (
"bufio"
"encoding/gob"
"net"
"time"
)
type tcpTransportSocket struct {
baseConn
conn net.Conn
enc *gob.Encoder
dec *gob.Decoder
encBuf *bufio.Writer
timeout time.Duration
}
func (t *tcpTransportSocket) Recv() ([]byte, []byte, map[string]string, int, error) {
// set timeout if its greater than 0
if t.timeout > time.Duration(0) {
t.conn.SetDeadline(time.Now().Add(t.timeout))
}
reqID, requestHeader, requestBody, err := t.baseConn.recvHeader()
if err != nil {
return nil, nil, nil, int(reqID), err
}
return requestHeader, requestBody, nil, int(reqID), nil
}
func (t *tcpTransportSocket) Send(Header []byte, Body []byte, Metadata map[string]string, ID int) error {
// set timeout if its greater than 0
if t.timeout > time.Duration(0) {
t.conn.SetDeadline(time.Now().Add(t.timeout))
}
headerLen := int32(len(Header))
totalLen := int32(len(Body)) + headerLen
//Assemble HighWay CommHeader
commonHeader, err := t.initCommHeader(ID, totalLen, headerLen)
if err != nil {
return err
}
err = t.sendFrame(commonHeader, Header, Body)
if err != nil {
return err
}
return t.baseConn.w.Flush()
}
func (t *tcpTransportSocket) Close() error {
return t.conn.Close()
}