Socket::Socket(SOCKET connection, const EndPoint &localEndPoint_, const char *localHostName_, const EndPoint &remoteEndPoint_, const char *remoteHostName_, SocketTransportLayer transport_, SocketType type_, size_t maxSendSize_) :connectSocket(connection), localEndPoint(localEndPoint_), localHostName(localHostName_), remoteEndPoint(remoteEndPoint_), remoteHostName(remoteHostName_), transport(transport_), type(type_), maxSendSize(maxSendSize_), writeOpen(true), readOpen(true) #ifdef WIN32 ,queuedReceiveBuffers(numConcurrentReceiveBuffers) ,queuedSendBuffers(numConcurrentSendBuffers) #endif { SetSendBufferSize(512 * 1024); SetReceiveBufferSize(512 * 1024); udpPeerAddress = remoteEndPoint.ToSockAddrIn(); }
void ghttpSetThrottle ( GHTTPRequest request, GHTTPBool throttle ) { GHIConnection * connection; // Get the connection object for this request. ////////////////////////////////////////////// connection = ghiRequestToConnection(request); if(!connection) return; connection->throttle = throttle; // Set the buffer size based on the throttle setting. ///////////////////////////////////////////////////// if(connection->socket != INVALID_SOCKET) SetReceiveBufferSize(connection->socket, throttle?ghiThrottleBufferSize:(8 * 1024)); }
/*************** ** CONNECTING ** ***************/ void ghiDoConnecting ( GHIConnection * connection ) { int rcode; SOCKADDR_IN address; int writeFlag; int exceptFlag; gsDebugFormat(GSIDebugCat_HTTP, GSIDebugType_State, GSIDebugLevel_Comment, "Connecting\n"); // If we don't have a socket yet, set it up. //////////////////////////////////////////// if(connection->socket == INVALID_SOCKET) { // Create the socket. ///////////////////// connection->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(connection->socket == INVALID_SOCKET) { connection->completed = GHTTPTrue; connection->result = GHTTPSocketFailed; connection->socketError = GOAGetLastError(connection->socket); return; } // Set the socket to non-blocking. ////////////////////////////////// if(!SetSockBlocking(connection->socket, 0)) { connection->completed = GHTTPTrue; connection->result = GHTTPSocketFailed; connection->socketError = GOAGetLastError(connection->socket); return; } // If throttling, use a small receive buffer. ///////////////////////////////////////////// if(connection->throttle) SetReceiveBufferSize(connection->socket, ghiThrottleBufferSize); // Setup the server address. //////////////////////////// memset(&address, 0, sizeof(SOCKADDR_IN)); address.sin_family = AF_INET; if (connection->proxyOverrideServer) address.sin_port = htons(connection->proxyOverridePort); else if(ghiProxyAddress) address.sin_port = htons(ghiProxyPort); else address.sin_port = htons(connection->serverPort); address.sin_addr.s_addr = connection->serverIP; // Start the connect. ///////////////////// //rcode = connect(connection->socket, (SOCKADDR *)&address, sizeof(SOCKADDR_IN)); rcode = connect(connection->socket, (SOCKADDR *)&address, sizeof(address)); if(gsiSocketIsError(rcode)) { int socketError = GOAGetLastError(connection->socket); if((socketError != WSAEWOULDBLOCK) && (socketError != WSAEINPROGRESS) && (socketError != WSAETIMEDOUT)) { connection->completed = GHTTPTrue; connection->result = GHTTPConnectFailed; connection->socketError = socketError; return; } } } // Check if the connect has completed. ////////////////////////////////////// rcode = GSISocketSelect(connection->socket, NULL, &writeFlag, &exceptFlag); if((gsiSocketIsError(rcode)) || ((rcode == 1) && exceptFlag)) { connection->completed = GHTTPTrue; connection->result = GHTTPConnectFailed; if(gsiSocketIsError(rcode)) connection->socketError = GOAGetLastError(connection->socket); else connection->socketError = 0; return; } // Check if we're connected. //////////////////////////// if((rcode == 1) && writeFlag) { // Progress. //////////// if (connection->encryptor.mEngine == GHTTPEncryptionEngine_None) connection->state = GHTTPSendingRequest; else connection->state = GHTTPSecuringSession; ghiCallProgressCallback(connection, NULL, 0); } }