You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.9 KiB
130 lines
3.9 KiB
|
|
#include <string>
|
|
#include <memory>
|
|
#include <functional>
|
|
#include <stdexcept>
|
|
|
|
#include <syslog.h>
|
|
#include <getopt.h>
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
|
|
#include <lib/libwebsockets.h>
|
|
|
|
namespace websockets {
|
|
struct Log {
|
|
Log(std::string const & name, int syslog_options, int debug_level){
|
|
setlogmask(LOG_UPTO (LOG_DEBUG));
|
|
openlog(name.c_str(), syslog_options, LOG_DAEMON);
|
|
lws_set_log_level(debug_level, lwsl_emit_syslog);
|
|
}
|
|
|
|
#define log_function(nice_name, lwsl_name)\
|
|
template <typename... T>\
|
|
void nice_name(std::string const & format, T... args){\
|
|
lwsl_name(format.c_str(), args...);\
|
|
}
|
|
|
|
log_function(notice, lwsl_notice)
|
|
log_function(error, lwsl_err)
|
|
log_function(warning, lwsl_warn)
|
|
log_function(debug, lwsl_debug)
|
|
|
|
#undef log_function
|
|
|
|
~Log(){
|
|
closelog();
|
|
}
|
|
};
|
|
|
|
struct Context {
|
|
std::unique_ptr<libwebsocket_context, decltype(libwebsocket_context_destroy)*> context;
|
|
|
|
Context(lws_context_creation_info& info)
|
|
: context(libwebsocket_create_context(&info), &libwebsocket_context_destroy)
|
|
{
|
|
if(context == nullptr){
|
|
lwsl_err("libwebsocket init failed\n");
|
|
throw std::runtime_error("libwebsocket init failed");
|
|
}
|
|
}
|
|
|
|
libwebsocket_context const * get_raw() const {
|
|
return context.get();
|
|
}
|
|
|
|
libwebsocket_context * get_raw() {
|
|
return context.get();
|
|
}
|
|
};
|
|
|
|
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);
|
|
}
|
|
virtual int call(libwebsocket_context& context, libwebsocket& wsi, libwebsocket_callback_reasons reason, T& user, void *in, size_t len){
|
|
return 0;
|
|
};
|
|
};
|
|
|
|
template <typename T>
|
|
struct TestProtocol : public Protocol<T> {
|
|
std::function<int(T&)> establish_func;
|
|
std::function<int(T&)> close_func;
|
|
std::function<std::string(T&)> write_func;
|
|
std::function<int(T&, std::string)> receive_func;
|
|
|
|
TestProtocol(decltype(establish_func) establish_func, decltype(close_func) close_func, decltype(write_func) write_func, decltype(receive_func) receive_func)
|
|
: establish_func(establish_func)
|
|
, close_func(close_func)
|
|
, write_func(write_func)
|
|
, receive_func(receive_func)
|
|
{}
|
|
|
|
virtual int call(libwebsocket_context& context, libwebsocket& wsi, libwebsocket_callback_reasons reason, T& user, void *in, size_t len){
|
|
switch (reason) {
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
establish_func(user);
|
|
break;
|
|
case LWS_CALLBACK_CLOSED:
|
|
close_func(user);
|
|
break;
|
|
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;
|
|
}
|
|
if (n < string_to_send.size()) {
|
|
lwsl_err("Partial write\n");
|
|
return -1;
|
|
}
|
|
break;
|
|
}
|
|
case LWS_CALLBACK_RECEIVE:
|
|
receive_func(user, std::string((char*)in, len));
|
|
libwebsocket_callback_on_writable(&context, &wsi);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
};
|
|
|
|
#define WSprotocol_callback(protocol)\
|
|
[](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)\
|
|
{ name, WSprotocol_callback(protocol), sizeof(decltype(protocol)::user_type) }
|
|
|
|
int default_main(int argc, char **argv, libwebsocket_protocols* protocols);
|
|
|
|
} // namespace websockets
|
|
|