Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
* src/stream.c (read_stream): Re-work after reading the book on
Browse files Browse the repository at this point in the history
poll(), always use read() and write() to get the actual socket errors
rather than trying to divine them from poll's erratic return events.
* src/main.c (main): Close the socket before reconnecting now that
read_stream() doesn't for us.
  • Loading branch information
keybuk committed Oct 9, 2005
1 parent ec6edf4 commit 545de34
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
2005-10-09 Scott James Remnant <scott@netsplit.com>

* src/stream.c (read_stream): Re-work after reading the book on
poll(), always use read() and write() to get the actual socket errors
rather than trying to divine them from poll's erratic return events.
* src/main.c (main): Close the socket before reconnecting now that
read_stream() doesn't for us.

* src/packet.h (RaceAtomType): The inter-sector columns actually
tell us which lap they went into the pits, and the sector time
becomes the time in the pits.
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ main (int argc,
return 2;
}

info (1, _("Reconnecting..."));
close (sock);
info (1, _("Reconnecting ...\n"));
}
}

Expand Down
69 changes: 27 additions & 42 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -151,63 +152,47 @@ read_stream (CurrentState *state, int sock)
{
struct pollfd poll_fd;
static int timer = 0;
int ret;
int numr, len;

poll_fd.fd = sock;
poll_fd.events = POLLIN;
poll_fd.revents = 0;

ret = poll (&poll_fd, 1, 100);
if (ret < 0) {
goto error;
} else if (ret == 0) {
numr = poll (&poll_fd, 1, 100);
if (numr > 0) {
unsigned char buf[512];

len = read (sock, buf, sizeof (buf));
if (len > 0) {
parse_stream_block (state, buf, len);
timer = 0;
return len;
} else if ((len < 0) && (errno != ECONNRESET)) {
return -1;
} else {
return 0;
}
} else if (numr < 0) {
return -1;
} else {
char buf[1];

if (timer++ < 10)
return 1;

/* Wake the server up */
buf[0] = 0x10;
ret = write (sock, buf, sizeof (buf));
if (ret < 0)
goto error;

update_time (state);

timer = 0;
return 1;
}

/* Error condition */
if (poll_fd.revents & (POLLERR | POLLNVAL))
goto error;

/* Server went away */
if (poll_fd.revents & POLLHUP) {
close (sock);
return 0;
}

/* Yay, data! */
if (poll_fd.revents & POLLIN) {
unsigned char buf[512];

ret = read (sock, buf, sizeof (buf));
if (ret < 0) {
goto error;
} else if (ret == 0) {
close (sock);
len = write (sock, buf, sizeof (buf));
if (len > 0) {
update_time (state);
timer = 0;
return len;
} else if ((len < 0) && (errno != EPIPE)) {
return -1;
} else {
return 0;
}

parse_stream_block (state, buf, ret);
}

timer = 0;
return ret;
error:
close (sock);
return -1;
}

/**
Expand Down

0 comments on commit 545de34

Please sign in to comment.