-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.lua
More file actions
157 lines (141 loc) · 5.4 KB
/
Copy pathSocket.lua
File metadata and controls
157 lines (141 loc) · 5.4 KB
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
---@meta
local SOCKERR = {
UNKNOWN_ERROR = "unknown error",
OK = "ok",
AGAIN = "temporary failure",
ADDRESS_IN_USE = "address in use",
CONNECTION_REFUSED = "connection refused",
DENIED = "access denied",
FAILED = "failed",
NETWORK_UNREACHABLE = "network unreachable",
NOT_FOUND = "not found",
NO_DATA = "no data",
OUT_OF_MEMORY = "out of memory",
TIMEOUT = "timeout",
UNSUPPORTED = "unsupported"
}
---@class socket
---An instance of a TCP socket
---Most of these functions will return two values if an error occurs;
---the first value is `nil` and the second value is an error `string`
---from [`socket.ERRORS`](lua://socket.ERRORS).
local _socket = {
---Creates a new socket for an incoming connection from a listening server socket
---@param self socket
---@return socket socket
accept = function(self) end,
---Add a callback for a named event.
---The returned id can be used to remove it later.
---Events get checked once per frame but can be checked
---manually using [`socket::poll`](lua://socket.poll)
---@param self socket
---@param event string Named callback event
---| '"received"' New data has been received and can be read
---| '"error"' An error has occurred on the socket
---@param callback function Callback function
---@return integer callbackId
add = function(self, event, callback) end,
---Bind the socket to a specific interface and port.
---Use `nil` for `address` to bind to all interfaces
---@param self socket
---@param address? string host
---@param port integer
---@return integer
bind = function(self, address, port) end,
---Opens a TCP connection to the specified address and port.
---**Caution**: This is a blocking call.
---The emulator will not respond until the connection either succeeds or fails
---@param self socket
---@param address string
---@param port integer
---@return integer
connect = function(self, address, port) end,
---Check if a socket has data ready to receive, and return true if so
---@param self socket
---@return boolean
hasdata = function(self) end,
---Begins listening for incoming connections.
---The socket must have first been bound with the
---[`socket::bind`](lua://socket.bind) function
---@param self socket
---@param backlog? integer `default=1`
---@return integer
listen = function(self, backlog) end,
---Manually check for events on this socket and dispatch associated callbacks
---@param self socket
poll = function(self) end,
---Read up to `maxBytes` bytes from the socket and return them.
---If the socket has been disconnected or an error occurs,
---it will return `nil, error` instead
---@param self socket
---@param maxBytes integer
---@return string | nil, string data
receive = function(self, maxBytes) end,
---Remove a callback with the previously returned id
---@param self socket
---@param cbid integer callback ID
remove = function(self, cbid) end,
---Writes a string to the socket.
---If `i` and `j` are provided, they have the same semantics
---as the parameters to [`string.sub`](lua://string.sub) to write a substring
---@param self socket
---@param data string
---@param i? integer `default=0`
---@param j? integer `default=0`
---@return integer index Last index written
send = function(self, data, i, j) end,
}
socket = {
---Create and bind a new socket to a specific interface and port.
---Use `nil` for address to bind to all interfaces
---@param address? string
---@param port integer
---@return socket socket
bind = function(address, port) end,
---Create and return a new TCP socket with a connection
---to the specified address and port.
---Caution: This is a blocking call.
---The emulator will not respond until the connection either succeeds or fails
---@param address string
---@param port integer
---@return socket socket
connect = function(address, port) end,
---Create a new TCP socket, for use with either
---[`socket::bind`](lua://socket.bind) or
---[`socket::connect`](lua://socket.connect) later
---@param self socket
---@return socket socket
tcp = function(self) end,
---@class socket.ERRORS
---Error strings corresponding to the
---[`C.SOCKERR`](lua://C.SOCKERR) error codes,
---indexed both by name and by value
ERRORS = {
[-1] = SOCKERR.UNKNOWN_ERROR,
[0] = SOCKERR.OK,
[1] = SOCKERR.AGAIN,
[2] = SOCKERR.ADDRESS_IN_USE,
[3] = SOCKERR.CONNECTION_REFUSED,
[4] = SOCKERR.DENIED,
[5] = SOCKERR.FAILED,
[6] = SOCKERR.NETWORK_UNREACHABLE,
[7] = SOCKERR.NOT_FOUND,
[8] = SOCKERR.NO_DATA,
[9] = SOCKERR.OUT_OF_MEMORY,
[10] = SOCKERR.TIMEOUT,
[11] = SOCKERR.UNSUPPORTED,
UNKNOWN_ERROR = SOCKERR.UNKNOWN_ERROR,
OK = SOCKERR.OK,
AGAIN = SOCKERR.AGAIN,
ADDRESS_IN_USE = SOCKERR.ADDRESS_IN_USE,
CONNECTION_REFUSED = SOCKERR.CONNECTION_REFUSED,
DENIED = SOCKERR.DENIED,
FAILED = SOCKERR.FAILED,
NETWORK_UNREACHABLE = SOCKERR.NETWORK_UNREACHABLE,
NOT_FOUND = SOCKERR.NOT_FOUND,
NO_DATA = SOCKERR.NO_DATA,
OUT_OF_MEMORY = SOCKERR.OUT_OF_MEMORY,
TIMEOUT = SOCKERR.TIMEOUT,
UNSUPPORTED = SOCKERR.UNSUPPORTED
}
}