diff options
| author | Ian Moffett <ian@osmora.org> | 2025-06-17 21:59:54 -0400 | 
|---|---|---|
| committer | Ian Moffett <ian@osmora.org> | 2025-06-17 21:59:54 -0400 | 
| commit | d1a4362489ac6196251221393ac42ccb1853fb70 (patch) | |
| tree | 457b04ae4c9982900445794a4ba23f327b078323 | |
| parent | de8dbdf8a432c31998fd523fa15c768d4ff67c91 (diff) | |
server: Move data polling loop above accept loopmain
Signed-off-by: Ian Moffett <ian@osmora.org>
| -rw-r--r-- | lib/libostp/server.c | 64 | 
1 files changed, 32 insertions, 32 deletions
| diff --git a/lib/libostp/server.c b/lib/libostp/server.c index 794eda0..0916d58 100644 --- a/lib/libostp/server.c +++ b/lib/libostp/server.c @@ -174,38 +174,6 @@ listener_poll(struct ostp_listener *lp)              continue;          } -        /* Check if the servers socket has new connections */ -        if (FD_ISSET(lp->serv_sockfd, &lp->client_fds)) { -            caddr_len = sizeof(caddr); -            client_sock = accept(lp->serv_sockfd, (struct sockaddr *)&caddr, -                &caddr_len); - -            if (client_sock < 0) { -                perror("accept"); -                continue; -            } - -            if (lp->client_count >= MAX_CLIENTS) { -                printf("New connection rejected, max clients reached\n"); -                close(client_sock); -                continue; -            } - -            for (int i = 0; i < MAX_CLIENTS; ++i) { -                c = &lp->clients[i]; -                if (c->sockfd < 0) { -                    c->sockfd = client_sock; -                    c->authed = 0; -                    ++lp->client_count; - -                    ip = inet_ntoa(caddr.sin_addr); -                    printf("Incoming connection from %s\n", ip); -                    handle_client(&caddr, c, lp); -                    break; -                } -            } -        } -          /* Handle data from clients */          for (int i = 1; i < MAX_CLIENTS; ++i) {              c = &lp->clients[i]; @@ -244,6 +212,38 @@ listener_poll(struct ostp_listener *lp)                  }              }          } + +        /* Check if the servers socket has new connections */ +        if (FD_ISSET(lp->serv_sockfd, &lp->client_fds)) { +            caddr_len = sizeof(caddr); +            client_sock = accept(lp->serv_sockfd, (struct sockaddr *)&caddr, +                &caddr_len); + +            if (client_sock < 0) { +                perror("accept"); +                continue; +            } + +            if (lp->client_count >= MAX_CLIENTS) { +                printf("New connection rejected, max clients reached\n"); +                close(client_sock); +                continue; +            } + +            for (int i = 0; i < MAX_CLIENTS; ++i) { +                c = &lp->clients[i]; +                if (c->sockfd < 0) { +                    c->sockfd = client_sock; +                    c->authed = 0; +                    ++lp->client_count; + +                    ip = inet_ntoa(caddr.sin_addr); +                    printf("Incoming connection from %s\n", ip); +                    handle_client(&caddr, c, lp); +                    break; +                } +            } +        }      }      close(client_sock); | 
