Fix listen and remove debug
This commit is contained in:
parent
90671b80ba
commit
9a6fadd558
2 changed files with 4 additions and 10 deletions
|
@ -56,20 +56,16 @@ void handle_new_tcp_client_connection(struct donar_server_ctx* ctx, struct epoll
|
||||||
socklen_t in_len;
|
socklen_t in_len;
|
||||||
struct epoll_event current_event;
|
struct epoll_event current_event;
|
||||||
|
|
||||||
printf("Accepting new TCP client\n");
|
|
||||||
in_len = sizeof(in_addr);
|
in_len = sizeof(in_addr);
|
||||||
conn_sock = accept(evt->data.fd, &in_addr, &in_len);
|
conn_sock = accept(evt->data.fd, &in_addr, &in_len);
|
||||||
if (conn_sock == -1) goto co_error;
|
if (conn_sock == -1) goto co_error;
|
||||||
printf("accept %d\n", conn_sock);
|
|
||||||
err = make_socket_non_blocking (conn_sock);
|
err = make_socket_non_blocking (conn_sock);
|
||||||
if (err == -1) goto co_error;
|
if (err == -1) goto co_error;
|
||||||
printf("make socket non blocking\n");
|
|
||||||
|
|
||||||
current_event.events = EPOLLIN | EPOLLET;
|
current_event.events = EPOLLIN | EPOLLET;
|
||||||
current_event.data.fd = conn_sock;
|
current_event.data.fd = conn_sock;
|
||||||
if (epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, conn_sock, ¤t_event) == -1)
|
if (epoll_ctl(ctx->epollfd, EPOLL_CTL_ADD, conn_sock, ¤t_event) == -1)
|
||||||
goto co_error;
|
goto co_error;
|
||||||
printf("add to epoll socket descriptor %d\n", conn_sock);
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
co_error:
|
co_error:
|
||||||
|
@ -80,13 +76,11 @@ co_error:
|
||||||
void handle_new_tcp_client_rw(struct donar_server_ctx* ctx, struct epoll_event* evt) {
|
void handle_new_tcp_client_rw(struct donar_server_ctx* ctx, struct epoll_event* evt) {
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
int nread, nwrite;
|
int nread, nwrite;
|
||||||
printf("Exchanging rw info with client for sock %d\n", evt->data.fd);
|
|
||||||
while(1) {
|
while(1) {
|
||||||
nread = read(evt->data.fd, buffer, sizeof(buffer));
|
nread = read(evt->data.fd, buffer, sizeof(buffer));
|
||||||
if (nread == 0) break;
|
if (nread == 0) break;
|
||||||
if (nread == -1 && errno == EAGAIN) break;
|
if (nread == -1 && errno == EAGAIN) break;
|
||||||
if (nread == -1) goto co_error;
|
if (nread == -1) goto co_error;
|
||||||
printf("successful read of %d", nread);
|
|
||||||
nwrite = write(evt->data.fd, buffer, nread);
|
nwrite = write(evt->data.fd, buffer, nread);
|
||||||
if (nwrite == -1 && errno == EAGAIN) {
|
if (nwrite == -1 && errno == EAGAIN) {
|
||||||
printf("Lost data EAGAIN\n");
|
printf("Lost data EAGAIN\n");
|
||||||
|
@ -132,7 +126,7 @@ void donar_server(struct donar_server_ctx* ctx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Start main loop\n");
|
printf("--- Start main loop\n");
|
||||||
int num_fd, n = 0;
|
int num_fd, n = 0;
|
||||||
while(1) {
|
while(1) {
|
||||||
num_fd = epoll_wait(ctx->epollfd, events, MAX_EVENTS, -1);
|
num_fd = epoll_wait(ctx->epollfd, events, MAX_EVENTS, -1);
|
||||||
|
@ -140,7 +134,7 @@ void donar_server(struct donar_server_ctx* ctx) {
|
||||||
perror("Failed to epoll_wait");
|
perror("Failed to epoll_wait");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("Loop with %d elements\n", num_fd);
|
|
||||||
for (n = 0 ; n < num_fd; n++) {
|
for (n = 0 ; n < num_fd; n++) {
|
||||||
if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (!(events[n].events & EPOLLIN))) {
|
if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (!(events[n].events & EPOLLIN))) {
|
||||||
/* An error has occured on this fd, or the socket is not
|
/* An error has occured on this fd, or the socket is not
|
||||||
|
|
|
@ -43,9 +43,9 @@ int create_tcp_server(char* service) {
|
||||||
struct addrinfo *result, *cursor;
|
struct addrinfo *result, *cursor;
|
||||||
|
|
||||||
memset(&conf, 0, sizeof(struct addrinfo));
|
memset(&conf, 0, sizeof(struct addrinfo));
|
||||||
conf.ai_family = AF_UNSPEC;
|
conf.ai_family = AF_INET; // AF_UNSPEC to listen on IPv6 or IPv4
|
||||||
conf.ai_socktype = SOCK_STREAM;
|
conf.ai_socktype = SOCK_STREAM;
|
||||||
conf.ai_flags = AI_PASSIVE;
|
conf.ai_flags = 0; // AI_PASSIVE to listen on 0.0.0.0
|
||||||
conf.ai_protocol = 0;
|
conf.ai_protocol = 0;
|
||||||
|
|
||||||
err = getaddrinfo(NULL, service, &conf, &result);
|
err = getaddrinfo(NULL, service, &conf, &result);
|
||||||
|
|
Loading…
Reference in a new issue