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

improved TCP connection handling (still to be improved)

This commit is contained in:
Steffen Vogel 2015-03-31 18:29:07 +02:00
parent d0fb87dbb1
commit d7c367911e
3 changed files with 24 additions and 18 deletions

View file

@ -118,20 +118,18 @@ int node_start(struct node *n)
int node_start_defer(struct node *n)
{
int ret;
struct socket *s = n->socket;
if (node_type(n) == TCPD) {
info("Wait for incoming TCP connection from node '%s'...", n->name);
ret = listen(n->socket->sd2, 1);
if (ret < 0)
s->sd = listen(s->sd2, 1);
if (s->sd < 0)
serror("Failed to listen on socket for node '%s'", n->name);
ret = accept(n->socket->sd2, NULL, NULL);
if (ret < 0)
s->sd = accept(s->sd2, NULL, NULL);
if (s->sd < 0)
serror("Failed to accept on socket for node '%s'", n->name);
n->socket->sd = ret;
}
return 0;

View file

@ -67,7 +67,9 @@ static void * path_run(void *arg)
/* Open deferred TCP connection */
node_start_defer(p->in);
// FIXME: node_start_defer(p->out);
FOREACH(&p->destinations, it)
node_start_defer(it->path->out);
/* Main thread loop */

View file

@ -67,11 +67,8 @@ int socket_open(struct node *n)
serror("Failed to bind socket");
/* Connect socket for sending */
if (node_type(n) == TCPD) {
/* Listening TCP sockets will be connected later by calling accept() */
if (node_type(n) == TCP) {
s->sd2 = s->sd;
}
else if (node_type(n) != IEEE_802_3) {
ret = connect(s->sd, (struct sockaddr *) &s->remote, sizeof(s->remote));
if (ret < 0)
serror("Failed to connect socket");
@ -153,18 +150,27 @@ int socket_read(struct node *n, struct msg *m)
int socket_write(struct node *n, struct msg *m)
{
struct socket *s = n->socket;
int ret;
int ret = -1;
/* Convert headers to network byte order */
m->sequence = htons(m->sequence);
if (node_type(n) == IEEE_802_3)
ret = sendto(s->sd, m, MSG_LEN(m->length), 0, (struct sockaddr *) &s->remote, sizeof(s->remote));
else
ret = send(s->sd, m, MSG_LEN(m->length), 0);
switch (node_type(n)) {
case IEEE_802_3:/* Connection-less protocols */
case IP:
case UDP:
ret = sendto(s->sd, m, MSG_LEN(m->length), 0, (struct sockaddr *) &s->remote, sizeof(s->remote));
break;
case TCP: /* Connection-oriented protocols */
case TCPD:
ret = send(s->sd, m, MSG_LEN(m->length), 0);
break;
default: { }
}
if (ret < 0)
serror("Failed send(to)");
serror("Failed send");
debug(10, "Message sent to node '%s': version=%u, type=%u, endian=%u, length=%u, sequence=%u",
n->name, m->version, m->type, m->endian, m->length, ntohs(m->sequence));