# HG changeset patch # User František Kučera # Date 1479662576 -3600 # Node ID 988b56d4a7b8c6f04337c84c260787139219ff53 # Parent 6ec32ee08feb8e438fe3266ced698fb5627b8edd libevent: identifikace serverových soketů a jednotlivých spojení (pomocí čísel FD) diff -r 6ec32ee08feb -r 988b56d4a7b8 c++/domain-socket-bridge/domain-socket-bridge.c --- a/c++/domain-socket-bridge/domain-socket-bridge.c Sat Nov 19 23:29:47 2016 +0100 +++ b/c++/domain-socket-bridge/domain-socket-bridge.c Sun Nov 20 18:22:56 2016 +0100 @@ -13,8 +13,6 @@ #include #include -#define MAX_LINE 16384 - static const char MESSAGE[] = "Hello, World!\n"; static const char PATH[] = "./roura"; @@ -59,6 +57,9 @@ return 1; } + // identifikátor serverového soketu (v současnosti číslo FD) + printf("Nasloucháme: sun_path = %s → socketId = %d\n", sun.sun_path, listener); + listener_event = event_new(base, listener, EV_READ | EV_PERSIST, listener_cb, (void*) base); if (!listener_event) { @@ -82,6 +83,7 @@ event_base_free(base); // smažeme soket na disku / soubor -- jinak by program příště spadl na bind() + // TODO: co když soket někdo přesune a místo něj dá jiný soubor? unlink(PATH); printf("done\n"); @@ -100,12 +102,13 @@ return; } else if (fd > FD_SETSIZE) { // FD_SETSIZE = 1024 -- Proč? Co když bude spojení víc? + fprintf(stderr, "fd (%d) > FD_SETSIZE (%d)", fd, FD_SETSIZE); close(fd); return; } evutil_make_socket_nonblocking(fd); - + print_socket_info(fd); bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); @@ -114,17 +117,22 @@ event_base_loopbreak(base); return; } - bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, (void*) "TODO: tady bude informace o spojení"); - bufferevent_setwatermark(bev, EV_READ, 0, MAX_LINE); + + // identifikátor navázaného spojení (v současnosti číslo FD) + int * connectionId = (int*) malloc(sizeof (fd)); + *connectionId = fd; + + + bufferevent_setcb(bev, conn_read_cb, conn_write_cb, conn_event_cb, (void*) connectionId); bufferevent_enable(bev, EV_READ | EV_WRITE); - printf("někdo se k nám připojil! / %p\n", bev); + printf("někdo se k nám připojil! socketId = %d → connectionId = %d / bev = %p\n", listener, *connectionId, bev); bufferevent_write(bev, MESSAGE, strlen(MESSAGE)); } static void conn_read_cb(struct bufferevent *bev, void *user_data) { - printf("conn_read_cb: user_data = '%s'\n", (char*) user_data); + printf("conn_read_cb: connectionId = %d\n", *((int*) user_data)); /* This callback is invoked when there is data to read on bev. */ struct evbuffer *input = bufferevent_get_input(bev); @@ -149,7 +157,7 @@ } static void conn_write_cb(struct bufferevent *bev, void *user_data) { - printf("conn_write_cb: user_data = '%s'\n", (char*) user_data); + printf("conn_write_cb: connectionId = %d\n", *((int*) user_data)); struct evbuffer *output = bufferevent_get_output(bev); if (evbuffer_get_length(output) == 0) { @@ -161,16 +169,18 @@ } static void conn_event_cb(struct bufferevent *bev, short events, void *user_data) { - printf("conn_event_cb: user_data = '%s'\n", (char*) user_data); + int connectionId = *((int*) user_data); + printf("conn_event_cb: connectionId = %d\n", connectionId); if (events & BEV_EVENT_EOF) { - printf("Connection closed.\n"); + printf("Connection closed: connectionId = %d\n", connectionId); } else if (events & BEV_EVENT_ERROR) { - printf("Got an error on the connection: %s\n", strerror(errno)); + printf("Got an error on the connectionId = %d: %s\n", connectionId, strerror(errno)); } // None of the other events can happen here, since we haven't enabled timeouts bufferevent_free(bev); + free(user_data); } static void signal_cb(evutil_socket_t sig, short events, void *user_data) {