32class char_backend_socket :
public sc_core::sc_module
35 cci::cci_param<std::string> p_address;
36 cci::cci_param<bool> p_server;
37 cci::cci_param<bool> p_nowait;
38 cci::cci_param<bool> p_sigquit;
41 asio::io_context m_io_context;
42 tcp::acceptor m_acceptor;
43 tcp::socket m_asio_socket;
44 std::thread m_rcv_thread;
46 tcp::endpoint m_local_endpoint;
48 static constexpr size_t m_buffer_size = 1024;
49 std::array<char, m_buffer_size> m_buffer;
52 const int m_tcp_backlog = 1;
55 std::atomic<bool> m_stop_rcv_thread =
false;
57 const std::chrono::milliseconds m_retry_delay = std::chrono::milliseconds(100);
64 char_backend_socket(sc_core::sc_module_name name)
65 : sc_core::sc_module(name)
66 , p_address(
"address",
"127.0.0.1:4001",
"socket address IP:Port")
67 , p_server(
"server",
true,
"type of socket: true if server - false if client")
68 , p_nowait(
"nowait",
true,
"setting socket in non-blocking mode")
69 , p_sigquit(
"sigquit",
false,
"Interpret 0x1c in the data stream as a sigquit")
70 , m_biflow_socket(
"biflow_socket")
71 , m_acceptor(m_io_context)
72 , m_asio_socket(m_io_context)
74 SCP_TRACE(()) <<
"char_backend_socket constructor";
76 if (!set_endpoint()) {
77 SCP_FATAL(()) <<
"Failed to set local endpoint";
80 m_biflow_socket.register_b_transport(
this, &char_backend_socket::writefn);
83 ~char_backend_socket() { cleanup_receive_thread(); }
85 void end_of_simulation() { cleanup_receive_thread(); }
87 void cleanup_receive_thread()
89 m_stop_rcv_thread =
true;
92 if (m_rcv_thread.joinable()) m_rcv_thread.join();
94 asio::error_code ignored_error;
95 m_asio_socket.close(ignored_error);
96 m_acceptor.close(ignored_error);
101 std::string param_ip, param_port;
102 asio::ip::address ip;
105 size_t count = std::count(p_address.get_value().begin(), p_address.get_value().end(),
':');
107 size_t first = p_address.get_value().find_first_of(
':');
108 param_ip = p_address.get_value().substr(0, first);
109 param_port = p_address.get_value().substr(first + 1);
111 SCP_ERR(()) <<
"malformed address, expecting IP:PORT (e.g. 127.0.0.1:4001)";
116 ip = asio::ip::make_address(param_ip, ec);
118 SCP_ERR(()) <<
"Invalid IP address: " << param_ip <<
", error: " << ec.message();
123 port =
static_cast<unsigned short>(std::stoi(param_port));
124 }
catch (
const std::invalid_argument& e) {
125 SCP_ERR(()) <<
"Invalid port: " << param_port <<
" (not a number)";
127 }
catch (
const std::out_of_range& e) {
128 SCP_ERR(()) <<
"Port out of range: " << param_port;
132 m_local_endpoint = tcp::endpoint(ip, port);
137 void start_of_simulation()
139 SCP_DEBUG(()) <<
"IP: " << m_local_endpoint.address() <<
", PORT: " << m_local_endpoint.port();
145 m_rcv_thread = std::thread(&char_backend_socket::rcv_thread,
this);
148 void end_of_elaboration() { m_biflow_socket.can_receive_any(); }
150 void setup_tcp_server()
154 m_acceptor.open(m_local_endpoint.protocol(), ec);
156 SCP_ERR(()) <<
"Failed to open acceptor: " << ec.message();
160 m_acceptor.set_option(asio::socket_base::reuse_address(
true), ec);
162 SCP_ERR(()) <<
"Failed to set reuse_address option: " << ec.message();
167 m_acceptor.bind(m_local_endpoint, ec);
169 SCP_ERR(()) <<
"Failed to bind acceptor: " << ec.message();
174 m_acceptor.listen(m_tcp_backlog, ec);
176 SCP_ERR(()) <<
"Failed to listen on acceptor: " << ec.message();
181 m_acceptor.non_blocking(
true, ec);
183 SCP_ERR(()) <<
"Failed to set acceptor to non-blocking: " << ec.message();
189 void writefn(tlm::tlm_generic_payload& txn, sc_core::sc_time& t)
191 while (!m_asio_socket.is_open()) {
195 SCP_WARN(()) <<
"waiting for socket connection on IP address: " << p_address.get_value();
196 std::this_thread::sleep_for(m_retry_delay);
201 uint8_t* data = txn.get_data_ptr();
202 size_t bytes_to_send = txn.get_streaming_width();
203 size_t bytes_transferred = asio::write(m_asio_socket, asio::buffer(data, bytes_to_send), ec);
204 if (ec || (bytes_transferred != bytes_to_send)) {
206 SCP_WARN(())(
"(Non blocking) socket closed");
209 SCP_WARN(())(
"(Blocking) socket closed.");
218 }
catch (
const asio::system_error& e) {
219 std::cout <<
"Caught exception: " << e.what() << std::endl;
223 void configure_socket()
227 m_asio_socket.non_blocking(
true, ec);
229 SCP_FATAL(()) <<
" Failed to set non blocking mode: " << ec.message();
233 m_asio_socket.set_option(asio::ip::tcp::no_delay(
true));
235 SCP_FATAL(()) <<
" Failed to disable Nagle's Algorithm: " << ec.message();
243 while (!m_stop_rcv_thread) {
246 m_acceptor.accept(m_asio_socket, ec);
247 switch (ec.value()) {
250 SCP_DEBUG(()) <<
"Accepted connection from " << m_asio_socket.remote_endpoint().address() <<
":"
251 << m_asio_socket.remote_endpoint().port();
253 case asio::error::would_block:
254 SCP_INFO(()) <<
"Accept failed for non blocking:" << ec.message();
255 std::this_thread::sleep_for(m_retry_delay);
258 SCP_WARN(()) <<
"Accept connection failed with error:" << ec.message();
259 std::this_thread::sleep_for(m_retry_delay);
262 }
else if (!p_server) {
264 m_asio_socket.connect(m_local_endpoint, ec);
268 SCP_INFO(()) <<
"failed to connect to " << m_local_endpoint.address() <<
":"
269 << m_local_endpoint.port() <<
" " << ec.message();
270 m_asio_socket.close();
271 std::this_thread::sleep_for(m_retry_delay);
276 if (m_asio_socket.is_open()) receive_loop();
279 SCP_DEBUG(()) <<
"Leaving receive thread";
286 while (!m_stop_rcv_thread && m_asio_socket.is_open()) {
287 size_t read_count = m_asio_socket.read_some(asio::buffer(m_buffer), ec);
288 switch (ec.value()) {
290 if (!m_stop_rcv_thread) forward_incoming_data(read_count);
292 case asio::error::would_block:
294 std::this_thread::sleep_for(m_retry_delay);
296 case asio::error::eof:
297 SCP_DEBUG(()) <<
"Remote endpoint disconnected";
298 m_asio_socket.close();
301 SCP_WARN(()) <<
"Read failed: " << ec.message();
302 m_asio_socket.close();
307 SCP_DEBUG(()) <<
"Leaving receive loop";
310 void forward_incoming_data(
size_t data_length)
312 for (
size_t i = 0; i < data_length; i++) {
313 unsigned char c = m_buffer[i];
314 if (p_sigquit && c == 0x1c) {
317 m_biflow_socket.enqueue(c);