Skip to content

Commit 2d5be1b

Browse files
committed
"A minor" code cleanup
womp womp womp womp womp womp womp womp
1 parent a08f7fa commit 2d5be1b

File tree

5 files changed

+400
-355
lines changed

5 files changed

+400
-355
lines changed

websocket/WebsocketClient.cpp

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
11
#include "WebsocketClient.h"
22

3+
WebsocketClient* WebsocketClient::FromUserdata(lua_State* L, const int narg)
4+
{
5+
return reinterpret_cast<WebsocketClient*>(luaL_checkudata(L, narg, "WebsocketClient"));
6+
}
7+
8+
WebsocketClient* WebsocketClient::NewUserdata(lua_State* L)
9+
{
10+
auto* pClient = reinterpret_cast<WebsocketClient*>(lua_newuserdata(L, sizeof(WebsocketClient)));
11+
new (pClient) WebsocketClient();
12+
13+
return pClient;
14+
}
15+
316
client::connection_ptr WebsocketClient::connect(const std::string& t_uri, websocketpp::lib::error_code& ec)
417
{
5-
client::connection_ptr connection = m_client.get_connection(t_uri, ec);
6-
if (ec) return client::connection_ptr();
7-
8-
m_client.connect(connection);
18+
client::connection_ptr connection = m_client.get_connection(t_uri, ec);
19+
if (ec) return client::connection_ptr();
20+
21+
m_client.connect(connection);
922

10-
return connection;
23+
return connection;
1124
}
1225

13-
void WebsocketClient::on_tick() {
14-
m_client.poll();
15-
m_client.reset();
26+
void WebsocketClient::on_tick()
27+
{
28+
m_client.poll();
29+
m_client.reset();
1630
}
1731

18-
context_ptr WebsocketClient::on_tls_init(websocketpp::connection_hdl) {
19-
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
32+
context_ptr WebsocketClient::on_tls_init(websocketpp::connection_hdl)
33+
{
34+
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
2035

21-
try {
22-
ctx->set_options(boost::asio::ssl::context::default_workarounds |
23-
boost::asio::ssl::context::no_sslv2 |
24-
boost::asio::ssl::context::no_sslv3 |
25-
boost::asio::ssl::context::single_dh_use);
36+
try
37+
{
38+
ctx->set_options(
39+
boost::asio::ssl::context::default_workarounds |
40+
boost::asio::ssl::context::no_sslv2 |
41+
boost::asio::ssl::context::no_sslv3 |
42+
boost::asio::ssl::context::single_dh_use);
2643

44+
ctx->set_verify_mode(boost::asio::ssl::verify_none);
45+
}
46+
catch (std::exception& e)
47+
{
48+
std::cout << e.what() << std::endl;
49+
}
2750

28-
ctx->set_verify_mode(boost::asio::ssl::verify_none);
29-
}
30-
catch (std::exception& e) {
31-
std::cout << e.what() << std::endl;
32-
}
33-
return ctx;
51+
return ctx;
3452
}

websocket/WebsocketClient.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
2-
#include <luajit/lua.hpp>
32

43
#include <websocketpp/config/asio_client.hpp>
54
#include <websocketpp/client.hpp>
5+
#include <luajit/lua.hpp>
66

77
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
88
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
@@ -12,23 +12,23 @@ using websocketpp::lib::placeholders::_1;
1212
using websocketpp::lib::placeholders::_2;
1313
using websocketpp::lib::bind;
1414

15-
class WebsocketClient
15+
struct WebsocketClient
1616
{
17-
public:
18-
client m_client;
17+
WebsocketClient() :
18+
m_client()
19+
{
20+
m_client.init_asio();
21+
m_client.set_tls_init_handler(std::bind(&WebsocketClient::on_tls_init, this, ::_1));
22+
};
1923

20-
WebsocketClient() :
21-
m_client()
22-
{
23-
m_client.init_asio();
24+
~WebsocketClient() = default;
2425

25-
m_client.set_tls_init_handler(std::bind(&WebsocketClient::on_tls_init, this, ::_1));
26-
27-
};
26+
static WebsocketClient* FromUserdata(lua_State* L, const int narg);
27+
static WebsocketClient* NewUserdata(lua_State* L);
2828

29-
~WebsocketClient() = default;
29+
client::connection_ptr connect(const std::string& t_uri, websocketpp::lib::error_code& ec);
30+
void on_tick();
31+
context_ptr on_tls_init(websocketpp::connection_hdl);
3032

31-
client::connection_ptr connect(const std::string& t_uri, websocketpp::lib::error_code& ec);
32-
void on_tick();
33-
context_ptr on_tls_init(websocketpp::connection_hdl);
33+
client m_client;
3434
};

websocket/WebsocketConnection.cpp

Lines changed: 114 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,128 @@
11
#include "WebsocketConnection.h"
22

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);
524

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+
}
1232

13-
lua_getfield(m_state, -1, "open");
33+
lua_getfield(m_state, -1, "open");
1434

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+
}
2041

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);
2344

24-
CHECK_STACK(m_state, 0);
45+
CHECK_STACK(m_state, 0);
2546
}
2647

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);
5074
}
5175

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);
73100
}
74101

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);
98128
}

0 commit comments

Comments
 (0)