-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.go
96 lines (76 loc) · 1.84 KB
/
engine.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
package ibapi
import (
"errors"
"net"
"log"
"strings"
)
const (
DefaultGateway = "127.0.0.1:4001"
)
var (
ErrBadServerHandshake = errors.New("Invalid handshake response")
ErrClosed = errors.New("Session closed")
)
type Engine struct {
conn net.Conn
replyReader *replyReader
requestBytes *requestBytes
}
func NewEngine(gateway string, clientId int64) (*Engine, error) {
e := new(Engine)
// dial conn
conn, err := net.Dial("tcp", gateway)
if err != nil {
return nil, err
}
e.conn = conn
e.replyReader = NewReplyReader(e.conn)
e.requestBytes = NewRequestBytes()
// exchange handshaking info: send client info, receive server info
c := &MsgOutClientHandshake{VerClient, clientId}
log.Println("Writing Client Handshake")
e.requestBytes.writeStruct(c)
err = e.writeRequestBytes()
if err != nil {
return nil, err
}
log.Println("Wrote Client Handshake")
log.Println("Reading Server Handshake")
rep := e.replyReader.readStruct(new(MsgInServerHandshake), 0)
log.Println("Read Server Handshake")
s, ok := rep.(*MsgInServerHandshake)
if !ok {
return nil, ErrBadServerHandshake
}
e.requestBytes.verServer = s.Version
log.Println("Server Time ", s.Time)
return e, nil
}
func (e *Engine) ReadReply() (interface{}, error) {
if e.conn == nil {
return nil, ErrClosed
}
return e.replyReader.Read()
}
func (e *Engine) WriteRequest(req interface{}) error {
if e.conn == nil {
return ErrClosed
}
// load to bytes then write to conn
err := e.requestBytes.Write(req)
if err != nil {
return err
}
return e.writeRequestBytes()
}
func (e *Engine) writeRequestBytes() error {
log.Printf("Writing bytes, %v", strings.Replace(e.requestBytes.String(), "\000", "-", -1))
_, err := e.conn.Write(e.requestBytes.Bytes())
return err
}
func (e *Engine) Stop() error {
err := e.conn.Close()
e.conn = nil
return err
}