-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
64 lines (52 loc) · 996 Bytes
/
server.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
package node2go
import (
"bufio"
"bytes"
"net"
)
type socket struct {
net.Listener
}
func (s *socket) read() (c chan *token, e chan *Err) {
c, e = make(chan *token), make(chan *Err)
go func() {
for {
conn, err := s.Listener.Accept()
if err != nil {
e <- newErr(conn, nilID, err)
continue
}
s := bufio.NewScanner(conn)
s.Split(bufio.ScanLines)
for s.Scan() {
if err := s.Err(); err != nil {
e <- newErr(conn, nilID, err)
continue
}
split := bytes.Split(s.Bytes(), []byte(";"))
if len(split) != 3 {
e <- newErr(conn, nilID, ErrMalformedMessage)
continue
}
if err != nil {
e <- newErr(conn, nilID, err)
}
c <- &token{
id: split[0],
Conn: conn,
FuncName: string(split[1]),
Data: split[2],
}
}
}
}()
return
}
func newSocket(addr string) (s *socket, err error) {
l, err := net.Listen("unix", addr)
if err != nil {
return
}
s = &socket{l}
return
}