libevent: identifikace serverových soketů a jednotlivých spojení (pomocí čísel FD)
1.1 --- a/c++/domain-socket-bridge/domain-socket-bridge.c Sat Nov 19 23:29:47 2016 +0100
1.2 +++ b/c++/domain-socket-bridge/domain-socket-bridge.c Sun Nov 20 18:22:56 2016 +0100
1.3 @@ -13,8 +13,6 @@
1.4 #include <sys/un.h>
1.5 #include <unistd.h>
1.6
1.7 -#define MAX_LINE 16384
1.8 -
1.9 static const char MESSAGE[] = "Hello, World!\n";
1.10
1.11 static const char PATH[] = "./roura";
1.12 @@ -59,6 +57,9 @@
1.13 return 1;
1.14 }
1.15
1.16 + // identifikátor serverového soketu (v současnosti číslo FD)
1.17 + printf("Nasloucháme: sun_path = %s → socketId = %d\n", sun.sun_path, listener);
1.18 +
1.19 listener_event = event_new(base, listener, EV_READ | EV_PERSIST, listener_cb, (void*) base);
1.20
1.21 if (!listener_event) {
1.22 @@ -82,6 +83,7 @@
1.23 event_base_free(base);
1.24
1.25 // smažeme soket na disku / soubor -- jinak by program příště spadl na bind()
1.26 + // TODO: co když soket někdo přesune a místo něj dá jiný soubor?
1.27 unlink(PATH);
1.28
1.29 printf("done\n");
1.30 @@ -100,12 +102,13 @@
1.31 return;
1.32 } else if (fd > FD_SETSIZE) {
1.33 // FD_SETSIZE = 1024 -- Proč? Co když bude spojení víc?
1.34 + fprintf(stderr, "fd (%d) > FD_SETSIZE (%d)", fd, FD_SETSIZE);
1.35 close(fd);
1.36 return;
1.37 }
1.38
1.39 evutil_make_socket_nonblocking(fd);
1.40 -
1.41 +
1.42 print_socket_info(fd);
1.43
1.44 bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
1.45 @@ -114,17 +117,22 @@
1.46 event_base_loopbreak(base);
1.47 return;
1.48 }
1.49 - bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, (void*) "TODO: tady bude informace o spojení");
1.50 - bufferevent_setwatermark(bev, EV_READ, 0, MAX_LINE);
1.51 +
1.52 + // identifikátor navázaného spojení (v současnosti číslo FD)
1.53 + int * connectionId = (int*) malloc(sizeof (fd));
1.54 + *connectionId = fd;
1.55 +
1.56 +
1.57 + bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, (void*) connectionId);
1.58 bufferevent_enable(bev, EV_READ | EV_WRITE);
1.59
1.60 - printf("někdo se k nám připojil! / %p\n", bev);
1.61 + printf("někdo se k nám připojil! socketId = %d → connectionId = %d / bev = %p\n", listener, *connectionId, bev);
1.62
1.63 bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
1.64 }
1.65
1.66 static void conn_read_cb(struct bufferevent *bev, void *user_data) {
1.67 - printf("conn_read_cb: user_data = '%s'\n", (char*) user_data);
1.68 + printf("conn_read_cb: connectionId = %d\n", *((int*) user_data));
1.69
1.70 /* This callback is invoked when there is data to read on bev. */
1.71 struct evbuffer *input = bufferevent_get_input(bev);
1.72 @@ -149,7 +157,7 @@
1.73 }
1.74
1.75 static void conn_write_cb(struct bufferevent *bev, void *user_data) {
1.76 - printf("conn_write_cb: user_data = '%s'\n", (char*) user_data);
1.77 + printf("conn_write_cb: connectionId = %d\n", *((int*) user_data));
1.78
1.79 struct evbuffer *output = bufferevent_get_output(bev);
1.80 if (evbuffer_get_length(output) == 0) {
1.81 @@ -161,16 +169,18 @@
1.82 }
1.83
1.84 static void conn_event_cb(struct bufferevent *bev, short events, void *user_data) {
1.85 - printf("conn_event_cb: user_data = '%s'\n", (char*) user_data);
1.86 + int connectionId = *((int*) user_data);
1.87 + printf("conn_event_cb: connectionId = %d\n", connectionId);
1.88
1.89 if (events & BEV_EVENT_EOF) {
1.90 - printf("Connection closed.\n");
1.91 + printf("Connection closed: connectionId = %d\n", connectionId);
1.92 } else if (events & BEV_EVENT_ERROR) {
1.93 - printf("Got an error on the connection: %s\n", strerror(errno));
1.94 + printf("Got an error on the connectionId = %d: %s\n", connectionId, strerror(errno));
1.95 }
1.96
1.97 // None of the other events can happen here, since we haven't enabled timeouts
1.98 bufferevent_free(bev);
1.99 + free(user_data);
1.100 }
1.101
1.102 static void signal_cb(evutil_socket_t sig, short events, void *user_data) {