📄 staftcpconnprovider.cpp
字号:
#endif}static void STAFTCPUpdateConnectionNetworkIDsFromInAddr( STAFConnection_t baseConn, in_addr *addr){ STAFTCPConnectionImpl *conn = static_cast<STAFTCPConnectionImpl *>(baseConn); STAFString_t ipAddress = 0; STAFString_t errorBuffer = 0; STAFRC_t ipAddrRC = STAFSocketGetPrintableAddressFromInAddr( addr, &ipAddress, &errorBuffer); if (ipAddrRC != kSTAFOk) { STAFString error( "Error getting printable IP address, " "STAFSocketGetPrintableAddressFromInAddr(), RC: " + STAFString(ipAddrRC) + ", Info: " + STAFString(errorBuffer, STAFString::kShallow)); STAFTrace::trace(kSTAFTraceError, error); errorBuffer = 0; conn->physicalNetworkID = "0.0.0.0"; } else { conn->physicalNetworkID = STAFString(ipAddress, STAFString::kShallow); } STAFString_t hostname = 0; STAFRC_t hostRC = STAFSocketGetNameByInAddr(addr, &hostname, &errorBuffer); if (hostRC != kSTAFOk) { STAFString error( "Error getting hostname (for IP address " + conn->physicalNetworkID + "), STAFSocketGetNameByInAddr(), RC: " + STAFString(ipAddrRC) + ", Info: " + STAFString(errorBuffer, STAFString::kShallow)); STAFTrace::trace(kSTAFTraceWarning, error); conn->logicalNetworkID = conn->physicalNetworkID; } else { conn->logicalNetworkID = STAFString(hostname, STAFString::kShallow); }}static unsigned int STAFTCPConnectionThread(void *data){ try { TCPConnectionData *connData = reinterpret_cast<TCPConnectionData *>(data); connData->connFunc(connData->provider, connData->connection, &gFuncTable, connData->provider->data); delete connData; } CATCH_STANDARD_TRACE("STAFTCPConnectionThread"); return 0;}static unsigned int STAFTCPRunThreadIPv6(void *providerImpl){#ifdef STAF_USE_IPV6 STAFTCPConnectionProviderImpl *provider = reinterpret_cast<STAFTCPConnectionProviderImpl *>(providerImpl); try { int maxSock, numSock = 0; int socks[2] = { 0 }; if (provider->serverSocket > provider->serverSocketIPv6) maxSock = provider->serverSocket; else maxSock = provider->serverSocketIPv6; fd_set readSocks, readSocks0; FD_ZERO(&readSocks0); if (STAFUtilIsValidSocket(provider->serverSocket)) { // Set the socket to be non-inheritable unsigned int osRC = 0; STAFSocket_t newSocket; if (STAFUtilGetNonInheritableSocket(provider->serverSocket, &newSocket, &osRC)) { STAFTrace::trace( kSTAFTraceError, STAFString("Error getting non-inheritable server socket:" " IPv4, STAFUtilGetNonInheritableSocket(), " "OS RC: ") + STAFString(osRC)); } else { provider->serverSocket = newSocket; } FD_SET(provider->serverSocket, &readSocks0); socks[numSock] = provider->serverSocket; numSock++; } if (STAFUtilIsValidSocket(provider->serverSocketIPv6)) { // Set the socket to be non-inheritable unsigned int osRC = 0; STAFSocket_t newSocket; if (STAFUtilGetNonInheritableSocket(provider->serverSocketIPv6, &newSocket, &osRC)) { STAFTrace::trace( kSTAFTraceError, STAFString("Error getting non-inheritable server socket:" " IPv6, STAFUtilGetNonInheritableSocket(), " "OS RC: ") + STAFString(osRC)); } else { provider->serverSocketIPv6 = newSocket; } FD_SET(provider->serverSocketIPv6, &readSocks0); socks[numSock] = provider->serverSocketIPv6; numSock++; } provider->syncSem->post(); while (provider->state == kSTAFConnectionProviderActive) { readSocks = readSocks0; if (select(maxSock + 1, &readSocks, NULL, NULL, NULL) < 0) { if (provider->state == kSTAFConnectionProviderStopped) break; STAFTrace::trace( kSTAFTraceError, STAFString("Error selecting on server sockets, " "socket RC: ") + STAFString(STAFSocketGetLastError())); continue; } for (int i = 0; i < numSock; i++) { if (FD_ISSET(socks[i], &readSocks)) { STAFTCPConnectionImpl connImpl; struct sockaddr_storage clientAddress; STAFSocketLen_t clientAddressLength = sizeof(clientAddress); do { connImpl.clientSocket = accept( socks[i], reinterpret_cast<struct sockaddr *>(&clientAddress), &clientAddressLength); } while (!STAFUtilIsValidSocket(connImpl.clientSocket) && (STAFSocketGetLastError() == SOCEINTR)); if (provider->state == kSTAFConnectionProviderStopped) break; if (!STAFUtilIsValidSocket(connImpl.clientSocket)) { STAFTrace::trace( kSTAFTraceError, STAFString("Error accepting on server socket, " "socket RC: ") + STAFString(STAFSocketGetLastError())); continue; } // Set the socket to be non-inheritable unsigned int osRC = 0; STAFSocket_t newSocket; if (STAFUtilGetNonInheritableSocket(connImpl.clientSocket, &newSocket, &osRC)) { STAFTrace::trace( kSTAFTraceError, STAFString("Error getting non-inheritable socket, " "STAFUtilGetNonInheritableSocket(), " "OS RC: ") + STAFString(osRC)); continue; } connImpl.clientSocket = newSocket; // Turn on the SO_KEEPALIVE socket option (which is turned // off by default on Windows) to make the socket send // keepalive messages on the session so that if one side // of the connection is terminated, the other side will be // notified (after the keepalive time which is 2 hours by // default for most operating systems. See Bug #1559514. bool bOptVal = true; setsockopt(connImpl.clientSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&bOptVal, sizeof(bool));#ifdef STAF_USE_SSL if (provider->secure.isEqualTo(sYes, kSTAFStringCaseInsensitive)) { // Do server side SSL. connImpl.ssl = SSL_new(provider->server_ctx); if (connImpl.ssl == NULL) { STAFTrace::trace(kSTAFTraceError, STAFString("Error getting server SSL object: ") + STAFString(ERR_error_string(ERR_get_error(), NULL))); continue; } // set connection to SSL state SSL_set_fd(connImpl.ssl, connImpl.clientSocket); // start the handshaking if (SSL_accept(connImpl.ssl) == -1) { STAFSocketClose(connImpl.clientSocket); SSL_free (connImpl.ssl); STAFTrace::trace(kSTAFTraceError, STAFString("Error in server SSL handshake: ") + STAFString(ERR_error_string(ERR_get_error(), NULL))); continue; } } connImpl.secure = provider->secure;#endif // Ok, let's perform the callback now TCPConnectionData connData; connData.connFunc = provider->connFunc; connData.provider = provider; connData.connection = new STAFTCPConnectionImpl(connImpl); STAFIPv6TCPUpdateConnectionNetworkIDsFromInAddr( connData.connection, (struct sockaddr*)&clientAddress, clientAddressLength); provider->threadManager->dispatch( STAFTCPConnectionThread, new TCPConnectionData(connData)); } // end if } // end for } // end while } CATCH_STANDARD_TRACE("STAFTCPRunThread"); try { provider->syncSem->post(); } CATCH_STANDARD_TRACE("STAFTCPRunThread");#endif return 0;}static unsigned int STAFTCPRunThreadIPv4(void *providerImpl){ STAFTCPConnectionProviderImpl *provider = reinterpret_cast<STAFTCPConnectionProviderImpl *>(providerImpl); try { provider->syncSem->post(); while (provider->state == kSTAFConnectionProviderActive) { STAFTCPConnectionImpl connImpl; struct sockaddr_in clientAddress = { 0 }; STAFSocketLen_t clientAddressLength = sizeof(clientAddress); do { connImpl.clientSocket = accept( provider->serverSocket, reinterpret_cast<struct sockaddr *>(&clientAddress), &clientAddressLength); } while (!STAFUtilIsValidSocket(connImpl.clientSocket) && (STAFSocketGetLastError() == SOCEINTR) && (provider->state != kSTAFConnectionProviderStopped)); if (provider->state == kSTAFConnectionProviderStopped) break; if (!STAFUtilIsValidSocket(connImpl.clientSocket)) { STAFTrace::trace(kSTAFTraceError, STAFString("Error accepting on server socket" ", socket RC: ") + STAFString(STAFSocketGetLastError())); continue; } // Set the socket to be non-inheritable unsigned int osRC = 0; STAFSocket_t newSocket; if (STAFUtilGetNonInheritableSocket(connImpl.clientSocket, &newSocket, &osRC)) { STAFTrace::trace( kSTAFTraceError, STAFString("Error getting non-inheritable client socket, " "STAFUtilGetNonInheritableSocket(), OS RC: ") + STAFString(osRC)); continue; } connImpl.clientSocket = newSocket; // Turn on the SO_KEEPALIVE socket option (which is turned // off by default on Windows) to make the socket send // keepalive messages on the session so that if one side // of the connection is terminated, the other side will be // notified (after the keepalive time which is 2 hours by // default for most operating systems. See Bug #1559514. bool bOptVal = true; setsockopt(connImpl.clientSocket, SOL_SOCKET, SO_KEEPALIVE, (char*)&bOptVal, sizeof(bool)); #ifdef STAF_USE_SSL if (provider->secure.isEqualTo(sYes, kSTAFStringCaseInsensitive)) { // Do server side SSL. connImpl.ssl = SSL_new(provider->server_ctx); if (connImpl.ssl == NULL) { STAFTrace::trace(kSTAFTraceError, STAFString("Error getting server SSL object: ") + STAFString(ERR_error_string(ERR_get_error(), NULL))); continue; } // set connection to SSL state SSL_set_fd(connImpl.ssl, connImpl.clientSocket); // start the handshaking if (SSL_accept(connImpl.ssl) == -1) { STAFSocketClose(connImpl.clientSocket); SSL_free (connImpl.ssl); STAFTrace::trace(kSTAFTraceError, STAFString("Error in server SSL handshake: ") + STAFString(ERR_error_string(ERR_get_error(), NULL))); continue; } } connImpl.secure = provider->secure;#endif // Ok, let's perform the callback now TCPConnectionData connData;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -