-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_handler.cpp
More file actions
89 lines (83 loc) · 3.68 KB
/
Copy pathconsole_handler.cpp
File metadata and controls
89 lines (83 loc) · 3.68 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
#include "console_handler.hpp"
#include "tcp_server.hpp"
#include <iostream>
#include <boost/endian/conversion.hpp>
#include <vector>
#include "tcp_connection.hpp"
#include <string>
#include <functional>
#include "demangle.h"
#include "allocator_handler.hpp"
void console_handler::read()
{
boost::asio::async_read_until(input_stream_,
read_buffer_,
'\n',
make_allocation_handler(console_read_handler_memory_,
std::bind(&console_handler::read_handler,
this,
std::placeholders::_1,
std::placeholders::_2)));
}
void console_handler::read_handler(const boost::system::error_code &err, size_t byte_read)
{
if (err) {
std::cerr << "Error reading from stdin: " << err.message() << std::endl;
} else {
unsigned int buffer_size = tcp_connection::HEADER_SIZE + byte_read;
std::shared_ptr<std::vector<unsigned char>> command(new std::vector<unsigned char>());
command->reserve(buffer_size);
boost::asio::buffers_iterator<boost::asio::const_buffer, unsigned char> buffer_iter = boost::asio::buffers_iterator<boost::asio::const_buffer, unsigned char>::begin(read_buffer_.data());
command->assign(tcp_connection::HEADER_SIZE, 0x00);
for (size_t i = tcp_connection::HEADER_SIZE; i < buffer_size; i++) {
command->emplace_back(*buffer_iter++);
}
read_buffer_.consume(byte_read);
if (memcmp("exit\n", &(*command)[tcp_connection::HEADER_SIZE], byte_read) == 0) {
tcpserver->stop();
return;
} else if (memcmp("clear\n", &(*command)[tcp_connection::HEADER_SIZE], byte_read) == 0) {
tcpserver->clear(); // close all connections but keep accepting new connections
} else if (memcmp("unaccept\n", &(*command)[tcp_connection::HEADER_SIZE], byte_read) == 0) {
tcpserver->unaccept(); // keep connecting new connections but don't allow new connection
} else if (memcmp("info\n", &(*command)[tcp_connection::HEADER_SIZE], byte_read) == 0) {
if (tcpserver->is_accepting()) {
std::cout << "Server is accepting" << std::endl;
} else {
std::cout << "Server is NOT accepting" << std::endl;
}
if (tcpserver->get_connection_list().empty()) {
std::cout << "No client" << std::endl;
} else {
std::cout << "Connection list: " << std::endl;
for(auto conn : tcpserver->get_connection_list()) {
std::cout << conn->socket().remote_endpoint().address().to_string() << ":" << conn->socket().remote_endpoint().port() << std::endl;
}
}
} else if (memcmp("hb\n", &(*command)[tcp_connection::HEADER_SIZE], byte_read) == 0) {
if (tcpserver->get_connection_list().empty()) {
std::cout << "No client" << std::endl;
} else {
*(unsigned int*)((*command).data()) = 0;
(*command)[4] = 0x55; (*command)[5] = 0xAA;
tcpserver->broadcast_message(command);
}
} else {
if (tcpserver->get_connection_list().empty()) {
std::cout << "No client" << std::endl;
} else {
*(unsigned int*)((*command).data()) = boost::endian::native_to_big((unsigned int)byte_read);
(*command)[4] = 0x55; (*command)[5] = 0xAA;
tcpserver->broadcast_message(command);
}
}
read();
}
}
console_handler::console_handler(boost::asio::io_service &io_service,
tcp_server *tcpserver) :
input_stream_(io_service, STDIN_FILENO),
tcpserver(tcpserver)
{
read();
}