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)
#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)

9
main.cpp

@ -31,6 +31,7 @@
int uid = 0;
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 {
unsigned int index;
};
@ -46,8 +47,8 @@ websockets::TestProtocol<User> 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<User> 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
};

28
websockets.h

@ -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;