2012-02-23 21:40:02 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Ross Lagerwall <rosslagerwall@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-02-25 07:36:49 +02:00
|
|
|
#include "event2/event-config.h"
|
|
|
|
|
2012-02-25 07:47:49 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2012-02-23 21:40:02 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2012-02-25 07:36:49 +02:00
|
|
|
#ifdef _EVENT_HAVE_SYS_TIME_H
|
2012-02-25 07:21:20 +02:00
|
|
|
#include <sys/time.h>
|
2012-02-25 07:36:49 +02:00
|
|
|
#endif
|
|
|
|
#ifdef _EVENT_HAVE_SYS_RESOURCE_H
|
2012-02-23 21:40:02 +02:00
|
|
|
#include <sys/resource.h>
|
2012-02-25 07:36:49 +02:00
|
|
|
#endif
|
2012-02-23 21:40:02 +02:00
|
|
|
|
|
|
|
#include "event2/event.h"
|
|
|
|
#include "event2/bufferevent.h"
|
2012-02-27 17:57:01 -05:00
|
|
|
#include "event2/buffer.h"
|
2012-02-23 21:40:02 +02:00
|
|
|
#include "event2/listener.h"
|
|
|
|
|
2012-02-25 07:21:20 +02:00
|
|
|
/* This test opens a server socket, and many client sockets which connect to
|
|
|
|
the server socket many times. It sets a low number for the max open file
|
|
|
|
limit to catch any file descriptor leaks. */
|
2012-02-27 17:57:01 -05:00
|
|
|
/* XXX This keeps two instances of this test from running in parallel. */
|
2012-02-23 21:40:02 +02:00
|
|
|
#define PORT 31456
|
|
|
|
|
|
|
|
/* Number of requests to make. Setting this too high might result in the machine
|
|
|
|
running out of ephemeral ports */
|
2012-02-25 07:21:20 +02:00
|
|
|
#define MAX_REQUESTS 4000
|
2012-02-23 21:40:02 +02:00
|
|
|
|
|
|
|
/* Provide storage for the address, both for the server & the clients */
|
|
|
|
static struct sockaddr_in sin;
|
|
|
|
|
|
|
|
/* Number of sucessful requests so far */
|
|
|
|
static int num_requests;
|
|
|
|
|
|
|
|
static void start_client(struct event_base *base);
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================
|
|
|
|
Server functions
|
|
|
|
===============================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Read a byte from the client and write it back */
|
|
|
|
static void
|
|
|
|
server_read_cb(struct bufferevent *bev, void *ctx)
|
|
|
|
{
|
2012-02-27 17:57:01 -05:00
|
|
|
while (evbuffer_get_length(bufferevent_get_input(bev))) {
|
|
|
|
unsigned char tmp;
|
|
|
|
bufferevent_read(bev, &tmp, 1);
|
|
|
|
bufferevent_write(bev, &tmp, 1);
|
|
|
|
}
|
2012-02-23 21:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for an EOF and then free the bufferevent */
|
|
|
|
static void
|
|
|
|
server_write_cb(struct bufferevent *bev, short events, void *ctx)
|
2012-02-27 17:57:01 -05:00
|
|
|
/*XXX rename */
|
2012-02-23 21:40:02 +02:00
|
|
|
{
|
|
|
|
if (events & BEV_EVENT_ERROR) {
|
2012-02-27 17:57:01 -05:00
|
|
|
/* XXX won't capture net errors on windows */
|
2012-02-23 21:40:02 +02:00
|
|
|
perror("Error from bufferevent");
|
|
|
|
exit(1);
|
|
|
|
} else if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
|
|
|
|
bufferevent_free(bev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept a client socket and set it up to for reading & writing */
|
|
|
|
static void
|
|
|
|
listener_accept_cb(struct evconnlistener *listener, evutil_socket_t sock,
|
|
|
|
struct sockaddr *addr, int len, void *ptr)
|
|
|
|
{
|
|
|
|
struct event_base *base = evconnlistener_get_base(listener);
|
|
|
|
struct bufferevent *bev = bufferevent_socket_new(base, sock,
|
|
|
|
BEV_OPT_CLOSE_ON_FREE);
|
|
|
|
|
|
|
|
bufferevent_setcb(bev, server_read_cb, NULL, server_write_cb, NULL);
|
|
|
|
bufferevent_enable(bev, EV_READ|EV_WRITE);
|
|
|
|
}
|
|
|
|
|
2012-02-25 07:21:20 +02:00
|
|
|
/* Start the server listening on PORT and start the first client. */
|
2012-02-23 21:40:02 +02:00
|
|
|
static void
|
2012-02-25 07:21:20 +02:00
|
|
|
start_loop(void)
|
2012-02-23 21:40:02 +02:00
|
|
|
{
|
|
|
|
struct event_base *base;
|
|
|
|
struct evconnlistener *listener;
|
|
|
|
|
|
|
|
base = event_base_new();
|
|
|
|
if (base == NULL) {
|
|
|
|
puts("Could not open event base!");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
listener = evconnlistener_new_bind(base, listener_accept_cb, NULL,
|
|
|
|
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
|
|
|
|
-1, (struct sockaddr *)&sin, sizeof(sin));
|
|
|
|
if (listener == NULL) {
|
|
|
|
perror("Could not create listener!");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-02-25 07:21:20 +02:00
|
|
|
start_client(base);
|
2012-02-23 21:40:02 +02:00
|
|
|
|
|
|
|
event_base_dispatch(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================
|
|
|
|
Client functions
|
|
|
|
===============================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Check that the server sends back the same byte that the client sent.
|
2012-02-25 07:21:20 +02:00
|
|
|
If MAX_REQUESTS have been reached, exit. Otherwise, start another client. */
|
2012-02-23 21:40:02 +02:00
|
|
|
static void
|
|
|
|
client_read_cb(struct bufferevent *bev, void *ctx)
|
|
|
|
{
|
|
|
|
unsigned char tmp;
|
|
|
|
struct event_base *base = bufferevent_get_base(bev);
|
|
|
|
|
|
|
|
bufferevent_read(bev, &tmp, 1);
|
|
|
|
if (tmp != 'A') {
|
|
|
|
puts("Incorrect data received!");
|
2012-02-25 07:21:20 +02:00
|
|
|
exit(2);
|
2012-02-23 21:40:02 +02:00
|
|
|
}
|
|
|
|
bufferevent_free(bev);
|
|
|
|
|
|
|
|
num_requests++;
|
|
|
|
if (num_requests == MAX_REQUESTS) {
|
|
|
|
event_base_loopbreak(base);
|
|
|
|
} else {
|
|
|
|
start_client(base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send a byte to the server. */
|
|
|
|
static void
|
|
|
|
client_write_cb(struct bufferevent *bev, short events, void *ctx)
|
2012-02-27 17:57:01 -05:00
|
|
|
/*XXX rename */
|
2012-02-23 21:40:02 +02:00
|
|
|
{
|
|
|
|
if (events & BEV_EVENT_CONNECTED) {
|
|
|
|
unsigned char tmp = 'A';
|
|
|
|
bufferevent_write(bev, &tmp, 1);
|
|
|
|
} else if (events & BEV_EVENT_ERROR) {
|
|
|
|
puts("Client socket got error!");
|
2012-02-25 07:21:20 +02:00
|
|
|
exit(2);
|
2012-02-23 21:40:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bufferevent_enable(bev, EV_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a client socket to connect to localhost on PORT. */
|
|
|
|
static void
|
|
|
|
start_client(struct event_base *base)
|
|
|
|
{
|
|
|
|
struct bufferevent *bev = bufferevent_socket_new(base, -1,
|
|
|
|
BEV_OPT_CLOSE_ON_FREE);
|
|
|
|
bufferevent_setcb(bev, client_read_cb, NULL, client_write_cb, NULL);
|
2012-02-27 17:57:01 -05:00
|
|
|
fprintf(stdout, ".");
|
|
|
|
fflush(stdout);
|
2012-02-23 21:40:02 +02:00
|
|
|
if (bufferevent_socket_connect(bev, (struct sockaddr *)&sin,
|
|
|
|
sizeof(sin)) < 0) {
|
2012-02-27 17:57:01 -05:00
|
|
|
/* XXX won't capture net errors on windows */
|
2012-02-23 21:40:02 +02:00
|
|
|
perror("Could not connect!");
|
|
|
|
bufferevent_free(bev);
|
2012-02-25 07:21:20 +02:00
|
|
|
exit(2);
|
2012-02-23 21:40:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2012-02-25 07:47:49 +02:00
|
|
|
#ifdef _EVENT_HAVE_SETRLIMIT
|
2012-02-23 21:40:02 +02:00
|
|
|
/* Set the fd limit to a low value so that any fd leak is caught without
|
|
|
|
making many requests. */
|
|
|
|
struct rlimit rl;
|
|
|
|
rl.rlim_cur = rl.rlim_max = 20;
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
|
|
|
|
perror("setrlimit");
|
2012-02-25 07:21:20 +02:00
|
|
|
exit(3);
|
2012-02-23 21:40:02 +02:00
|
|
|
}
|
2012-02-25 07:36:49 +02:00
|
|
|
#endif
|
2012-02-23 21:40:02 +02:00
|
|
|
|
2012-02-25 07:47:49 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA WSAData;
|
|
|
|
WSAStartup(0x101, &WSAData);
|
|
|
|
#endif
|
|
|
|
|
2012-02-23 21:40:02 +02:00
|
|
|
/* Set up an address, used by both client & server. */
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = htonl(0x7f000001);
|
|
|
|
sin.sin_port = htons(PORT);
|
|
|
|
|
2012-02-25 07:21:20 +02:00
|
|
|
start_loop();
|
2012-02-23 21:40:02 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-27 17:57:01 -05:00
|
|
|
|
|
|
|
/* XXX why does this test cause so much latency sometimes (OSX 10.5)? */
|