forked from riscv-software-src/riscv-isa-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocketif.cc
74 lines (64 loc) · 2.44 KB
/
socketif.cc
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
// See LICENSE for license details.
#include "socketif.h"
#ifdef HAVE_BOOST_ASIO
#include <iostream>
socketif_t::socketif_t()
{
try { // create socket server
using boost::asio::ip::tcp;
io_service_ptr = new boost::asio::io_service;
acceptor_ptr = new tcp::acceptor(*io_service_ptr, tcp::endpoint(tcp::v4(), 0));
// acceptor is created passing argument port=0, so O.S. will choose a free port
std::string name = boost::asio::ip::host_name();
std::cout << "Listening for debug commands on " << name.substr(0,name.find('.'))
<< " port " << acceptor_ptr->local_endpoint().port() << " ." << std::endl;
// at the end, add space and some other character for convenience of javascript .split(" ")
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
exit(-1);
}
}
socketif_t::~socketif_t()
{
delete io_service_ptr;
delete acceptor_ptr;
}
// read input command string
std::string socketif_t::rin(std::ostream &sout_)
{
std::string s;
try {
socket_ptr.reset(new boost::asio::ip::tcp::socket(*io_service_ptr));
acceptor_ptr->accept(*socket_ptr); // wait for someone to open connection
boost::asio::streambuf buf;
boost::asio::read_until(*socket_ptr, buf, "\n"); // wait for command
s = boost::asio::buffer_cast<const char*>(buf.data());
boost::erase_all(s, "\r"); // get rid off any cr and lf
boost::erase_all(s, "\n");
// The socket client is a web server and it appends the IP of the computer
// that sent the command from its web browser.
// For now, erase the IP if it is there.
boost::regex re(" ((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
"(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$");
s = boost::regex_replace(s, re, (std::string)"");
// TODO: check the IP against the IP used to upload RISC-V source files
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
// output goes to socket
sout_.rdbuf(&bout);
return s;
}
// write sout_ to socket (via bout)
void socketif_t::wout() {
try {
boost::system::error_code ignored_error;
boost::asio::write(*socket_ptr, bout, boost::asio::transfer_all(), ignored_error);
socket_ptr->close(); // close the socket after each command input/ouput
// This is need to in order to make the socket interface
// acessible by HTTP GET via a socket client in a web server.
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
#endif