This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
dbgsockettransport.go
167 lines (143 loc) · 3.04 KB
/
dbgsockettransport.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
package duktape
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"sync"
"unsafe"
)
type SocketTransport struct {
ctx *Context
uData interface{}
requestFunc DebugRequestFunc
detachedFunc DebugDetachedFunc
listener net.Listener
closed bool
conn *connection
m sync.Mutex
}
type connection struct {
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
errorListener func(err error)
}
func NewSocketTransport(ctx *Context,
network, addr string,
requestFunc DebugRequestFunc,
detachedFunc DebugDetachedFunc,
uData interface{}) (*SocketTransport, error) {
listener, err := net.Listen(network, addr)
if err != nil {
return nil, err
}
return &SocketTransport{
ctx: ctx,
uData: uData,
requestFunc: requestFunc,
detachedFunc: detachedFunc,
listener: listener,
m: sync.Mutex{},
}, nil
}
func (s *SocketTransport) attach() error {
debugger := DukDebugger()
return debugger.Attach(
s.ctx,
s.conn.read,
s.conn.write,
s.conn.peek,
nil,
s.conn.writeFlush,
s.requestFunc,
s.detachedFunc,
s.uData,
)
}
func (s *SocketTransport) Close() error {
s.m.Lock()
defer s.m.Unlock()
s.closed = true
if s.conn != nil {
if err := s.conn.conn.Close(); err != nil {
return err
}
}
return s.listener.Close()
}
func (s *SocketTransport) Listen(errorListener func(err error)) {
acceptConnection := func(conn net.Conn) error {
s.m.Lock()
defer s.m.Unlock()
if s.closed {
return nil
}
if s.conn != nil {
return errors.New("only one debugger can be connected at a time")
}
c := &connection{
conn: conn,
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
errorListener: errorListener,
}
s.conn = c
if err := s.attach(); err != nil {
return err
}
return nil
}
go func() {
for {
if s.closed {
return
}
accepted, err := s.listener.Accept()
if err != nil {
if errorListener != nil {
errorListener(err)
}
continue
}
if err := acceptConnection(accepted); err != nil {
if errorListener != nil {
errorListener(err)
}
}
}
}()
}
func (c *connection) read(uData unsafe.Pointer, buffer []byte) uint {
r, err := io.ReadAtLeast(c.reader, buffer, 1)
if err != nil {
if err != io.EOF {
if c.errorListener != nil {
c.errorListener(errors.New(fmt.Sprintf("could not read from socket: %v", err)))
}
}
return 0
}
return uint(r)
}
func (c *connection) write(uData unsafe.Pointer, buffer []byte) uint {
w, err := c.writer.Write(buffer)
if err != nil {
if c.errorListener != nil {
c.errorListener(errors.New(fmt.Sprintf("could not write to socket: %v", err)))
}
return 0
}
return uint(w)
}
func (c *connection) peek(uData unsafe.Pointer) uint {
return uint(c.reader.Buffered())
}
func (c *connection) writeFlush(uData unsafe.Pointer) {
if err := c.writer.Flush(); err != nil {
if c.errorListener != nil {
c.errorListener(errors.New(fmt.Sprintf("could not flush socket: %v", err)))
}
}
}