Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.
/ readsb Public archive

Some network/socket tweaks... #37

Merged
merged 1 commit into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some network/socket tweaks...
1) enable keepalives on accept()'d connections
2) add periodic read() to connections w/o a reader (to try to trigger
socket errors for stale connections)... every 30 sec
  • Loading branch information
TanerH committed Jan 1, 2020
commit 0f2f6d5ff6d221a65d8602ec6da02073672b074a
2 changes: 2 additions & 0 deletions anet.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len)
}
break;
}
/* Turn on keepalives */
anetTcpKeepAlive(err,fd);
return fd;
}

Expand Down
31 changes: 28 additions & 3 deletions net_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,25 @@ void writeJsonToFile (const char *file, struct char_buffer cb) {
return;
#endif
}
static void periodicReadFromClient(struct client *c) {
int nread, err;
char buf[512];

/* FIXME: Not Win32 safe networking */
nread = read(c->fd, buf, sizeof(buf));
err = errno;

if (nread < 0 && (err == EAGAIN || err == EWOULDBLOCK)) {
return;
}
if (nread <= 0) { // Other errors, or EOF
fprintf(stderr, "%s: Socket Error: %s: %s port %s (fd %d)\n",
c->service->descr, nread < 0 ? strerror(err) : "EOF", c->host, c->port,
c->fd);
modesCloseClient(c);
return;
}
}

//
//=========================================================================
Expand Down Expand Up @@ -2240,7 +2259,7 @@ static void modesReadFromClient(struct client *c) {
fprintf(stderr, "%s: Remote server disconnected: %s port %s (fd %d, SendQ %d, RecvQ %d)\n",
c->service->descr, c->con->address, c->con->port, c->fd, c->sendq_len, c->buflen);
} else if (Modes.debug & MODES_DEBUG_NET) {
fprintf(stderr, "%s: Listen client disconnected: %s port %s(fd %d, SendQ %d, RecvQ %d)\n",
fprintf(stderr, "%s: Listen client disconnected: %s port %s (fd %d, SendQ %d, RecvQ %d)\n",
c->service->descr, c->host, c->port, c->fd, c->sendq_len, c->buflen);
}
modesCloseClient(c);
Expand All @@ -2257,7 +2276,7 @@ static void modesReadFromClient(struct client *c) {
}

if (nread < 0) { // Other errors
fprintf(stderr, "%s: Receive Error: %s: %s port %s(fd %d, SendQ %d, RecvQ %d)\n",
fprintf(stderr, "%s: Receive Error: %s: %s port %s (fd %d, SendQ %d, RecvQ %d)\n",
c->service->descr, strerror(err), c->host, c->port,
c->fd, c->sendq_len, c->buflen);
modesCloseClient(c);
Expand Down Expand Up @@ -2858,8 +2877,14 @@ void modesNetPeriodicWork(void) {
for (c = Modes.clients; c; c = c->next) {
if (!c->service)
continue;
if (c->service->read_handler)
if (c->service->read_handler) {
modesReadFromClient(c);
} else if ((c->last_read + 30000) <= now) {
// This is called if there is no read handler - we just read and discard to try to trigger socket errors
// (if 30 sec have passed)
periodicReadFromClient(c);
c->last_read = now;
}

// Only if there is a sendq do we check to see if we need to flush it.
// 5ms XXX magic number XXX
Expand Down
1 change: 1 addition & 0 deletions net_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ struct client
int modeac_requested; // 1 if this Beast output connection has asked for A/C
uint64_t last_flush;
uint64_t last_send;
uint64_t last_read; // This is used on write-only clients to help check for dead connections
char buf[MODES_CLIENT_BUF_SIZE + 4]; // Read buffer+padding
void *sendq; // Write buffer - allocated later
int sendq_len; // Amount of data in SendQ
Expand Down