Merge commit '09987d0f9d32e860f1391bb9c75b799501e2d141' as 'Subtrees/websocket'

This commit is contained in:
Vinnie Falco
2013-06-20 17:15:11 -07:00
255 changed files with 50217 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
BOOST_LIBS=boost_system boost_date_time boost_program_options boost_thread boost_regex
include ../common.mk
LDFLAGS := $(LDFLAGS) -lpthread
chat_server: chat_server.o chat.o
$(CXX) $(CFLAGS) $^ -o $@ $(LDFLAGS)
%.o: %.cpp
$(CXX) -c $(CFLAGS) -o $@ $^
# cleanup by removing generated files
#
.PHONY: clean
clean:
rm -f *.o chat_server

View File

@@ -0,0 +1,20 @@
## chat_server
##
Import('env')
Import('boostlibs')
Import('wslib')
Import('platform_libs')
localenv = env.Clone ()
sources = ["chat_server.cpp","chat.cpp"]
LIBS = [wslib, platform_libs] + boostlibs(['system',
'date_time',
'regex',
'thread'])
prg = localenv.Program('chat_server', sources, LIBS = LIBS)
Return('prg')

View File

@@ -0,0 +1,197 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "chat.hpp"
#include <boost/algorithm/string/replace.hpp>
using namespace websocketchat;
//using chat_server_handler::connection_ptr;
void chat_server_handler::validate(connection_ptr con) {
std::stringstream err;
// We only know about the chat resource
if (con->get_resource() != "/chat") {
err << "Request for unknown resource " << con->get_resource();
throw(websocketpp::http::exception(err.str(),websocketpp::http::status_code::NOT_FOUND));
}
// Require specific origin example
if (con->get_origin() != "http://zaphoyd.com") {
err << "Request from unrecognized origin: " << con->get_origin();
throw(websocketpp::http::exception(err.str(),websocketpp::http::status_code::FORBIDDEN));
}
}
void chat_server_handler::on_open(connection_ptr con) {
std::cout << "client " << con << " joined the lobby." << std::endl;
m_connections.insert(std::pair<connection_ptr,std::string>(con,get_con_id(con)));
// send user list and signon message to all clients
send_to_all(serialize_state());
con->send(encode_message("server","Welcome, use the /alias command to set a name, /help for a list of other commands."));
send_to_all(encode_message("server",m_connections[con]+" has joined the chat."));
}
void chat_server_handler::on_close(connection_ptr con) {
std::map<connection_ptr,std::string>::iterator it = m_connections.find(con);
if (it == m_connections.end()) {
// this client has already disconnected, we can ignore this.
// this happens during certain types of disconnect where there is a
// deliberate "soft" disconnection preceeding the "hard" socket read
// fail or disconnect ack message.
return;
}
std::cout << "client " << con << " left the lobby." << std::endl;
const std::string alias = it->second;
m_connections.erase(it);
// send user list and signoff message to all clients
send_to_all(serialize_state());
send_to_all(encode_message("server",alias+" has left the chat."));
}
void chat_server_handler::on_message(connection_ptr con, message_ptr msg) {
if (msg->get_opcode() != websocketpp::frame::opcode::TEXT) {
return;
}
std::cout << "message from client " << con << ": " << msg->get_payload() << std::endl;
// check for special command messages
if (msg->get_payload() == "/help") {
// print command list
con->send(encode_message("server","avaliable commands:<br />&nbsp;&nbsp;&nbsp;&nbsp;/help - show this help<br />&nbsp;&nbsp;&nbsp;&nbsp;/alias foo - set alias to foo",false));
return;
}
if (msg->get_payload().substr(0,7) == "/alias ") {
std::string response;
std::string alias;
if (msg->get_payload().size() == 7) {
response = "You must enter an alias.";
con->send(encode_message("server",response));
return;
} else {
alias = msg->get_payload().substr(7);
}
response = m_connections[con] + " is now known as "+alias;
// store alias pre-escaped so we don't have to do this replacing every time this
// user sends a message
// escape JSON characters
boost::algorithm::replace_all(alias,"\\","\\\\");
boost::algorithm::replace_all(alias,"\"","\\\"");
// escape HTML characters
boost::algorithm::replace_all(alias,"&","&amp;");
boost::algorithm::replace_all(alias,"<","&lt;");
boost::algorithm::replace_all(alias,">","&gt;");
m_connections[con] = alias;
// set alias
send_to_all(serialize_state());
send_to_all(encode_message("server",response));
return;
}
// catch other slash commands
if ((msg->get_payload())[0] == '/') {
con->send(encode_message("server","unrecognized command"));
return;
}
// create JSON message to send based on msg
send_to_all(encode_message(m_connections[con],msg->get_payload()));
}
// {"type":"participants","value":[<participant>,...]}
std::string chat_server_handler::serialize_state() {
std::stringstream s;
s << "{\"type\":\"participants\",\"value\":[";
std::map<connection_ptr,std::string>::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); it++) {
s << "\"" << (*it).second << "\"";
if (++it != m_connections.end()) {
s << ",";
}
it--;
}
s << "]}";
return s.str();
}
// {"type":"msg","sender":"<sender>","value":"<msg>" }
std::string chat_server_handler::encode_message(std::string sender,std::string msg,bool escape) {
std::stringstream s;
// escape JSON characters
boost::algorithm::replace_all(msg,"\\","\\\\");
boost::algorithm::replace_all(msg,"\"","\\\"");
// escape HTML characters
if (escape) {
boost::algorithm::replace_all(msg,"&","&amp;");
boost::algorithm::replace_all(msg,"<","&lt;");
boost::algorithm::replace_all(msg,">","&gt;");
}
s << "{\"type\":\"msg\",\"sender\":\"" << sender
<< "\",\"value\":\"" << msg << "\"}";
return s.str();
}
std::string chat_server_handler::get_con_id(connection_ptr con) {
std::stringstream endpoint;
//endpoint << con->get_endpoint();
endpoint << con;
return endpoint.str();
}
void chat_server_handler::send_to_all(std::string data) {
std::map<connection_ptr,std::string>::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); it++) {
(*it).first->send(data);
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef CHAT_HPP
#define CHAT_HPP
// com.zaphoyd.websocketpp.chat protocol
//
// client messages:
// alias [UTF8 text, 16 characters max]
// msg [UTF8 text]
//
// server messages:
// {"type":"msg","sender":"<sender>","value":"<msg>" }
// {"type":"participants","value":[<participant>,...]}
#include "../../src/websocketpp.hpp"
#include <map>
#include <string>
#include <vector>
using websocketpp::server;
namespace websocketchat {
class chat_server_handler : public server::handler {
public:
void validate(connection_ptr con);
// add new connection to the lobby
void on_open(connection_ptr con);
// someone disconnected from the lobby, remove them
void on_close(connection_ptr con);
void on_message(connection_ptr con, message_ptr msg);
private:
std::string serialize_state();
std::string encode_message(std::string sender,std::string msg,bool escape = true);
std::string get_con_id(connection_ptr con);
void send_to_all(std::string data);
// list of outstanding connections
std::map<connection_ptr,std::string> m_connections;
};
typedef boost::shared_ptr<chat_server_handler> chat_server_handler_ptr;
}
#endif // CHAT_HPP

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "chat.hpp"
#include "../../src/websocketpp.hpp"
#include <boost/asio.hpp>
#include <iostream>
using namespace websocketchat;
using websocketpp::server;
int main(int argc, char* argv[]) {
short port = 9003;
if (argc == 2) {
// TODO: input validation?
port = atoi(argv[1]);
}
try {
// create an instance of our handler
server::handler::ptr handler(new chat_server_handler());
// create a server that listens on port `port` and uses our handler
server endpoint(handler);
endpoint.alog().set_level(websocketpp::log::alevel::CONNECT);
endpoint.alog().set_level(websocketpp::log::alevel::DISCONNECT);
endpoint.elog().set_level(websocketpp::log::elevel::RERROR);
endpoint.elog().set_level(websocketpp::log::elevel::FATAL);
// setup server settings
// Chat server should only be receiving small text messages, reduce max
// message size limit slightly to save memory, improve performance, and
// guard against DoS attacks.
//server->set_max_message_size(0xFFFF); // 64KiB
std::cout << "Starting chat server on port " << port << std::endl;
endpoint.listen(port);
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}