//////////////////////////////////////////////////////////////// Function name : DataSender::SendBuffer Return type : bool Argument : char* String Description : Send a string of data to a socket. The outgoing format on the socket will be a 4-byte length followed by the string of characters. ////////////////////////////////////////////////////////////////
| 44 | // |
| 45 | ///////////////////////////////////////////////////////////////////// |
| 46 | bool DataSender::ReceiveString(std::string* pString) |
| 47 | { |
| 48 | uint32_t netLen = 0 ; |
| 49 | |
| 50 | // Make sure we return an empty string if we get an error |
| 51 | pString->clear() ; |
| 52 | |
| 53 | // Read the length of the string (the first 4 bytes) |
| 54 | bool ok = ReceiveBuffer((char*)&netLen, sizeof(netLen)) ; |
| 55 | |
| 56 | // Convert the length from network byte ordering back to our local order |
| 57 | uint32_t len = ntohl(netLen) ; |
| 58 | |
| 59 | // If we got a zero length string. |
| 60 | if (len == 0) |
| 61 | { |
| 62 | return ok ; |
| 63 | } |
| 64 | |
| 65 | // Create the buffer into which we'll receive data |
| 66 | char* buffer = new char[len + 1] ; |
| 67 | |
| 68 | // Receive the string |
| 69 | ok = ok && ReceiveBuffer(buffer, len) ; |
| 70 | |
| 71 | // Make it null terminated |
| 72 | buffer[len] = 0 ; |
| 73 | |
| 74 | // Return the result in the string |
| 75 | if (ok) |
| 76 | { |
| 77 | pString->assign(buffer) ; |
| 78 | } |
| 79 | |
| 80 | // Release our temp buffer |
| 81 | delete [] buffer ; |
| 82 | |
| 83 | return ok ; |
| 84 | } |
| 85 | |
| 86 | void DataSender::Close() |
| 87 | { |