diff options
| author | Aaron Marks <nymacro@gmail.com> | 2005-07-13 02:59:19 +0000 |
|---|---|---|
| committer | Aaron Marks <nymacro@gmail.com> | 2005-07-13 02:59:19 +0000 |
| commit | b426d3e5d269007f95360e40f906c5568a5abc99 (patch) | |
| tree | 0dd6a4ad8ed1db374b74b6ea01d0eacb7dd50abb /src/messagein.cpp | |
| parent | 960f631a2be601357424f9e6c5cdc64b68bff856 (diff) | |
| download | manaserv-b426d3e5d269007f95360e40f906c5568a5abc99.tar.gz manaserv-b426d3e5d269007f95360e40f906c5568a5abc99.tar.xz manaserv-b426d3e5d269007f95360e40f906c5568a5abc99.zip | |
Implemented MessageOut.
Rewrote MessageIn::readString - it was overly complex and didn't operate
correctly.
Update test client so it sends a login message.
Diffstat (limited to 'src/messagein.cpp')
| -rw-r--r-- | src/messagein.cpp | 75 |
1 files changed, 23 insertions, 52 deletions
diff --git a/src/messagein.cpp b/src/messagein.cpp index aae45c2..d916e71 100644 --- a/src/messagein.cpp +++ b/src/messagein.cpp @@ -90,58 +90,29 @@ long MessageIn::readLong() std::string MessageIn::readString(int length) { - if (packet) // If there a good packet - { - int stringLength = 0; - std::string readString = ""; + int stringLength = 0; + std::string readString = ""; - if ( length != -1 ) // if the length isn't specified, read it in the packet - { - // If the length of the packet is sufficient for us to read the length of the string - if ( (pos + sizeof(short)) <= packet->length ) - { - // We first read the length of the string given by the short before it. - stringLength = (short)SDLNet_Read16(&(packet->data[pos])); - pos += sizeof(short); - } - else - { - // Place us to the end as the size is a short - pos = packet->length; - } - } - else // if the length is specified - { - stringLength = length; - } + if (packet) { + // get string length + if (length < 0) { + stringLength = (short) packet->data[pos]; + pos += sizeof(short); + } else { + stringLength = length; + } - if ( stringLength > 0 ) - { - if ( (pos + stringLength) <= packet->length ) // If there's enough length to copy - { - char tempChar[stringLength+1]; - memcpy(&tempChar, packet->data, stringLength); - readString = tempChar; // We first copy the entire char array - // And then, we copy from the pos - // to the stringLength. - readString = readString.substr(pos,stringLength); - pos += stringLength; - } - else // if there isn't, copy till the end - { - char tempChar[packet->length +1]; - memcpy(&tempChar, packet->data, packet->length - 1); - readString = tempChar; // We first copy the entire char array - // And then, we copy from the pos - // to the length - 1 of the string. - readString = readString.substr(pos,packet->length - 1 - pos); - pos = packet->length - 1; - } - } - else - { - // The length of the string is zero ? - } - } // End if there is a packet - return ""; + // read the string + char *tmpString = new char[stringLength + 1]; + memcpy(tmpString, (void*)&packet->data[pos], stringLength); + tmpString[stringLength] = 0; + pos += stringLength; + + readString = tmpString; + delete tmpString; + } else { + return ""; + } + + return readString; } |
