Browse Source

cleaned up a bit more and some bugfixes

master
Joshua Moerman 12 years ago
parent
commit
9fd8792595
  1. 2
      CMakeLists.txt
  2. 9
      main.cpp
  3. 28
      websockets.h

2
CMakeLists.txt

@ -13,7 +13,7 @@ cmake_minimum_required(VERSION 2.8)
#join("${warnings}" " " warnings) #join("${warnings}" " " warnings)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${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) project(server)

9
main.cpp

@ -31,6 +31,7 @@
int uid = 0; int uid = 0;
std::map<int, std::string> people_online; std::map<int, std::string> people_online;
// NOTE: we cannot use C++ ctors and dtors, because it will be allocated by libwebsockets (= C lib)
struct User { struct User {
unsigned int index; unsigned int index;
}; };
@ -46,8 +47,8 @@ websockets::TestProtocol<User> default_protocol{
return 0; return 0;
}, },
[](User& user) -> std::string{ [](User& user) -> std::string{
std::string string_to_send = "People online: " + std::to_string(people_online.size()); std::string string_to_send = "Other People:";
for(auto x : people_online) string_to_send += ", " + x.second; for(auto x : people_online) if(x.first != user.index) string_to_send += " " + x.second;
return string_to_send; return string_to_send;
}, },
[](User& user, std::string in) -> int{ [](User& user, std::string in) -> int{
@ -56,8 +57,8 @@ websockets::TestProtocol<User> default_protocol{
} }
}; };
static struct libwebsocket_protocols protocols[] = { static libwebsocket_protocols protocols[] = {
WSstandard_protocol("default", default_protocol, User), WSstandard_protocol("default", default_protocol),
{ NULL, NULL, 0 } // end of list { NULL, NULL, 0 } // end of list
}; };

28
websockets.h

@ -22,7 +22,7 @@ namespace websockets {
#define log_function(nice_name, lwsl_name)\ #define log_function(nice_name, lwsl_name)\
template <typename... T>\ template <typename... T>\
void nice_name(std::string const & format, T... args){\ 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) log_function(notice, lwsl_notice)
@ -60,6 +60,7 @@ namespace websockets {
template <typename T> template <typename T>
struct Protocol { struct Protocol {
typedef T user_type;
int callback(libwebsocket_context *context, libwebsocket *wsi, libwebsocket_callback_reasons reason, void *user, void *in, size_t len){ 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); 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){ 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) { switch (reason) {
case LWS_CALLBACK_ESTABLISHED: case LWS_CALLBACK_ESTABLISHED:
establish_func(user); establish_func(user);
@ -92,9 +91,12 @@ namespace websockets {
case LWS_CALLBACK_CLOSED: case LWS_CALLBACK_CLOSED:
close_func(user); close_func(user);
break; break;
case LWS_CALLBACK_SERVER_WRITEABLE: case LWS_CALLBACK_SERVER_WRITEABLE:{
string_to_send = write_func(user); std::string string_to_send = write_func(user);
n = libwebsocket_write(&wsi, (unsigned char *)string_to_send.c_str(), string_to_send.size(), LWS_WRITE_TEXT); // 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) { if (n < 0) {
lwsl_err("ERROR %d writing to socket, hanging up\n", n); lwsl_err("ERROR %d writing to socket, hanging up\n", n);
return 1; return 1;
@ -104,7 +106,7 @@ namespace websockets {
return -1; return -1;
} }
break; break;
}
case LWS_CALLBACK_RECEIVE: case LWS_CALLBACK_RECEIVE:
receive_func(user, std::string((char*)in, len)); receive_func(user, std::string((char*)in, len));
libwebsocket_callback_on_writable(&context, &wsi); 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)\ [](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);} {return protocol.callback(context, wsi, reason, user, in, len);}
#define WSstandard_protocol(name, protocol, user)\ #define WSstandard_protocol(name, protocol)\
{ name, WSprotocol_callback(protocol), sizeof(user) } { name, WSprotocol_callback(protocol), sizeof(decltype(protocol)::user_type) }
static struct option options[] = { static option options[] = {
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "debug", required_argument, NULL, 'd' }, { "debug", required_argument, NULL, 'd' },
{ "port", required_argument, NULL, 'p' }, { "port", required_argument, NULL, 'p' },
@ -142,7 +144,7 @@ namespace websockets {
const char *interface = NULL; const char *interface = NULL;
int syslog_options = LOG_PID | LOG_PERROR; int syslog_options = LOG_PID | LOG_PERROR;
int listen_port; int listen_port;
struct lws_context_creation_info info; lws_context_creation_info info;
int debug_level = 7; int debug_level = 7;
@ -180,10 +182,6 @@ namespace websockets {
websockets::Log log("lwsts", syslog_options, debug_level); 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"); log.notice("Running in server mode\n");
listen_port = port; listen_port = port;