Skip to content

Commit 364ef5b

Browse files
committed
Open listening socket only when we have something to deliver
Signed-off-by: Fini Jastrow <[email protected]>
1 parent fdea8b5 commit 364ef5b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

aids.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ typedef struct {
116116
int connected; // are we connected to the EventID server
117117
char *host;
118118
char *service;
119+
char ever_got_id;
119120
} context_t;
120121

121122

@@ -236,6 +237,7 @@ static int ID_connect_to(const char *address, const char *service)
236237
int sock, c, err, opt = 1;
237238
struct addrinfo *a;
238239

240+
fprintf(stdout, "Trying to connect to %s\r\n", address);
239241
sock = socket(AF_INET, SOCK_STREAM, 0);
240242
if (sock < 0) {
241243
fprintf(stderr, "ID_connect() socket failed: %s\r\n", strerror(errno));
@@ -302,7 +304,7 @@ static int ID_connect(context_t *ctx)
302304
// and stores it in our ringbuffer
303305
//
304306
// runs in a sub-thread
305-
static void* ID_collect(void* nyx)
307+
/* _Noreturn */ static void* ID_collect(void* nyx)
306308
{
307309
context_t *ctx = (context_t *)nyx;
308310
char buff[ID_MESSAGE_SIZE];
@@ -384,7 +386,7 @@ static void* ID_collect(void* nyx)
384386
ctx->events[ctx->events_idx].offset = offset;
385387
}
386388
printf("Connected to EventID server and getting data\r\n");
387-
389+
ctx->ever_got_id = 1;
388390
}
389391
ctx->events[ctx->events_idx].t.tv_sec = tv.tv_sec;
390392
ctx->events[ctx->events_idx].t.tv_usec = tv.tv_usec;
@@ -446,7 +448,7 @@ static void deliver_id(context_t *ctx)
446448
return;
447449
}
448450

449-
printf("waiting for connections...\r\n");
451+
printf("Waiting for incoming connections...\r\n");
450452

451453
for (;;) { // main accept() loop
452454
sin_size = sizeof(client_addr);
@@ -516,6 +518,10 @@ int main(int argc, char *argv[])
516518
// connect to the EventID Server to gather information
517519
pthread_create(&t, NULL, &ID_collect, ctx);
518520
#endif
521+
// active wait for connection *cough*
522+
do sleep(1);
523+
while (!ctx->ever_got_id);
524+
519525
// deliver EventIDs with less jitter to users
520526
deliver_id(ctx);
521527

0 commit comments

Comments
 (0)