1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

fix: Add error checking

fix: Naming and comment

fix: Remove socket_tcp_connection from header file
Signed-off-by: Jitpanu Maneeratpongsuk <jitpanu.maneeratpongsuk@rwth-aachen.de>
This commit is contained in:
Jitpanu Maneeratpongsuk 2025-01-29 12:07:42 +00:00 committed by al3xa23
parent d3cbf876cf
commit 7a2d689199
2 changed files with 54 additions and 42 deletions

View file

@ -24,7 +24,7 @@ struct Socket {
int sd; // The socket descriptor
int clt_sd; // TCP client socket descriptor
int verify_source; // Verify the source address of incoming packets against socket::remote.
bool tcp_connect = false; // TCP connection status bit
bool tcp_connected = false; // TCP connection status bit
enum SocketLayer
layer; // The OSI / IP layer which should be used for this socket
@ -70,8 +70,6 @@ int socket_read(NodeCompat *n, struct Sample *const smps[], unsigned cnt);
int socket_parse(NodeCompat *n, json_t *json);
void socket_tcp_connection(NodeCompat *n, Socket *s);
char *socket_print(NodeCompat *n);
} // namespace node

View file

@ -339,11 +339,17 @@ int villas::node::socket_stop(NodeCompat *n) {
if (s->sd >= 0) {
// Close client socket descriptor.
if (s->layer == SocketLayer::TCP_SERVER)
if (s->layer == SocketLayer::TCP_SERVER) {
close(s->clt_sd);
if (ret)
throw SystemError("Failed to close TCP client socket descriptor");
}
ret = close(s->sd);
//Reset socket descriptor
s->sd = -1;
if (ret)
return ret;
}
@ -354,6 +360,47 @@ int villas::node::socket_stop(NodeCompat *n) {
return 0;
}
static void socket_tcp_connection(NodeCompat *n, Socket *s) {
int ret;
if (s->layer == SocketLayer::TCP_CLIENT) {
if (s->sd >= 0) {
ret = close(s->sd);
if (ret < 0)
throw SystemError("Failed to close socket descriptor");
}
s->sd = socket(s->in.saddr.sa.sa_family, SOCK_STREAM, 0);
if (s->sd < 0)
throw SystemError("Failed to create socket");
// Attempt to connect to TCP server.
int retries = 0;
while (retries < MAX_CONNECTION_RETRIES) {
n->logger->info("Attempting to connect to TCP server: attempt={}...", retries + 1);
ret = connect(s->sd, (struct sockaddr *)&s->out.saddr, sizeof(s->in.saddr));
if (ret == 0) {
s->tcp_connected = true;
break;
} else {
retries++;
if (retries < MAX_CONNECTION_RETRIES) {
sleep(RETRIES_DELAY);
}
}
}
if (ret < 0)
throw SystemError("Failed to conenct to TCP server");
} else if (s->layer == SocketLayer::TCP_SERVER) {
ret = listen(s->sd, 5);
if (ret < 0)
throw SystemError("Failed to listen for TCP client connection");
// Accept client connection and get client socket descriptor.
s->clt_sd = accept(s->sd, nullptr, nullptr);
if (s->clt_sd < 0) {
throw SystemError("Failed to accept TCP client connection");
}
s->tcp_connected = true;
}
}
int villas::node::socket_read(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
int ret;
@ -370,14 +417,14 @@ int villas::node::socket_read(NodeCompat *n, struct Sample *const smps[],
if (s->layer == SocketLayer::TCP_CLIENT) {
// Receive data from server.
if (!s->tcp_connect)
villas::node::socket_tcp_connection(n, s);
if (!s->tcp_connected)
socket_tcp_connection(n, s);
bytes = recv(s->sd, s->in.buf, s->in.buflen, 0);
} else if (s->layer == SocketLayer::TCP_SERVER) {
// Receive data from client.
if (!s->tcp_connect)
villas::node::socket_tcp_connection(n, s);
if (!s->tcp_connected)
socket_tcp_connection(n, s);
bytes = recv(s->clt_sd, s->in.buf, s->in.buflen, 0);
} else {
@ -391,7 +438,7 @@ int villas::node::socket_read(NodeCompat *n, struct Sample *const smps[],
throw SystemError("Failed recvfrom()");
} else if (bytes == 0) {
if (s->layer == SocketLayer::TCP_CLIENT || s->layer == SocketLayer::TCP_SERVER)
s->tcp_connect = false;
s->tcp_connected = false;
return 0;
}
@ -436,39 +483,6 @@ int villas::node::socket_read(NodeCompat *n, struct Sample *const smps[],
return ret;
}
void villas::node::socket_tcp_connection(NodeCompat *n, Socket *s) {
int ret;
if (s->layer == SocketLayer::TCP_CLIENT) {
close(s->sd);
s->sd = socket(s->in.saddr.sa.sa_family, SOCK_STREAM, 0);
// Attemp to connect to TCP server.
int retries = 0;
while (retries < MAX_CONNECTION_RETRIES) {
n->logger->info("Attempting to connect to server: attempt={}...", retries + 1);
ret = connect(s->sd, (struct sockaddr *)&s->out.saddr, sizeof(s->in.saddr));
if (ret == 0) {
s->tcp_connect = true;
break;
} else {
retries++;
if (retries < MAX_CONNECTION_RETRIES) {
sleep(RETRIES_DELAY);
}
}
}
if (ret < 0)
throw SystemError("Failed to conenct to TCP server");
} else if (s->layer == SocketLayer::TCP_SERVER) {
listen(s->sd, 5);
// Accept client connection and get client socket descriptor.
s->clt_sd = accept(s->sd, nullptr, nullptr);
if (s->clt_sd < 0) {
throw SystemError("Failed to accept connection");
}
s->tcp_connect = true;
}
}
int villas::node::socket_write(NodeCompat *n, struct Sample *const smps[],
unsigned cnt) {
auto *s = n->getData<struct Socket>();