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