-
Notifications
You must be signed in to change notification settings - Fork 17
/
definitions.go
162 lines (130 loc) · 3.86 KB
/
definitions.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
package sonic
import (
"io"
"net"
)
type AsyncCallback func(error, int)
type AcceptCallback func(error, Conn)
type AcceptPacketCallback func(error, PacketConn)
// AsyncReader is the interface that wraps the AsyncRead and AsyncReadAll methods.
type AsyncReader interface {
// AsyncRead reads up to `len(b)` bytes into `b` asynchronously.
//
// This call should not block. The provided completion handler is called in the following cases:
// - a read of up to `n` bytes completes
// - an error occurs
//
// Callers should always process the returned bytes before considering the error err. Implementations of AsyncRead
// are discouraged from invoking the handler with 0 bytes and a nil error.
//
// Ownership of the byte slice must be retained by callers, which must guarantee that it remains valid until the
// callback is invoked.
AsyncRead(b []byte, cb AsyncCallback)
// AsyncReadAll reads exactly `len(b)` bytes into b asynchronously.
AsyncReadAll(b []byte, cb AsyncCallback)
}
// AsyncWriter is the interface that wraps the AsyncWrite and AsyncWriteAll methods.
type AsyncWriter interface {
// AsyncWrite writes up to `len(b)` bytes from `b` asynchronously.
//
// This call does not block. The provided completion handler is called in the following cases:
// - a write of up to `n` bytes completes
// - an error occurs
//
// Ownership of the byte slice must be retained by callers, which must guarantee that it remains valid until the
// callback is invoked. This function does not modify the given byte slice, even temporarily.
AsyncWrite(b []byte, cb AsyncCallback)
// AsyncWriteAll writes exactly `len(b)` bytes into the underlying data stream asynchronously.
AsyncWriteAll(b []byte, cb AsyncCallback)
}
type AsyncReadWriter interface {
AsyncReader
AsyncWriter
}
type AsyncReaderFrom interface {
AsyncReadFrom(AsyncReader, AsyncCallback)
}
type AsyncWriterTo interface {
AsyncWriteTo(AsyncWriter, AsyncCallback)
}
type FileDescriptor interface {
RawFd() int
io.Closer
io.ReadWriter
AsyncReadWriter
AsyncCanceller
}
type File interface {
FileDescriptor
io.Seeker
}
type AsyncCanceller interface {
// Cancel cancells all asynchronous operations on the next layer.
Cancel()
}
// Stream represents a full-duplex connection between two processes, where data represented as bytes may be received
// reliably in the same order they were written.
type Stream interface {
RawFd() int
AsyncStream
SyncStream
}
type AsyncStream interface {
AsyncReadStream
AsyncWriteStream
io.Closer
}
type AsyncReadStream interface {
AsyncReader
AsyncCanceller
io.Closer
}
type AsyncWriteStream interface {
AsyncWriter
AsyncCanceller
io.Closer
}
type SyncStream interface {
SyncReadStream
SyncWriteStream
io.Closer
}
type SyncReadStream interface {
io.Reader
io.Closer
}
type SyncWriteStream interface {
io.Writer
io.Closer
}
// Conn is a generic stream-oriented network connection.
type Conn interface {
FileDescriptor
net.Conn
}
type AsyncReadCallbackPacket func(error, int, net.Addr)
type AsyncWriteCallbackPacket func(error)
// PacketConn is a generic packet-oriented connection.
type PacketConn interface {
ReadFrom([]byte) (n int, addr net.Addr, err error)
AsyncReadFrom([]byte, AsyncReadCallbackPacket)
AsyncReadAllFrom([]byte, AsyncReadCallbackPacket)
WriteTo([]byte, net.Addr) error
AsyncWriteTo([]byte, net.Addr, AsyncWriteCallbackPacket)
Close() error
Closed() bool
LocalAddr() net.Addr
RawFd() int
}
// Listener is a generic network listener for stream-oriented protocols.
type Listener interface {
// Accept waits for and returns the next connection to the listener synchronously.
Accept() (Conn, error)
// AsyncAccept waits for and returns the next connection to the listener asynchronously.
AsyncAccept(AcceptCallback)
// Close closes the listener.
Close() error
// Addr returns the listener's network address.
Addr() net.Addr
RawFd() int
}