From 9fd87925957929a9ab44c46ec186ed517946ea4b Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Fri, 1 Mar 2013 18:13:35 +0100 Subject: [PATCH] cleaned up a bit more and some bugfixes --- CMakeLists.txt | 2 +- main.cpp | 9 +++++---- websockets.h | 28 +++++++++++++--------------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0dcc55..2c8b1fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ cmake_minimum_required(VERSION 2.8) #join("${warnings}" " " warnings) #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${warnings}") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -g3") project(server) diff --git a/main.cpp b/main.cpp index 362ef7a..8444ad8 100644 --- a/main.cpp +++ b/main.cpp @@ -31,6 +31,7 @@ int uid = 0; std::map people_online; +// NOTE: we cannot use C++ ctors and dtors, because it will be allocated by libwebsockets (= C lib) struct User { unsigned int index; }; @@ -46,8 +47,8 @@ websockets::TestProtocol default_protocol{ return 0; }, [](User& user) -> std::string{ - std::string string_to_send = "People online: " + std::to_string(people_online.size()); - for(auto x : people_online) string_to_send += ", " + x.second; + std::string string_to_send = "Other People:"; + for(auto x : people_online) if(x.first != user.index) string_to_send += " " + x.second; return string_to_send; }, [](User& user, std::string in) -> int{ @@ -56,8 +57,8 @@ websockets::TestProtocol default_protocol{ } }; -static struct libwebsocket_protocols protocols[] = { - WSstandard_protocol("default", default_protocol, User), +static libwebsocket_protocols protocols[] = { + WSstandard_protocol("default", default_protocol), { NULL, NULL, 0 } // end of list }; diff --git a/websockets.h b/websockets.h index b7e26d5..8be6ec9 100644 --- a/websockets.h +++ b/websockets.h @@ -22,7 +22,7 @@ namespace websockets { #define log_function(nice_name, lwsl_name)\ template \ 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 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(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 - " - "licensed under LGPL2.1\n"); - log.notice("Running in server mode\n"); listen_port = port;