Skip to content

Commit 2f2e6c6

Browse files
committed
Add optional error callback.
1 parent 8e31501 commit 2f2e6c6

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

Wu.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ const char* WuClientStateString(WuClientState state) {
5454
}
5555
}
5656

57+
void WuHandleError(WuHost* wu, const char* description) {
58+
wu->errorHandler(description, wu->errorHandlerData);
59+
}
60+
61+
void WuHandleErrno(WuHost* wu, const char* description) {
62+
snprintf(wu->errBuf, sizeof(wu->errBuf), "%s: %s", description,
63+
strerror(errno));
64+
WuHandleError(wu, wu->errBuf);
65+
}
66+
5767
struct WuConnectionBuffer {
5868
size_t size = 0;
5969
int fd = -1;
@@ -204,7 +214,7 @@ void WuHandleHttpRequest(WuHost* wu, WuConnectionBuffer* conn) {
204214
kMaxHttpRequestLength - conn->size);
205215
if (count == -1) {
206216
if (errno != EAGAIN) {
207-
perror("read");
217+
WuHandleErrno(wu, "failed to read from TCP socket");
208218
close(conn->fd);
209219
wu->bufferPool->Reclaim(conn);
210220
}
@@ -660,13 +670,19 @@ int32_t WuCryptoInit(WuHost* wu, const WuConf* conf) {
660670
}
661671

662672
int32_t WuInit(WuHost* wu, const WuConf* conf) {
673+
memset(wu, 0, sizeof(WuHost));
663674
wu->arena = (WuArena*)calloc(1, sizeof(WuArena));
664675
WuArenaInit(wu->arena, 1 << 20);
665676
wu->time = MsNow() * 0.001;
666677
wu->dt = 0.0;
667678

679+
wu->errorHandler =
680+
conf->errorHandler ? conf->errorHandler : [](const char*, void*) {};
681+
wu->errorHandlerData = conf->errorHandlerData;
682+
668683
wu->port = atoi(conf->port);
669684
if (!WuCryptoInit(wu, conf)) {
685+
WuHandleError(wu, "failed to init crypto");
670686
return 0;
671687
}
672688

@@ -683,7 +699,7 @@ int32_t WuInit(WuHost* wu, const WuConf* conf) {
683699

684700
s = listen(wu->tcpfd, SOMAXCONN);
685701
if (s == -1) {
686-
perror("listen");
702+
WuHandleErrno(wu, "tcp listen failed");
687703
return 0;
688704
}
689705

@@ -700,7 +716,7 @@ int32_t WuInit(WuHost* wu, const WuConf* conf) {
700716

701717
wu->epfd = epoll_create1(0);
702718
if (wu->epfd == -1) {
703-
perror("epoll_create");
719+
WuHandleErrno(wu, "epoll_create");
704720
return 0;
705721
}
706722

@@ -721,14 +737,14 @@ int32_t WuInit(WuHost* wu, const WuConf* conf) {
721737

722738
s = epoll_ctl(wu->epfd, EPOLL_CTL_ADD, wu->tcpfd, &event);
723739
if (s == -1) {
724-
perror("epoll_ctl");
740+
WuHandleErrno(wu, "EPOLL_CTL_ADD tcpfd");
725741
return 0;
726742
}
727743

728744
event.data.ptr = udpBuf;
729745
s = epoll_ctl(wu->epfd, EPOLL_CTL_ADD, wu->udpfd, &event);
730746
if (s == -1) {
731-
perror("epoll_ctl");
747+
WuHandleErrno(wu, "EPOLL_CTL_ADD udpfd");
732748
return 0;
733749
}
734750

@@ -807,7 +823,7 @@ int32_t WuServe(WuHost* wu, WuEvent* evt) {
807823
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
808824
break;
809825
} else {
810-
perror("accept");
826+
WuHandleErrno(wu, "tcp accept");
811827
break;
812828
}
813829
}
@@ -826,7 +842,7 @@ int32_t WuServe(WuHost* wu, WuEvent* evt) {
826842
event.data.ptr = conn;
827843
if (epoll_ctl(wu->epfd, EPOLL_CTL_ADD, infd, &event) == -1) {
828844
close(infd);
829-
perror("epoll_ctl");
845+
WuHandleErrno(wu, "EPOLL_CTL_ADD infd");
830846
}
831847
} else {
832848
close(infd);

Wu.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ struct WuEvent {
2929
} as;
3030
};
3131

32+
typedef void (*WuErrorFn)(const char* err, void* userData);
33+
3234
struct WuConf {
3335
const char* host;
3436
const char* port;
37+
WuErrorFn errorHandler;
38+
void* errorHandlerData;
3539
};
3640

3741
struct WuHost {
@@ -56,6 +60,10 @@ struct WuHost {
5660
ssl_ctx_st* sslCtx;
5761

5862
const WuConf* conf;
63+
64+
char errBuf[512];
65+
void* errorHandlerData;
66+
WuErrorFn errorHandler;
5967
};
6068

6169
int32_t WuInit(WuHost* wu, const WuConf* conf);

WuNetwork.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ ssize_t SocketWrite(int fd, const uint8_t* buf, size_t len) {
2424
ssize_t r = write(fd, buf + written, towrite - written);
2525
if (r == 0) return written;
2626
if (r == -1) {
27-
perror("socketwrite");
2827
return -1;
2928
}
3029

@@ -41,15 +40,13 @@ ssize_t SocketWrite(int fd, const char* buf, size_t len) {
4140
int MakeNonBlocking(int sfd) {
4241
int flags = fcntl(sfd, F_GETFL, 0);
4342
if (flags == -1) {
44-
perror("fcntl");
4543
return -1;
4644
}
4745

4846
flags |= O_NONBLOCK;
4947

5048
int s = fcntl(sfd, F_SETFL, flags);
5149
if (s == -1) {
52-
perror("set fcntl");
5350
return -1;
5451
}
5552

@@ -68,7 +65,6 @@ int CreateSocket(const char* port, SocketType type) {
6865
int s = getaddrinfo(NULL, port, &hints, &result);
6966

7067
if (s != 0) {
71-
printf("getaddrinfo: %s\n", gai_strerror(s));
7268
return -1;
7369
}
7470

@@ -82,9 +78,7 @@ int CreateSocket(const char* port, SocketType type) {
8278
}
8379

8480
int enable = 1;
85-
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
86-
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
87-
}
81+
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
8882

8983
s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
9084
if (s == 0) {
@@ -97,7 +91,6 @@ int CreateSocket(const char* port, SocketType type) {
9791
freeaddrinfo(result);
9892

9993
if (rp == NULL) {
100-
printf("Failed to bind\n");
10194
return -1;
10295
}
10396

examples/EchoServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <string.h>
34
#include <chrono>
45
#include <thread>
56
#include "../Wu.h"
@@ -8,6 +9,7 @@ int main(int argc, char** argv) {
89
WuHost wu;
910

1011
WuConf conf;
12+
memset(&conf, 0, sizeof(conf));
1113

1214
if (argc > 2) {
1315
conf.host = argv[1];
@@ -17,6 +19,8 @@ int main(int argc, char** argv) {
1719
conf.port = "9555";
1820
}
1921

22+
conf.errorHandler = [](const char* e, void*) { printf("%s\n", e); };
23+
2024
if (!WuInit(&wu, &conf)) {
2125
printf("init fail\n");
2226
return 1;

0 commit comments

Comments
 (0)