|
1 | 1 | #include "WebsocketConnection.h" |
2 | 2 |
|
3 | | -void WebsocketConnection::on_open(websocketpp::connection_hdl hdl) { |
4 | | - STORE_STACK(m_state); |
| 3 | +WebsocketConnection* WebsocketConnection::FromUserdata(lua_State* L, const int narg) |
| 4 | +{ |
| 5 | + return reinterpret_cast<WebsocketConnection*>(luaL_checkudata(L, narg, "WebsocketConnection")); |
| 6 | +} |
| 7 | + |
| 8 | +WebsocketConnection* WebsocketConnection::NewUserdata(lua_State* L, client::connection_ptr&& conn) |
| 9 | +{ |
| 10 | + auto* pConnection = reinterpret_cast<WebsocketConnection*>( |
| 11 | + lua_newuserdata(L, sizeof(WebsocketConnection))); |
| 12 | + |
| 13 | + lua_createtable(L, 0, 0); |
| 14 | + const int ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 15 | + |
| 16 | + new (pConnection) WebsocketConnection(L, std::move(conn), ref); |
| 17 | + |
| 18 | + return pConnection; |
| 19 | +} |
| 20 | + |
| 21 | +void WebsocketConnection::on_open(websocketpp::connection_hdl hdl) |
| 22 | +{ |
| 23 | + STORE_STACK(m_state); |
5 | 24 |
|
6 | | - lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
7 | | - if (lua_type(m_state, -1) != LUA_TTABLE) { |
8 | | - lua_pop(m_state, 1); |
9 | | - CHECK_STACK(m_state, 0); |
10 | | - return; |
11 | | - } |
| 25 | + lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
| 26 | + if (lua_type(m_state, -1) != LUA_TTABLE) |
| 27 | + { |
| 28 | + lua_pop(m_state, 1); |
| 29 | + CHECK_STACK(m_state, 0); |
| 30 | + return; |
| 31 | + } |
12 | 32 |
|
13 | | - lua_getfield(m_state, -1, "open"); |
| 33 | + lua_getfield(m_state, -1, "open"); |
14 | 34 |
|
15 | | - if (lua_type(m_state, -1) != LUA_TFUNCTION) { |
16 | | - lua_pop(m_state, 2); |
17 | | - CHECK_STACK(m_state, 0); |
18 | | - return; |
19 | | - } |
| 35 | + if (lua_type(m_state, -1) != LUA_TFUNCTION) |
| 36 | + { |
| 37 | + lua_pop(m_state, 2); |
| 38 | + CHECK_STACK(m_state, 0); |
| 39 | + return; |
| 40 | + } |
20 | 41 |
|
21 | | - lua_call(m_state, 0, 0); |
22 | | - lua_pop(m_state, 1); |
| 42 | + lua_call(m_state, 0, 0); |
| 43 | + lua_pop(m_state, 1); |
23 | 44 |
|
24 | | - CHECK_STACK(m_state, 0); |
| 45 | + CHECK_STACK(m_state, 0); |
25 | 46 | } |
26 | 47 |
|
27 | | -void WebsocketConnection::on_fail(websocketpp::connection_hdl hdl) { |
28 | | - STORE_STACK(m_state); |
29 | | - lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
30 | | - if (lua_type(m_state, -1) != LUA_TTABLE) { |
31 | | - lua_pop(m_state, 1); |
32 | | - CHECK_STACK(m_state, 0); |
33 | | - return; |
34 | | - } |
35 | | - lua_getfield(m_state, -1, "error"); |
36 | | - |
37 | | - if (lua_type(m_state, -1) != LUA_TFUNCTION) { |
38 | | - lua_pop(m_state, 2); |
39 | | - CHECK_STACK(m_state, 0); |
40 | | - return; |
41 | | - } |
42 | | - |
43 | | - const auto& ec = m_connection->get_ec(); |
44 | | - lua_pushinteger(m_state, ec.value()); |
45 | | - lua_pushstring(m_state, ec.message().c_str()); |
46 | | - |
47 | | - lua_call(m_state, 2, 0); |
48 | | - lua_pop(m_state, 1); |
49 | | - CHECK_STACK(m_state, 0); |
| 48 | +void WebsocketConnection::on_fail(websocketpp::connection_hdl hdl) |
| 49 | +{ |
| 50 | + STORE_STACK(m_state); |
| 51 | + lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
| 52 | + if (lua_type(m_state, -1) != LUA_TTABLE) |
| 53 | + { |
| 54 | + lua_pop(m_state, 1); |
| 55 | + CHECK_STACK(m_state, 0); |
| 56 | + return; |
| 57 | + } |
| 58 | + lua_getfield(m_state, -1, "error"); |
| 59 | + |
| 60 | + if (lua_type(m_state, -1) != LUA_TFUNCTION) |
| 61 | + { |
| 62 | + lua_pop(m_state, 2); |
| 63 | + CHECK_STACK(m_state, 0); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const auto& ec = m_connection->get_ec(); |
| 68 | + lua_pushinteger(m_state, ec.value()); |
| 69 | + lua_pushstring(m_state, ec.message().c_str()); |
| 70 | + |
| 71 | + lua_call(m_state, 2, 0); |
| 72 | + lua_pop(m_state, 1); |
| 73 | + CHECK_STACK(m_state, 0); |
50 | 74 | } |
51 | 75 |
|
52 | | -void WebsocketConnection::on_message(websocketpp::connection_hdl hdl, message_ptr msg) { |
53 | | - STORE_STACK(m_state); |
54 | | - lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
55 | | - if (lua_type(m_state, -1) != LUA_TTABLE) { |
56 | | - lua_pop(m_state, 1); |
57 | | - CHECK_STACK(m_state, 0); |
58 | | - return; |
59 | | - } |
60 | | - lua_getfield(m_state, -1, "message"); |
61 | | - |
62 | | - if (lua_type(m_state, -1) != LUA_TFUNCTION) { |
63 | | - lua_pop(m_state, 2); |
64 | | - CHECK_STACK(m_state, 0); |
65 | | - return; |
66 | | - } |
67 | | - |
68 | | - lua_pushstring(m_state, msg->get_payload().c_str()); |
69 | | - |
70 | | - lua_call(m_state, 1, 0); |
71 | | - lua_pop(m_state, 1); |
72 | | - CHECK_STACK(m_state, 0); |
| 76 | +void WebsocketConnection::on_message(websocketpp::connection_hdl hdl, message_ptr msg) |
| 77 | +{ |
| 78 | + STORE_STACK(m_state); |
| 79 | + lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
| 80 | + if (lua_type(m_state, -1) != LUA_TTABLE) |
| 81 | + { |
| 82 | + lua_pop(m_state, 1); |
| 83 | + CHECK_STACK(m_state, 0); |
| 84 | + return; |
| 85 | + } |
| 86 | + lua_getfield(m_state, -1, "message"); |
| 87 | + |
| 88 | + if (lua_type(m_state, -1) != LUA_TFUNCTION) |
| 89 | + { |
| 90 | + lua_pop(m_state, 2); |
| 91 | + CHECK_STACK(m_state, 0); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + lua_pushstring(m_state, msg->get_payload().c_str()); |
| 96 | + |
| 97 | + lua_call(m_state, 1, 0); |
| 98 | + lua_pop(m_state, 1); |
| 99 | + CHECK_STACK(m_state, 0); |
73 | 100 | } |
74 | 101 |
|
75 | | -void WebsocketConnection::on_close(websocketpp::connection_hdl hdl) { |
76 | | - STORE_STACK(m_state); |
77 | | - lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
78 | | - if (lua_type(m_state, -1) != LUA_TTABLE) { |
79 | | - lua_pop(m_state, 1); |
80 | | - CHECK_STACK(m_state, 0); |
81 | | - return; |
82 | | - } |
83 | | - lua_getfield(m_state, -1, "close"); |
84 | | - |
85 | | - if (lua_type(m_state, -1) != LUA_TFUNCTION) { |
86 | | - lua_pop(m_state, 2); |
87 | | - CHECK_STACK(m_state, 0); |
88 | | - return; |
89 | | - } |
90 | | - |
91 | | - const auto& ec = m_connection->get_ec(); |
92 | | - lua_pushinteger(m_state, ec.value()); |
93 | | - lua_pushstring(m_state, ec.message().c_str()); |
94 | | - |
95 | | - lua_call(m_state, 2, 0); |
96 | | - lua_pop(m_state, 1); |
97 | | - CHECK_STACK(m_state, 0); |
| 102 | +void WebsocketConnection::on_close(websocketpp::connection_hdl hdl) |
| 103 | +{ |
| 104 | + STORE_STACK(m_state); |
| 105 | + lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_refCallbacks); |
| 106 | + if (lua_type(m_state, -1) != LUA_TTABLE) |
| 107 | + { |
| 108 | + lua_pop(m_state, 1); |
| 109 | + CHECK_STACK(m_state, 0); |
| 110 | + return; |
| 111 | + } |
| 112 | + lua_getfield(m_state, -1, "close"); |
| 113 | + |
| 114 | + if (lua_type(m_state, -1) != LUA_TFUNCTION) |
| 115 | + { |
| 116 | + lua_pop(m_state, 2); |
| 117 | + CHECK_STACK(m_state, 0); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + const auto& ec = m_connection->get_ec(); |
| 122 | + lua_pushinteger(m_state, ec.value()); |
| 123 | + lua_pushstring(m_state, ec.message().c_str()); |
| 124 | + |
| 125 | + lua_call(m_state, 2, 0); |
| 126 | + lua_pop(m_state, 1); |
| 127 | + CHECK_STACK(m_state, 0); |
98 | 128 | } |
0 commit comments