Exemple #1
0
void socket_release(void)
{
    if(g_event_listener_async){
        evconnlistener_free(g_event_listener_async);
    }
    if(g_event_listener_cli){
        evconnlistener_free(g_event_listener_cli);
    }
    event_base_free(g_event_base);
}
cServerHandleImpl::~cServerHandleImpl()
{
	if (m_ConnListener != nullptr)
	{
		evconnlistener_free(m_ConnListener);
	}
	if (m_SecondaryConnListener != nullptr)
	{
		evconnlistener_free(m_SecondaryConnListener);
	}
}
Exemple #3
0
int main(int argc, char **argv){
	struct event_base *base;
	struct evconnlistener *listener;
	struct event *signal_event;

	struct sockaddr_in sin;

	base = event_base_new();
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);

	listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
	    LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
	    (struct sockaddr*)&sin,
	    sizeof(sin));

	signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);


	event_base_dispatch(base);

	evconnlistener_free(listener);
	event_free(signal_event);
	event_base_free(base);

	printf("done\n");
	return 0;
}
Exemple #4
0
void server_free(server_t *server)
{
    if(server == NULL)
        return;

    if(server->addr)
    {
        free(server->addr);
    }

    if(server->socket)
    {
        close(server->socket);
    }

    if(server->evbase)
    {
        event_base_free(server->evbase);
    }

    if(server->evlistener)
    {
        evconnlistener_free(server->evlistener);
    }

    if(server->evsignal)
    {
        event_base_free(server->evsignal);
    }

    free(server);

    return;
}
int
main(int argc, char **argv)
{
    SSL_CTX *ctx;
    struct evconnlistener *listener;
    struct event_base *evbase;
    struct sockaddr_in sin;

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(9999);
    sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */

    ctx = evssl_init();
    if (ctx == NULL)
        return 1;
    evbase = event_base_new();
    listener = evconnlistener_new_bind(
                   evbase, ssl_acceptcb, (void *)ctx,
                   LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 1024,
                   (struct sockaddr *)&sin, sizeof(sin));

    event_base_loop(evbase, 0);

    evconnlistener_free(listener);
    SSL_CTX_free(ctx);

    return 0;
}
Exemple #6
0
void main(int argc, char **argv)
{
        int server_addrlen;
        struct evconnlistener *listener;

        base = event_base_new();
        if (!base) {
                perror("event_base_new()");
                exit(1);
        }

        memset(&client_addr, 0, sizeof(client_addr));
		memset(&server_addr, 0, sizeof(server_addr));
        client_addrlen = sizeof(client_addr);
        server_addrlen = sizeof(server_addr);

        evutil_parse_sockaddr_port(argv[1], (struct sockaddr*)&server_addr, &server_addrlen); 
        evutil_parse_sockaddr_port(argv[2], (struct sockaddr*)&client_addr, &client_addrlen);

        listener = evconnlistener_new_bind(base, accept_cb, NULL,
            LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE,
            -1, (struct sockaddr*)&server_addr, server_addrlen);

        event_base_dispatch(base);

        evconnlistener_free(listener);
        event_base_free(base);

}
Exemple #7
0
void CServer::ShutdownReal()
{
	IServer::Ptr ref = shared_from_this();
	sLogInfo("server(%s:%d) shutdown", local_ip_.c_str(), local_port_);
	evconnlistener_free(evconn_listener_);
	net_shutdown_listener_.Disconnect();
}
Exemple #8
0
int bp_server_deinit(bp_server_s *server)
{
	if (server->listener) {
		evconnlistener_free(server->listener);
	}
	return 0;
}
Exemple #9
0
int NetServer::ServerThread(void* param) {
	event_base_dispatch(net_evbase);
	for(auto bit = users.begin(); bit != users.end(); ++bit) {
		bufferevent_disable(bit->first, EV_READ);
		bufferevent_free(bit->first);
	}
	users.clear();
	evconnlistener_free(listener);
	listener = 0;
	if(broadcast_ev) {
		evutil_socket_t fd;
		event_get_assignment(broadcast_ev, 0, &fd, 0, 0, 0);
		evutil_closesocket(fd);
		event_free(broadcast_ev);
		broadcast_ev = 0;
	}
	if(duel_mode) {
		event_free(duel_mode->etimer);
		delete duel_mode;
	}
	duel_mode = 0;
	event_base_free(net_evbase);
	net_evbase = 0;
	return 0;
}
Exemple #10
0
int jmm_uninit_event()
{
    if(event.listener){
        evconnlistener_free(event.listener);
        event.listener = NULL;
    }
    if(event.child){
        event_free(event.child);
        event.child = NULL;
    }
    if(event.ctrl_c){
        event_free(event.ctrl_c);
        event.ctrl_c = NULL;
    }
    if(event.usr1){
        event_free(event.usr1);
        event.usr1 = NULL;
    }
    if (event.term) {
        event_free(event.term);
        event.term = NULL;
    }

    return JMM_SUCCESS;
}
Exemple #11
0
void http_server_free(struct http_server *hs)
{
	if (hs) {
		if (hs->uris != NULL) {
			struct http_uri *hu = NULL;
			while (hs->uris != NULL) {
				hu = hs->uris;
				hs->uris = hu->next;
				http_uri_free(hu);
			}
		}

		if (hs->listener) {
			evconnlistener_free(hs->listener);
		}

		if (hs->hc) {
			struct http_connection *hc = hs->hc->next;
			if (hc) {
				while (hc != hs->hc) {
					dbprintf("http connections %p not free\n", hc);
					http_connection_free(hc);
					hc = hs->hc->next;
				}
				free(hs->hc);
			}
		}

		free(hs);
	}
}
Exemple #12
0
int main() {
	//SIGPIPE ignore
	struct sigaction act;
	act.sa_handler = SIG_IGN;
	if (sigaction(SIGPIPE, &act, NULL) == 0) {
		LOG("SIGPIPE ignore");
	}

	//initialize
	setbuf(stdout, NULL);
	base = event_base_new();
	assert(base);

	struct sockaddr_in srvaddr;
	srvaddr.sin_family = AF_INET;
	srvaddr.sin_addr.s_addr = INADDR_ANY;
	srvaddr.sin_port = htons(10086);

	listener = evconnlistener_new_bind(base, listencb, NULL, LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE, 500, (const struct sockaddr*)&srvaddr, sizeof(struct sockaddr));
	assert(listener);

	event_base_dispatch(base);

	LOG("loop exit");

	evconnlistener_free(listener);
	event_base_free(base);

	return 0;
}
Exemple #13
0
	MMaster::~MMaster()
	{
		if (server_base_ != NULL)
			event_base_free (server_base_);
		if (server_conn_listenner_ != NULL)
			evconnlistener_free (server_conn_listenner_);
	}
