-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
130 lines (114 loc) · 4.26 KB
/
server.cpp
File metadata and controls
130 lines (114 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
#include <unordered_map>
#include "Connection.hpp"
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::connection_hdl;
// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
std::map<connection_hdl, Connection*, std::owner_less<connection_hdl>> ws_to_conn;
std::unordered_map<Connection*, connection_hdl> conn_to_ws;
Client client;
server wsserver;
char* forward_port;
void on_open(server* s, connection_hdl hdl) {
websocketpp::lib::error_code ec;
auto conn = client.connect("127.0.0.1", forward_port);
if (!conn) {
s->close(hdl, websocketpp::close::status::normal, "server port not open", ec);
if (ec) {
std::cout << "Close failed because: " << ec.message() << std::endl;
}
} else {
std::cout << "Connected to 127.0.0.1:" << forward_port << std::endl;
ws_to_conn[hdl] = conn;
conn_to_ws[conn] = hdl;
}
}
void on_message(server* s, connection_hdl hdl, message_ptr msg) {
auto m = msg->get_payload();
auto connection = ws_to_conn[hdl];
connection->send_raw(m.c_str(), m.size());
}
void on_fail(server* s, connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
std::cout << con->get_response_header("Client") << " failed because "
<< con->get_ec().message();
if (ws_to_conn.find(hdl) == ws_to_conn.end()) {
return;
}
auto conn = ws_to_conn[hdl];
ws_to_conn.erase(hdl);
conn_to_ws.erase(conn);
conn->close();
}
void on_close(server* s, connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
std::cout << con->get_response_header("Client") << " closed because "
<< con->get_remote_close_reason() << ", close code: "
<< con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code());
if (ws_to_conn.find(hdl) == ws_to_conn.end()) {
return;
}
auto conn = ws_to_conn[hdl];
ws_to_conn.erase(hdl);
conn_to_ws.erase(conn);
conn->close();
}
void on_local(Connection *connection, Connection::Event evt) {
websocketpp::lib::error_code ec;
if (evt == Connection::OnRecv) {
std::vector<uint8_t> data = connection->recv_buffer;
connection->recv_buffer.clear();
wsserver.send(conn_to_ws[connection], data.data(), data.size(),
websocketpp::frame::opcode::binary, ec);
if (ec) {
std::cout << "Send failed because: " << ec.message() << std::endl;
}
} else if (evt == Connection::OnClose) {
if (conn_to_ws.find(connection) == conn_to_ws.end()) {
return;
}
auto handle = conn_to_ws[connection];
ws_to_conn.erase(handle);
conn_to_ws.erase(connection);
wsserver.close(handle, websocketpp::close::status::normal, "bye", ec);
if (ec) {
std::cout << "Close failed because: " << ec.message() << std::endl;
}
}
}
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << argv[0] << " listen_port forward_port" << std::endl;
return -1;
}
uint32_t listen_port = strtoul(argv[1], nullptr, 0);
forward_port = argv[2];
wsserver.set_access_channels(websocketpp::log::alevel::all);
wsserver.clear_access_channels(websocketpp::log::alevel::frame_payload);
wsserver.init_asio();
wsserver.start_perpetual();
wsserver.set_message_handler(bind(&on_message, &wsserver, ::_1, ::_2));
wsserver.set_fail_handler(bind(&on_fail, &wsserver, ::_1));
wsserver.set_close_handler(bind(&on_close, &wsserver, ::_1));
wsserver.set_open_handler(bind(&on_open, &wsserver, ::_1));
wsserver.listen(listen_port);
wsserver.start_accept();
std::cout << "listening on port " << listen_port << std::endl;
try {
while (true) {
client.poll(on_local, 0);
wsserver.poll();
usleep(1000);
}
} catch (...) {
wsserver.stop_perpetual();
std::cout << "Bye!" << std::endl;
}
}