diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-14 23:38:42 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-14 23:38:42 +0000 |
commit | caea63c835228efb23edf8755c60012cd2dfe1ce (patch) | |
tree | 1dbc0177cfbcfcfdd58f8abc928e64ffda15aa16 /src | |
parent | 0841923d1ebd1283e5b1d2cff48ed34f860f288b (diff) | |
download | manaserv-caea63c835228efb23edf8755c60012cd2dfe1ce.tar.gz manaserv-caea63c835228efb23edf8755c60012cd2dfe1ce.tar.bz2 manaserv-caea63c835228efb23edf8755c60012cd2dfe1ce.tar.xz manaserv-caea63c835228efb23edf8755c60012cd2dfe1ce.zip |
Removed Packet class and the UNKNOWN gender.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/accounthandler.cpp | 26 | ||||
-rw-r--r-- | src/chathandler.cpp | 19 | ||||
-rw-r--r-- | src/client.cpp | 6 | ||||
-rw-r--r-- | src/connectionhandler.cpp | 10 | ||||
-rw-r--r-- | src/connectionhandler.h | 8 | ||||
-rw-r--r-- | src/defines.h | 3 | ||||
-rw-r--r-- | src/gamehandler.cpp | 11 | ||||
-rw-r--r-- | src/messagein.cpp | 79 | ||||
-rw-r--r-- | src/messagein.h | 9 | ||||
-rw-r--r-- | src/messageout.cpp | 31 | ||||
-rw-r--r-- | src/messageout.h | 16 | ||||
-rw-r--r-- | src/netcomputer.cpp | 8 | ||||
-rw-r--r-- | src/netcomputer.h | 12 | ||||
-rw-r--r-- | src/packet.cpp | 40 | ||||
-rw-r--r-- | src/packet.h | 47 |
16 files changed, 89 insertions, 242 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5aedb6b2..ffdf3f8a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,9 +9,7 @@ tmwclient_SOURCES = client.cpp \ messageout.h \ messageout.cpp \ messagein.h \ - messagein.cpp \ - packet.h \ - packet.cpp + messagein.cpp tmwserv_SOURCES = main.cpp \ configuration.h \ @@ -52,8 +50,6 @@ tmwserv_SOURCES = main.cpp \ messageout.cpp \ netcomputer.h \ netcomputer.cpp \ - packet.h \ - packet.cpp \ skill.h \ skill.cpp \ resourcemanager.cpp \ diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index c78786c6..a4e9c7ba 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -284,8 +284,8 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) store.flush(computer.getAccount()); // flush changes result.writeByte(ERRMSG_OK); - computer.send(result.getPacket()); - + computer.send(result); + // Send new characters infos back to client MessageOut charInfo(APMSG_CHAR_INFO); int slot = chars.size() - 1; @@ -298,7 +298,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) charInfo.writeShort(chars[slot]->getMoney()); for (int j = 0; j < NB_RSTAT; ++j) charInfo.writeShort(chars[slot]->getRawStat(j)); - computer.send(charInfo.getPacket()); + computer.send(charInfo); return; } break; @@ -401,7 +401,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // return result if (result.getDataSize() > 0) - computer.send(result.getPacket()); + computer.send(result); } void @@ -457,34 +457,34 @@ AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg) computer.setAccount(acc); reply.writeByte(ERRMSG_OK); - computer.send(reply.getPacket()); + computer.send(reply); // Return information about available characters Players &chars = computer.getAccount()->getCharacters(); LOG_INFO(username << "'s account has " << chars.size() << " character(s).", 1); - + // Send characters list for (unsigned int i = 0; i < chars.size(); i++) { MessageOut charInfo(APMSG_CHAR_INFO); charInfo.writeByte(i); // Slot charInfo.writeString(chars[i]->getName()); - charInfo.writeByte(unsigned(short(chars[i]->getGender()))); + charInfo.writeByte((unsigned char) chars[i]->getGender()); charInfo.writeByte(chars[i]->getHairStyle()); charInfo.writeByte(chars[i]->getHairColor()); charInfo.writeByte(chars[i]->getLevel()); charInfo.writeShort(chars[i]->getMoney()); for (int j = 0; j < NB_RSTAT; ++j) charInfo.writeShort(chars[i]->getRawStat(j)); - computer.send(charInfo.getPacket()); + computer.send(charInfo); } return; } } - computer.send(reply.getPacket()); + computer.send(reply); } void @@ -504,7 +504,7 @@ AccountHandler::handleLogoutMessage(AccountClient &computer, MessageIn &msg) reply.writeByte(ERRMSG_OK); } - computer.send(reply.getPacket()); + computer.send(reply); } void @@ -584,7 +584,7 @@ AccountHandler::handleRegisterMessage(AccountClient &computer, MessageIn &msg) } } - computer.send(reply.getPacket()); + computer.send(reply); } void @@ -634,7 +634,7 @@ AccountHandler::handleUnregisterMessage(AccountClient &computer, } } - computer.send(reply.getPacket()); + computer.send(reply); } void @@ -677,5 +677,5 @@ AccountHandler::handlePasswordChangeMessage(AccountClient &computer, reply.writeByte(ERRMSG_OK); } - computer.send(reply.getPacket()); + computer.send(reply); } diff --git a/src/chathandler.cpp b/src/chathandler.cpp index 163d6208..7e322654 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -28,7 +28,6 @@ #include "messagein.h" #include "messageout.h" #include "netcomputer.h" -#include "packet.h" #include "utils/logger.h" #include "utils/stringfilter.h" @@ -77,7 +76,7 @@ void registerChatClient(std::string const &token, std::string const &name, int l MessageOut result; result.writeShort(CPMSG_CONNECT_RESPONSE); result.writeByte(ERRMSG_OK); - computer->send(result.getPacket()); + computer->send(result); } else { @@ -148,7 +147,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) pendingLogins.erase(i); result.writeShort(CPMSG_CONNECT_RESPONSE); result.writeByte(ERRMSG_OK); - computer.send(result.getPacket()); + computer.send(result); return; } @@ -420,8 +419,8 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) break; } - if (result.getPacket()->length > 0) - computer.send(result.getPacket()); + if (result.getDataSize() > 0) + computer.send(result); } void ChatHandler::handleCommand(ChatClient &computer, std::string const &command) @@ -430,7 +429,7 @@ void ChatHandler::handleCommand(ChatClient &computer, std::string const &command MessageOut result; result.writeShort(CPMSG_ERROR); result.writeByte(CHAT_UNHANDLED_COMMAND); - computer.send(result.getPacket()); + computer.send(result); } void ChatHandler::warnPlayerAboutBadWords(ChatClient &computer) @@ -439,7 +438,7 @@ void ChatHandler::warnPlayerAboutBadWords(ChatClient &computer) MessageOut result; result.writeShort(CPMSG_ERROR); result.writeByte(CHAT_USING_BAD_WORDS); // The Channel - computer.send(result.getPacket()); + computer.send(result); LOG_INFO(computer.characterName << " says bad words.", 2); } @@ -460,7 +459,7 @@ void ChatHandler::announce(ChatClient &computer, std::string const &text) { result.writeShort(CPMSG_ERROR); result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); - computer.send(result.getPacket()); + computer.send(result); LOG_INFO(computer.characterName << " couldn't make an announcement due to insufficient rights.", 2); } @@ -478,7 +477,7 @@ void ChatHandler::sayToPlayer(ChatClient &computer, std::string const &playerNam i != i_end; ++i) { if (static_cast< ChatClient * >(*i)->characterName == playerName) { - (*i)->send(result.getPacket()); + (*i)->send(result); break; } } @@ -519,7 +518,7 @@ void ChatHandler::sendInChannel(short channelId, MessageOut &msg) j = std::find(users.begin(), j_end, static_cast< ChatClient * >(*i)->characterName); if (j != j_end) { - (*i)->send(msg.getPacket()); + (*i)->send(msg); } } } diff --git a/src/client.cpp b/src/client.cpp index 9074a598..d513e146 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -26,7 +26,6 @@ #include "defines.h" #include "messageout.h" #include "messagein.h" -#include "packet.h" #if defined WIN32 #include "../testclient_private.h" @@ -349,7 +348,7 @@ int main(int argc, char *argv[]) } // end if process_enet: - while (enet_host_service(client, &event, 1000)) { + while (enet_host_service(client, &event, 2000)) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: printf("Connected to server\n"); @@ -398,8 +397,7 @@ void parsePacket(char *data, int recvLength) { // Response handling // Transforming it into a MessageIn object if (recvLength >= 2) { - Packet *packet = new Packet(data, recvLength); - MessageIn msg(packet); // (MessageIn frees packet) + MessageIn msg(data, recvLength); switch (msg.getId()) { case APMSG_REGISTER_RESPONSE: diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index df31d385..2a89c00f 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -26,7 +26,6 @@ #include "messagein.h" #include "messageout.h" #include "netcomputer.h" -#include "packet.h" #include "utils/logger.h" @@ -142,9 +141,8 @@ void ConnectionHandler::process() // Make sure that the packet is big enough (> short) if (event.packet->dataLength >= 2) { - Packet *packet = new Packet((char *)event.packet->data, - event.packet->dataLength); - MessageIn msg(packet); // (MessageIn frees packet) + MessageIn msg((char *)event.packet->data, + event.packet->dataLength); processMessage(comp, msg); } else { LOG_ERROR("Message too short from " << ipaddr, 0); @@ -169,11 +167,11 @@ void ConnectionHandler::process() } } -void ConnectionHandler::sendToEveryone(MessageOut &msg) +void ConnectionHandler::sendToEveryone(const MessageOut &msg) { for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); i != i_end; ++i) { - (*i)->send(msg.getPacket()); + (*i)->send(msg); } } diff --git a/src/connectionhandler.h b/src/connectionhandler.h index f43b1237..7f3355db 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -87,7 +87,7 @@ class ConnectionHandler /** * Send packet to every client, used for announcements. */ - void sendToEveryone(MessageOut &); + void sendToEveryone(const MessageOut &msg); /** * Return the number of connected clients. @@ -95,15 +95,15 @@ class ConnectionHandler unsigned int getClientNumber(); private: - ENetAddress address; /**< Includes the port to listen to. */ - ENetHost *host; /**< The host that listen for connections. */ + ENetAddress address; /**< Includes the port to listen to. */ + ENetHost *host; /**< The host that listen for connections. */ protected: /** * Called when a computer connects to the server. Initialize * an object derived of NetComputer. */ - virtual NetComputer *computerConnected(ENetPeer *) = 0; + virtual NetComputer *computerConnected(ENetPeer *peer) = 0; /** * Called when a computer reconnects to the server. diff --git a/src/defines.h b/src/defines.h index e3a85e60..a9cd8a54 100644 --- a/src/defines.h +++ b/src/defines.h @@ -50,8 +50,7 @@ typedef enum { */ typedef enum { GENDER_MALE, - GENDER_FEMALE, - GENDER_UNKNOWN + GENDER_FEMALE } Genders; // Network related diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 6dfab787..af08db46 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -31,7 +31,6 @@ #include "messagein.h" #include "messageout.h" #include "netcomputer.h" -#include "packet.h" #include "state.h" #include "utils/logger.h" @@ -76,7 +75,7 @@ void registerGameClient(std::string const &token, PlayerPtr ch) MessageOut result; result.writeShort(GPMSG_CONNECT_RESPONSE); result.writeByte(ERRMSG_OK); - computer->send(result.getPacket()); + computer->send(result); } else { @@ -152,7 +151,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) pendingLogins.erase(i); result.writeShort(GPMSG_CONNECT_RESPONSE); result.writeByte(ERRMSG_OK); - computer.send(result.getPacket()); + computer.send(result); return; } @@ -220,7 +219,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) } if (result.getDataSize() > 0) - computer.send(result.getPacket()); + computer.send(result); } void GameHandler::sayAround(GameClient &computer, std::string const &text) @@ -250,7 +249,7 @@ void GameHandler::sayAround(GameClient &computer, std::string const &text) if (areAround(listenerXY.first, listenerXY.second, speakerXY.first, speakerXY.second)) { - (*i)->send(msg.getPacket()); + (*i)->send(msg); } } } @@ -262,7 +261,7 @@ void GameHandler::sendTo(PlayerPtr beingPtr, MessageOut &msg) PlayerPtr clientChar = static_cast<GameClient *>(*i)->getCharacter(); if (clientChar.get() == beingPtr.get()) { - (*i)->send(msg.getPacket()); + (*i)->send(msg); break; } } diff --git a/src/messagein.cpp b/src/messagein.cpp index 54d74809..e0a4b27b 100644 --- a/src/messagein.cpp +++ b/src/messagein.cpp @@ -27,10 +27,9 @@ #include <enet/enet.h> -#include "packet.h" - -MessageIn::MessageIn(Packet *packet): - mPacket(packet), +MessageIn::MessageIn(const char *data, int length): + mData(data), + mLength(length), mPos(0) { // Read the message ID @@ -39,78 +38,64 @@ MessageIn::MessageIn(Packet *packet): MessageIn::~MessageIn() { - delete mPacket; // To be removed if the packet is deleted elsewhere. } char MessageIn::readByte() { char value = -1; - if (mPacket) + if (mPos < mLength) { - if (mPos < mPacket->length) - { - value = mPacket->data[mPos]; - } - mPos += 1; + value = mData[mPos]; } + mPos += 1; return value; } short MessageIn::readShort() { short value = -1; - if (mPacket) + if (mPos + 2 <= mLength) { - if (mPos + 2 <= mPacket->length) - { - uint16_t t; - memcpy(&t, mPacket->data + mPos, 2); - value = ENET_NET_TO_HOST_16(t); - } - mPos += 2; + uint16_t t; + memcpy(&t, mData + mPos, 2); + value = ENET_NET_TO_HOST_16(t); } + mPos += 2; return value; } long MessageIn::readLong() { long value = -1; - if (mPacket) + if (mPos + 4 <= mLength) { - if (mPos + 4 <= mPacket->length) - { - uint32_t t; - memcpy(&t, mPacket->data + mPos, 4); - value = ENET_NET_TO_HOST_32(t); - } - mPos += 4; + uint32_t t; + memcpy(&t, mData + mPos, 4); + value = ENET_NET_TO_HOST_32(t); } + mPos += 4; return value; } std::string MessageIn::readString(int length) { - if (mPacket) - { - // Get string length - if (length < 0) { - length = readShort(); - } - - // Make sure the string isn't erroneous - if (length < 0 || mPos + length > mPacket->length) { - mPos = mPacket->length + 1; - return ""; - } + // Get string length + if (length < 0) { + length = readShort(); + } - // Read the string - char const *stringBeg = mPacket->data + mPos; - char const *stringEnd = (char const *)memchr(stringBeg, '\0', length); - std::string readString(stringBeg, - stringEnd ? stringEnd - stringBeg : length); - mPos += length; - return readString; + // Make sure the string isn't erroneous + if (length < 0 || mPos + length > mLength) { + mPos = mLength + 1; + return ""; } - return ""; + // Read the string + char const *stringBeg = mData + mPos; + char const *stringEnd = (char const *)memchr(stringBeg, '\0', length); + std::string readString(stringBeg, + stringEnd ? stringEnd - stringBeg : length); + mPos += length; + + return readString; } diff --git a/src/messagein.h b/src/messagein.h index 6545c8c4..e80f3f4f 100644 --- a/src/messagein.h +++ b/src/messagein.h @@ -37,7 +37,7 @@ class MessageIn /** * Constructor. */ - MessageIn(Packet *packet); + MessageIn(const char *data, int length); /** * Destructor. @@ -58,12 +58,13 @@ class MessageIn std::string readString(int length = -1); private: - Packet *mPacket; /**< The packet being processed. */ + const char *mData; /**< Packet data */ + unsigned int mLength; /**< Length of data in bytes */ short mId; /**< The message ID. */ /** - * Actual position in the packet. From 0 to packet->length. - * A value bigger than packet->length means EOP was reached when reading it. + * 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 639038a0..d9578577 100644 --- a/src/messageout.cpp +++ b/src/messageout.cpp @@ -27,10 +27,7 @@ #include <enet/enet.h> -#include "packet.h" - MessageOut::MessageOut(): - mPacket(0), mData(0), mDataSize(0), mPos(0) @@ -38,7 +35,6 @@ MessageOut::MessageOut(): } MessageOut::MessageOut(short id): - mPacket(0), mData(0), mDataSize(0), mPos(0) @@ -48,10 +44,6 @@ MessageOut::MessageOut(short id): MessageOut::~MessageOut() { - if (mPacket) { - delete mPacket; - } - if (mData) { free(mData); } @@ -116,26 +108,3 @@ MessageOut::writeString(const std::string &string, int length) } mPos += length; } - -const Packet* -MessageOut::getPacket() -{ - if (!mPacket) - { - mPacket = new Packet(mData, mDataSize); - } - - return mPacket; -} - -char* -MessageOut::getData() -{ - return mData; -} - -unsigned int -MessageOut::getDataSize() -{ - return mDataSize; -} diff --git a/src/messageout.h b/src/messageout.h index 41a6b983..dac20f71 100644 --- a/src/messageout.h +++ b/src/messageout.h @@ -26,8 +26,6 @@ #include <iosfwd> -class Packet; - /** * Used for building an outgoing message. */ @@ -60,21 +58,16 @@ class MessageOut void writeString(const std::string &string, int length = -1); /** - * Returns an instance of Packet derived from the written data. Use for - * sending the packet. No more writing to the packet may be done after - * a call to this method. - */ - const Packet *getPacket(); - - /** * Returns the content of the message. */ - char *getData(); + char* + getData() const { return mData; } /** * Returns the length of the data. */ - unsigned int getDataSize(); + unsigned int + getDataSize() const { return mDataSize; } private: /** @@ -86,7 +79,6 @@ class MessageOut */ void expand(size_t size); - Packet *mPacket; /**< Created packet. */ char *mData; /**< Data building up. */ unsigned int mDataSize; /**< Size of data. */ unsigned int mPos; /**< Position in the data. */ diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp index b326f921..bce31788 100644 --- a/src/netcomputer.cpp +++ b/src/netcomputer.cpp @@ -25,7 +25,7 @@ #include "chatchannelmanager.h" #include "connectionhandler.h" -#include "packet.h" +#include "messageout.h" #include "state.h" #include "utils/logger.h" @@ -47,12 +47,12 @@ void NetComputer::disconnect(const std::string &reason) enet_peer_disconnect(mPeer, 0); } -void NetComputer::send(const Packet *p) +void NetComputer::send(const MessageOut &msg) { - LOG_INFO("Sending packet of length " << p->length, 2); + LOG_INFO("Sending packet of length " << msg.getDataSize(), 2); // Create a reliable packet. - ENetPacket *packet = enet_packet_create(p->data, p->length, + ENetPacket *packet = enet_packet_create(msg.getData(), msg.getDataSize(), ENET_PACKET_FLAG_RELIABLE); // Send the packet to the peer over channel id 0. diff --git a/src/netcomputer.h b/src/netcomputer.h index e1e328dd..49dfdc93 100644 --- a/src/netcomputer.h +++ b/src/netcomputer.h @@ -34,8 +34,7 @@ // Forward declaration class ConnectionHandler; -class ClientConnectionHandler; -class Packet; +class MessageOut; /** * This class represents a known computer on the network. For example a @@ -67,14 +66,13 @@ class NetComputer disconnect(const std::string &reason); /** - * Queues (FIFO) a packet for sending to a client. + * Queues a message for sending to a client. * - * Note: When we'd want to allow communication through UDP, we could - * introduce the reliable argument, which would cause a UDP message - * to be sent when set to false. + * TODO: Add support for reliable and maybe also channels now that we + * TODO: have ENet. */ void - send(const Packet *p); + send(const MessageOut &msg); //void send(Packet *p, bool reliable = true); private: diff --git a/src/packet.cpp b/src/packet.cpp deleted file mode 100644 index 1994ef98..00000000 --- a/src/packet.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#include "packet.h" - -#include <cstring> - -Packet::Packet(const char *data, int length): - length(length) -{ - // Create a copy of the data - this->data = new char[length]; - memcpy(this->data, data, length); -} - -Packet::~Packet() -{ - // Clean up the data - delete[] data; -} diff --git a/src/packet.h b/src/packet.h deleted file mode 100644 index 888bfbce..00000000 --- a/src/packet.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#ifndef _TMWSERV_PACKET_H_ -#define _TMWSERV_PACKET_H_ - -/** - * A packet wraps a certain amount of bytes for sending and receiving. - */ -class Packet -{ - public: - /** - * Constructor. - */ - Packet(const char *data, int length); - - /** - * Destructor. - */ - ~Packet(); - - char *data; /**< Packet data */ - unsigned int length; /**< Length of data in bytes */ -}; - -#endif |