[idms-dbma-devel]: COMMIT - r106 - branches/nonblocking_io/lib
Status: Pre-Alpha
Brought to you by:
nkukard
|
From: <sv...@li...> - 2005-05-09 19:55:48
|
Author: nkukard
Date: 2005-05-09 19:55:13 +0000 (Mon, 09 May 2005)
New Revision: 106
Modified:
branches/nonblocking_io/lib/socklib.c
Log:
* Merged in revision 100 from trunk
Modified: branches/nonblocking_io/lib/socklib.c
===================================================================
--- branches/nonblocking_io/lib/socklib.c 2005-05-09 13:14:36 UTC (rev 105)
+++ branches/nonblocking_io/lib/socklib.c 2005-05-09 19:55:13 UTC (rev 106)
@@ -1,6 +1,6 @@
/*
* socklib.c - Socket handling functions
- * Copyright (C) 2002-2004, Linux Based Systems Design
+ * Copyright (C) 2002-2005, Linux Based Systems Design
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -51,28 +51,9 @@
-/* Initialize socket address */
-static int init_sockaddr(struct sockaddr_in *sin, const char *hostname, unsigned short int port)
-{
- struct hostent* hostinfo;
- sin->sin_family = AF_INET;
- sin->sin_port = htons(port);
- hostinfo = gethostbyname(hostname);
-
- if (!hostinfo)
- {
- d_printf(D_FATAL,__FUNCTION__,"Unknown host %s",hostname);
- return D_FATAL;
- }
- sin->sin_addr = *(struct in_addr*) hostinfo->h_addr;
-
- return D_OK;
-}
-
-
/* FIXME - per thread data */
static GList *connectionList = NULL;
fd_set outputFDs;
@@ -200,7 +181,8 @@
{
fd_set tmp_wfds;
fd_set tmp_rfds;
-
+
+
/* Restore our params... */
tmp_wfds = outputFDs;
tmp_rfds = inputFDs;
@@ -274,7 +256,7 @@
/* Check if we were triggered to reload the fd's */
if (FD_ISSET(workerPipe[0],&tmp_rfds))
{
- read(workerPipe[0],&tmpBuf,4096);
+ read(workerPipe[0],&tmpBuf,SOCK_IO_BUF_LEN);
fprintf(stderr,"Triggered reload\n");
break;
}
@@ -440,6 +422,8 @@
}
} /* if (timeout) */
} /* if (item->data) */
+ else
+ D_PRINT(D_ERROR,"item->data is NULL");
item = g_list_next(item);
}
@@ -451,39 +435,8 @@
}
-/* Create a socket */
-static int make_socket(struct sockaddr_in sin)
-{
- int sock;
- int one = 1;
- int result;
- // Create the socket
- sock = socket(PF_INET,SOCK_STREAM,0);
- if (sock < 0)
- {
- d_printf(D_FATAL,"make_socket()",strerror(errno));
- return D_FATAL;
- }
- // Set a few thingies...
- result = setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(one));
- if (result < 0)
- {
- d_printf(D_FATAL,"make_socket()","Failed to set SO_REUSEADDR");
- return D_FATAL;
- }
- // Bind
- if (bind(sock,(struct sockaddr*)&sin,(socklen_t) sizeof(sin))<0)
- {
- d_printf(D_FATAL,"make_socket()",sys_errlist[errno]);
- return D_FATAL;
- }
- // And return
- return(sock);
-}
-
-
/* Function to create a connection that will be passed down to the protocol handler */
static struct clientConnection_t* createConnection(struct sockData_t *sockData, int sockfd, char* remoteHost)
{
@@ -494,7 +447,7 @@
aClient = (struct clientConnection_t*) malloc0(sizeof(struct clientConnection_t));
if (aClient == NULL)
{
- d_printf(D_FATAL,"createConnection()","Failed to allocate memory for aClient");
+ D_PRINT(D_FATAL,"Failed to allocate memory for aClient");
return NULL;
}
@@ -669,6 +622,8 @@
struct timeval tv, tmp_tv;
GList* item;
GThread *thread1;
+ int optVal = 1;
+ int sock;
@@ -685,34 +640,59 @@
// Check we have our data...
if (!(sockData = item->data))
{
- d_printf(D_FATAL,"server_init()","No socket data specified");
+ D_PRINT(D_FATAL,"No socket data specified");
return D_FATAL;
}
- d_printf(D_FATAL,"server_init()","IP Addr: %s, Port: %i, Cons: %i",
+ D_PRINT(D_FATAL,"IP Addr: %s, Port: %i, Cons: %i",
sockData->ipAddress,
sockData->listenPort,
sockData->maxConnections);
/* Initialize socket address */
- if (init_sockaddr(&myaddr,sockData->ipAddress,sockData->listenPort) == D_FATAL)
+ struct hostent* hostinfo;
+ myaddr.sin_family = AF_INET;
+ myaddr.sin_port = htons(sockData->listenPort);
+ hostinfo = gethostbyname(sockData->ipAddress);
+ if (!hostinfo)
{
- d_printf(D_FATAL,"server_init()","Failed to initialize socket address");
+ D_PRINT(D_FATAL,"Unknown host %s",sockData->ipAddress);
return D_FATAL;
}
-
+ myaddr.sin_addr = *(struct in_addr*) hostinfo->h_addr;
+
/* Create socket... */
- sockData->boundFD = make_socket(myaddr);
+
+ // Create the socket
+ if ((sock=socket(PF_INET,SOCK_STREAM,0))< 0)
+ {
+ D_PRINT(D_FATAL,strerror(errno));
+ return D_FATAL;
+ }
+ // Set socket options
+ if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&optVal,sizeof(optVal)) < 0)
+ {
+ D_PRINT(D_FATAL,"Failed to set SO_REUSEADDR");
+ return D_FATAL;
+ }
+ // Bind
+ if (bind(sock,(struct sockaddr*)&myaddr,(socklen_t) sizeof(myaddr))<0)
+ {
+ D_PRINT(D_FATAL,"Error binding: %s",sys_errlist[errno]);
+ return D_FATAL;
+ }
+
+ sockData->boundFD = sock;
if (sockData->boundFD == D_FATAL)
{
- d_printf(D_FATAL,"server_init()","Failed to create socket");
+ D_PRINT(D_FATAL,"Failed to create socket");
return D_FATAL;
}
/* Listen for connections */
if (listen(sockData->boundFD,SOMAXCONN) == -1)
{
- d_printf(D_FATAL,"server_init()","Failed to listen on boundFD: %s",strerror(errno));
+ D_PRINT(D_FATAL,"Failed to listen on boundFD: %s",strerror(errno));
return D_FATAL;
}
@@ -744,10 +724,10 @@
{
case -1:
// if (errno == EINTR)
-// d_printf(D_DEBUG,"server_init()","Child died");
+// D_PRINT(D_DEBUG,"Child died");
// ;
// else
- d_printf(D_NOTICE,__FUNCTION__,"select() returned error: %s",strerror(errno));
+ D_PRINT(D_NOTICE,"select() returned error: %s",strerror(errno));
break;
case 0: /* Select did nothing, hit the timeout */
@@ -761,7 +741,7 @@
/* Check if there is actually data we can use */
if (!(sockData = item->data))
{
- d_printf(D_FATAL,__FUNCTION__,"sockData cannot be null\n");
+ D_PRINT(D_FATAL,"sockData cannot be null\n");
return D_FATAL;
}
@@ -780,14 +760,14 @@
remoteHost = strdup(inet_ntoa(clientAddr.sin_addr));
if (!remoteHost)
{
- d_printf(D_FATAL,__FUNCTION__,"Failed to allocate memory for remoteHost");
+ D_PRINT(D_FATAL,"Failed to allocate memory for remoteHost");
return D_FATAL;
}
/* Setup the client connection */
clientConnection = createConnection(sockData,newfd,remoteHost);
if (clientConnection == NULL)
{
- d_printf(D_FATAL,__FUNCTION__,"Failed to create client connection");
+ D_PRINT(D_FATAL,"Failed to create client connection");
return D_FATAL;
}
/* Pass connection onto worker threads */
@@ -802,3 +782,4 @@
}
}
+// vim: ts=4
|