Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

void Impala::Util::Channel::InitSocket (  )  [inline, private]

Definition at line 382 of file Channel.h.

References HandleError(), ILOG_INFO, mConnection, mIsServer, mMsgSock, mMsgSockEvent, mNrPorts, mPort, mServerName, mSock, mSockConnected, and mSockEvent.

Referenced by Channel().

00383     {
00384 #ifdef unix
00385         signal(SIGPIPE, SIG_IGN); // block broken pipe signal
00386 #else
00387         WORD wVersionRequested = MAKEWORD(1,1);
00388         WSADATA wsaData;
00389 
00390         // Initialize WinSock and check version
00391         if (WSAStartup(wVersionRequested, &wsaData) != 0)
00392             HandleError("WSAStartup", true);
00393 
00394         // Check socket DLL supports 1.1 or higher
00395         double socklib_ver = HIBYTE(wsaData.wVersion) / 10.0;
00396         socklib_ver += LOBYTE(wsaData.wVersion);
00397         if (socklib_ver < 1.1)
00398             HandleError("socket library must support 1.1 or greater", true);
00399 #endif
00400 
00401         for (int i=0 ; i<mNrPorts ; i++)
00402         {
00403             struct sockaddr_in address;
00404             address.sin_family = AF_INET;
00405             address.sin_port = htons(mPort + i);
00406             struct hostent* hp;
00407             if (mIsServer)
00408             {
00409                 address.sin_addr.s_addr = htonl(INADDR_ANY);
00410             }
00411             else
00412             {
00413                 // gethostbyname returns a structure including the network
00414                 // address of the specified host.
00415                 if ((hp = gethostbyname(mServerName.c_str())) == NULL)
00416                     HandleError("InitSocket: gethostbyname", true);
00417                 // initialise the address structure with the resolved IP address
00418                 memcpy((char *) &address.sin_addr, (char *) hp->h_addr,
00419                        hp->h_length);
00420             }
00421 
00422             SOCKET sock;
00423             // Create socket.
00424             if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
00425                 HandleError("InitSocket: open stream socket", true);
00426             if (mIsServer)
00427             {
00428                 if (bind(sock, (struct sockaddr *) &address,
00429                          sizeof(address)) == SOCKET_ERROR)
00430                 {
00431                     HandleError("InitSocket: bind socket", true);
00432                 }
00433             }
00434             else
00435             {
00436                 int nrTries = 5;
00437                 bool done = false;
00438                 while (!done)
00439                 {
00440                     int res = connect(sock, (struct sockaddr *) &address,
00441                                       sizeof(address));
00442                     if (res == SOCKET_ERROR)
00443                     {
00444                         nrTries--;
00445                         if (nrTries < 0)
00446                         {
00447                             HandleError("InitSocket: connect socket", true);
00448                             done = true;
00449                         }
00450                         else
00451                         {
00452                             ILOG_INFO("Connect failed, re-trying " << nrTries);
00453                         }
00454                     }
00455                     else
00456                     {
00457                         done = true;
00458                     }
00459                 }
00460             }
00461             mSock.push_back(sock);
00462             mSockConnected.push_back(false);
00463             mConnection.push_back("not connected");
00464         }
00465         mSockEvent.resize(mNrPorts);
00466         mMsgSock.resize(mNrPorts);
00467         mMsgSockEvent.resize(mNrPorts);
00468     }

Here is the call graph for this function:


Generated on Thu Jan 13 09:24:09 2011 for ImpalaSrc by  doxygen 1.5.1