Skip to content

Commit

Permalink
feat: return pandio error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldziuba03 committed Sep 2, 2024
1 parent fc0a692 commit f523519
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
2 changes: 1 addition & 1 deletion samples/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void handle_read(pd_tcp_t *stream, char *buf, size_t len) {
void handle_connect(pd_tcp_t *stream, int status) {
if (status < 0) {
pd_tcp_close(stream);
printf("Connect failed.\n");
printf("Connect failed with err code: %d.\n", status);
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ int pd_errmap(pd_errno_t err) {
}

int pd_errno() {
int sys_err = errno();
return pd_errmap(sys_err);
return pd_errmap(errno);
}

int pd__set_nonblocking(pd_fd_t fd) {
Expand Down
47 changes: 25 additions & 22 deletions src/sys/tcp_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* SOFTWARE.
*/

#include "pandio/err.h"
#include "pandio/tcp.h"
#include "internal.h"
#include <stdio.h>
Expand Down Expand Up @@ -101,7 +102,7 @@ int pd_tcp_listen(pd_tcp_server_t *server,

pd_socket_t lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd <= 0) {
return -1;
return pd_errno();
}

struct sockaddr_in address;
Expand All @@ -113,22 +114,22 @@ int pd_tcp_listen(pd_tcp_server_t *server,

if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
pd__closesocket(lfd);
return -1;
return pd_errno();
}

if (bind(lfd, (struct sockaddr*)&address, sizeof(address)) < 0) {
pd__closesocket(lfd);
return -1;
return pd_errno();
}

if (pd__set_nonblocking(lfd) < 0) {
pd__closesocket(lfd);
return -1;
return pd_errno();
}

if (listen(lfd, SOMAXCONN) < 0) {
pd__closesocket(lfd);
return -1;
return pd_errno();
}

server->fd = lfd;
Expand Down Expand Up @@ -158,7 +159,7 @@ void pd_tcp_close(pd_tcp_t *stream) {
while (!queue_empty(&stream->writes)) {
struct queue_node *next = queue_pop(&stream->writes);
pd_write_t *write_op = container_of(next, pd_write_t, qnode);
write_op->cb(write_op, -1);
write_op->cb(write_op, PD_ECANCELED);
}

// schedule close callback to the next event loop iteration.
Expand Down Expand Up @@ -215,7 +216,7 @@ void pd__tcp_read(pd_tcp_t *stream) {
stream->on_data(stream, buf, 0);
return;
} else {
stream->on_data(stream, buf, -1);
stream->on_data(stream, buf, pd_errno());
}
} else {
stream->on_data(stream, buf, nread);
Expand Down Expand Up @@ -246,9 +247,8 @@ void pd__tcp_write(pd_tcp_t *stream)
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return;
} else {
perror("write");
queue_pop(&stream->writes);
write_op->cb(write_op, -1);
write_op->cb(write_op, pd_errno());
return;
}
}
Expand Down Expand Up @@ -308,8 +308,12 @@ void pd__tcp_connect_io(pd_event_t *event, unsigned events) {
return;

if (events & PD_CLOSE) {
int err = 0;
socklen_t errsize = sizeof(err);
getsockopt(stream->fd, SOL_SOCKET, SO_ERROR, &err, &errsize);

if (stream->on_connect)
stream->on_connect(stream, -1);
stream->on_connect(stream, pd_errmap(err));
return;
}

Expand All @@ -329,21 +333,20 @@ void pd__tcp_connect_io(pd_event_t *event, unsigned events) {
int pd_tcp_connect(pd_tcp_t *stream, const char *host, int port, void (*on_connect)(pd_tcp_t*, int)) {
pd_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return -1;
return pd_errno();
}

if (pd__set_nonblocking(fd) < 0) {
pd__closesocket(fd);
return -1;
return pd_errno();
}

struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
if (inet_pton(AF_INET, host, &address.sin_addr) <= 0) {
pd__closesocket(fd);
return -1;
return pd_errno();
}

stream->fd = fd;
Expand All @@ -354,7 +357,7 @@ int pd_tcp_connect(pd_tcp_t *stream, const char *host, int port, void (*on_conne
if (connect(fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
if (errno != EINPROGRESS) {
pd__closesocket(fd);
return -1;
return pd_errno();
}
}

Expand Down Expand Up @@ -399,7 +402,7 @@ void pd_tcp_write_async(pd_tcp_t *stream, pd_write_t *write_op) {

void pd_tcp_write(pd_tcp_t *stream, pd_write_t *write_op) {
if (stream->status != PD_TCP_ACTIVE) {
write_op->cb(write_op, -1);
write_op->cb(write_op, PD_ECANCELED);
return;
}

Expand All @@ -415,7 +418,7 @@ void pd_tcp_write(pd_tcp_t *stream, pd_write_t *write_op) {
return;
}

write_op->cb(write_op, -1);
write_op->cb(write_op, pd_errno());
return;
}

Expand All @@ -434,7 +437,7 @@ void pd_tcp_write(pd_tcp_t *stream, pd_write_t *write_op) {

int pd_tcp_nodelay(pd_tcp_t *stream, int enable) {
if (setsockopt(stream->fd,IPPROTO_TCP,TCP_NODELAY,&enable,sizeof(enable)) < 0) {
return -1;
return pd_errno();
}

return 0;
Expand All @@ -443,15 +446,15 @@ int pd_tcp_nodelay(pd_tcp_t *stream, int enable) {

int pd_tcp_keepalive(pd_tcp_t *stream, int enable, int delay) {
if(setsockopt(stream->fd,SOL_SOCKET,SO_KEEPALIVE,&enable, sizeof(enable)) < 0)
return -1;
return pd_errno();

if (!enable)
return 0;

// TCP_KEEPIDLE is used by Linux, TCP_KEEPALIVE is used by BSDs and macOS.
#ifdef TCP_KEEPIDLE
if (setsockopt(stream->fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)) < 0)
return -1;
return pd_errno();
#elif TCP_KEEPALIVE
if (setsockopt(stream->fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof (delay)) < 0)
return -1;
Expand All @@ -460,13 +463,13 @@ int pd_tcp_keepalive(pd_tcp_t *stream, int enable, int delay) {
#ifdef TCP_KEEPCNT
int cnt = 10;
if (setsockopt(stream->fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)) < 0)
return -1;
return pd_errno();
#endif

#ifdef TCP_KEEPINTVL
int interval = 1;
if (setsockopt(stream->fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval)) < 0)
return -1;
return pd_errno();
#endif

return 0;
Expand Down
52 changes: 28 additions & 24 deletions src/sys/tcp_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

#include "pandio/tcp.h"
#include "pandio/err.h"
#include "internal.h"
#include <stdio.h>

// how many times issue AcceptEx?
#define SIMULTANEOUS_ACCEPTS 32
Expand Down Expand Up @@ -106,8 +106,7 @@ int pd_tcp_listen(pd_tcp_server_t *server,
server->on_connection = on_connection;

if (server->fd == INVALID_SOCKET) {
printf("Unable to initialize socket\n");
return -1;
return pd_errno();
}

struct sockaddr_in address;
Expand All @@ -118,17 +117,17 @@ int pd_tcp_listen(pd_tcp_server_t *server,
const char opt = 1;
if (setsockopt(server->fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
closesocket(server->fd);
return -1;
return pd_errno();
}

if (bind(server->fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
closesocket(server->fd);
return -1;
return pd_errno();
}

if (listen(server->fd, SOMAXCONN) < 0) {
closesocket(server->fd);
return -1;
return pd_errno();
}

/* we need to load dynamically MS extension to get AcceptEx function */
Expand All @@ -144,7 +143,7 @@ int pd_tcp_listen(pd_tcp_server_t *server,

if (res == SOCKET_ERROR) {
closesocket(server->fd);
return -1;
return pd_errno();
}
}

Expand All @@ -155,6 +154,10 @@ int pd_tcp_listen(pd_tcp_server_t *server,

for (int i = 0; i < SIMULTANEOUS_ACCEPTS; ++i) {
pd__accept_op_t *op = malloc(sizeof(pd__accept_op_t));
if (op == NULL) {
return PD_ENOMEM;
}

pd__tcp_post_acceptex(server, op);
}

Expand Down Expand Up @@ -240,7 +243,7 @@ void pd_write_init(pd_write_t *write_op,

void pd_tcp_write(pd_tcp_t *stream, pd_write_t *write_op) {
if (stream->status != PD_TCP_ACTIVE) {
write_op->cb(write_op, -1);
write_op->cb(write_op, PD_ECANCELED);
return;
}

Expand All @@ -251,9 +254,9 @@ void pd_tcp_write(pd_tcp_t *stream, pd_write_t *write_op) {

write_op->handle = stream;

if (status == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
// TODO: map system error codes to library specific codes
write_op->cb(write_op, -1);
int err;
if (status == SOCKET_ERROR && (err = WSAGetLastError()) != WSA_IO_PENDING) {
write_op->cb(write_op, pd_errmap(err));
return;
} else {
stream->writes_size++;
Expand Down Expand Up @@ -368,9 +371,9 @@ void pd__tcp_connect_io(pd_event_t *event) {
pd__tcp_post_recv(stream);
stream->status = PD_TCP_ACTIVE;
} else {
DWORD err = GetLastError();
assert(err != ERROR_IO_PENDING); // if we got this function called, then operation should be completed, right?
status = -1;
DWORD err = WSAGetLastError();
assert(err != WSA_IO_PENDING); // if we got this function called, then operation should be completed, right?
status = pd_errmap(err);
}

if (stream->on_connect)
Expand All @@ -394,7 +397,7 @@ int pd_tcp_connect(pd_tcp_t *stream, const char *host, int port, void (*on_conne
address.sin_port = htons(port);
if (inet_pton(AF_INET, host, &address.sin_addr) <= 0) {
closesocket(fd);
return -1;
return pd_errno();
}

struct sockaddr_in local_addr = { 0 };
Expand All @@ -404,7 +407,7 @@ int pd_tcp_connect(pd_tcp_t *stream, const char *host, int port, void (*on_conne

if (bind(fd, (struct sockaddr*)&local_addr, sizeof(local_addr)) == SOCKET_ERROR) {
closesocket(fd);
return -1;
return pd_errno();
}
// load ConnectEx function only once
if (!pd__connectex) {
Expand All @@ -416,21 +419,21 @@ int pd_tcp_connect(pd_tcp_t *stream, const char *host, int port, void (*on_conne

if (loaded == SOCKET_ERROR) {
closesocket(fd);
return -1;
return pd_errno();
}
}

pd_event_t *connect_ev = malloc(sizeof(pd_event_t));
// not enough memory:
if (!connect_ev) {
closesocket(fd);
return -1;
return PD_ENOMEM;
}

if (CreateIoCompletionPort((HANDLE)fd, stream->ctx->poll_fd, 0, 0) == NULL) {
free(connect_ev);
closesocket(fd);
return -1;
return pd_errno();
}

pd__event_init(connect_ev);
Expand All @@ -439,11 +442,12 @@ int pd_tcp_connect(pd_tcp_t *stream, const char *host, int port, void (*on_conne
int status = pd__connectex(fd, (struct sockaddr*)&address, sizeof(struct sockaddr_in),
NULL, 0, NULL, &connect_ev->overlapped);

if (status == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING) {
DWORD err;
if (status == SOCKET_ERROR && (err = WSAGetLastError()) != WSA_IO_PENDING) {
free(connect_ev);
closesocket(fd);
CloseHandle((HANDLE)fd);
return -1;
return pd_errmap(err);
}

return 0;
Expand All @@ -468,7 +472,7 @@ int pd_tcp_keepalive(pd_tcp_t *stream, int enable, int delay) {
SO_KEEPALIVE,
(const char*) &enable,
sizeof(enable)) < 0) {
return -1;
return pd_errno();
}

if (!enable)
Expand All @@ -479,7 +483,7 @@ int pd_tcp_keepalive(pd_tcp_t *stream, int enable, int delay) {
TCP_KEEPALIVE,
(const char*) &delay,
sizeof (delay)) < 0) {
return -1;
return pd_errno();
}

return 0;
Expand All @@ -492,7 +496,7 @@ int pd_tcp_nodelay(pd_tcp_t *stream, int enable) {
TCP_NODELAY,
(const char *) &enable,
sizeof(enable)) < 0) {
return -1;
return pd_errno();
}

return 0;
Expand Down
Loading

0 comments on commit f523519

Please sign in to comment.