Exemplo n.º 1
0
static void
server_cb (EV_P_ ev_io *w, int revents)
{
    int new_fd;
    client_t *my_client;

    /* NOTES: possible event bits are EV_READ and EV_ERROR
     * however, EV_ERROR shouldn't really happen, so if (EV_ERROR) fatal
     * otherwise assume EV_READ */
    if (revents & EV_ERROR)
    {
        fprintf(stderr, "FATAL: unexpected EV_ERROR on server event watcher\n");
        exit(1);
    }

    /* assume EV_READ */
    new_fd = unix_accept(server_fd);
    if (new_fd == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
        return;     /* nothing interesting happened */
    else if (new_fd == -1)
    {
        fprintf(stderr, "accept failed unexpectedly: %s\n", strerror(errno));
        return;
    }
    
    fprintf(stderr, "accepted connection: fd: %d\n", new_fd);

    /* register client connection in state */
    if (nr_clients <= IRCD_CLIENTS_MAX
        && (my_client = malloc(sizeof(*my_client))))
    {
        list_push((list_t **)&client_list, (list_t *)my_client);
        nr_clients++;

        /* save client info and init watcher */
        my_client->fd = new_fd;
        my_client->timestamp = time(NULL);
        my_client->type = CLIENT_UNREGISTERED;
        my_client->more = NULL;
        my_client->in_buf.index = 0;
        my_client->out_buf = NULL;
        ev_io_init(&my_client->w, &client_cb, new_fd, EV_READ);
        ev_io_start(EV_A_ &my_client->w);
        my_client->w.data = my_client; /* lol recursion */
        return;
    }
    else if (my_client)
        /* we hit max clients?? */
        fprintf(stderr, "too many clients, dropping connection %d\n", new_fd);
    else
        fprintf(stderr, "malloc: %s\n", strerror(errno));
    close(new_fd);
    free(my_client);
}
Exemplo n.º 2
0
void server_main(void)
{
	int     newsock, sock;
	char	c;

	GO_AWAY;
	sock = unix_server(CONNAME);
	for (;;) {
		newsock = unix_accept(sock);
		c = 0;
		read(newsock, &c, 1);
		if (c && c == '0') {
			unix_done(sock, CONNAME);
			exit(0);
		}
		close(newsock);
	}
}