|
|
@ -22,7 +22,7 @@ namespace websockets { |
|
|
|
#define log_function(nice_name, lwsl_name)\ |
|
|
|
template <typename... T>\ |
|
|
|
void nice_name(std::string const & format, T... args){\ |
|
|
|
lwsl_name(format.c_str(), std::forward(args)...);\ |
|
|
|
lwsl_name(format.c_str(), args...);\ |
|
|
|
} |
|
|
|
|
|
|
|
log_function(notice, lwsl_notice) |
|
|
@ -60,6 +60,7 @@ namespace websockets { |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
struct Protocol { |
|
|
|
typedef T user_type; |
|
|
|
int callback(libwebsocket_context *context, libwebsocket *wsi, libwebsocket_callback_reasons reason, void *user, void *in, size_t len){ |
|
|
|
return call(*context, *wsi, reason, *static_cast<T*>(user), in, len); |
|
|
|
} |
|
|
@ -83,8 +84,6 @@ namespace websockets { |
|
|
|
{} |
|
|
|
|
|
|
|
virtual int call(libwebsocket_context& context, libwebsocket& wsi, libwebsocket_callback_reasons reason, T& user, void *in, size_t len){ |
|
|
|
int n = 0; |
|
|
|
std::string string_to_send; |
|
|
|
switch (reason) { |
|
|
|
case LWS_CALLBACK_ESTABLISHED: |
|
|
|
establish_func(user); |
|
|
@ -92,9 +91,12 @@ namespace websockets { |
|
|
|
case LWS_CALLBACK_CLOSED: |
|
|
|
close_func(user); |
|
|
|
break; |
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE: |
|
|
|
string_to_send = write_func(user); |
|
|
|
n = libwebsocket_write(&wsi, (unsigned char *)string_to_send.c_str(), string_to_send.size(), LWS_WRITE_TEXT); |
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:{ |
|
|
|
std::string string_to_send = write_func(user); |
|
|
|
// we need the extra bytes padding on both sides :(
|
|
|
|
unsigned char * buf = new unsigned char [LWS_SEND_BUFFER_PRE_PADDING + string_to_send.size() + LWS_SEND_BUFFER_POST_PADDING]; |
|
|
|
memcpy(&buf[LWS_SEND_BUFFER_PRE_PADDING], string_to_send.c_str(), string_to_send.size()); |
|
|
|
int n = libwebsocket_write(&wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], string_to_send.size(), LWS_WRITE_TEXT); |
|
|
|
if (n < 0) { |
|
|
|
lwsl_err("ERROR %d writing to socket, hanging up\n", n); |
|
|
|
return 1; |
|
|
@ -104,7 +106,7 @@ namespace websockets { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
case LWS_CALLBACK_RECEIVE: |
|
|
|
receive_func(user, std::string((char*)in, len)); |
|
|
|
libwebsocket_callback_on_writable(&context, &wsi); |
|
|
@ -120,11 +122,11 @@ namespace websockets { |
|
|
|
[](libwebsocket_context *context, libwebsocket *wsi, libwebsocket_callback_reasons reason, void *user, void *in, size_t len)\ |
|
|
|
{return protocol.callback(context, wsi, reason, user, in, len);} |
|
|
|
|
|
|
|
#define WSstandard_protocol(name, protocol, user)\ |
|
|
|
{ name, WSprotocol_callback(protocol), sizeof(user) } |
|
|
|
#define WSstandard_protocol(name, protocol)\ |
|
|
|
{ name, WSprotocol_callback(protocol), sizeof(decltype(protocol)::user_type) } |
|
|
|
|
|
|
|
|
|
|
|
static struct option options[] = { |
|
|
|
static option options[] = { |
|
|
|
{ "help", no_argument, NULL, 'h' }, |
|
|
|
{ "debug", required_argument, NULL, 'd' }, |
|
|
|
{ "port", required_argument, NULL, 'p' }, |
|
|
@ -142,7 +144,7 @@ namespace websockets { |
|
|
|
const char *interface = NULL; |
|
|
|
int syslog_options = LOG_PID | LOG_PERROR; |
|
|
|
int listen_port; |
|
|
|
struct lws_context_creation_info info; |
|
|
|
lws_context_creation_info info; |
|
|
|
|
|
|
|
int debug_level = 7; |
|
|
|
|
|
|
@ -180,10 +182,6 @@ namespace websockets { |
|
|
|
|
|
|
|
websockets::Log log("lwsts", syslog_options, debug_level); |
|
|
|
|
|
|
|
log.notice("libwebsockets echo test - " |
|
|
|
"(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - " |
|
|
|
"licensed under LGPL2.1\n"); |
|
|
|
|
|
|
|
log.notice("Running in server mode\n"); |
|
|
|
listen_port = port; |
|
|
|
|
|
|
|