rsrv: avoid redundant getpeername()

also do some extra, and redundant checks,
on accepted sockets.
This commit is contained in:
Michael Davidsaver
2018-11-14 09:55:09 -08:00
parent cae597d21c
commit 716f102dd6
2 changed files with 12 additions and 18 deletions
+11 -17
View File
@@ -83,20 +83,22 @@ static void req_server (void *pParm)
while (TRUE) {
SOCKET clientSock;
struct sockaddr sockAddr;
osiSockAddr sockAddr;
osiSocklen_t addLen = sizeof(sockAddr);
while (castcp_ctl == ctlPause) {
epicsThreadSleep(0.1);
}
clientSock = epicsSocketAccept ( IOC_sock, &sockAddr, &addLen );
if ( clientSock == INVALID_SOCKET ) {
clientSock = epicsSocketAccept ( IOC_sock, &sockAddr.sa, &addLen );
if ( clientSock == INVALID_SOCKET ||
sockAddr.sa.sa_family != AF_INET ||
addLen < sizeof(sockAddr.ia) ) {
char sockErrBuf[64];
epicsSocketConvertErrnoToString (
sockErrBuf, sizeof ( sockErrBuf ) );
errlogPrintf("CAS: Client accept error: %s\n",
sockErrBuf );
errlogPrintf("CAS: Client accept error: %s (%d)\n",
sockErrBuf, (int)addLen );
epicsThreadSleep(15.0);
continue;
}
@@ -105,7 +107,7 @@ static void req_server (void *pParm)
struct client *pClient;
/* socket passed in is closed if unsuccessful here */
pClient = create_tcp_client ( clientSock );
pClient = create_tcp_client ( clientSock, &sockAddr );
if ( ! pClient ) {
epicsThreadSleep ( 15.0 );
continue;
@@ -1405,12 +1407,11 @@ void casExpandRecvBuffer ( struct client *pClient, ca_uint32_t size )
/*
* create_tcp_client ()
*/
struct client *create_tcp_client ( SOCKET sock )
struct client *create_tcp_client (SOCKET sock , const osiSockAddr *peerAddr)
{
int status;
struct client *client;
int intTrue = TRUE;
osiSocklen_t addrSize;
unsigned priorityOfEvents;
/* socket passed in is destroyed here if unsuccessful */
@@ -1419,6 +1420,8 @@ struct client *create_tcp_client ( SOCKET sock )
return NULL;
}
client->addr = peerAddr->ia;
/*
* see TCP(4P) this seems to make unsolicited single events much
* faster. I take care of queue up as load increases.
@@ -1470,15 +1473,6 @@ struct client *create_tcp_client ( SOCKET sock )
}
#endif
addrSize = sizeof ( client->addr );
status = getpeername ( sock, (struct sockaddr *)&client->addr,
&addrSize );
if ( status < 0 ) {
epicsPrintf ("CAS: peer address fetch failed\n");
destroy_tcp_client (client);
return NULL;
}
client->evuser = (struct event_user *) db_init_events ();
if ( ! client->evuser ) {
errlogPrintf ("CAS: unable to init the event facility\n");
+1 -1
View File
@@ -230,7 +230,7 @@ void rsrv_online_notify_task (void *);
void cast_server (void *);
struct client *create_client ( SOCKET sock, int proto );
void destroy_client ( struct client * );
struct client *create_tcp_client ( SOCKET sock );
struct client *create_tcp_client ( SOCKET sock, const osiSockAddr* peerAddr );
void destroy_tcp_client ( struct client * );
void casAttachThreadToClient ( struct client * );
int camessage ( struct client *client );