Skip to content

Commit

Permalink
Accept client connections in a loop rather than accepting one per-wak…
Browse files Browse the repository at this point in the history
…eup.

Ideally this should lead to better accept performance in that it should empty the accept backlog without having to go back for more events.
  • Loading branch information
stormbrew committed Feb 20, 2011
1 parent ebd0e2c commit 8656f23
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/cloudbridge.cpp
Expand Up @@ -2,6 +2,7 @@
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <fstream>
Expand Down Expand Up @@ -163,6 +164,12 @@ int main(int argc, char **argv)
int on = 1;
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

// go into nonblocking mode
int flags;
if (-1 == (flags = fcntl(listen_socket, F_GETFL, 0)))
flags = 0;
fcntl(listen_socket, F_SETFL, flags | O_NONBLOCK);

if (bind(listen_socket, addrinfo->ai_addr, addrinfo->ai_addrlen))
{
printf("Error binding socket. Errno: %d\n", errno);
Expand Down
11 changes: 7 additions & 4 deletions src/listen_handler.cpp
Expand Up @@ -11,8 +11,11 @@ void listen_handler::operator()(struct ev_loop *loop, evx_io *watcher, int reven
{
struct sockaddr_in remote_addr;
socklen_t addr_len = sizeof(remote_addr);
int socket = accept(listen_socket, reinterpret_cast<struct sockaddr*>(&remote_addr), &addr_len);

connections->get_server_stats().incr("Connections");
buffered_connection::create_connection(loop, socket, buffered_connection::client_handler_ptr(new connection_finder(connections, host_key_secret)));
int socket;
while (-1 != (socket = accept(listen_socket, reinterpret_cast<struct sockaddr*>(&remote_addr), &addr_len)))
{
connections->get_server_stats().incr("Connections");
buffered_connection::create_connection(loop, socket, buffered_connection::client_handler_ptr(new connection_finder(connections, host_key_secret)));
addr_len = sizeof(remote_addr);
}
}

0 comments on commit 8656f23

Please sign in to comment.