Use a wrapper for logging
This commit is contained in:
parent
8725d1c1f1
commit
7973b5a5e8
7 changed files with 91 additions and 99 deletions
32
server.c
32
server.c
|
@ -16,52 +16,47 @@ int server_setup(server_t *server, unsigned int port)
|
|||
server->socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server->socket == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
||||
errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
fprintf(stderr,"[%s] socket created\n", __func__);
|
||||
LOG("socket created");
|
||||
|
||||
/* try to avoid "Address already in use" error */
|
||||
opt = 1; /* true value for setsockopt option */
|
||||
if (setsockopt(server->socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
||||
errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
/* turn off Nagle algorithm */
|
||||
opt = 1; /* true value for setsockopt option */
|
||||
if (setsockopt(server->socket, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int)) == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
||||
errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
/* bind server address to a socket */
|
||||
if (bind(server->socket, (struct sockaddr *) &server->address, sizeof(server->address)) == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
||||
errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
fprintf(stderr,"[%s] bind successful\n", __func__);
|
||||
LOG("bind successful");
|
||||
|
||||
/* save server port number */
|
||||
server->port = port;
|
||||
fprintf(stderr,"[%s] assigned port %u\n", __func__, server->port); // ntohs(server->address.sin_port)
|
||||
LOG("assigned port %u", server->port); // ntohs(server->address.sin_port)
|
||||
|
||||
/* listen for a client connection, allow (max-1) connections in queue */
|
||||
if (listen(server->socket, (SERVER_MAX_CONNECTIONS - 1)) == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__,
|
||||
errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
time2string(time(NULL), timestamp);
|
||||
fprintf(stderr,"[%s] server is up @ %s\n", __func__, timestamp);
|
||||
LOG("server is up @ %s", timestamp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -76,7 +71,7 @@ void server_close(server_t *server)
|
|||
}
|
||||
|
||||
time2string(time(NULL), timestamp);
|
||||
fprintf(stderr,"[%s] socket closed, server is down @ %s\n", __func__, timestamp);
|
||||
LOG("socket closed, server is down @ %s", timestamp);
|
||||
}
|
||||
|
||||
int server_accept(server_t *server, client_t *accepted_client)
|
||||
|
@ -89,13 +84,13 @@ int server_accept(server_t *server, client_t *accepted_client)
|
|||
accepted_client->socket = accept(server->socket, (struct sockaddr *) &accepted_client->address, (socklen_t *) &namelen);
|
||||
if (accepted_client->socket == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
/* make the client socket non-blocking */
|
||||
if (fcntl(accepted_client->socket, F_SETFL, O_NONBLOCK) == -1)
|
||||
{
|
||||
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
|
||||
LOG("[@%d] error %d: %s", __LINE__, errno, strerror(errno));
|
||||
return -errno;
|
||||
}
|
||||
|
||||
|
@ -109,7 +104,6 @@ int server_accept(server_t *server, client_t *accepted_client)
|
|||
|
||||
/* print client information */
|
||||
time2string(accepted_client->last_active, timestamp);
|
||||
fprintf(stderr, "[%s] accepted client %s @ %s\n",
|
||||
__func__, accepted_client->ip_string, timestamp);
|
||||
LOG("accepted client %s @ %s", accepted_client->ip_string, timestamp);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue