#include <iostream>
#include <map>
#include<thread>
#include <exception>
#include <typeinfo>
#include<boost/property_tree/ptree.hpp>
#include<boost/property_tree/json_parser.hpp>
#include<boost/property_tree/exceptions.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include "libs/connection_data.hpp"
#include "libs/json_handler.hpp"
/********************************************************/
/********************************************************/
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
/********************************************************/
/********************************************************/
class print_server {
public:
print_server() : m_next_sessionid(1) {
m_server.init_asio();
m_server.set_open_handler(bind(&print_server::on_open,this,::_1));
m_server.set_close_handler(bind(&print_server::on_close,this,::_1));
m_server.set_message_handler(bind(&print_server::on_message,this,::_1,::_2));
}
/********************************************************/
/********************************************************/
void on_open(connection_hdl hdl) {
connection_data data;
data.sessionid = m_next_sessionid++;
data.name = "";
m_connections[hdl] = data;
}
/********************************************************/
/********************************************************/
void on_close(connection_hdl hdl) {
connection_data& data = get_data_from_hdl(hdl);
std::cout << "Closing connection " << data.name
<< " with sessionid " << data.sessionid << std::endl;
m_connections.erase(hdl);
}
/********************************************************/
/********************************************************/
void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_data& data = get_data_from_hdl(hdl);
try {
m_server.send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
data.json=jsonh::json2ptree(msg->get_payload());
jsonh::print_session_ID(data);
std::cout<<"message: "<<msg->get_payload()<<std::endl;
if (data.name == "") {
data.name = msg->get_payload();
std::cout << "Setting name of connection with sessionid "
<< data.sessionid << " to " << data.name << std::endl;
} else {
std::cout << "Got a message from connection " << data.name
<< " with sessionid " << data.sessionid << std::endl;
}
}
/********************************************************/
/********************************************************/
connection_data& get_data_from_hdl(connection_hdl hdl) {
auto it = m_connections.find(hdl);
if (it == m_connections.end()) {
// this connection is not in the list. This really shouldn't happen
// and probably means something else is wrong.
throw std::invalid_argument("No data available for session");
}
return it->second;
}
/********************************************************/
/********************************************************/
void run(uint16_t &port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
/********************************************************/
/********************************************************/
void exit_server() {
m_server.stop();
}
/********************************************************/
/********************************************************/
private:
typedef std::map<connection_hdl,connection_data,std::owner_less<connection_hdl>> con_list;
unsigned int m_next_sessionid;
server m_server;
con_list m_connections;
};
void control(print_server &server){
while(1){
std::string input;
std::cout<<"please enter a command"<<std::endl;
std::cout<<"EXIT to enter"<<std::endl;
std::cin>>input;
if(input=="EXIT"){
std::cout<<"exiting!"<<std::endl;
server.exit_server();
break;
}
}
}
/********************************************************/
/********************************************************/
int main() {
uint16_t port=9002;
print_server server;
std::thread t1(control,std::ref(server));
server.run(port);
t1.join();
}
/********************************************************/
/********************************************************/
void exit_server() {
m_server.stop_listening();
std::cout<<"server is listening?: "<<m_server.is_listening()<<std::endl;
m_server.stop();
}
server is listening?: 0
[2014-05-15 17:44:14] [info] asio async_shutdown error: system:9 (Bad file descriptor)
[2014-05-15 17:44:14] [disconnect] Failed: Operation canceled
[2014-05-15 17:44:14] [info] handle_accept error: Operation canceled
[2014-05-15 17:44:14] [info] Stopping acceptance of new connections because the underlying transport is no longer listening.
$ ./associative_storage.wspp
please enter a command
EXIT to enter
[2014-05-15 17:44:45] [info] asio listen error: system:98 (Address already in use)
terminate called after throwing an instance of 'std::error_code'
--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
void exit_server() {
m_server.stop_listening();
for (auto it : m_connections) {
try{
m_server.close(it.first,404,"shutdown");
}catch(websocketpp::lib::error_code ec){
std::cout<<"lib::error_code "<<ec<<std::endl;
}
}
}
[2014-05-16 01:34:33] [info] asio async_shutdown error: system:9 (Bad file descriptor)
[2014-05-16 01:34:33] [disconnect] Failed: Operation canceled
[2014-05-16 01:34:33] [info] handle_accept error: Operation canceled
[2014-05-16 01:34:33] [info] Stopping acceptance of new connections because the underlying transport is no longer listening.
lib::error_code websocketpp.processor:25
void exit_server() {
m_server.stop_listening();
for (auto& it : m_connections) {
try{
m_server.close(it.first, websocketpp::close::status::normal, "");
}catch(websocketpp::lib::error_code ec){
std::cout<<"lib::error_code "<<ec<<std::endl;
}
}
}
void start_accept(lib::error_code & ec) { if (!transport_type::is_listening()) { ec = error::make_error_code(error::async_accept_not_listening); return; }
ec = lib::error_code(); connection_ptr con = get_connection();
transport_type::async_accept( lib::static_pointer_cast<transport_con_type>(con), lib::bind(&type::handle_accept,this,con,lib::placeholders::_1), ec );
if (ec && con) { // If the connection was constructed but the accept failed, // terminate the connection to prevent memory leaks con->terminate(lib::error_code()); } }