Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
associative_storage.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <map>
3#include <exception>
6
8
10using websocketpp::lib::placeholders::_1;
11using websocketpp::lib::placeholders::_2;
12using websocketpp::lib::bind;
13
16 std::string name;
17};
18
20public:
21 print_server() : m_next_sessionid(1) {
22 m_server.init_asio();
23
24 m_server.set_open_handler(bind(&print_server::on_open,this,::_1));
25 m_server.set_close_handler(bind(&print_server::on_close,this,::_1));
26 m_server.set_message_handler(bind(&print_server::on_message,this,::_1,::_2));
27 }
28
30 connection_data data;
31
32 data.sessionid = m_next_sessionid++;
33 data.name.clear();
34
35 m_connections[hdl] = data;
36 }
37
40
41 std::cout << "Closing connection " << data.name
42 << " with sessionid " << data.sessionid << std::endl;
43
44 m_connections.erase(hdl);
45 }
46
49
50 if (data.name.empty()) {
51 data.name = msg->get_payload();
52 std::cout << "Setting name of connection with sessionid "
53 << data.sessionid << " to " << data.name << std::endl;
54 } else {
55 std::cout << "Got a message from connection " << data.name
56 << " with sessionid " << data.sessionid << std::endl;
57 }
58 }
59
61 auto it = m_connections.find(hdl);
62
63 if (it == m_connections.end()) {
64 // this connection is not in the list. This really shouldn't happen
65 // and probably means something else is wrong.
66 throw std::invalid_argument("No data available for session");
67 }
68
69 return it->second;
70 }
71
72 void run(uint16_t port) {
73 m_server.listen(port);
74 m_server.start_accept();
75 m_server.run();
76 }
77private:
78 typedef std::map<connection_hdl,connection_data,std::owner_less<connection_hdl>> con_list;
79
80 int m_next_sessionid;
81 server m_server;
82 con_list m_connections;
83};
84
85int main() {
87 server.run(9002);
88}
int main()
websocketpp::server< websocketpp::config::asio > server
void on_message(connection_hdl hdl, server::message_ptr msg)
connection_data & get_data_from_hdl(connection_hdl hdl)
void run(uint16_t port)
void on_close(connection_hdl hdl)
void on_open(connection_hdl hdl)
void set_message_handler(message_handler h)
Definition endpoint.hpp:322
void set_open_handler(open_handler h)
Definition endpoint.hpp:277
void set_close_handler(close_handler h)
Definition endpoint.hpp:282
Server endpoint role based on the given config.
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
unsigned short uint16_t
Definition stdint.h:125