Browse Source

forgot to add .cpp, cleaned up CMakeLists, and some stuff

master
Joshua Moerman 12 years ago
parent
commit
b21f851fc8
  1. 23
      CMakeLists.txt
  2. 43
      main.cpp
  3. 7
      vanillabuild
  4. 75
      websockets.cpp
  5. 86
      websockets.h

23
CMakeLists.txt

@ -1,24 +1,12 @@
cmake_minimum_required(VERSION 2.8)
#include(motor/cmake/set_system_include_flag.cmake)
#include(motor/cmake/address_sanitizer.cmake)
#include(motor/cmake/enable_gnu11.cmake)
#include(motor/cmake/warning_settings.cmake)
#get_sane_warning_flags(warnings)
#include(motor/cmake/join.cmake)
#join("${warnings}" " " warnings)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${warnings}")
set(CMAKE_CSS_FLAGS "#{CMAKE_CSS_FLAGS} -Wdeprecated-declarations")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -g3")
project(server)
add_subdirectory(${PROJECT_SOURCE_DIR}/contrib/libwebsockets/)
set(WITHOUT_TESTAPPS CACHE BOOL ON)
find_package(Boost 1.48 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
@ -28,7 +16,8 @@ set(boost ${Boost_LIBRARIES})
include_directories("${PROJECT_SOURCE_DIR}")
include_directories(SYSTEM "${PROJECT_SOURCE_DIR}/contrib/libwebsockets/")
set(all_friggin_libraries ${boost} websockets_shared)
set(all_libraries ${boost} websockets)
file(GLOB all_files "*.cpp")
add_executable(server main.cpp)
target_link_libraries(server ${all_friggin_libraries})
add_executable(server ${all_files})
target_link_libraries(server ${all_libraries})

43
main.cpp

@ -1,26 +1,3 @@
/*
* libwebsockets-test-echo - libwebsockets echo test implementation
*
* This implements both the client and server sides. It defaults to
* serving, use --client <remote address> to connect as client.
*
* Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <map>
#include <string>
@ -37,24 +14,28 @@ struct User {
};
websockets::TestProtocol<User> default_protocol{
[](User& user) -> int{
// connection established
[](User& user) -> int{
user.index = uid++;
people_online[user.index] = "";
return 0;
},
[](User& user) -> int{
},
// connection closed
[](User& user) -> int{
people_online.erase(user.index);
return 0;
},
[](User& user) -> std::string{
},
// write (will always come after receive)
[](User& user) -> std::string{
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{
},
// receive
[](User& user, std::string in) -> int{
people_online[user.index] = in;
return 0;
}
}
};
static libwebsocket_protocols protocols[] = {

7
vanillabuild

@ -0,0 +1,7 @@
#!/bin/bash
mkdir -p build
cd build
cmake ..
make -j1
cd ..

75
websockets.cpp

@ -0,0 +1,75 @@
#include "websockets.h"
namespace websockets {
option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "ssl", no_argument, NULL, 's' },
{ "interface", required_argument, NULL, 'i' },
{ NULL, 0, 0, 0 }
};
int default_main(int argc, char **argv, libwebsocket_protocols* protocols){
int port = 7681;
int use_ssl = 0;
char interface_name[128] = "";
const char *interface = NULL;
int n = 0;
while (n >= 0) {
n = getopt_long(argc, argv, "i:hsp:", options, NULL);
if (n < 0) continue;
switch (n) {
case 's':
use_ssl = 1; /* 1 = take care about cert verification, 2 = allow anything */
break;
case 'p':
port = atoi(optarg);
break;
case 'i':
strncpy(interface_name, optarg, sizeof interface_name);
interface_name[(sizeof interface_name) - 1] = '\0';
interface = interface_name;
break;
case '?':
case 'h':
fprintf(stderr, "Usage: libwebsockets-test-echo [--ssl] [--port=<p>] [--interface=iface]");
exit(1);
}
}
websockets::Log log("lwsts", LOG_PID | LOG_PERROR, 7);
log.notice("Running in server mode\n");
lws_context_creation_info info;
memset(&info, 0, sizeof info);
info.port = port;
info.iface = interface;
info.protocols = protocols;
info.gid = -1;
info.uid = -1;
if (use_ssl) {
info.ssl_cert_filepath = "libwebsockets-test-server.pem";
info.ssl_private_key_filepath = "libwebsockets-test-server.key.pem";
}
websockets::Context context(info);
static bool force_exit = false;
signal(SIGINT, [](int){ force_exit = true; });
while (!force_exit) {
auto ret = libwebsocket_service(context.get_raw(), 10);
if(ret < 0) break;
}
log.notice("libwebsockets-test-echo exited cleanly\n");
return 0;
}
} // namespace websockets

86
websockets.h

@ -125,90 +125,6 @@ namespace websockets {
#define WSstandard_protocol(name, protocol)\
{ name, WSprotocol_callback(protocol), sizeof(decltype(protocol)::user_type) }
static option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "debug", required_argument, NULL, 'd' },
{ "port", required_argument, NULL, 'p' },
{ "ssl", no_argument, NULL, 's' },
{ "interface", required_argument, NULL, 'i' },
{ NULL, 0, 0, 0 }
};
inline int default_main(int argc, char **argv, libwebsocket_protocols* protocols){
int n = 0;
int port = 7681;
int use_ssl = 0;
int opts = 0;
char interface_name[128] = "";
const char *interface = NULL;
int syslog_options = LOG_PID | LOG_PERROR;
int listen_port;
lws_context_creation_info info;
int debug_level = 7;
memset(&info, 0, sizeof info);
while (n >= 0) {
n = getopt_long(argc, argv, "i:hsp:d:D"
, options, NULL);
if (n < 0)
continue;
switch (n) {
case 'd':
debug_level = atoi(optarg);
break;
case 's':
use_ssl = 1; /* 1 = take care about cert verification, 2 = allow anything */
break;
case 'p':
port = atoi(optarg);
break;
case 'i':
strncpy(interface_name, optarg, sizeof interface_name);
interface_name[(sizeof interface_name) - 1] = '\0';
interface = interface_name;
break;
case '?':
case 'h':
fprintf(stderr, "Usage: libwebsockets-test-echo "
"[--ssl] "
"[--port=<p>] "
"[-d <log bitfield>]\n");
exit(1);
}
}
websockets::Log log("lwsts", syslog_options, debug_level);
log.notice("Running in server mode\n");
listen_port = port;
info.port = listen_port;
info.iface = interface;
info.protocols = protocols;
if (use_ssl) {
info.ssl_cert_filepath = "libwebsockets-test-server.pem";
info.ssl_private_key_filepath = "libwebsockets-test-server.key.pem";
}
info.gid = -1;
info.uid = -1;
info.options = opts;
static bool force_exit = false;
signal(SIGINT, [](int){ force_exit = true; });
websockets::Context context(info);
while (!force_exit) {
auto ret = libwebsocket_service(context.get_raw(), 10);
if(ret < 0) break;
}
log.notice("libwebsockets-test-echo exited cleanly\n");
return 0;
}
int default_main(int argc, char **argv, libwebsocket_protocols* protocols);
} // namespace websockets