Ejemplo n.º 1
0
static int
basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
{
    struct basic_test_data *data = ptr;

    if (testcase->flags & TT_NO_LOGS)
        event_set_log_callback(NULL);

    if (testcase->flags & TT_NEED_SOCKETPAIR) {
        if (data->pair[0] != -1)
            evutil_closesocket(data->pair[0]);
        if (data->pair[1] != -1)
            evutil_closesocket(data->pair[1]);
    }

    if (testcase->flags & TT_NEED_DNS) {
        evdns_shutdown(0);
    }

    if (testcase->flags & TT_NEED_BASE) {
        if (data->base) {
            event_base_assert_ok_(data->base);
            event_base_free(data->base);
        }
    }

    if (testcase->flags & TT_FORK)
        libevent_global_shutdown();

    free(data);

    return 1;
}
Ejemplo n.º 2
0
void cNetworkSingleton::Terminate(void)
{
	ASSERT(!m_HasTerminated);

	// Wait for the lookup thread to stop
	m_LookupThread.Stop();

	// Wait for the LibEvent event loop to terminate:
	event_base_loopbreak(m_EventBase);
	m_EventLoopThread.join();

	// Remove all objects:
	{
		cCSLock Lock(m_CS);
		m_Connections.clear();
		m_Servers.clear();
	}

	// Free the underlying LibEvent objects:
	event_base_free(m_EventBase);

	libevent_global_shutdown();

	// Set the HasTerminated flag:
	// (Only set the flag after everything has been removed, to avoid the random failures in the Google-test, caused by links terminating after this flag was set)
	m_HasTerminated = true;
}
Ejemplo n.º 3
0
void endsocket(void)
{
	if(isconnected())
		shutdown(sock->socket,2);

	closesocket(sock->socket);
	if(sock->buf != NULL)
		bufferevent_free(sock->buf);
	event_base_loopbreak(sock->base);
	_sleep(500); //wait for libevent loop to break before unloading base.
	event_base_free(sock->base);
	libevent_global_shutdown();
	WSACleanup();
	free(sock);
}
Ejemplo n.º 4
0
int
main(int argc, const char **argv)
{
#ifdef _WIN32
    WORD wVersionRequested;
    WSADATA wsaData;

    wVersionRequested = MAKEWORD(2, 2);

    (void) WSAStartup(wVersionRequested, &wsaData);
#endif

#ifndef _WIN32
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
        return 1;
#endif

#ifdef _WIN32
    tinytest_skip(testgroups, "http/connection_retry");
    tinytest_skip(testgroups, "http/https_connection_retry");
#endif

#ifndef EVENT__DISABLE_THREAD_SUPPORT
    if (!getenv("EVENT_NO_DEBUG_LOCKS"))
        evthread_enable_lock_debugging();
#endif

    if (getenv("EVENT_DEBUG_MODE")) {
        event_enable_debug_mode();
        libevent_tests_running_in_debug_mode = 1;
    }
    if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
        event_enable_debug_logging(EVENT_DBG_ALL);
    }

    tinytest_set_aliases(testaliases);

    evutil_weakrand_seed_(&test_weakrand_state, 0);

    if (tinytest_main(argc,argv,testgroups))
        return 1;

    libevent_global_shutdown();

    return 0;
}
Ejemplo n.º 5
0
void
lgtd_cleanup(void)
{
    lgtd_lifx_discovery_close();
    lgtd_listen_close_all();
    lgtd_command_pipe_close_all();
    lgtd_client_close_all();
    lgtd_lifx_broadcast_close();
    lgtd_lifx_gateway_close_all();
    lgtd_timer_stop_all();
    lgtd_close_signal_handling();
    event_base_free(lgtd_ev_base);
#if LIBEVENT_VERSION_NUMBER >= 0x02010100
    libevent_global_shutdown();
#endif
    if (lgtd_opts.pidfile) {
        unlink(lgtd_opts.pidfile);
    }
}
Ejemplo n.º 6
0
int main(){
	int fd = socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(5000);
	addr.sin_addr.s_addr = INADDR_ANY;
	int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr));
	assert(ret!=-1);
	ret = listen(fd,5);
	assert(ret!=-1);
	struct event_base* base = event_base_new();
	struct event* ev = event_new(base,fd,EV_READ|EV_PERSIST,on_accept,base);
	//struct event* ev = event_new(base,fd,EV_READ,on_accept,base);
	event_add(ev,NULL);
	event_base_dispatch(base);
	event_free(ev);
	event_base_free(base);
	libevent_global_shutdown();
	return 0;
}
Ejemplo n.º 7
0
int
main(int argc, char **argv)
{
	struct event *evfifo;
	struct event_base* base;
#ifdef _WIN32
	HANDLE socket;
	/* Open a file. */
	socket = CreateFileA("test.txt",	/* open File */
			GENERIC_READ,		/* open for reading */
			0,			/* do not share */
			NULL,			/* no security */
			OPEN_EXISTING,		/* existing file only */
			FILE_ATTRIBUTE_NORMAL,	/* normal file */
			NULL);			/* no attr. template */

	if (socket == INVALID_HANDLE_VALUE)
		return 1;

#else
	struct event *signal_int;
	struct stat st;
	const char *fifo = "event.fifo";
	int socket;

	if (lstat(fifo, &st) == 0) {
		if ((st.st_mode & S_IFMT) == S_IFREG) {
			errno = EEXIST;
			perror("lstat");
			exit(1);
		}
	}

	unlink(fifo);
	if (mkfifo(fifo, 0600) == -1) {
		perror("mkfifo");
		exit(1);
	}

	socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);

	if (socket == -1) {
		perror("open");
		exit(1);
	}

	fprintf(stderr, "Write data to %s\n", fifo);
