コード例 #1
0
ファイル: event.cpp プロジェクト: ajacksified/breeze
    Base::Base( )
    : m_base( NULL ), m_started( false )
    {
        m_base = event_base_new( );

        evthread_make_base_notifiable( m_base );
    }
コード例 #2
0
void TS3::telnetClientThread(TS3 * parent, std::string server)
{
	int port = 10011;
	parent->_base = event_base_new();
	parent->_dns_base = evdns_base_new(parent->_base, 1);

	evthread_use_pthreads();
	evthread_make_base_notifiable(parent->_base);

	parent->_msg_event = event_new(parent->_base, -1, EV_READ, sendTS3message, parent);
	event_add(parent->_msg_event, nullptr);

	parent->_bev = bufferevent_socket_new(parent->_base, -1, BEV_OPT_CLOSE_ON_FREE);
	bufferevent_setcb(parent->_bev, telnetMessage, nullptr, telnetEvent, parent);
	bufferevent_enable(parent->_bev, EV_READ|EV_WRITE);
	timeval t;
	t.tv_sec = 60;
	t.tv_usec = 0;
	bufferevent_set_timeouts(parent->_bev, &t, nullptr);
	bufferevent_socket_connect_hostname(
		parent->_bev, parent->_dns_base, AF_UNSPEC, server.c_str(), port);

	// FIXME needs a reliable restart mechanism
	auto result = event_base_dispatch(parent->_base);

	if (result == -1)
		LOG(ERROR) << "Failed to start event loop";
}
コード例 #3
0
ファイル: regress_thread.c プロジェクト: jimmy-kuo/websearch
static void
thread_basic(void *arg)
{
	THREAD_T threads[NUM_THREADS];
	struct event ev;
	struct timeval tv;
	int i;
	struct basic_test_data *data = arg;
	struct event_base *base = data->base;

	struct event *notification_event = NULL;
	struct event *sigchld_event = NULL;

	EVTHREAD_ALLOC_LOCK(count_lock, 0);
	tt_assert(count_lock);

	tt_assert(base);
	if (evthread_make_base_notifiable(base)<0) {
		tt_abort_msg("Couldn't make base notifiable!");
	}

#ifndef WIN32
	if (data->setup_data && !strcmp(data->setup_data, "forking")) {
		pid_t pid;
		int status;
		sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base);
		/* This piggybacks on the th_notify_fd weirdly, and looks
		 * inside libevent internals.  Not a good idea in non-testing
		 * code! */
		notification_event = event_new(base,
		    base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb,
		    NULL);
		event_add(sigchld_event, NULL);
		event_add(notification_event, NULL);

		if ((pid = fork()) == 0) {
			event_del(notification_event);
			if (event_reinit(base) < 0) {
				TT_FAIL(("reinit"));
				exit(1);
			}
			event_assign(notification_event, base,
			    base->th_notify_fd[0], EV_READ|EV_PERSIST,
			    notify_fd_cb, NULL);
			event_add(notification_event, NULL);
	 		goto child;
		}

		event_base_dispatch(base);

		if (waitpid(pid, &status, 0) == -1)
			tt_abort_perror("waitpid");
		TT_BLATHER(("Waitpid okay\n"));

		tt_assert(got_sigchld);
		tt_int_op(notification_fd_used, ==, 0);

		goto end;
	}
