diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.cpp | 13 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/messagein.cpp | 64 | ||||
-rw-r--r-- | src/messagein.h | 3 | ||||
-rw-r--r-- | src/messageout.cpp | 41 |
5 files changed, 54 insertions, 73 deletions
diff --git a/src/client.cpp b/src/client.cpp index d99690ca..ce134427 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -42,7 +42,7 @@ int main(int argc, char *argv[]) #include "../config.h" #endif std::cout << "The Mana World Test Client v" << PACKAGE_VERSION << std::endl; - + if (enet_initialize () != 0) { printf("An error occurred while initializing ENet.\n"); @@ -104,9 +104,9 @@ int main(int argc, char *argv[]) std::cout << "8) Create character" << std::endl; std::cout << "Choose your option: "; std::cin >> answer; - + MessageOut msg; - + switch (answer) { case 0: // Disconnection @@ -115,7 +115,7 @@ int main(int argc, char *argv[]) } exit = true; break; - + case 1: // Register msg.writeShort(CMSG_REGISTER); @@ -312,7 +312,7 @@ int main(int argc, char *argv[]) continue; break; } // end switch - + // Send prepared message if (!exit && connected) { ENetPacket *packet = enet_packet_create( @@ -322,7 +322,6 @@ int main(int argc, char *argv[]) // Send the packet to the peer over channel id 0. enet_peer_send(peer, 0, packet); } // end if - } // end if while (enet_host_service(client, &event, 1000)) { @@ -338,7 +337,7 @@ int main(int argc, char *argv[]) << " was received from " << event.peer->address.host << std::endl; - + parsePacket((char *)event.packet->data, event.packet->dataLength); diff --git a/src/main.cpp b/src/main.cpp index a4114072..22ac6a2a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -387,7 +387,7 @@ int main(int argc, char *argv[]) elapsedWorldTicks = worldTimer.poll(); if (elapsedWorldTicks > 0) { worldTime += elapsedWorldTicks; - + if (elapsedWorldTicks > 1) { LOG_WARN(elapsedWorldTicks -1 <<" World Tick(s) skipped because of insufficient time. please buy a faster machine ;-)", 0); @@ -407,7 +407,7 @@ int main(int argc, char *argv[]) // other tasks sched_yield(); }; - + /*ENetEvent netEvent; while (enet_host_service(server, &netEvent, 3000) > 0) @@ -421,7 +421,7 @@ int main(int argc, char *argv[]) netEvent.peer->data = (void *)"Client information"; break; - + case ENET_EVENT_TYPE_RECEIVE: { printf("A packet of length %u containing %s was received from %s on channel %u.\n", diff --git a/src/messagein.cpp b/src/messagein.cpp index c1736bbd..2559f7fb 100644 --- a/src/messagein.cpp +++ b/src/messagein.cpp @@ -44,37 +44,30 @@ MessageIn::~MessageIn() char MessageIn::readByte() { + char value = -1; if (mPacket) { - if ( mPos < mPacket->length ) // if there is enough to read + if (mPos < mPacket->length) { - return mPacket->data[mPos++]; - } - else - { - mPos = mPacket->length - 1; - return 0; + value = mPacket->data[mPos]; } + mPos += 1; } - return -1; + return value; } short MessageIn::readShort() { short value = -1; - if (mPacket) { - if ( (mPos + sizeof(short)) <= mPacket->length ) - { - value = ENET_NET_TO_HOST_16(*(short *)(mPacket->data + mPos)); - mPos += sizeof(short); - } - else + if (mPos + 2 <= mPacket->length) { - // We do nothing. And keep what's left to be read by a function - // that requires less length. + uint16_t t; + memcpy(&t, mPacket->data + mPos, 2); + value = ENET_NET_TO_HOST_16(t); } + mPos += 2; } return value; } @@ -82,51 +75,40 @@ short MessageIn::readShort() long MessageIn::readLong() { long value = -1; - if (mPacket) { - if ( (mPos + sizeof(long)) <= mPacket->length ) + if (mPos + 4 <= mPacket->length) { - value = ENET_NET_TO_HOST_32(*(long *)(mPacket->data + mPos)); - mPos += sizeof(long); - } - else - { - // We do nothing. And keep what's left to be read by a function - // that requires less length. + uint32_t t; + memcpy(&t, mPacket->data + mPos, 4); + value = ENET_NET_TO_HOST_32(t); } + mPos += 4; } return value; } std::string MessageIn::readString(int length) { - int stringLength = 0; - std::string readString = ""; - if (mPacket) { // Get string length if (length < 0) { - stringLength = readShort(); - } else { - stringLength = length; + length = readShort(); } // Make sure the string isn't erroneus - if (mPos + length > mPacket->length) { + if (length < 0 || mPos + length > mPacket->length) { + mPos = mPacket->length + 1; return ""; } // Read the string - char *tmpString = new char[stringLength + 1]; - memcpy(tmpString, (void*)&mPacket->data[mPos], stringLength); - tmpString[stringLength] = 0; - mPos += stringLength; - - readString = tmpString; - delete[] tmpString; + int stringLength = strnlen(mPacket->data + mPos, length); + std::string readString(mPacket->data + mPos, stringLength); + mPos += length; + return readString; } - return readString; + return ""; } diff --git a/src/messagein.h b/src/messagein.h index 5d6d2278..6545c8c4 100644 --- a/src/messagein.h +++ b/src/messagein.h @@ -62,7 +62,8 @@ class MessageIn short mId; /**< The message ID. */ /** - * Actual Position in the packet. From 0 to packet->length - 1. + * Actual position in the packet. From 0 to packet->length. + * A value bigger than packet->length means EOP was reached when reading it. */ unsigned int mPos; }; diff --git a/src/messageout.cpp b/src/messageout.cpp index 7fa6e1fd..e8336694 100644 --- a/src/messageout.cpp +++ b/src/messageout.cpp @@ -58,55 +58,54 @@ MessageOut::expand(size_t bytes) void MessageOut::writeByte(char value) { - expand(mPos + sizeof(char)); + expand(mPos + 1); mData[mPos] = value; - mPos += sizeof(char); + mPos += 1; } void MessageOut::writeShort(short value) { - expand(mPos + sizeof(short)); - (*(short *)&mData[mPos]) = ENET_HOST_TO_NET_16(value); - mPos += sizeof(short); + expand(mPos + 2); + uint16_t t = ENET_HOST_TO_NET_16(value); + memcpy(mData + mPos, &t, 2); + mPos += 2; } void MessageOut::writeLong(long value) { - expand(mPos + sizeof(long)); - (*(long *)&mData[mPos]) = ENET_HOST_TO_NET_32(value); - mPos += sizeof(long); + expand(mPos + 4); + uint32_t t = ENET_HOST_TO_NET_32(value); + memcpy(mData + mPos, &t, 4); + mPos += 4; } void MessageOut::writeString(const std::string &string, int length) { - std::string toWrite = string; - + int stringLength = string.length(); if (length < 0) { // Write the length at the start if not fixed - writeShort(string.length()); - expand(mPos + string.length()); + writeShort(stringLength); + length = stringLength; } - else if (length < (int)string.length()) + else if (length < stringLength) { // Make sure the length of the string is no longer than specified - toWrite = string.substr(0, length); - - expand(mPos + length); + stringLength = length; } + expand(mPos + length); // Write the actual string - memcpy(&mData[mPos], (void*)toWrite.c_str(), toWrite.length()); - mPos += toWrite.length(); + memcpy(mData + mPos, string.c_str(), stringLength); // Pad remaining space with zeros - if (length > (int)toWrite.length()) + if (length > stringLength) { - memset(&mData[mPos], '\0', length - toWrite.length()); - mPos += length - toWrite.length(); + memset(mData + mPos + stringLength, '\0', length - stringLength); } + mPos += length; } const Packet* |