Browse Source

Optimize some socket initialization handling

personal/stbuehler/wip
Stefan Bühler 12 years ago
parent
commit
a2ed50dedd
  1. 16
      src/common/utils.c
  2. 12
      src/main/server.c

16
src/common/utils.c

@ -641,7 +641,7 @@ liSocketAddress li_sockaddr_from_string(const GString *str, guint tcp_default_po
}
liSocketAddress li_sockaddr_local_from_socket(gint fd) {
static liSockAddr sa;
liSockAddr sa;
socklen_t l = sizeof(sa);
liSocketAddress saddr = { 0, NULL };
@ -651,13 +651,17 @@ liSocketAddress li_sockaddr_local_from_socket(gint fd) {
saddr.addr = (liSockAddr*) g_slice_alloc0(l);
saddr.len = l;
getsockname(fd, (struct sockaddr*) saddr.addr, &l);
if (l <= sizeof(sa)) {
memcpy(saddr.addr, &sa.plain, l);
} else {
getsockname(fd, (struct sockaddr*) saddr.addr, &l);
}
return saddr;
}
liSocketAddress li_sockaddr_remote_from_socket(gint fd) {
static liSockAddr sa;
liSockAddr sa;
socklen_t l = sizeof(sa);
liSocketAddress saddr = { 0, NULL };
@ -667,7 +671,11 @@ liSocketAddress li_sockaddr_remote_from_socket(gint fd) {
saddr.addr = (liSockAddr*) g_slice_alloc0(l);
saddr.len = l;
getpeername(fd, (struct sockaddr*) saddr.addr, &l);
if (l <= sizeof(sa)) {
memcpy(saddr.addr, &sa.plain, l);
} else {
getpeername(fd, (struct sockaddr*) saddr.addr, &l);
}
return saddr;
}

12
src/main/server.c

@ -416,8 +416,8 @@ static void li_server_listen_cb(struct ev_loop *loop, ev_io *w, int revents) {
liServer *srv = sock->srv;
int s;
liSocketAddress remote_addr;
struct sockaddr sa;
socklen_t l = sizeof(sa);
liSockAddr sa;
socklen_t l;
UNUSED(loop);
UNUSED(revents);
@ -432,7 +432,8 @@ static void li_server_listen_cb(struct ev_loop *loop, ev_io *w, int revents) {
return;
}
if (-1 == (s = accept(w->fd, &sa, &l))) break;
l = sizeof(sa);
if (-1 == (s = accept(w->fd, &sa.plain, &l))) break;
wrk = srv->main_worker;
min_load = g_atomic_int_get(&wrk->connection_load);
@ -440,13 +441,12 @@ static void li_server_listen_cb(struct ev_loop *loop, ev_io *w, int revents) {
if (l <= sizeof(sa)) {
remote_addr.addr = g_slice_alloc(l);
remote_addr.len = l;
memcpy(remote_addr.addr, &sa, l);
memcpy(remote_addr.addr, &sa.plain, l);
} else {
remote_addr = li_sockaddr_remote_from_socket(s);
}
l = sizeof(sa); /* reset l */
li_fd_init(s);
li_fd_no_block(s); /* we don't fork, don't care about FD_CLOEXEC */
for (i = 1; i < srv->worker_count; i++) {
liWorker *wt = g_array_index(srv->workers, liWorker*, i);

Loading…
Cancel
Save