コード例 #4
0
ファイル: test_libevent.c プロジェクト: javajolt/eventlibs
int main (int argc, char **argv) {
    signal(SIGPIPE, SIG_IGN);

    event_enable_debug_mode();
    int result = evthread_use_pthreads();
    if (result < 0) {
        printf("Could not use pthreads with libevent\n");
        exit(1);
    }

    struct event_base *base = event_base_new();
    if (base == NULL) {
        printf("Could not create libevent base\n");
        exit(1);
    }
    result = evthread_make_base_notifiable(base);
    if (result) {
        printf("Could not initialize libevent base\n");
        exit(1);
    }

    riak_context ctx_data[10];
    for(int i = 0; i < 10; i++) {

        riak_context *ctx = &ctx_data[i];
        riak_context_init(ctx);
        if (ctx->err) {
            printf("Error: %s\n", ctx->errstr);
            return 1;
        }
        result = riak_context_connect(ctx, "httpbin.org", "80");
    //    result = riak_context_connect(ctx, "localhost", "6074");
        if (result) {
            printf("Could not connect to host\n");
            exit(1);
        }
        result = riak_libevent_init(ctx, base);
        if (result) {
            printf("Could not initialize libevent\n");
            exit(1);
        }
        riak_send(ctx, "GET / HTTP/1.1\r\nHost: httpbin.org\r\n\r\n", result_fn);
    }
    event_base_dispatch(base);
    // cleanup
    event_base_free(base);
    return 0;
}
コード例 #5
0
tcpConnectStatus TcpTransport::connect(const string &strServerURL,
                                       int timeOutMillisecs /* = 3000 */) {
  string hostName;
  short portNumber;
  if (!UtilAll::SplitURL(strServerURL, hostName, portNumber)) {
    return e_connectFail;
  }

  boost::lock_guard<boost::mutex> lock(m_socketLock);

  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = inet_addr(hostName.c_str());
  sin.sin_port = htons(portNumber);

  m_eventBase = event_base_new();
  m_bufferEvent = bufferevent_socket_new(
      m_eventBase, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
  bufferevent_setcb(m_bufferEvent, readNextMessageIntCallback, NULL, eventcb,
                    this);
  bufferevent_enable(m_bufferEvent, EV_READ | EV_WRITE);
  bufferevent_setwatermark(m_bufferEvent, EV_READ, 4, 0);

  setTcpConnectStatus(e_connectWaitResponse);
  if (bufferevent_socket_connect(m_bufferEvent, (struct sockaddr *)&sin,
                                 sizeof(sin)) < 0) {
    LOG_INFO("connect to fd:%d failed", bufferevent_getfd(m_bufferEvent));
    setTcpConnectStatus(e_connectFail);
    freeBufferEvent();
    return e_connectFail;
  } else {
    int fd = bufferevent_getfd(m_bufferEvent);
    LOG_INFO("try to connect to fd:%d, addr:%s", fd, (hostName.c_str()));
    /*struct timeval timeout;
    timeout.tv_sec = timeOutMillisecs/1000;
    timeout.tv_usec = 0;
    struct event* evtimeout = evtimer_new(m_eventBase, timeoutcb, this);
    evtimer_add(evtimeout, &timeout);*/
    evthread_make_base_notifiable(m_eventBase);
    m_ReadDatathread =
        new boost::thread(boost::bind(&TcpTransport::runThread, this));
    return e_connectWaitResponse;
  }
}
コード例 #6
0
void httpServerThread(GithubWebhooks * parent, std::uint16_t port)
{
	parent->_eventBase = event_base_new();
	parent->_evhttp = evhttp_new(parent->_eventBase);
	parent->_breakLoop = event_new(parent->_eventBase, -1, EV_READ, terminateServer, parent);
	event_add(parent->_breakLoop, nullptr);
	if (evhttp_bind_socket(parent->_evhttp , "0.0.0.0", port) == -1)
	{
		LOG(ERROR) << "Can't bind socket on port " << port;
		return;
	}
	evhttp_set_gencb(parent->_evhttp , httpHandler, parent);

	evthread_use_pthreads();
	evthread_make_base_notifiable(parent->_eventBase);

	if (event_base_dispatch(parent->_eventBase) == -1)
		LOG(ERROR) << "Failed to start event loop";
}
コード例 #7
0
ファイル: TcpClient.cpp プロジェクト: jjfsq1985/StudyLab
void* TcpClient::ConnectThread(void *pParam)
{
    TcpClient *pClient = (TcpClient *)pParam;

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    inet_pton(AF_INET, pClient->m_cIPAddr, (void*)&sin.sin_addr);
    sin.sin_port = htons(pClient->m_nPort);

    int nCycle = 15;
    while (pClient->m_bConnecting)
    {
        if (nCycle < 15)
        {
            nCycle++;
            Sleep(1000);
            continue;
        }
        Tprintf(L"Connecting\n");
        nCycle = 0;
        struct event_base *base = event_base_new();
        assert(base != NULL);
#if defined (WIN32)
        evthread_use_windows_threads();
#else
        evthread_use_pthreads();
#endif
        evthread_make_base_notifiable(base);
        struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
        bufferevent_setcb(bev, read_cb, NULL, event_cb, pClient);
        bufferevent_enable(bev, EV_READ | EV_WRITE | EV_PERSIST);
        //连接
        if (bufferevent_socket_connect(bev, (SOCKADDR*)&sin, sizeof(SOCKADDR)) < 0)
        {
            bufferevent_free(bev);
            return NULL;
        }
        event_base_dispatch(base);

        event_base_free(base);
    }
    return NULL;
}
コード例 #8
0
		void HttpServer::startServer(int port, int nCPU, char *rootDir) {
			struct sockaddr_in listenAddr;
			struct event_base *base;
			struct evconnlistener *listener;

			if (rootDir == nullptr) {
				rootDir_ = Configuration::ROOT_DIR;
			} else {
				rootDir_ = rootDir;
			}
			base = event_base_new();
			if (!base) {
				throw std::runtime_error("Can't create base");
			}
			if (evthread_make_base_notifiable(base) < 0) {
				event_base_free(base);
				throw std::runtime_error("Couldn't make base notifiable!");
			}
			memset(&listenAddr, 0, sizeof(listenAddr));
			listenAddr.sin_family = AF_INET;
			listenAddr.sin_addr.s_addr = htonl(0);
			if (port == 0) {
				port = Configuration::PORT;
			}
			listenAddr.sin_port = htons(port);
			if (nCPU == 0) {
				nCPU = Configuration::nCPU;
			}
			WorkQueue::workqueue_init((workqueue_t *) &workqueue, nCPU);
			listener = evconnlistener_new_bind(base, acceptConnCb, (void *) &workqueue,
											   LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE | LEV_OPT_THREADSAFE, SOMAXCONN,
											   (struct sockaddr *) &listenAddr, sizeof(listenAddr));
			if (listener == NULL) {
				event_base_free(base);
				WorkQueue::workqueue_shutdown(&workqueue);
				throw std::runtime_error("Port is busy");
			}
			evconnlistener_set_error_cb(listener, acceptErrorCb);
			event_base_dispatch(base);
			event_base_free(base);
			WorkQueue::workqueue_shutdown(&workqueue);
		}
コード例 #9
0
void
regress_threads(void *arg)
{
	struct event_base *base;
        (void) arg;

	pthread_mutex_init(&count_lock, NULL);

        if (evthread_use_pthreads()<0)
		tt_abort_msg("Couldn't initialize pthreads!");

        base = event_base_new();
        if (evthread_make_base_notifiable(base)<0) {
                tt_abort_msg("Couldn't make base notifiable!");
        }

	pthread_basic(base);

	pthread_mutex_destroy(&count_lock);

	event_base_free(base);
end:
        ;
}