| 1 |
#include "wx/defs.h" |
| 2 |
#ifdef __WINDOWS__ // __WINDOWS__ defined by wx/defs.h |
| 3 |
// includes windows.h and if wxUSE_WINSOCK2 is true includes winsock2.h |
| 4 |
#include "wx/msw/wrapwin.h" |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "SocketAPI/clSocketBase.h" |
| 8 |
#include "clWebSocketClient.h" |
| 9 |
|
| 10 |
// No boost or other external libraries, just plain old good C++11 |
| 11 |
#define ASIO_STANDALONE 1 |
| 12 |
#define _WEBSOCKETPP_CPP11_THREAD_ 1 |
| 13 |
|
| 14 |
#include "file_logger.h" |
| 15 |
|
| 16 |
#include <websocketpp/client.hpp> |
| 17 |
#include <websocketpp/config/asio_no_tls_client.hpp> |
| 18 |
|
| 19 |
wxDEFINE_EVENT(wxEVT_WEBSOCKET_CONNECTED, clCommandEvent); |
| 20 |
wxDEFINE_EVENT(wxEVT_WEBSOCKET_DISCONNECTED, clCommandEvent); |
| 21 |
wxDEFINE_EVENT(wxEVT_WEBSOCKET_ONMESSAGE, clCommandEvent); |
| 22 |
wxDEFINE_EVENT(wxEVT_WEBSOCKET_ERROR, clCommandEvent); |
| 23 |
|
| 24 |
typedef websocketpp::client<websocketpp::config::asio_client> Client_t; |
| 25 |
// pull out the type of messages sent by our config |
| 26 |
typedef websocketpp::config::asio_client::message_type::ptr message_ptr; |
| 27 |
|
| 28 |
class clWebSocketHelperThread : public clJoinableThread |
| 29 |
{ |
| 30 |
wxEvtHandler* m_owner = nullptr; |
| 31 |
wxString m_url; |
| 32 |
Client_t* m_client = nullptr; |
| 33 |
|
| 34 |
public: |
| 35 |
clWebSocketHelperThread(wxEvtHandler* owner, const wxString& url, Client_t* c) |
| 36 |
: m_owner(owner) |
| 37 |
, m_url(url) |
| 38 |
, m_client(c) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
virtual ~clWebSocketHelperThread() {} |
| 43 |
|
| 44 |
void* Entry() |
| 45 |
{ |
| 46 |
while(!TestDestroy()) { |
| 47 |
try { |
| 48 |
// Start the main loop |
| 49 |
// We use a second try/catch block here to make sure that the error reporting is done |
| 50 |
// Start the ASIO io_service run loop |
| 51 |
// this will cause a single connection to be made to the server. c.run() |
| 52 |
// will exit when this connection is closed. |
| 53 |
m_client->run(); |
| 54 |
|
| 55 |
} catch(websocketpp::exception& e) { |
| 56 |
// Report an error |
| 57 |
clCommandEvent event(wxEVT_WEBSOCKET_ERROR); |
| 58 |
event.SetString(e.what()); |
| 59 |
m_owner->AddPendingEvent(event); |
| 60 |
} |
| 61 |
} |
| 62 |
m_owner->CallAfter(&clWebSocketClient::OnHelperThreadExit); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
using websocketpp::lib::bind; |
| 68 |
using websocketpp::lib::placeholders::_1; |
| 69 |
using websocketpp::lib::placeholders::_2; |
| 70 |
|
| 71 |
// This message handler will be invoked once for each incoming message |
| 72 |
static void on_ws_message(clWebSocketClient* c, websocketpp::connection_hdl hdl, message_ptr msg) |
| 73 |
{ |
| 74 |
LOG_IF_TRACE { clDEBUG1() << "<--" << msg->get_payload(); } |
| 75 |
clCommandEvent event(wxEVT_WEBSOCKET_ONMESSAGE); |
| 76 |
event.SetString(msg->get_payload()); |
| 77 |
event.SetEventObject(c); |
| 78 |
c->GetOwner()->AddPendingEvent(event); |
| 79 |
} |
| 80 |
|
| 81 |
static void on_ws_open_handler(clWebSocketClient* c, websocketpp::connection_hdl hdl) |
| 82 |
{ |
| 83 |
LOG_IF_TRACE { clDEBUG1() << "<-- Connected!"; } |
| 84 |
c->SetConnectionHandle(hdl); |
| 85 |
clCommandEvent event(wxEVT_WEBSOCKET_CONNECTED); |
| 86 |
event.SetEventObject(c); |
| 87 |
c->GetOwner()->AddPendingEvent(event); |
| 88 |
} |
| 89 |
|
| 90 |
static void on_ws_fail_handler(clWebSocketClient* c, websocketpp::connection_hdl hdl) |
| 91 |
{ |
| 92 |
LOG_IF_TRACE { clDEBUG1() << "<-- Error!"; } |
| 93 |
clCommandEvent event(wxEVT_WEBSOCKET_ERROR); |
| 94 |
event.SetEventObject(c); |
| 95 |
c->GetOwner()->AddPendingEvent(event); |
| 96 |
} |
| 97 |
|
| 98 |
clWebSocketClient::clWebSocketClient(wxEvtHandler* owner) |
| 99 |
: m_owner(owner) |
| 100 |
{ |
| 101 |
DoInit(); |
| 102 |
} |
| 103 |
|
| 104 |
clWebSocketClient::~clWebSocketClient() |
| 105 |
{ |
| 106 |
Close(); |
| 107 |
DoCleanup(); |
| 108 |
} |
| 109 |
|
| 110 |
void clWebSocketClient::StartLoop(const wxString& url) |
| 111 |
{ |
| 112 |
if(m_helperThread) { |
| 113 |
throw clSocketException("A websocket loop is already running"); |
| 114 |
} |
| 115 |
|
| 116 |
Client_t* c = GetClient<Client_t>(); |
| 117 |
if(!c) { |
| 118 |
throw clSocketException("Invalid connection!"); |
| 119 |
} |
| 120 |
try { |
| 121 |
std::string uri = url.mb_str(wxConvUTF8).data(); |
| 122 |
// Register our message handler |
| 123 |
websocketpp::lib::error_code ec; |
| 124 |
Client_t::connection_ptr con = c->get_connection(uri, ec); |
| 125 |
if(ec) { |
| 126 |
throw clSocketException(ec.message()); |
| 127 |
} |
| 128 |
|
| 129 |
// Note that connect here only requests a connection. No network messages are |
| 130 |
// exchanged until the event loop starts running in the next line. |
| 131 |
c->connect(con); |
| 132 |
} catch(websocketpp::exception& e) { |
| 133 |
m_connection_handle.reset(); |
| 134 |
throw clSocketException(e.what()); |
| 135 |
} |
| 136 |
|
| 137 |
// Run the main loop in background thread |
| 138 |
m_helperThread = new clWebSocketHelperThread(this, url, c); |
| 139 |
m_helperThread->Start(); |
| 140 |
} |
| 141 |
|
| 142 |
void clWebSocketClient::Send(const wxString& data) |
| 143 |
{ |
| 144 |
Client_t* c = GetClient<Client_t>(); |
| 145 |
if(!c) { |
| 146 |
throw clSocketException("Invalid connection!"); |
| 147 |
} |
| 148 |
if(m_connection_handle.expired()) { |
| 149 |
throw clSocketException("Invalid connection handle!"); |
| 150 |
} |
| 151 |
|
| 152 |
try { |
| 153 |
std::string str = data.mb_str(wxConvUTF8).data(); |
| 154 |
websocketpp::lib::error_code ec; |
| 155 |
c->send(GetConnectionHandle(), str, websocketpp::frame::opcode::TEXT, ec); |
| 156 |
|
| 157 |
} catch(websocketpp::exception& e) { |
| 158 |
throw clSocketException(e.what()); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void clWebSocketClient::Close() |
| 163 |
{ |
| 164 |
Client_t* c = GetClient<Client_t>(); |
| 165 |
if(!c) { |
| 166 |
return; |
| 167 |
} |
| 168 |
c->stop(); |
| 169 |
DoCleanup(); |
| 170 |
} |
| 171 |
|
| 172 |
void clWebSocketClient::DoCleanup() |
| 173 |
{ |
| 174 |
// Cleanup: first close the helper thread |
| 175 |
wxDELETE(m_helperThread); |
| 176 |
m_connection_handle.reset(); |
| 177 |
Client_t* c = GetClient<Client_t>(); |
| 178 |
wxDELETE(c); |
| 179 |
m_client = nullptr; |
| 180 |
} |
| 181 |
|
| 182 |
void clWebSocketClient::OnHelperThreadExit() |
| 183 |
{ |
| 184 |
DoCleanup(); |
| 185 |
clCommandEvent event(wxEVT_WEBSOCKET_DISCONNECTED); |
| 186 |
event.SetEventObject(this); |
| 187 |
GetOwner()->AddPendingEvent(event); |
| 188 |
} |
| 189 |
|
| 190 |
void clWebSocketClient::DoInit() |
| 191 |
{ |
| 192 |
// Dont initialise again |
| 193 |
if(m_client) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
try { |
| 198 |
m_client = new Client_t(); |
| 199 |
Client_t* c = GetClient<Client_t>(); |
| 200 |
// c->set_access_channels(websocketpp::log::alevel::all); |
| 201 |
c->clear_access_channels(websocketpp::log::alevel::all); |
| 202 |
c->init_asio(); |
| 203 |
c->set_message_handler(bind(&on_ws_message, this, ::_1, ::_2)); |
| 204 |
c->set_open_handler(bind(&on_ws_open_handler, this, ::_1)); |
| 205 |
c->set_fail_handler(bind(&on_ws_fail_handler, this, ::_1)); |
| 206 |
} catch(websocketpp::exception& e) { |
| 207 |
clERROR() << e.what(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void clWebSocketClient::Initialise() { DoInit(); } |