#endif
	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
#ifdef _WIN32
	evfifo = event_new(base, (evutil_socket_t)socket, EV_READ|EV_PERSIST, fifo_read,
                           event_self_cbarg());
#else
	/* catch SIGINT so that event.fifo can be cleaned up */
	//如果收到SIGINT,则退出eventloop循环
	signal_int = evsignal_new(base, SIGINT, signal_cb, base);
	event_add(signal_int, NULL);//将事件加入

	//处理socket的收事件
	evfifo = event_new(base, socket, EV_READ|EV_PERSIST, fifo_read,
                           event_self_cbarg());
#endif

	/* Add it to the active events, without a timeout */
	event_add(evfifo, NULL);//将此读事件加入

	event_base_dispatch(base);//事件循环
	//开始销毁
	event_base_free(base);
#ifdef _WIN32
	CloseHandle(socket);
#else
	close(socket);
	unlink(fifo);
#endif
	libevent_global_shutdown();
	return (0);
}
Ejemplo n.º 8
0
int main(int argc, char * * argv)
{
	struct sigaction sa;
	upnpc_t upnp;
	char * multicast_if = NULL;

	if(argc > 1) {
		multicast_if = argv[1];
	}

	memset(&sa, 0, sizeof(struct sigaction));
	sa.sa_handler = sighandler;
	if(sigaction(SIGINT, &sa, NULL) < 0) {
		perror("sigaction");
	}

	if(find_local_address() < 0) {
		fprintf(stderr, "failed to get local address\n");
		return 1;
	}
#ifdef DEBUG
	event_enable_debug_mode();
#if LIBEVENT_VERSION_NUMBER >= 0x02010100
	event_enable_debug_logging(EVENT_DBG_ALL);	/* Libevent 2.1.1 */
#endif /* LIBEVENT_VERSION_NUMBER >= 0x02010100 */
#endif /* DEBUG */
	printf("Using libevent %s\n", event_get_version());
	if(LIBEVENT_VERSION_NUMBER != event_get_version_number()) {
		fprintf(stderr, "WARNING build using libevent %s", LIBEVENT_VERSION);
	}

	base = event_base_new();
	if(base == NULL) {
		fprintf(stderr, "event_base_new() failed\n");
		return 1;
	}
#ifdef DEBUG
	printf("Using Libevent with backend method %s.\n",
        event_base_get_method(base));
#endif /* DEBUG */

	if(upnpc_init(&upnp, base, multicast_if, ready, soap, &upnp) != UPNPC_OK) {
		fprintf(stderr, "upnpc_init() failed\n");
		return 1;
	}
	upnpc_set_local_address(&upnp, local_address, 50000);
#ifdef ENABLE_UPNP_EVENTS
	upnpc_set_event_callback(&upnp, event_callback);
#endif /* ENABLE_UPNP_EVENTS */
	if(upnpc_start(&upnp) != UPNPC_OK) {
		fprintf(stderr, "upnp_start() failed\n");
		return 1;
	}

	event_base_dispatch(base);	/* TODO : check return value */
	printf("finishing...\n");

	upnpc_finalize(&upnp);
	event_base_free(base);

#if LIBEVENT_VERSION_NUMBER >= 0x02010100
	libevent_global_shutdown();	/* Libevent 2.1.1 */
#endif
	return 0;
}
Ejemplo n.º 9
0
int main(int argc, char **argv)
{
    natsConnection      *conn  = NULL;
    natsOptions         *opts  = NULL;
    natsSubscription    *sub   = NULL;
    natsStatus          s      = NATS_OK;
    struct event_base   *evLoop= NULL;

    nats_Open(-1);

    opts = parseArgs(argc, argv, usage);

    printf("Listening on '%s'.\n", subj);

    // One time initialization of things that we need.
    natsLibevent_Init();

    // Create a loop.
    evLoop = event_base_new();
    if (evLoop == NULL)
        s = NATS_ERR;

    // Indicate which loop and callbacks to use once connected.
    if (s == NATS_OK)
        s = natsOptions_SetEventLoop(opts, (void*) evLoop,
                                     natsLibevent_Attach,
                                     natsLibevent_Read,
                                     natsLibevent_Write,
                                     natsLibevent_Detach);

    if (s == NATS_OK)
        s = natsConnection_Connect(&conn, opts);

    if (s == NATS_OK)
        s = natsConnection_Subscribe(&sub, conn, subj, onMsg, NULL);

    // For maximum performance, set no limit on the number of pending messages.
    if (s == NATS_OK)
        s = natsSubscription_SetPendingLimits(sub, -1, -1);

    // Run the event loop.
    // This call will return when the connection is closed (either after
    // receiving all messages, or disconnected and unable to reconnect).
    if (s == NATS_OK)
        event_base_dispatch(evLoop);

    if (s == NATS_OK)
    {
        printPerf("Received", count, start, elapsed);
    }
    else
    {
        printf("Error: %d - %s\n", s, natsStatus_GetText(s));
        nats_PrintLastErrorStack(stderr);
    }

    // Destroy all our objects to avoid report of memory leak
    natsSubscription_Destroy(sub);
    natsConnection_Destroy(conn);
    natsOptions_Destroy(opts);

    if (evLoop != NULL)
        event_base_free(evLoop);

    // To silence reports of memory still in used with valgrind
    nats_Close();
    libevent_global_shutdown();

    return 0;
}
Ejemplo n.º 10
0
 void General::shutdown()
 {
     libevent_global_shutdown();
 }
