Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* |
michael@0 | 2 | This exmple program provides a trivial server program that listens for TCP |
michael@0 | 3 | connections on port 9995. When they arrive, it writes a short message to |
michael@0 | 4 | each client connection, and closes each connection once it is flushed. |
michael@0 | 5 | |
michael@0 | 6 | Where possible, it exits cleanly in response to a SIGINT (ctrl-c). |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include <string.h> |
michael@0 | 11 | #include <errno.h> |
michael@0 | 12 | #include <stdio.h> |
michael@0 | 13 | #include <signal.h> |
michael@0 | 14 | #ifndef WIN32 |
michael@0 | 15 | #include <netinet/in.h> |
michael@0 | 16 | # ifdef _XOPEN_SOURCE_EXTENDED |
michael@0 | 17 | # include <arpa/inet.h> |
michael@0 | 18 | # endif |
michael@0 | 19 | #include <sys/socket.h> |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | #include <event2/bufferevent.h> |
michael@0 | 23 | #include <event2/buffer.h> |
michael@0 | 24 | #include <event2/listener.h> |
michael@0 | 25 | #include <event2/util.h> |
michael@0 | 26 | #include <event2/event.h> |
michael@0 | 27 | |
michael@0 | 28 | static const char MESSAGE[] = "Hello, World!\n"; |
michael@0 | 29 | |
michael@0 | 30 | static const int PORT = 9995; |
michael@0 | 31 | |
michael@0 | 32 | static void listener_cb(struct evconnlistener *, evutil_socket_t, |
michael@0 | 33 | struct sockaddr *, int socklen, void *); |
michael@0 | 34 | static void conn_writecb(struct bufferevent *, void *); |
michael@0 | 35 | static void conn_eventcb(struct bufferevent *, short, void *); |
michael@0 | 36 | static void signal_cb(evutil_socket_t, short, void *); |
michael@0 | 37 | |
michael@0 | 38 | int |
michael@0 | 39 | main(int argc, char **argv) |
michael@0 | 40 | { |
michael@0 | 41 | struct event_base *base; |
michael@0 | 42 | struct evconnlistener *listener; |
michael@0 | 43 | struct event *signal_event; |
michael@0 | 44 | |
michael@0 | 45 | struct sockaddr_in sin; |
michael@0 | 46 | #ifdef WIN32 |
michael@0 | 47 | WSADATA wsa_data; |
michael@0 | 48 | WSAStartup(0x0201, &wsa_data); |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | base = event_base_new(); |
michael@0 | 52 | if (!base) { |
michael@0 | 53 | fprintf(stderr, "Could not initialize libevent!\n"); |
michael@0 | 54 | return 1; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | memset(&sin, 0, sizeof(sin)); |
michael@0 | 58 | sin.sin_family = AF_INET; |
michael@0 | 59 | sin.sin_port = htons(PORT); |
michael@0 | 60 | |
michael@0 | 61 | listener = evconnlistener_new_bind(base, listener_cb, (void *)base, |
michael@0 | 62 | LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1, |
michael@0 | 63 | (struct sockaddr*)&sin, |
michael@0 | 64 | sizeof(sin)); |
michael@0 | 65 | |
michael@0 | 66 | if (!listener) { |
michael@0 | 67 | fprintf(stderr, "Could not create a listener!\n"); |
michael@0 | 68 | return 1; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base); |
michael@0 | 72 | |
michael@0 | 73 | if (!signal_event || event_add(signal_event, NULL)<0) { |
michael@0 | 74 | fprintf(stderr, "Could not create/add a signal event!\n"); |
michael@0 | 75 | return 1; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | event_base_dispatch(base); |
michael@0 | 79 | |
michael@0 | 80 | evconnlistener_free(listener); |
michael@0 | 81 | event_free(signal_event); |
michael@0 | 82 | event_base_free(base); |
michael@0 | 83 | |
michael@0 | 84 | printf("done\n"); |
michael@0 | 85 | return 0; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | static void |
michael@0 | 89 | listener_cb(struct evconnlistener *listener, evutil_socket_t fd, |
michael@0 | 90 | struct sockaddr *sa, int socklen, void *user_data) |
michael@0 | 91 | { |
michael@0 | 92 | struct event_base *base = user_data; |
michael@0 | 93 | struct bufferevent *bev; |
michael@0 | 94 | |
michael@0 | 95 | bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); |
michael@0 | 96 | if (!bev) { |
michael@0 | 97 | fprintf(stderr, "Error constructing bufferevent!"); |
michael@0 | 98 | event_base_loopbreak(base); |
michael@0 | 99 | return; |
michael@0 | 100 | } |
michael@0 | 101 | bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL); |
michael@0 | 102 | bufferevent_enable(bev, EV_WRITE); |
michael@0 | 103 | bufferevent_disable(bev, EV_READ); |
michael@0 | 104 | |
michael@0 | 105 | bufferevent_write(bev, MESSAGE, strlen(MESSAGE)); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | static void |
michael@0 | 109 | conn_writecb(struct bufferevent *bev, void *user_data) |
michael@0 | 110 | { |
michael@0 | 111 | struct evbuffer *output = bufferevent_get_output(bev); |
michael@0 | 112 | if (evbuffer_get_length(output) == 0) { |
michael@0 | 113 | printf("flushed answer\n"); |
michael@0 | 114 | bufferevent_free(bev); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | static void |
michael@0 | 119 | conn_eventcb(struct bufferevent *bev, short events, void *user_data) |
michael@0 | 120 | { |
michael@0 | 121 | if (events & BEV_EVENT_EOF) { |
michael@0 | 122 | printf("Connection closed.\n"); |
michael@0 | 123 | } else if (events & BEV_EVENT_ERROR) { |
michael@0 | 124 | printf("Got an error on the connection: %s\n", |
michael@0 | 125 | strerror(errno));/*XXX win32*/ |
michael@0 | 126 | } |
michael@0 | 127 | /* None of the other events can happen here, since we haven't enabled |
michael@0 | 128 | * timeouts */ |
michael@0 | 129 | bufferevent_free(bev); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | static void |
michael@0 | 133 | signal_cb(evutil_socket_t sig, short events, void *user_data) |
michael@0 | 134 | { |
michael@0 | 135 | struct event_base *base = user_data; |
michael@0 | 136 | struct timeval delay = { 2, 0 }; |
michael@0 | 137 | |
michael@0 | 138 | printf("Caught an interrupt signal; exiting cleanly in two seconds.\n"); |
michael@0 | 139 | |
michael@0 | 140 | event_base_loopexit(base, &delay); |
michael@0 | 141 | } |