summaryrefslogtreecommitdiff
path: root/src/base/tcp_server.c
diff options
context:
space:
mode:
authorTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:53:48 +0200
committerTomas Chvatal (scarabeus) <tomas.chvatal@gmail.com>2008-10-22 22:53:48 +0200
commit130d45bfc935da1702a7b5c246a099ea3b4a0bdf (patch)
tree5cc4b029f24e7a3cfaf1fcbc671772059a9b8e73 /src/base/tcp_server.c
parent44daf03024fbf7cbffa10349044786f6ab784821 (diff)
Ident all code correctly.
Diffstat (limited to 'src/base/tcp_server.c')
-rw-r--r--src/base/tcp_server.c372
1 files changed, 183 insertions, 189 deletions
diff --git a/src/base/tcp_server.c b/src/base/tcp_server.c
index 14e7e22..14d1794 100644
--- a/src/base/tcp_server.c
+++ b/src/base/tcp_server.c
@@ -46,248 +46,242 @@
static sock_tcp_t *sock_server_tcp;
static sock_tcp_t *sock_server_tcp_second;
-client_t* newTcpClient(sock_tcp_t *sock_tcp)
+client_t *
+newTcpClient(sock_tcp_t * sock_tcp)
{
- client_t *new;
+ client_t *new;
- assert( sock_tcp != NULL );
+ assert(sock_tcp != NULL);
- new = newAnyClient();
- new->type = CLIENT_TYPE_TCP;
- new->socket_tcp = sock_tcp;
- new->recvBuffer = newBuffer(SERVER_TCP_BUFFER_LIMIT);
- new->sendBuffer = newBuffer(SERVER_TCP_BUFFER_LIMIT);
+ new = newAnyClient();
+ new->type = CLIENT_TYPE_TCP;
+ new->socket_tcp = sock_tcp;
+ new->recvBuffer = newBuffer(SERVER_TCP_BUFFER_LIMIT);
+ new->sendBuffer = newBuffer(SERVER_TCP_BUFFER_LIMIT);
#ifdef PUBLIC_SERVER
- char str_log[STR_LOG_SIZE];
- char str_ip[STR_IP_SIZE];
+ char str_log[STR_LOG_SIZE];
+ char str_ip[STR_IP_SIZE];
- getSockTcpIp(sock_tcp, str_ip, STR_IP_SIZE);
- sprintf(str_log, _("New client \"%s\" on port \"%d\" connected"), str_ip, getSockTcpPort(sock_tcp));
- addToLog(LOG_INF, str_log);
+ getSockTcpIp(sock_tcp, str_ip, STR_IP_SIZE);
+ sprintf(str_log, _("New client \"%s\" on port \"%d\" connected"), str_ip,
+ getSockTcpPort(sock_tcp));
+ addToLog(LOG_INF, str_log);
#endif
- return new;
+ return new;
}
-void sendTcpClientBuffer(client_t *client)
+void
+sendTcpClientBuffer(client_t * client)
{
- void *data;
- int len;
- int res;
+ void *data;
+ int len;
+ int res;
- len = getBufferSize(client->sendBuffer);
+ len = getBufferSize(client->sendBuffer);
- if( len == 0 )
- {
- return;
- }
+ if (len == 0) {
+ return;
+ }
- data = getBufferData(client->sendBuffer);
+ data = getBufferData(client->sendBuffer);
- res = writeTcpSocket(client->socket_tcp, data, len);
- //printf("writeTcpSocket = %d\n", res);
+ res = writeTcpSocket(client->socket_tcp, data, len);
+ //printf("writeTcpSocket = %d\n", res);
- if( res < 0 )
- {
- client->status = NET_STATUS_ZOMBIE;
- return;
- }
+ if (res < 0) {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
+ }
- cutBuffer(client->sendBuffer, res);
+ cutBuffer(client->sendBuffer, res);
}
-void destroyTcpClient(client_t *client)
+void
+destroyTcpClient(client_t * client)
{
- eventMsgInCheckFront(client);
- sendTcpClientBuffer(client);
+ eventMsgInCheckFront(client);
+ sendTcpClientBuffer(client);
#ifdef PUBLIC_SERVER
- char str_log[STR_LOG_SIZE];
- char str_ip[STR_IP_SIZE];
+ char str_log[STR_LOG_SIZE];
+ char str_ip[STR_IP_SIZE];
- getSockTcpIp(client->socket_tcp, str_ip, STR_IP_SIZE);
- sprintf(str_log, _("Client \"%s\" on port \"%d\" disconnected"), str_ip, getSockTcpPort(client->socket_tcp));
- addToLog(LOG_INF, str_log);
+ getSockTcpIp(client->socket_tcp, str_ip, STR_IP_SIZE);
+ sprintf(str_log, _("Client \"%s\" on port \"%d\" disconnected"), str_ip,
+ getSockTcpPort(client->socket_tcp));
+ addToLog(LOG_INF, str_log);
#endif
- closeTcpSocket(client->socket_tcp);
- destroyBuffer(client->recvBuffer);
- destroyBuffer(client->sendBuffer);
+ closeTcpSocket(client->socket_tcp);
+ destroyBuffer(client->recvBuffer);
+ destroyBuffer(client->sendBuffer);
- destroyAnyClient(client);
+ destroyAnyClient(client);
}
-int initTcpServer(char *ip4, int port4, char *ip6, int port6)
+int
+initTcpServer(char *ip4, int port4, char *ip6, int port6)
{
- int ret;
+ int ret;
- ret = 0;
+ ret = 0;
- if( ip4 != NULL )
- {
- sock_server_tcp = bindTcpSocket(ip4, port4, PROTO_UDPv4);
+ if (ip4 != NULL) {
+ sock_server_tcp = bindTcpSocket(ip4, port4, PROTO_UDPv4);
- if( sock_server_tcp != NULL )
- {
- ret++;
- disableNagle(sock_server_tcp);
+ if (sock_server_tcp != NULL) {
+ ret++;
+ disableNagle(sock_server_tcp);
#ifdef DEBUG
- printf(_("Starting server: \"%s\" on port: \"%d\"\n"), ip4, port4);
+ printf(_("Starting server: \"%s\" on port: \"%d\"\n"), ip4,
+ port4);
#endif
- }
- else
- {
+ }
+ else {
#ifdef DEBUG
- printf(_("Starting server: \"%s\" on port: \"%d\" FAILED!\n"), ip4, port4);
+ printf(_("Starting server: \"%s\" on port: \"%d\" FAILED!\n"),
+ ip4, port4);
#endif
- }
- }
+ }
+ }
- if( ip6 != NULL )
- {
- sock_server_tcp_second = bindTcpSocket(ip6, port6, PROTO_UDPv6);
+ if (ip6 != NULL) {
+ sock_server_tcp_second = bindTcpSocket(ip6, port6, PROTO_UDPv6);
- if( sock_server_tcp_second != NULL )
- {
- ret++;
- disableNagle(sock_server_tcp_second);
+ if (sock_server_tcp_second != NULL) {
+ ret++;
+ disableNagle(sock_server_tcp_second);
#ifdef DEBUG
- printf(_("Starting server: \"%s\" on port: \"%d\"\n"), ip6, port6);
+ printf(_("Starting server: \"%s\" on port: \"%d\"\n"), ip6,
+ port6);
#endif
- }
- else
- {
+ }
+ else {
#ifdef DEBUG
- printf(_("Starting server: \"%s\" on port: \"%d\" FAILED!\n"), ip6, port6);
+ printf(_("Starting server: \"%s\" on port: \"%d\" FAILED!\n"),
+ ip6, port6);
#endif
- }
- }
+ }
+ }
- return ret;
+ return ret;
}
-static void eventNewClient(sock_tcp_t *server_sock)
+static void
+eventNewClient(sock_tcp_t * server_sock)
{
- list_t *listClient;
- sock_tcp_t *sock;
- client_t *client;
-
- listClient = getListServerClient();
+ list_t *listClient;
+ sock_tcp_t *sock;
+ client_t *client;
- sock = getTcpNewClient(server_sock);
- disableNagle(sock);
- setTcpSockNonBlock(sock);
+ listClient = getListServerClient();
- client = newTcpClient(sock);
- addList(listClient, client);
-}
+ sock = getTcpNewClient(server_sock);
+ disableNagle(sock);
+ setTcpSockNonBlock(sock);
-static void eventTcpClient(client_t *client)
-{
- char buffer[STR_PROTO_SIZE];
- int ret;
-
- memset(buffer, 0, STR_PROTO_SIZE);
-
- ret = readTcpSocket(client->socket_tcp, buffer, STR_PROTO_SIZE-1);
- //printf("readTcpSocket = %d\n", ret);
-
- if( ret <= 0 )
- {
- client->status = NET_STATUS_ZOMBIE;
- return;
- }
-
- if( addBuffer(client->recvBuffer, buffer, ret) < 0 )
- {
- client->status = NET_STATUS_ZOMBIE;
- return;
- }
-
- while( getBufferLine(client->recvBuffer, buffer, STR_PROTO_SIZE) >= 0 )
- {
- addList(client->listRecvMsg, strdup(buffer) );
- }
+ client = newTcpClient(sock);
+ addList(listClient, client);
}
-void setServerTcpSelect()
+static void
+eventTcpClient(client_t * client)
{
- list_t *listClient;
- int i;
+ char buffer[STR_PROTO_SIZE];
+ int ret;
- listClient = getListServerClient();
+ memset(buffer, 0, STR_PROTO_SIZE);
- if( sock_server_tcp != NULL )
- {
- addSockToSelectRead(sock_server_tcp->sock);
- }
+ ret = readTcpSocket(client->socket_tcp, buffer, STR_PROTO_SIZE - 1);
+ //printf("readTcpSocket = %d\n", ret);
- if( sock_server_tcp_second != NULL )
- {
- addSockToSelectRead(sock_server_tcp_second->sock);
- }
+ if (ret <= 0) {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
+ }
- for( i = 0 ; i < listClient->count ; i++ )
- {
- client_t *client;
-
- client = (client_t *)listClient->list[i];
-
- if( client->status != NET_STATUS_OK || client->type != CLIENT_TYPE_TCP )
- {
- continue;
- }
+ if (addBuffer(client->recvBuffer, buffer, ret) < 0) {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
+ }
- addSockToSelectRead(client->socket_tcp->sock);
- }
+ while (getBufferLine(client->recvBuffer, buffer, STR_PROTO_SIZE) >= 0) {
+ addList(client->listRecvMsg, strdup(buffer));
+ }
}
-int selectServerTcpSocket()
+void
+setServerTcpSelect()
{
- list_t *listClient;
- int count;
- int i;
+ list_t *listClient;
+ int i;
- count = 0;
+ listClient = getListServerClient();
- if( sock_server_tcp != NULL )
- {
- if( isChangeSockInSelectRead(sock_server_tcp->sock) )
- {
- eventNewClient(sock_server_tcp);
- count++;
- }
- }
+ if (sock_server_tcp != NULL) {
+ addSockToSelectRead(sock_server_tcp->sock);
+ }
- if( sock_server_tcp_second != NULL )
- {
- if( isChangeSockInSelectRead(sock_server_tcp_second->sock) )
- {
- eventNewClient(sock_server_tcp_second);
- count++;
- }
- }
+ if (sock_server_tcp_second != NULL) {
+ addSockToSelectRead(sock_server_tcp_second->sock);
+ }
- listClient = getListServerClient();
+ for (i = 0; i < listClient->count; i++) {
+ client_t *client;
- for( i = 0 ; i < listClient->count ; i++ )
- {
- client_t *client;
+ client = (client_t *) listClient->list[i];
- client = (client_t *)listClient->list[i];
+ if (client->status != NET_STATUS_OK
+ || client->type != CLIENT_TYPE_TCP) {
+ continue;
+ }
- if( client->status != NET_STATUS_OK || client->type != CLIENT_TYPE_TCP )
- {
- continue;
- }
+ addSockToSelectRead(client->socket_tcp->sock);
+ }
+}
- if( isChangeSockInSelectRead(client->socket_tcp->sock) )
- {
- //printf("sChangeSockInSelectRead\n");
- eventTcpClient(client);
- count++;
- }
+int
+selectServerTcpSocket()
+{
+ list_t *listClient;
+ int count;
+ int i;
+
+ count = 0;
+
+ if (sock_server_tcp != NULL) {
+ if (isChangeSockInSelectRead(sock_server_tcp->sock)) {
+ eventNewClient(sock_server_tcp);
+ count++;
+ }
+ }
+
+ if (sock_server_tcp_second != NULL) {
+ if (isChangeSockInSelectRead(sock_server_tcp_second->sock)) {
+ eventNewClient(sock_server_tcp_second);
+ count++;
+ }
+ }
+
+ listClient = getListServerClient();
+
+ for (i = 0; i < listClient->count; i++) {
+ client_t *client;
+
+ client = (client_t *) listClient->list[i];
+
+ if (client->status != NET_STATUS_OK
+ || client->type != CLIENT_TYPE_TCP) {
+ continue;
+ }
+
+ if (isChangeSockInSelectRead(client->socket_tcp->sock)) {
+ //printf("sChangeSockInSelectRead\n");
+ eventTcpClient(client);
+ count++;
+ }
/*
if( isChangeSockInSelectWrite(client->socket_tcp->sock) )
{
@@ -295,30 +289,30 @@ int selectServerTcpSocket()
sendTcpClientBuffer(client);
count++;
}
-*/
- }
+*/
+ }
- return count;
+ return count;
}
-void quitTcpServer()
+void
+quitTcpServer()
{
- if( sock_server_tcp != NULL )
- {
+ if (sock_server_tcp != NULL) {
#ifdef DEBUG
- printf(_("Closing port: \"%d\"\n"), getSockTcpPort(sock_server_tcp));
+ printf(_("Closing port: \"%d\"\n"), getSockTcpPort(sock_server_tcp));
#endif
- closeTcpSocket(sock_server_tcp);
- }
+ closeTcpSocket(sock_server_tcp);
+ }
- if( sock_server_tcp_second != NULL )
- {
+ if (sock_server_tcp_second != NULL) {
#ifdef DEBUG
- printf(_("Closing port: \"%d\"\n"), getSockTcpPort(sock_server_tcp_second));
+ printf(_("Closing port: \"%d\"\n"),
+ getSockTcpPort(sock_server_tcp_second));
#endif
- closeTcpSocket(sock_server_tcp_second);
- }
+ closeTcpSocket(sock_server_tcp_second);
+ }
#ifdef DEBUG
- printf("Quitting TCP\n");
+ printf("Quitting TCP\n");
#endif
}