40 cci::cci_param<std::string> p_address;
41 cci::cci_param<bool> p_server;
42 cci::cci_param<bool> p_nowait;
43 cci::cci_param<bool> p_sigquit;
54#pragma message("char_backend_socket not yet implemented for WIN32")
66 SCP_ERR(()) <<
"setting socket in non-blocking mode failed: " << std::strerror(
errno);
71 SCP_WARN(()) <<
"setting up TCP_NODELAY option failed: " << std::strerror(
errno);
75 void start_of_simulation()
77 size_t count = std::count(p_address.get_value().begin(), p_address.get_value().end(),
':');
79 size_t first = p_address.get_value().find_first_of(
':');
80 ip = p_address.get_value().substr(0,
first);
81 port = p_address.get_value().substr(
first + 1);
83 SCP_ERR(()) <<
"malformed address, expecting IP:PORT (e.g. 127.0.0.1:4001)";
87 SCP_DEBUG(()) <<
"IP: " << ip <<
", PORT: " << port;
90 setup_tcp_server(ip, port);
92 setup_tcp_client(ip, port);
95 new std::thread(&char_backend_socket::rcv_thread,
this);
99 : sc_core::sc_module(name)
100 , p_address(
"address",
"127.0.0.1:4001",
"socket address IP:Port")
101 , p_server(
"server",
true,
"type of socket: true if server - false if client")
102 , p_nowait(
"nowait",
true,
"setting socket in non-blocking mode")
103 , p_sigquit(
"sigquit",
false,
"Interpret 0x1c in the data stream as a sigquit")
104 , socket(
"biflow_socket")
106 SCP_TRACE(()) <<
"char_backend_socket constructor";
115 if (p_server && m_socket < 0) {
131 }
else if (!p_server && m_socket < 0) {
134 setup_tcp_client(ip, port);
142 if (
poll(&
fd, 1, 1000) == 0) {
145 if (
fd.revents > 0x4) {
149 SCP_FATAL(())(
"Non waiting Socket closed");
151 SCP_WARN(())(
"Socket closed, will wait for new connection");
155 ret = ::read(m_socket, m_buf, 1);
157 for (
int i = 0;
i <
ret;
i++) {
158 unsigned char c = m_buf[
i];
159 if (p_sigquit &&
c == 0x1c) {
168 void writefn(tlm::tlm_generic_payload&
txn, sc_core::sc_time&
t)
170 while (m_socket < 0) {
174 SCP_WARN(()) <<
"waiting for socket connection on IP address: " << p_address.get_value();
179 for (
int i = 0;
i <
txn.get_streaming_width();
i++) {
181 if ((::write(m_socket, &data[
i], 1)) != 1) {
183 SCP_WARN(())(
"(Blocking) write did not complete (EAGAIN)");
187 SCP_WARN(())(
"(Non blocking) socket closed");
190 SCP_FATAL(())(
"(Blocking) socket closed.");
200 void setup_tcp_server(std::string ip, std::string port)
207 SCP_DEBUG(()) <<
"setting up TCP server on " << ip <<
":" << port;
212 if (m_srv_socket < 0) {
213 SCP_ERR(()) <<
"socket failed: " << std::strerror(
errno);
220 iport = std::stoi(port);
229 ::close(m_srv_socket);
231 SCP_ERR(()) <<
"bind to port '" <<
iport <<
"' failed: " << std::strerror(
errno);
237 ::close(m_srv_socket);
239 SCP_ERR(()) <<
"listen failed: " << std::strerror(
errno);
245 void setup_tcp_client(std::string ip, std::string port)
252 SCP_INFO(()) <<
"setting up TCP client connection to " << ip <<
":" << port;
256 if (m_socket == -1) {
257 SCP_ERR(()) <<
"socket failed: " << std::strerror(
errno);
267 SCP_ERR(()) <<
"getaddrinfo failed: " << std::strerror(
errno);
272 SCP_ERR(()) <<
"connect failed: " << std::strerror(
errno);
void register_b_transport(MODULE *mod, void(MODULE::*cb)(tlm::tlm_generic_payload &, sc_core::sc_time &))
Register b_transport to be called whenever data is received from the socket.
Definition biflow-socket.h:227