Example #1
0
void YYLibEvent::do_accept(evutil_socket_t listener, short event, void *arg)
{
	listener_state * liststate = (listener_state*)arg;
	if (!liststate)
		return;
	YYLibEvent * libevent = liststate->libevent;
	if (!libevent) return;

    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr*)&ss, &slen);
	if (fd < 0)
	{
		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
		{	// there is no connect

		}
		else if (errno == EMFILE)
		{
			closesocket(fd);
			LOG::Error("fd is not enougth!!");
		}
	}
	else
	{
        fd_state *state;
        evutil_make_socket_nonblocking(fd);
        state = alloc_fd_state(libevent, fd, liststate);
		assert(state);
        assert(state->write_event);
        event_add(state->read_event, NULL);
    }
}
Example #2
0
void do_accept(evutil_socket_t listener, short event, void *arg)
{
	struct event_base* base = (event_base*)arg;
	struct sockaddr_storage ss;
	socklen_t slen = sizeof(ss);
	int fd = accept(listener, (struct sockaddr*)&ss, &slen);
	if (fd < 0)
	{
		perror("accept");	
	}
	else if(fd > FD_SETSIZE)
	{
		close(fd);
	}
	else
	{
		struct fd_state* state;
		evutil_make_socket_nonblocking(fd);
		state = alloc_fd_state(base, fd);
		assert(state);
		//assert(state->write_event);
		assert(state->read_event);
		//event_add(state->write_event, NULL);
		//event_base_set(base, state->write_event);
		struct timeval five_seconds = {5,0};
		event_add(state->read_event, &five_seconds);
		event_add(state->write_event, NULL);
    	//event_base_set(base, state->read_event);
	} 
}
Example #3
0
void
WorkerServer::accept_callback(evutil_socket_t listen_fd, short event, void* arg)
{
    std::cout << "accept client connect" << std::endl;

    struct event *read_event;
    struct event *error_event;
    struct sockaddr_storage client;
    socklen_t length = sizeof(client);
    int fd = accept(listen_fd, (struct sockaddr*)&client, &length);
    if(fd < 0)
    {
        if(errno == EAGAIN || errno == EWOULDBLOCK) {
            std::cout << "errno" << std::endl;
            close(fd);
            return;
        }
        perror("accept error");
        return;
    } else {
        struct fd_state *state;
        evutil_make_socket_nonblocking(fd);
        state = alloc_fd_state(base, fd);
        event_add(state->read_event, 0);
    }
}
Example #4
0
void lh_epoll(int timeout) {
	if (rs_state == rs_stopping) {
        rs_state = rs_stopped;
        close(_listener);
        _listener = 0;
        //fixme: free states
		close(_epoll);
		_epoll = 0;
        lh_clear_callback();
        return;
    } else if (rs_state == rs_stopped) {
        return;
    }
	
	int n = epoll_wait(_epoll, _events, MAXEVENTS, timeout);
	for (int i = 0; i < n; i++){
		struct epoll_event *event = _events + i;
		if (_listener == event->data.fd) { //accept
			struct sockaddr_storage ss;
			socklen_t slen = sizeof(ss);
			int fd = accept(_listener, (struct sockaddr*)&ss, &slen);
			//static int na = 0;
			//printf("accept:%d\n", ++na);
			if (fd < 0) {
				perror("accept");
			} else {
				int err = fcntl(fd, F_SETFL, O_NONBLOCK);
				if (err){
					perror ("make_non_blocking");
					return;
				}
				struct fd_state *state = alloc_fd_state(fd);
				struct epoll_event evt;
				evt.data.ptr = state;
				evt.events = EPOLLIN;
				err = epoll_ctl (_epoll, EPOLL_CTL_ADD, fd, &evt);
				if (err)
					return;
			}
		} else {
			struct fd_state *state = (struct fd_state*)event->data.ptr;
			enum io_tatus status = s_ok;
			
			if ((event->events & EPOLLERR) ||(event->events & EPOLLHUP)) { //error
				free_fd_state(state);
			} else if (event->events & EPOLLIN) { //read
				status = do_read(state);
			} else if (event->events & EPOLLOUT) { //write
				status = do_write(state);
			}
			if (status == s_error || status == s_disconnect) {
				free_fd_state(state);
			}
		}
	}
}
void do_accept(evutil_socket_t listener, short event, void *arg)
{
    struct event_base *base = arg;
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr*)&ss, &slen);
    if (fd < 0) { // XXXX eagain??
        perror("accept");
    } else if (fd > FD_SETSIZE) {
        close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
    } else {
        struct fd_state *state;
        evutil_make_socket_nonblocking(fd);
        state = alloc_fd_state(base, fd);
        assert(state); /*XXX err*/
        assert(state->write_event);
        event_add(state->read_event, NULL);
    }
}
Example #6
0
void run(void)
{
    int listener;
    struct fd_state *state[FD_SETSIZE];
    struct sockaddr_in sin;
    int i, maxfd;
    fd_set readset, writeset, exset;

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(8081);

    for (i = 0; i < FD_SETSIZE; ++i)
        state[i] = NULL;

    listener = socket(AF_INET, SOCK_STREAM, 0);
    make_nonblocking(listener);

    if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        perror("bind");
        return;
    }

    if (listen(listener, 16)<0) {
        perror("listen");
        return;
    }

    FD_ZERO(&readset);
    FD_ZERO(&writeset);
    FD_ZERO(&exset);

    while (1) {
        maxfd = listener;

        FD_ZERO(&readset);
        FD_ZERO(&writeset);
        FD_ZERO(&exset);

        FD_SET(listener, &readset);

        for (i=0; i < FD_SETSIZE; ++i) {
            if (state[i]) {
                if (i > maxfd)
                    maxfd = i;
                FD_SET(i, &readset);
                if (state[i]->writing) {
                    FD_SET(i, &writeset);
                }
            }
        }

        if (select(maxfd+1, &readset, &writeset, &exset, NULL) < 0) {
            perror("select");
            return;
        }

        if (FD_ISSET(listener, &readset)) {
            struct sockaddr_storage ss;
            socklen_t slen = sizeof(ss);
            int fd = accept(listener, (struct sockaddr*)&ss, &slen);
            if (fd < 0) {
                perror("accept");
            } else if (fd > FD_SETSIZE) {
                close(fd);
            } else {
                make_nonblocking(fd);
                state[fd] = alloc_fd_state();

                char send_buf[1024] = "Connected";
                send(fd, send_buf, 1024, 0);

                assert(state[fd]);/*XXX*/
            }
        }

        for (i=0; i < maxfd+1; ++i) {
            int r = 0;
            if (i == listener)
                continue;

            if (FD_ISSET(i, &readset)) {
                r = do_read(i, state[i]);
            }
            if (r == 0 && FD_ISSET(i, &writeset)) {
                r = do_write(i, state[i]);
            }
            if (r) {
                free_fd_state(state[i]);
                state[i] = NULL;
                close(i);
            }
        }
    }
}
Example #7
0
void
run(void)
{
    evutil_socket_t sender;
    struct sockaddr_in sin;
    struct event_base *base;
    struct event *write_event = NULL;
    struct event *read_event = NULL;
    struct fd_state *state;
    int result;

    base = event_base_new();
    if (!base)
        return; /*XXXerr*/

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin.sin_port = htons(40713);

    sender = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    evutil_make_socket_nonblocking(sender);

#ifndef WIN32
    {
        int one = 1;
        setsockopt(sender, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    }
#endif

    /*
    if (bind(sender, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        perror("bind");
        return;
    }

    sender_event = event_new(base, sender, EV_READ|EV_PERSIST, do_read, (void*)base);
    /-*XXX check it *-/
    event_add(sender_event, NULL);
    //*/

    result = connect(sender, (struct sockaddr*)&sin, sizeof(sin));
    if (result < 0 || result == SOCKET_ERROR) {
        if (WSAGetLastError() == WSAEWOULDBLOCK) {
            printf("WSAGetLastError() = WSAEWOULDBLOCK\r\n");
            Sleep(1000);
            printf("Press any key to continue...\r\n");
            //system("pause");
        }
        else {
            printf("connect error!\r\n");
            printf("WSAGetLastError() = 0x%08X\r\n", WSAGetLastError());
            system("pause");
            perror("connect");
            return;
        }
    }

    //printf("Press any key to continue...\r\n");
    //system("pause");

    //write_event = event_new(base, sender, EV_WRITE|EV_PERSIST, do_write, (void*)base);
    //read_event  = event_new(base, sender, EV_READ |EV_PERSIST, do_read,  (void*)base);

    state = alloc_fd_state(base, sender);

    /*
    if (write_event) {
        printf("event_add(write_event, NULL);\r\n");
        event_add(write_event, NULL);
    }
    //*/

    if (state && state->write_event) {
        printf("event_add(state->write_event, NULL);\r\n");
        event_add(state->write_event, NULL);
    }

    if (state && state->read_event) {
        printf("event_add(state->read_event, NULL);\r\n");
        event_add(state->read_event, NULL);
    }

    printf("event_base_dispatch(base);\r\n");
    event_base_dispatch(base);

    /*
    if (write_event)
        event_free(write_event);
    if (read_event)
        event_free(read_event);
    //*/

    if (state)
        free_fd_state(state);
}