bool AiTankController::scanAround(float gameTime) { if (!isAttached()) return false; if (gameTime < lastScanTime + fScanAroundSkipTime) return false; lastScanTime = gameTime; vector<Entity *> entities; Point2f posTank; Entity &tankEntity = getTankEntity(); if (!tankEntity.getStatus(Entity::ESI_Position, &posTank)) return false; PROFILE_BEGINPOINT(AiTankController::scanAroundA); EntityManager::getSingleton().getEntities(entities, AiSightEntityFilter(posTank, tankEntity)); PROFILE_CHECKPOINT(AiTankController::scanAroundB); Point2f aimPos; float aimDistance = 1001.f; for (size_t i = 0; i < entities.size(); i++) { Entity *e = entities[i]; Point2f ePos; switch(e->getTypeId()) { case ETI_Tank: if (e != &tankEntity && e->getStatus(Entity::ESI_Position, &ePos)) { float distance = (ePos - posTank).Length(); if (distance > 50.f && distance < aimDistance) { aimDistance = distance; aimPos = ePos; target = e; } } break; case ETI_Item: if (operates.empty() && e->getStatus(Entity::ESI_Position, &ePos)) { Operate moveTo(Operate::CI_MoveTo); if (findPathTo(ePos, moveTo.wayPoints)) { operates.push(moveTo); i = entities.size(); break; } else { // just give up the item // moveTo(ePos); } } break; } } PROFILE_ENDPOINT(); return true; }
/* * Monitors the server side of the local TCP abstraction for data that can be * transmitted to the remote half of the pipe */ ULONG HttpTunnel::SendThreadFunc() { fd_set FdSet; UCHAR ReadBuffer[16384]; LONG BytesRead; INT Result; // This is the song that never ends... while (1) { FD_ZERO( &FdSet); FD_SET( LocalTcpServerSide, &FdSet); PROFILE_CHECKPOINT("select ==>"); // Wait for some data... Result = select( LocalTcpServerSide + 1, &FdSet, NULL, NULL, NULL); PROFILE_CHECKPOINT("select <=="); // If select failed or there was no new data, act accordingly else risk // the fist of the evil witch if (Result < 0) { CPassiveX::Log( TEXT("SendThreadFunc(): TUNNEL_IN: Select failed, %lu.\n"), WSAGetLastError()); break; } else if (Result == 0) continue; PROFILE_CHECKPOINT("recv ==>"); // Read in data from the local server side of the TCP connection BytesRead = recv( LocalTcpServerSide, (char *)ReadBuffer, sizeof(ReadBuffer), 0); PROFILE_CHECKPOINT("recv <=="); // On error or end of file... if (BytesRead <= 0) { CPassiveX::Log( TEXT("SendThreadFunc(): TUNNEL_IN: Read 0 or fewer bytes, erroring out (%lu).\n"), BytesRead); break; } CPassiveX::Log( TEXT("SendThreadFunc(): TUNNEL_IN: Transmitting %lu bytes of data to remote side.\n"), BytesRead); PROFILE_CHECKPOINT("TransmitToRemote ==>"); // Transmit the data to the remote side if ((Result = TransmitToRemote( ReadBuffer, BytesRead)) != ERROR_SUCCESS) { CPassiveX::Log( TEXT("SendThreadFunc(): TUNNEL_IN: TransmitToRemote failed, %lu.\n"), Result); } PROFILE_CHECKPOINT("TransmitToRemote <=="); } // Exit the process if the send thread ends ExitProcess(0); return 0; }
/* * Transmits an HTTP request to the target host, optionally waiting for a * response */ DWORD HttpTunnel::TransmitHttpRequest( IN LPTSTR Method, IN LPTSTR Uri, IN PVOID RequestPayload, IN ULONG RequestPayloadLength, IN ULONG WaitResponseTimeout, OUT LPDWORD ResponseCode, OUT PVOID *ResponsePayload, OUT LPDWORD ResponsePayloadLength) { HINTERNET RequestHandle = NULL; HINTERNET ConnectHandle = NULL; PUCHAR OutBuffer = NULL; DWORD OutBufferLength = 0; UCHAR ReadBuffer[8192]; DWORD ReadBufferLength; DWORD Result = ERROR_SUCCESS; do { PROFILE_CHECKPOINT("InternetConnect ==>"); // Open a connection handle if (!(ConnectHandle = InternetConnect( InternetHandle, HttpHost, HttpPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL))) { Result = GetLastError(); break; } PROFILE_CHECKPOINT("InternetConnect <=="); // If we were supplied a wait response timeout, set it if (WaitResponseTimeout) InternetSetOption( ConnectHandle, INTERNET_OPTION_RECEIVE_TIMEOUT, &WaitResponseTimeout, sizeof(WaitResponseTimeout)); PROFILE_CHECKPOINT("HttpOpenRequest ==>"); // Open a request handle if (!(RequestHandle = HttpOpenRequest( ConnectHandle, Method ? Method : TEXT("GET"), Uri, NULL, NULL, NULL, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, NULL))) { Result = GetLastError(); break; } PROFILE_CHECKPOINT("HttpOpenRequest <=="); PROFILE_CHECKPOINT("HttpSendRequest ==>"); // Send and endthe request if ((!HttpSendRequest( RequestHandle, NULL, 0, RequestPayload, RequestPayloadLength))) { Result = GetLastError(); break; } PROFILE_CHECKPOINT("HttpSendRequest <=="); // If we wont be waiting for a response, break out now and return if (!WaitResponseTimeout) { Result = ERROR_SUCCESS; break; } // Keep looping until we've read the entire request or an error is // encountered while (1) { PUCHAR NewBuffer; ReadBufferLength = sizeof(ReadBuffer); PROFILE_CHECKPOINT("InternetReadFile ==>"); if (!InternetReadFile( RequestHandle, ReadBuffer, ReadBufferLength, &ReadBufferLength)) { Result = GetLastError(); break; } else if (!ReadBufferLength) { Result = ERROR_SUCCESS; break; } PROFILE_CHECKPOINT("InternetReadFile <=="); // Append the buffer to the output buffer if (!OutBuffer) NewBuffer = (PUCHAR)malloc( ReadBufferLength); else NewBuffer = (PUCHAR)realloc( OutBuffer, OutBufferLength + ReadBufferLength); if (!NewBuffer) { Result = ERROR_NOT_ENOUGH_MEMORY; break; } memcpy( NewBuffer + OutBufferLength, ReadBuffer, ReadBufferLength); OutBuffer = NewBuffer; OutBufferLength += ReadBufferLength; } // Query the status code of the response if (ResponseCode) { DWORD ResponseCodeSize = sizeof(DWORD); if (!HttpQueryInfo( RequestHandle, HTTP_QUERY_STATUS_CODE, ResponseCode, &ResponseCodeSize, NULL)) { CPassiveX::Log( TEXT("HttpQueryInfo failed, %lu."), GetLastError()); *ResponseCode = 0; } } } while (0); PROFILE_CHECKPOINT("Finished TransmitHttpRequest"); // Close handles if (RequestHandle) InternetCloseHandle( RequestHandle); if (ConnectHandle) InternetCloseHandle( ConnectHandle); // Set the output pointers or free up the output buffer if (Result == ERROR_SUCCESS) { if (ResponsePayload) *ResponsePayload = OutBuffer; if (ResponsePayloadLength) *ResponsePayloadLength = OutBufferLength; FailedConnections = 0; } else { // If we fail to connect... if (Result == ERROR_INTERNET_CANNOT_CONNECT) { FailedConnections++; if (FailedConnections > 10) { CPassiveX::Log("TransmitHttpRequest(): Failed to connect to HTTP server (%lu), exiting.", FailedConnections); ExitProcess(0); } } if (OutBuffer) free( OutBuffer); } return Result; }
/* * Transmits an HTTP request to the target host, optionally waiting for a * response */ DWORD HttpTunnel::TransmitHttpRequest( IN LPTSTR Method, IN LPTSTR Uri, IN PVOID RequestPayload, IN ULONG RequestPayloadLength, IN ULONG WaitResponseTimeout, OUT LPDWORD ResponseCode, OUT PVOID *ResponsePayload, OUT LPDWORD ResponsePayloadLength) { HINTERNET RequestHandle = NULL; HINTERNET ConnectHandle = NULL; PUCHAR OutBuffer = NULL; DWORD OutBufferLength = 0; UCHAR ReadBuffer[8192]; DWORD ReadBufferLength; DWORD Result = ERROR_SUCCESS; PCHAR AdditionalHeaders = NULL; CHAR FullUri[1024]; // Construct the full URI if (HttpUriBase && HttpUriBase[0]) sprintf_s(FullUri, sizeof(FullUri) - 1, "%s%s", HttpUriBase, Uri); else strncpy_s(FullUri, 1024, Uri, sizeof(FullUri) - 1); FullUri[sizeof(FullUri) - 1] = 0; do { PROFILE_CHECKPOINT("InternetConnect ==>"); // Open a connection handle if (!(ConnectHandle = InternetConnect( InternetHandle, HttpHost, HttpPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL))) { Result = GetLastError(); break; } PROFILE_CHECKPOINT("InternetConnect <=="); // If we were supplied a wait response timeout, set it if (WaitResponseTimeout) InternetSetOption( ConnectHandle, INTERNET_OPTION_RECEIVE_TIMEOUT, &WaitResponseTimeout, sizeof(WaitResponseTimeout)); PROFILE_CHECKPOINT("HttpOpenRequest ==>"); // Open a request handle if (!(RequestHandle = HttpOpenRequest( ConnectHandle, Method ? Method : TEXT("GET"), FullUri, NULL, NULL, NULL, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, NULL))) { Result = GetLastError(); break; } // If we were assigned an HTTP session identifier, then allocate an // additional header for transmission to the remote side. if (HttpSid) { size_t size = strlen(HttpSid) + 32; // Yeah, I'm lame, this is easy to sig. Improve me if you care! if(( AdditionalHeaders = (PCHAR)malloc(size) )) sprintf_s( AdditionalHeaders, size, "X-Sid: sid=%s\r\n", HttpSid ); } PROFILE_CHECKPOINT("HttpOpenRequest <=="); PROFILE_CHECKPOINT("HttpSendRequest ==>"); // Send and endthe request if ((!HttpSendRequest( RequestHandle, AdditionalHeaders, (AdditionalHeaders) ? -1L : 0, RequestPayload, RequestPayloadLength))) { Result = GetLastError(); break; } PROFILE_CHECKPOINT("HttpSendRequest <=="); // If we wont be waiting for a response, break out now and return if (!WaitResponseTimeout) { Result = ERROR_SUCCESS; break; } // Keep looping until we've read the entire request or an error is // encountered while (1) { PUCHAR NewBuffer; ReadBufferLength = sizeof(ReadBuffer); PROFILE_CHECKPOINT("InternetReadFile ==>"); if (!InternetReadFile( RequestHandle, ReadBuffer, ReadBufferLength, &ReadBufferLength)) { Result = GetLastError(); break; } else if (!ReadBufferLength) { Result = ERROR_SUCCESS; break; } PROFILE_CHECKPOINT("InternetReadFile <=="); // Append the buffer to the output buffer if (!OutBuffer) NewBuffer = (PUCHAR)malloc( ReadBufferLength); else NewBuffer = (PUCHAR)realloc( OutBuffer, OutBufferLength + ReadBufferLength); if (!NewBuffer) { Result = ERROR_NOT_ENOUGH_MEMORY; break; } memcpy( NewBuffer + OutBufferLength, ReadBuffer, ReadBufferLength); OutBuffer = NewBuffer; OutBufferLength += ReadBufferLength; } // Query the status code of the response if (ResponseCode) { DWORD ResponseCodeSize = sizeof(DWORD); if (!HttpQueryInfo( RequestHandle, HTTP_QUERY_STATUS_CODE, ResponseCode, &ResponseCodeSize, NULL)) { CPassiveX::Log( TEXT("HttpQueryInfo failed, %lu."), GetLastError()); *ResponseCode = 0; } } } while (0); PROFILE_CHECKPOINT("Finished TransmitHttpRequest"); // Close handles if (RequestHandle) InternetCloseHandle( RequestHandle); if (ConnectHandle) InternetCloseHandle( ConnectHandle); if (AdditionalHeaders) free(AdditionalHeaders); // Set the output pointers or free up the output buffer if (Result == ERROR_SUCCESS) { if (ResponsePayload) *ResponsePayload = OutBuffer; if (ResponsePayloadLength) *ResponsePayloadLength = OutBufferLength; FailedConnections = 0; } else { // If we fail to connect... if (Result == ERROR_INTERNET_CANNOT_CONNECT) { FailedConnections++; if (FailedConnections > 10) { CPassiveX::Log("TransmitHttpRequest(): Failed to connect to HTTP server (%lu), exiting.", FailedConnections); ExitProcess(0); } } if (OutBuffer) free( OutBuffer); } return Result; }