Ejemplo n.º 11
0
int main (int argc, char **argv)
{
  int i, j;
  const char *s1, *s2;

  evthread_use_windows_threads ();

  s1 = evutil_socket_error_to_string (WSAEINTR);

  for (i = 0; i < 3; i++) {
    printf ("\niteration %d:\n\n", i);
    E(WSAEINTR);
    E(WSAEACCES);
    E(WSAEFAULT);
    E(WSAEINVAL);
    E(WSAEMFILE);
    E(WSAEWOULDBLOCK);
    E(WSAEINPROGRESS);
    E(WSAEALREADY);
    E(WSAENOTSOCK);
    E(WSAEDESTADDRREQ);
    E(WSAEMSGSIZE);
    E(WSAEPROTOTYPE);
    E(WSAENOPROTOOPT);
    E(WSAEPROTONOSUPPORT);
    E(WSAESOCKTNOSUPPORT);
    E(WSAEOPNOTSUPP);
    E(WSAEPFNOSUPPORT);
    E(WSAEAFNOSUPPORT);
    E(WSAEADDRINUSE);
    E(WSAEADDRNOTAVAIL);
    E(WSAENETDOWN);
    E(WSAENETUNREACH);
    E(WSAENETRESET);
    E(WSAECONNABORTED);
    E(WSAECONNRESET);
    E(WSAENOBUFS);
    E(WSAEISCONN);
    E(WSAENOTCONN);
    E(WSAESHUTDOWN);
    E(WSAETIMEDOUT);
    E(WSAECONNREFUSED);
    E(WSAEHOSTDOWN);
    E(WSAEHOSTUNREACH);
    E(WSAEPROCLIM);
    E(WSASYSNOTREADY);
    E(WSAVERNOTSUPPORTED);
    E(WSANOTINITIALISED);
    E(WSAEDISCON);
    E(WSATYPE_NOT_FOUND);
    E(WSAHOST_NOT_FOUND);
    E(WSATRY_AGAIN);
    E(WSANO_RECOVERY);
    E(WSANO_DATA);
    E(0xdeadbeef); /* test the case where no message is available */

    /* fill up the hash table a bit to make sure it grows properly */
    for (j = 0; j < 50; j++) {
      int err;
      evutil_secure_rng_get_bytes(&err, sizeof(err));
      evutil_socket_error_to_string(err);
    }
  }

  s2 = evutil_socket_error_to_string (WSAEINTR);
  if (s1 != s2)
    printf ("caching failed!\n");

  libevent_global_shutdown ();

  return EXIT_SUCCESS;
}
Ejemplo n.º 12
0
int main(int argc, char **argv)
{
    natsConnection      *conn  = NULL;
    natsOptions         *opts  = NULL;
    natsSubscription    *sub   = NULL;
    natsStatus          s      = NATS_OK;
    struct event_base   *evLoop= NULL;
    THREAD_T            pub;
    threadInfo          info;

    nats_Open(-1);

    opts = parseArgs(argc, argv, usage);

    printf("Sending %" PRId64 " messages to subject '%s'\n", total, subj);

    // One time initialization of things that we need.
    natsLibevent_Init();

    // Create a loop.
    evLoop = event_base_new();
    if (evLoop == NULL)
        s = NATS_ERR;

    // Indicate which loop and callbacks to use once connected.
    if (s == NATS_OK)
        s = natsOptions_SetEventLoop(opts, (void*) evLoop,
                                     natsLibevent_Attach,
                                     natsLibevent_Read,
                                     natsLibevent_Write,
                                     natsLibevent_Detach);

    if (s == NATS_OK)
        s = natsConnection_Connect(&conn, opts);

    if (s == NATS_OK)
        start = nats_Now();

    if (s == NATS_OK)
    {
        info.conn   = conn;
        info.status = NATS_OK;

        THREAD_START(pub, pubThread, (void*) &info);
    }

    if (s == NATS_OK)
    {
        event_base_dispatch(evLoop);

        THREAD_JOIN(pub);
        s = info.status;
    }

    if (s == NATS_OK)
    {
        printPerf("Sent", count, start, elapsed);
    }
    else
    {
        printf("Error: %d - %s\n", s, natsStatus_GetText(s));
        nats_PrintLastErrorStack(stderr);
    }

    // Destroy all our objects to avoid report of memory leak
    natsSubscription_Destroy(sub);
    natsConnection_Destroy(conn);
    natsOptions_Destroy(opts);

    if (evLoop != NULL)
        event_base_free(evLoop);

    // To silence reports of memory still in used with valgrind
    nats_Close();
    libevent_global_shutdown();

    return 0;
}