static void
regress_listener_error(void *arg)
{
	struct basic_test_data *data = arg;
	struct event_base *base = data->base;
	struct evconnlistener *listener = NULL;
	int count = 1;
	unsigned int flags = LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE;

	if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
		flags |= LEV_OPT_THREADSAFE;
	}

	/* send, so that pair[0] will look 'readable'*/
	tt_int_op(send(data->pair[1], "hello", 5, 0), >, 0);

	/* Start a listener with a bogus socket. */
	listener = evconnlistener_new(base, acceptcb, &count,
	    flags, 0,
	    data->pair[0]);
	tt_assert(listener);

	evconnlistener_set_error_cb(listener, errorcb);

	tt_assert(listener);

	event_base_dispatch(base);
	tt_int_op(count,==,1000); /* set by error cb */

end:
	if (listener)
		evconnlistener_free(listener);
}
Exemple #15
0
int main(int argc, char *argv[])
{
    struct event_base *base = event_base_new();
    assert(base!=NULL);

    struct evconnlistener *listener;
    struct event *signal_event;
    struct sockaddr_in myAddr;

    bzero(&myAddr,sizeof(myAddr));
    myAddr.sin_family = AF_INET;
    myAddr.sin_port = htons(PORT);

    listener = evconnlistener_new_bind(base,(evconnlistener_cb)listen_cb,(void*)base,
            LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE,-1,
            (struct sockaddr *)&myAddr,sizeof(myAddr));
    assert(listener!=NULL);

    signal_event = evsignal_new(base,SIGINT,signal_cb,(void*)base);
    assert(signal_event != NULL||event_add(signal_event,NULL)<0);

    event_base_dispatch(base);

    evconnlistener_free(listener);
    event_free(signal_event);
    event_base_free(base);


    return 0;
}
Exemple #16
0
int network_manager_stop(struct network_manager* manager) {
	if (NULL != manager) {
		printf("a\n");
		// event_base_loopexit(manager->base, NULL);
		// event_active(manager->listener, EV_TIMEOUT, 0);				
		if (NULL != manager->listener) {
			evconnlistener_free(manager->listener);
			manager->listener = NULL;
		}
		printf("b\n");
		if (NULL != manager->event) {
			bufferevent_free(manager->event);
			manager->event = NULL;
		}		
		printf("c\n");		
		if (NULL != manager->base) {
			event_base_free(manager->base);
			manager->base = NULL;
		}				
		printf("d\n");		
		if (0 != manager->thread) {
			pthread_cancel(manager->thread); // I don`t want to cancel but I can`t find quit method :(
			pthread_join(manager->thread, NULL);
			manager->thread = 0;
		}
		printf("e\n");
	}
}
Exemple #17
0
int v4c_srv_run(struct event_base *base, unsigned long port)
{
    struct sockaddr_v4v sa = {
        .sa_family = AF_V4V,
        .sa_addr = {
            .domain = V4V_DOMID_ANY,
            .port = port
        }
    };
    int rc;

    assert(!listener);
    assert(!client);
    assert(!input);
    assert(base);

    listener =
        evconnlistener_new_bind(base, v4c_srv_accept, NULL,
                                LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
                                (struct sockaddr *)&sa, sizeof (sa));
    if (!listener) {
        rc = EVUTIL_SOCKET_ERROR();
        WRN("Failed to initialized listener (%s).",
            evutil_socket_error_to_string(rc));
        return -rc;
    }
    evconnlistener_set_error_cb(listener, v4c_srv_accept_error);

    event_base_dispatch(base);

    event_free(input);
    evconnlistener_free(listener);

    return 0;
}
Exemple #18
0
void
peers_free(struct peers* p)
{
	free_all_peers(p->peers, p->peers_count);
	free_all_peers(p->clients, p->clients_count);
	if (p->listener != NULL)
		evconnlistener_free(p->listener);
	free(p);
}
static int clean_server(tcp_listener_relay_server_type* server) {
  if(server) {
	  if(server->l) {
		  evconnlistener_free(server->l);
		  server->l = NULL;
	  }
  }
  return 0;
}
Exemple #20
0
void NetworkManager::dispatch()
{
    if(m_eventBase && m_listener)
    {
        printf("network dispatch...\n");
        event_base_dispatch(m_eventBase);
        evconnlistener_free(m_listener);
        event_base_free(m_eventBase);
    }
}
Exemple #21
0
// Initializes TCP and LibEvent, then listens for connections on port
// and calls accept_cb when they occur.  Terminates cleanly on SIGINT.
// Returns 0 on clean shutdown; otherwise nonzero
int InitAndListenLoop(int port, evconnlistener_cb accept_cb, struct telex_conf *conf)
{
#ifdef WIN32
	WSADATA WSAData;
	WSAStartup(0x101, &WSAData);
#endif

	struct event_base *base;
	base = event_base_new();
	if (!base) {
		LogFatal("listener", "Could not initialize libevent");
		return 1;
	}
	//void event_enable_debug_mode(void);

	conf->dns_base = evdns_base_new(base, 1);
	if (!conf->dns_base) {
		LogFatal("listener", "Could not initialize dns");
		return 1;
	}

	struct evconnlistener *listener;
	listener = listener_init_local(base, port, accept_cb, conf, NULL);
	if (!listener) {
		LogFatal("listener", "Could not create listener");
		return 1;
	}

	struct event *signal_event;
	signal_event = evsignal_new(base, SIGINT, sigint_cb, (void *)base);
	if (!signal_event || event_add(signal_event, NULL)<0) {
		LogFatal("listener", "Could not create/add signal event");
		return 1;
	}

    struct event *sigpipe_event;
	sigpipe_event = evsignal_new(base, SIGPIPE, sigpipe_cb, (void *)base);
	if (!sigpipe_event || event_add(sigpipe_event, NULL)<0) {
		LogFatal("listener", "Could not create/add signal event");
		return 1;
	}

	// handle events ...
	LogTrace("listener", "Starting dispatch");
	event_base_dispatch(base);

	evconnlistener_free(listener);
	event_free(signal_event);
	event_free(sigpipe_event);
	event_base_free(base);

	LogInfo("listener", "Shutdown complete");
	return 0;
}
Exemple #22
0
void server_event_run(server_t *server)
{
    log_info(__FILE__, __LINE__, "Starting server to accept client requests.");

    event_base_dispatch(server->evbase);

    evconnlistener_free(server->evlistener);
    event_base_free(server->evbase);

    log_info(__FILE__, __LINE__, "Stop server to accept client requests.");
    return;
} 
Exemple #23
0
int main(int argc, char **argv)
{
  struct event_base *base;
  struct evconnlistener *listener;
  struct sockaddr_in sin;
  struct event *evstop;

  int port = 9876;

  if (argc > 1) {
    port = atoi(argv[1]);
  }
  if (port<=0 || port>65535) {
    puts("Invalid port");
    return 1;
  }

  signal(SIGPIPE, SIG_IGN);

  base = event_base_new();
  if (!base) {
    puts("Couldn't open event base");
    return 1;
  }

  evstop = evsignal_new(base, SIGHUP, signal_cb, base);
  evsignal_add(evstop, NULL);

  /* Clear the sockaddr before using it, in case there are extra
   *          * platform-specific fields that can mess us up. */
  memset(&sin, 0, sizeof(sin));
  /* This is an INET address */
  sin.sin_family = AF_INET;
  /* Listen on 0.0.0.0 */
  sin.sin_addr.s_addr = htonl(0);
  /* Listen on the given port. */
  sin.sin_port = htons(port);

  listener = evconnlistener_new_bind(base, accept_conn_cb, NULL,
      LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1,
      (struct sockaddr*)&sin, sizeof(sin));
  if (!listener) {
    perror("Couldn't create listener");
    return 1;
  }

  event_base_dispatch(base);

  evconnlistener_free(listener);
  event_free(evstop);
  event_base_free(base);
  return 0;
}
Exemple #24
0
void tcp_receiver_free(struct tcp_receiver* r)
{
	int i;
	/*释放监视的事件*/
	for (i = 0; i < carray_count(r->bevs); ++i)
		bufferevent_free(carray_at(r->bevs, i));
	/*释放监听*/
	evconnlistener_free(r->listener);

	carray_free(r->bevs);
	free(r);
}
Exemple #25
0
int main1(int argc, char **argv)
{
    struct event_base *base;
    struct evconnlistener *listener;
    struct event *signal_event;

    struct sockaddr_in sin;

#ifdef WIN32
    WSADATA wsa_data;
    WSAStartup(0x0201, &wsa_data);
#endif

    base = event_base_new();
    if (!base) 
    {
        fprintf(stderr, "Could not initialize libevent!\n");
        return 1;
    }

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);

    listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
        LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
        (struct sockaddr*)&sin,
        sizeof(sin));

    if (!listener) 
    {
        fprintf(stderr, "Could not create a listener!\n");
        return 1;
    }

    signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);

    if (!signal_event || event_add(signal_event, NULL)<0) 
    {
        fprintf(stderr, "Could not create/add a signal event!\n");
        return 1;
    }

    event_base_dispatch(base);

    evconnlistener_free(listener);
    event_free(signal_event);
    event_base_free(base);

    printf("done\n");
    return 0;
}
Exemple #26
0
int smpp_listener_start(SMPPServer *smpp_server) {
    struct sockaddr_in sin;
    /* Create new event base */
    smpp_server->event_base = event_base_new();
    if (!smpp_server->event_base) {
        error(0, "Couldn't open event base");
        return 1;
    }

    /* Clear the sockaddr before using it, in case there are extra
     * platform-specific fields that can mess us up. */
    memset(&sin, 0, sizeof (sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(smpp_server->smpp_port);
    sin.sin_addr.s_addr = htonl(INADDR_ANY);

    /* Create a new listener */
    info(0, "Starting SMPP server on port %ld", smpp_server->smpp_port);
    smpp_server->event_listener = evconnlistener_new_bind(smpp_server->event_base, smpp_listener_connection_callback, smpp_server,
            LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
            (struct sockaddr *) &sin, sizeof (sin));
    if (!smpp_server->event_listener) {
        panic(0,"Couldn't create listener");
        return 1;
    }


    smpp_server->ip_blocklist = dict_create(512, (void(*)(void *))smpp_blocked_ip_destroy);

    smpp_bearerbox_init(smpp_server);
    smpp_esme_init(smpp_server);
    smpp_queues_init(smpp_server);
    
    
    
    evconnlistener_set_error_cb(smpp_server->event_listener, smpp_listener_accept_error_callback);
    
    event_base_dispatch(smpp_server->event_base);
    
    
    smpp_queues_shutdown(smpp_server);
    smpp_esme_shutdown(smpp_server);
    smpp_bearerbox_shutdown(smpp_server);

    dict_destroy(smpp_server->ip_blocklist);
    
    evconnlistener_free(smpp_server->event_listener);
    
    event_base_free(smpp_server->event_base);
    
    return 0;
}
Exemple #27
0
void CListener::ShutDown()
{
	if (m_eState == eListened)
	{
		if (m_pListener)
		{
			evconnlistener_free(m_pListener);
			m_pListener = NULL;
		}

		m_eState = eUnListen;
	}
}
Exemple #28
0
void evwsconnlistener_free(struct evwsconnlistener *levws) {
  if (levws == NULL) {
    return;
  }
  struct evwspendingconn* curr = levws->head;
  while (curr) {
    struct evwspendingconn* temp = curr;
    curr = curr->next;
    free_pending(temp);
  }
  evconnlistener_free(levws->lev);
  free(levws);
}
Exemple #29
0
	int
main(int argc, char **argv)
{
	int socklen;

	struct evconnlistener *listener;

	if (argc < 3)
		syntax();
	memset(&listen_on_addr, 0, sizeof(listen_on_addr));
	socklen = sizeof(listen_on_addr);
	if (evutil_parse_sockaddr_port(argv[1], (struct sockaddr*)&listen_on_addr, &socklen) < 0) 
	{
		int p = atoi(argv[1]);
		struct sockaddr_in *sin = (struct sockaddr_in*)&listen_on_addr;
		if (p < 1 || p > 65535)
			syntax();
		sin->sin_port = htons(p);
		sin->sin_addr.s_addr = htonl(0x7f000001);
		sin->sin_family = AF_INET;
		socklen = sizeof(struct sockaddr_in);
	}

	memset(&connect_to_addr, 0, sizeof(connect_to_addr));
	connect_to_addrlen = sizeof(connect_to_addr);
	if (evutil_parse_sockaddr_port(argv[2], (struct sockaddr*)&connect_to_addr, &connect_to_addrlen) < 0)
		syntax();

	base = event_base_new();
	if (!base) {
		perror("event_base_new()");
		return 1;
	}


	listener = evconnlistener_new_bind(base, accept_cb, NULL,
			LEV_OPT_CLOSE_ON_FREE|LEV_OPT_CLOSE_ON_EXEC|LEV_OPT_REUSEABLE,
			-1, (struct sockaddr*)&listen_on_addr, socklen);

	if (! listener) {
		fprintf(stderr, "Couldn't open listener.\n");
		event_base_free(base);
		return 1;
	}
	event_base_dispatch(base);

	evconnlistener_free(listener);
	event_base_free(base);

	return 0;
}
Exemple #30
0
CNetListener::~CNetListener(void)
{
    struct evconnlistener *pListener;
    for (std::vector<void *>::iterator it = m_vcListener.begin(); m_vcListener.end() != it; ++it)
    {
        pListener = (struct evconnlistener *)(*it);
        evconnlistener_free(pListener);
    }

    for (std::vector<class CNetInfo *>::iterator it = m_vcNetInfo.begin(); m_vcNetInfo.end() != it; ++it)
    {
        H_SafeDelete((*it));
    }
}