-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathhvlua_ws.cpp
More file actions
303 lines (277 loc) · 11.1 KB
/
Copy pathhvlua_ws.cpp
File metadata and controls
303 lines (277 loc) · 11.1 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
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
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include "hvlua.h"
#include "hvlua_util.h" // hvlua_parse_reconnect
#ifdef HVLUA_WITH_HTTP
#include <deque>
#include <memory>
#include <string>
#include "EventLoop.h"
#include "WebSocketClient.h"
using namespace hv;
// hv.ws — coroutine-synchronous WebSocket client. Single-loop model (mirrors
// hvlua_http.cpp / hvlua_redis.cpp): the WebSocketClient is bound to the CURRENT
// loop, so onopen/onmessage/onclose fire on this same loop thread and resume the
// coroutine directly, no cross-thread hop.
//
// WebSocket is message-DRIVEN (the peer may push at any time), unlike the
// request/response clients. So instead of a per-call callback we buffer inbound
// messages in a queue and expose a coroutine-synchronous ws:recv():
// local ws, err = hv.ws.connect("ws://127.0.0.1:8888/path")
// ws:send("hello")
// local msg, err = ws:recv() -- suspends until a message arrives / closed
// ws:close()
//
// recv() returns a buffered message immediately if one is queued; otherwise it
// suspends the coroutine until onmessage (resume with msg) or onclose (resume
// with nil,"closed"). Only one recv() may be pending at a time.
static const char* WS_CLIENT_MT = "hv.ws.client.mt";
typedef std::deque<std::string> WsInbox;
struct LuaWsClient {
WebSocketClient* client;
WsInbox inbox; // buffered inbound messages
HvLuaCoroutine* recv_co; // coroutine waiting in recv(), or NULL
bool connected; // handshake completed (reset on close)
bool reconnect; // auto-reconnect enabled
bool opened_once; // onopen has fired at least once
};
// Resume a pending recv() coroutine (if any) with the front queued message, or
// with an error when the socket is gone: (nil,"reconnecting") if auto-reconnect
// is enabled (transient), else (nil,"closed") (terminal).
static void ws_try_deliver(LuaWsClient* box) {
if (box->recv_co == NULL) return;
lua_State* co = hvlua_coroutine_state(box->recv_co);
if (co == NULL) { // coroutine was GC'd / stale
hvlua_cancel(box->recv_co);
box->recv_co = NULL;
return;
}
if (!box->inbox.empty()) {
std::string msg = std::move(box->inbox.front());
box->inbox.pop_front();
HvLuaCoroutine* co_tok = box->recv_co;
box->recv_co = NULL;
lua_pushlstring(co, msg.data(), msg.size());
hvlua_resume(co_tok, 1);
} else if (!box->connected) {
HvLuaCoroutine* co_tok = box->recv_co;
box->recv_co = NULL;
lua_pushnil(co);
// Distinguish a transient disconnect (reconnecting) from a terminal
// close so the script can choose to keep waiting or stop.
lua_pushstring(co, box->reconnect ? "reconnecting" : "closed");
hvlua_resume(co_tok, 2);
}
}
static int ws_client_gc(lua_State* L) {
LuaWsClient* box = (LuaWsClient*)luaL_checkudata(L, 1, WS_CLIENT_MT);
if (box) {
if (box->recv_co) { // release a still-suspended recv token
hvlua_cancel(box->recv_co);
box->recv_co = NULL;
}
if (box->client) {
delete box->client; // external loop (not owner): does NOT stop it
box->client = NULL;
}
box->inbox.~WsInbox(); // placement-constructed; destroy explicitly
}
return 0;
}
// Continuation for connect: (ws) on success, or (nil,err) already on stack.
static int ws_connect_k(lua_State* L, int status, lua_KContext ctx) {
(void)status; (void)ctx;
if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
lua_pop(L, 1); // drop the `true` success marker
lua_pushvalue(L, 1); // return the ws userdata (self, stack slot 1)
return 1;
}
return 2; // (nil, err)
}
// hv.ws.connect(url [, opts]) -> ws | nil, err
// opts = { headers = {..}, ping_interval = ms, reconnect = {min_delay, max_delay,
// delay_policy, max_retry} }
static int l_ws_connect(lua_State* L) {
const char* url = luaL_checkstring(L, 1);
LuaWsClient* box = NULL;
{
EventLoopPtr loop = currentThreadEventLoopPtr;
if (!loop) {
lua_pushnil(L);
lua_pushstring(L, "hv.ws: no shared event loop on this thread");
return 2;
}
// userdata carries the client + inbox; placement-new the non-POD members.
box = (LuaWsClient*)lua_newuserdata(L, sizeof(LuaWsClient));
new (&box->inbox) WsInbox();
box->recv_co = NULL;
box->connected = false;
box->reconnect = false;
box->opened_once = false;
box->client = new WebSocketClient(loop); // bound to current loop, not owner
luaL_setmetatable(L, WS_CLIENT_MT);
lua_replace(L, 1); // move ws userdata to slot 1 for ws_connect_k
} // ~loop runs here, before the yield
// Parse headers later in the scope that ends before lua_yieldk.
if (lua_istable(L, 2)) {
lua_getfield(L, 2, "ping_interval");
if (lua_isinteger(L, -1)) box->client->setPingInterval((int)lua_tointeger(L, -1));
lua_pop(L, 1);
reconn_setting_t reconn;
if (hvlua_parse_reconnect(L, 2, &reconn)) {
box->reconnect = true;
box->client->setReconnect(&reconn);
}
}
box->client->onopen = [box]() {
// Connection established (initial or after a reconnect): mark connected
// and reset for the (possibly reused) session.
box->connected = true;
if (!box->opened_once) {
// Initial connect: resume the connect() coroutine held in recv_co.
box->opened_once = true;
lua_State* co = hvlua_coroutine_state(box->recv_co);
if (co == NULL) { return; }
HvLuaCoroutine* tok = box->recv_co;
box->recv_co = NULL;
lua_pushboolean(co, 1); // success marker for ws_connect_k
hvlua_resume(tok, 1);
}
// A reconnect's onopen does not resume anything; new recv() calls wait
// for the next message.
};
box->client->onmessage = [box](const std::string& msg) {
box->inbox.push_back(msg);
ws_try_deliver(box);
};
box->client->onclose = [box]() {
box->connected = false;
// Wake whoever is waiting. On the initial connect this is the connect()
// coroutine (handshake failed -> never opened_once); ws_try_deliver
// resumes it with (nil, "closed"/"reconnecting"). After a successful
// open it's a pending recv(). With auto-reconnect on, the socket will
// retry underneath and a future onopen/onmessage resumes fresh recvs.
ws_try_deliver(box);
};
box->recv_co = hvlua_suspend(L); // reuse recv_co to hold the connect wait
// NOTE: lua_yieldk below never returns to this C++ frame (longjmp in a
// C-built Lua), so destructors of locals here are SKIPPED. Scope the
// non-trivial `headers` (std::map) so it is destroyed BEFORE the yield;
// open() copies what it needs. Keep only the trivial `ret` for the check.
int ret;
{
http_headers headers = DefaultHeaders;
if (lua_istable(L, 2)) {
lua_getfield(L, 2, "headers");
if (lua_istable(L, -1)) {
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
if (lua_type(L, -2) == LUA_TSTRING && lua_type(L, -1) == LUA_TSTRING) {
headers[lua_tostring(L, -2)] = lua_tostring(L, -1);
}
lua_pop(L, 1);
}
}
lua_pop(L, 1); // pop headers (or nil)
}
ret = box->client->open(url, headers);
} // ~headers runs here, before the yield
if (ret != 0) {
hvlua_cancel(box->recv_co);
box->recv_co = NULL;
lua_pushnil(L);
lua_pushfstring(L, "hv.ws: open failed (%d)", ret);
return 2;
}
return lua_yieldk(L, 0, (lua_KContext)0, ws_connect_k);
}
// Continuation for recv: (msg) or (nil,err) already on stack.
static int ws_recv_k(lua_State* L, int status, lua_KContext ctx) {
(void)status; (void)ctx;
return lua_gettop(L) >= 2 && lua_isnil(L, -2) ? 2 : 1;
}
// ws:recv() -> msg | nil, err (coroutine-synchronous)
static int l_ws_recv(lua_State* L) {
LuaWsClient* box = (LuaWsClient*)luaL_checkudata(L, 1, WS_CLIENT_MT);
if (box == NULL || box->client == NULL) {
lua_pushnil(L); lua_pushstring(L, "closed"); return 2;
}
if (box->recv_co != NULL) {
lua_pushnil(L); lua_pushstring(L, "hv.ws: recv already pending"); return 2;
}
// fast path: a message is already queued -> return it without suspending.
if (!box->inbox.empty()) {
std::string msg = std::move(box->inbox.front());
box->inbox.pop_front();
lua_pushlstring(L, msg.data(), msg.size());
return 1;
}
if (!box->connected) {
lua_pushnil(L);
lua_pushstring(L, box->reconnect ? "reconnecting" : "closed");
return 2;
}
box->recv_co = hvlua_suspend(L);
return lua_yieldk(L, 0, (lua_KContext)0, ws_recv_k);
}
// ws:send(msg [, "binary"]) -> nbytes | nil, err (non-blocking, no suspend)
static int l_ws_send(lua_State* L) {
LuaWsClient* box = (LuaWsClient*)luaL_checkudata(L, 1, WS_CLIENT_MT);
size_t len = 0;
const char* data = luaL_checklstring(L, 2, &len);
if (box == NULL || box->client == NULL || !box->connected) {
// Not connected (never opened, or between reconnect attempts): sending
// now would silently drop, so report it instead.
lua_pushnil(L);
lua_pushstring(L, (box && box->reconnect) ? "reconnecting" : "closed");
return 2;
}
enum ws_opcode opcode = WS_OPCODE_TEXT;
if (lua_isstring(L, 3) && std::string(lua_tostring(L, 3)) == "binary") {
opcode = WS_OPCODE_BINARY;
}
int n = box->client->send(data, (int)len, opcode);
if (n < 0) {
lua_pushnil(L); lua_pushstring(L, "hv.ws: send failed"); return 2;
}
lua_pushinteger(L, n);
return 1;
}
// ws:close()
static int l_ws_close(lua_State* L) {
LuaWsClient* box = (LuaWsClient*)luaL_checkudata(L, 1, WS_CLIENT_MT);
if (box && box->client) {
// Explicit close is a terminal user action: disable auto-reconnect so
// recv() reports "closed" (not "reconnecting") and the socket stays down.
box->reconnect = false;
box->connected = false;
box->client->close();
}
return 0;
}
static const luaL_Reg ws_methods[] = {
{ "recv", l_ws_recv },
{ "send", l_ws_send },
{ "close", l_ws_close },
{ NULL, NULL }
};
static const luaL_Reg ws_funcs[] = {
{ "connect", l_ws_connect },
{ NULL, NULL }
};
extern "C" void hvlua_open_ws(lua_State* L) {
hvlua_new_class(L, WS_CLIENT_MT, ws_client_gc, ws_methods);
lua_pop(L, 1);
lua_getglobal(L, "hv");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
}
luaL_newlib(L, ws_funcs);
lua_setfield(L, -2, "ws");
lua_setglobal(L, "hv");
}
#endif // HVLUA_WITH_HTTP