-
Notifications
You must be signed in to change notification settings - Fork 10
/
rfunction.go
368 lines (288 loc) · 6.94 KB
/
rfunction.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright 2012-2015, Rolf Veen and contributors.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ogdl
import (
"encoding/binary"
"errors"
"log"
"net"
"time"
)
// RFunction represents a remote function (also known as a remote procedure
// call).
type RFunction struct {
Host string
Protocol int
conn net.Conn
}
func NewRFunction(host string) *RFunction {
return &RFunction{host, 1, nil}
}
// connect opens a connection to a TCP/IP server.
func (rf *RFunction) connect() error {
tcpAddr, err := net.ResolveTCPAddr("tcp", rf.Host)
if err != nil {
return err
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return err
}
rf.conn = conn
return nil
}
func TCPRawServer(host string, handler func(c net.Conn, b []byte) []byte, timeout int) error {
l, err := net.Listen("tcp", host)
if err != nil {
return err
}
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Println(err)
continue
}
// Handle the connection in a new goroutine.
go rawhandler(conn, handler, timeout)
}
}
func TCPServerV2(host string, handler func(net.Conn, *Graph) *Graph, timeout int) error {
l, err := net.Listen("tcp", host)
if err != nil {
return err
}
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Println(err)
continue
}
// Handle the connection in a new goroutine.
go handlerV2(conn, handler, timeout)
}
}
func TCPServerV1(host string, handler func(net.Conn, *Graph) *Graph, timeout int) error {
l, err := net.Listen("tcp", host)
if err != nil {
return err
}
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Println(err)
continue
}
// Handle the connection in a new goroutine.
go handlerV1(conn, handler, timeout)
}
}
func handlerV1(c net.Conn, handler func(net.Conn, *Graph) *Graph, timeout int) {
for {
// Set a time out (maximum time until next message)
c.SetReadDeadline(time.Now().Add(time.Second * time.Duration(timeout)))
// Read the incoming object
p := newBinParser(c)
g := p.Parse()
if g == nil {
c.Close()
return
}
r := handler(c, g)
// Write to result back in binary format
b := r.Binary()
c.Write(b)
}
}
func handlerV2(c net.Conn, handler func(net.Conn, *Graph) *Graph, timeout int) {
b4 := make([]byte, 4)
log.Println("connection accepted")
for {
// Set a time out (maximum time until next message)
c.SetReadDeadline(time.Now().Add(time.Second * time.Duration(timeout)))
// Read message: LEN(uint32) BYTES
// Read 4 bytes (LEN)
i, err := c.Read(b4)
// Set a time out (maximum time for receiving the body)
c.SetReadDeadline(time.Now().Add(time.Millisecond * 1000))
if err != nil || i != 4 {
log.Println("connection closed", i, err)
c.Close()
return
}
l := int(binary.BigEndian.Uint32(b4))
// read body of message
buf := make([]byte, l)
if buf == nil {
log.Printf("connection closed: cannot allocate % bytes\n", l)
c.Close()
return
}
i, err = c.Read(buf)
if err != nil || i != l {
log.Println("connection pre-closed", err)
c.Close()
return
}
g := FromBinary(buf)
r := handler(c, g)
// Write message back
buf = r.Binary()
binary.BigEndian.PutUint32(b4, uint32(len(buf)))
c.Write(b4)
c.Write(buf)
}
}
func rawhandler(c net.Conn, handler func(c net.Conn, body []byte) []byte, timeout int) {
b4 := make([]byte, 4)
log.Println("connection accepted")
for {
// Set a time out (maximum time until next message)
c.SetReadDeadline(time.Now().Add(time.Second * time.Duration(timeout)))
// Read message: LEN(uint32) BYTES
// Read 4 bytes (LEN)
i, err := c.Read(b4)
// Set a time out (maximum time for receiving the body)
c.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
if err != nil || i != 4 {
log.Println("connection closed", i, err)
c.Close()
return
}
l := int(binary.BigEndian.Uint32(b4))
// read body of message
buf := make([]byte, l)
if buf == nil {
log.Println("connection closed: not more memory")
c.Close()
return
}
i, err = c.Read(buf)
if err != nil || i != l {
log.Println("connection pre-closed", err)
c.Close()
return
}
r := handler(c, buf)
// Write back
l = len(r)
binary.BigEndian.PutUint32(b4, uint32(l))
c.Write(b4)
c.Write(r)
}
}
// Call makes a remote call. It sends the given Graph in binary format to the server
// and returns the response Graph.
func (rf *RFunction) Call(g *Graph) (*Graph, error) {
var r *Graph
var err error
if rf.conn == nil {
err = rf.connect()
if err != nil {
return nil, err
}
if rf.conn == nil {
return nil, errors.New("Connection = nil")
}
}
if rf.Protocol == 2 {
r, err = rf.callV2(g)
} else {
r, err = rf.callV1(g)
}
if err != nil {
err = rf.connect()
if err != nil {
return nil, err
}
if rf.conn == nil {
return nil, errors.New("Connection = nil")
}
if rf.Protocol == 2 {
r, err = rf.callV2(g)
} else {
r, err = rf.callV1(g)
}
}
return r, err
}
func (rf *RFunction) callV2(g *Graph) (*Graph, error) {
buf := g.Binary()
buf2 := make([]byte, 4+len(buf))
for i := 0; i < len(buf); i++ {
buf2[i+4] = buf[i]
}
b4 := make([]byte, 4)
binary.BigEndian.PutUint32(buf2, uint32(len(buf)))
// Write request (len + body)
rf.conn.SetDeadline(time.Now().Add(time.Second * 10))
rf.conn.Write(buf2)
// Read header response
rf.conn.SetReadDeadline(time.Now().Add(time.Second * 10))
j, _ := rf.conn.Read(b4)
if j != 4 {
rf.conn = nil
log.Println("callv2 error, message header")
return nil, errors.New("error in message header")
}
l := binary.BigEndian.Uint32(b4)
// Read body response
buf3 := make([]byte, l)
j, err := rf.conn.Read(buf3)
if err != nil {
rf.conn = nil
log.Println("callv2", err)
return nil, err
}
if j != int(l) {
rf.conn = nil
log.Println("callv2 error, message len")
return nil, errors.New("error in message len")
}
g = FromBinary(buf3)
return g, nil
}
func (rf *RFunction) callV1(g *Graph) (*Graph, error) {
b := g.Binary()
rf.conn.SetDeadline(time.Now().Add(time.Second * 10))
n, err := rf.conn.Write(b)
if err != nil {
rf.conn = nil
log.Println("callv1", err)
return nil, err
}
if n < len(b) {
rf.conn = nil
log.Println("callv1", err)
return nil, errors.New("could not write all bytes")
}
p := newBinParser(rf.conn)
if p.r == nil {
rf.conn = nil
log.Println("callv1, rf.conn buffered reader = nil")
return nil, errors.New("callv1, rf.conn buffered reader = nil")
}
rf.conn.SetReadDeadline(time.Now().Add(time.Second * 10))
c := p.read()
if c == -1 {
rf.conn = nil
log.Println("callv1", "EOS")
return nil, errors.New("unexpected EOS")
}
p.unread()
r := p.Parse()
if r == nil || r.Len() == 0 {
return nil, errors.New("nil response")
}
return r, nil
}
// Close closes the underlying connection, if open.
func (rf *RFunction) Close() {
if rf.conn != nil {
rf.conn.Close()
rf.conn = nil
}
}