HRESULT Read(SOCKET socket, MemoryBuffer& buffer) { const size_t position = buffer.GetPosition(); const size_t size = buffer.GetSize(); int length = static_cast<int>(size - position); char* data = reinterpret_cast<char*>(buffer.GetData()) + position; const int status = recv(socket, data, length, 0); if (status <= 0) { const DWORD code = WSAGetLastError(); const HRESULT result = (WSAEWOULDBLOCK == code) ? S_FALSE : E_FAIL; return result; } length -= status; buffer.SetPosition(position + status); if (length > 0) { return S_FALSE; } return S_OK; }
int ehttp::parse_out_chunks( void *cookie, MemoryBuffer& memoryBuffer, vector<HTTP_CHUNK> &chunks) { const BYTE * bufferAddr = (BYTE*)memoryBuffer.GetData(); DWORD bufferSize = memoryBuffer.GetDataLength(); // Get our boundary to search for string boundary = request_header["Content-Type"]; boundary = "--" + boundary.substr(boundary.find("boundary=") + 9); // Now get a pointer to each chunk BYTE* temp = (BYTE*)bufferAddr; DWORD boundaryLen = boundary.length(); DWORD foundChunkCount = 0; while((temp + boundaryLen) < (bufferAddr + bufferSize)) { if(memcmp(temp, boundary.c_str(), boundaryLen) == 0) { HTTP_CHUNK tempChunk; tempChunk.Data = temp; // Initialize the file data for this chunk tempChunk.FileData.Data = NULL; tempChunk.FileData.Length = 0; chunks.push_back(tempChunk); temp += boundaryLen; foundChunkCount++; } else temp++; } // Now we have our chunk positions, lets get our lengths vector<DWORD> sizes; DWORD size = 0; for(DWORD x = 0; x < foundChunkCount - 1; x++) { size = chunks.at(x + 1).Data - chunks.at(x).Data; //size = chunks.at(x + 1) - chunks.at(x); chunks.at(x).Length = size; } size = (bufferAddr + bufferSize) - chunks.at(foundChunkCount - 1).Data; chunks.at(foundChunkCount - 1).Length = size; #ifdef DUMP_TO_FILE if(!FileExists("game:\\HTTPDumps\\")) _mkdir("game:\\HTTPDumps\\"); if(!FileExists("game:\\HTTPDumps\\Chunks\\")) _mkdir("game:\\HTTPDumps\\Chunks\\"); for(int nCount = 0; nCount < (int)chunks.size(); nCount++) { FILE * fHandle; string fileName = sprintfaA("game:\\HTTPDumps\\Chunks\\ChunkDump_%d.bin", nCount); fopen_s(&fHandle, fileName.c_str(), "wb"); fwrite(chunks.at(nCount).Data, 1, chunks.at(nCount).Length, fHandle); fclose(fHandle); } #endif // If it found chunks, return that it was successful return foundChunkCount == 0 ? EHTTP_ERR_GENERIC : EHTTP_ERR_OK; }
int ehttp:: parse_message( int fd, void *cookie, MemoryBuffer &message ) { if( !contentlength ) return EHTTP_ERR_OK; DebugMsg( "HTTPServer-embed","Parsed content length:%d\r\n",contentlength); DebugMsg( "HTTPServer-embed","Actual message length read in:%d\r\n", message.GetDataLength()); unsigned int recieved = message.GetDataLength(); while( recieved < contentlength) { input_buffer[0]=0; int r = recv((int)fd, input_buffer, INPUT_BUFFER_SIZE, 0); if( r < 0 ) return EHTTP_ERR_GENERIC; message.Add(input_buffer, r); DebugMsg( "HTTPServer-embed", "Message Length: %d", r); // Decrement our recieved content recieved += r; } if(request_header["Content-Type"].find("multipart") == 0) { DebugMsg("HTTPServer-embed", "Parsing Multipart Upload"); if(parse_out_chunks(cookie, message, post_chunks) == EHTTP_ERR_OK) { if(parse_out_entries(cookie, post_chunks) == EHTTP_ERR_OK) { if(parse_out_entry_headers( cookie, post_chunks) == EHTTP_ERR_OK) { DebugMsg("HTTPServer-embed", "Parsing Successful"); } } } } else { // Got here, good, we got the entire reported msg length //DebugMsg( "HTTPServer-embed","Entire message is <%s>\r\n",message.c_str()); string test = (char*)message.GetData(); parse_out_pairs(cookie, test, post_parms); } return EHTTP_ERR_OK; }
HRESULT CLrpStClientImpl::Invoke(MemoryBuffer& buffer) { uint32 size = static_cast<uint32>(buffer.GetSize()); if (size < sizeof(uint32)) { throw runtime_error("Input buffer is too small"); } buffer.SetPosition(0); WriteInt32(size - 4, buffer); buffer.SetPosition(12); uint16 componentId = ReadUInt16(buffer); uint16 methodId = ReadUInt16(buffer); Translate(componentId, methodId); buffer.SetPosition(12); WriteUInt16(componentId, buffer); WriteUInt16(methodId, buffer); CTimeout timeout(m_operationTimeoutInMs); if (!SendEx(m_socket, timeout, buffer.GetData(), buffer.GetSize())) { closesocket(m_socket); m_socket = INVALID_SOCKET; throw runtime_error("Couldn't send data"); } if (!ReceiveEx(m_socket, timeout, buffer)) { closesocket(m_socket); m_socket = INVALID_SOCKET; throw runtime_error("Couldn't receive data"); } buffer.SetPosition(12); const HRESULT result = ReadInt32(buffer); return result; }
HRESULT Write(SOCKET socket, MemoryBuffer& buffer) { const size_t position = buffer.GetPosition(); const size_t size = buffer.GetSize(); int length = static_cast<int>(size - position); const char* data = reinterpret_cast<const char*>(buffer.GetData()) + position; const int status = send(socket, data, length, 0); if (status < 0) { return E_FAIL; } length -= status; buffer.SetPosition(position + status); if (length > 0) { return S_FALSE; } return S_OK; }
HRESULT ConvertMemoryBufferToBMPBuffer( MemoryBuffer &image_in, MemoryBuffer &image_out) { // Check to ensure that our data has a valid length before proceeding if( image_in.GetDataLength() == 0 ) return S_FALSE; // Set up a timer class to profile our code ATG::Timer m_Timer; double timeStart, timeStop; // Begin Profiling our Texture Creation Code timeStart = m_Timer.GetAbsoluteTime(); IDirect3DTexture9 * pTexture; HRESULT retVal = D3DXCreateTextureFromFileInMemoryEx( CFreestyleApp::getInstance().m_pd3dDevice, image_in.GetData(), image_in.GetDataLength(), D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 1, D3DUSAGE_CPU_CACHED_MEMORY, D3DFMT_LIN_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); // End Profiling our Texture Creation Code timeStop = m_Timer.GetAbsoluteTime(); DebugMsg("ConvertImageToBMP", "Texture Creation took %4.2f seconds to complete", (float)(timeStop - timeStart)); // If our texture was created successfully, if(retVal = D3D_OK) { //Begin Profiling our Data Manipulation Code timeStart = m_Timer.GetAbsoluteTime(); // Get our level desc D3DSURFACE_DESC desc; pTexture->GetLevelDesc(0, &desc); // Now lock our data D3DLOCKED_RECT lock; RECT rect = {0, 0, desc.Width, desc.Height}; pTexture->LockRect(0, &lock, &rect, D3DLOCK_READONLY); //Read our data DWORD headerSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); DWORD dataLen = lock.Pitch * desc.Height; DWORD * dataBuffer = (DWORD*)malloc( dataLen + headerSize ); DWORD * address = (DWORD*)lock.pBits; // Create file our header BITMAPFILEHEADER* fHead = (BITMAPFILEHEADER*)dataBuffer; fHead->bfType = 0x424D; // "BM" fHead->bfSize = SwapDWORD(dataLen + headerSize); fHead->bfReserved1 = 0; fHead->bfReserved2 = 0; fHead->bfOffBits = SwapDWORD(headerSize); // Create our info header BITMAPINFOHEADER* iHead = (BITMAPINFOHEADER*)(fHead + 1); ZeroMemory(iHead, sizeof(BITMAPINFOHEADER)); iHead->biSize = SwapDWORD(sizeof(BITMAPINFOHEADER)); iHead->biWidth = SwapDWORD(desc.Width); iHead->biHeight = SwapDWORD(desc.Height); iHead->biPlanes = SwapWORD(1); iHead->biBitCount = SwapWORD(32); iHead->biSizeImage = SwapDWORD(dataLen); // Copy over our raw (BGRA) DWORD* rawPtr = (DWORD*)(iHead + 1); for(int y = desc.Height - 1; y >= 0; y--) { for(DWORD x = 0; x < desc.Width; x++) { DWORD cp = (y * lock.Pitch) + (x * 4); DWORD * temp = (DWORD*)(address + (cp / 4)); *rawPtr = SwapDWORD(*temp); rawPtr++; } } // Unlock our texture pTexture->UnlockRect(0); // End Profiling our Data Modification Code timeStop = m_Timer.GetAbsoluteTime(); DebugMsg("ConvertImageToBMP", "Data Modification took %4.2f seconds to complete", (float)(timeStop - timeStart)); // Begin Profiling our Memory Copy Code timeStart = m_Timer.GetAbsoluteTime(); // Copy our completed data to our new buffer image_out.Add(dataBuffer, (headerSize + dataLen)); // End Profiling our Memory Copy Code timeStop = m_Timer.GetAbsoluteTime(); DebugMsg("ConvertImageToBMP", "Image Memory Copy took %4.2f seconds to complete", (float)(timeStop - timeStart)); } else { DebugMsg("ConvertImageInMemoryToBMPBuffer", "Conversion To BMP From Memory Failed. [%X]", retVal); } return retVal; }