diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-07-16 18:37:38 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-07-16 18:37:38 +0000 |
commit | 49bc551cc81f23c6e5243e1ddb0ddf6d0159c544 (patch) | |
tree | 7cb6958a430334830c6fa2ef14eafb0051ecc54b /src | |
parent | b38bedf85a7582d20a1fee993cc6078f9463b40a (diff) | |
download | manaserv-49bc551cc81f23c6e5243e1ddb0ddf6d0159c544.tar.gz manaserv-49bc551cc81f23c6e5243e1ddb0ddf6d0159c544.tar.bz2 manaserv-49bc551cc81f23c6e5243e1ddb0ddf6d0159c544.tar.xz manaserv-49bc551cc81f23c6e5243e1ddb0ddf6d0159c544.zip |
Removed a lot of tabs used for indenting (please don't do that...), redid
MessageOut a bit so that it uses SDL_net, writes strings correctly and doesn't
rely on Packet to be expandable. Also specified message IDs explicitly.
Diffstat (limited to 'src')
-rw-r--r-- | src/accounthandler.cpp | 89 | ||||
-rw-r--r-- | src/chathandler.cpp | 26 | ||||
-rw-r--r-- | src/client.cpp | 4 | ||||
-rw-r--r-- | src/connectionhandler.cpp | 95 | ||||
-rw-r--r-- | src/dalstorage.cpp | 3 | ||||
-rw-r--r-- | src/defines.h | 65 | ||||
-rw-r--r-- | src/messagehandler.h | 5 | ||||
-rw-r--r-- | src/messagein.cpp | 72 | ||||
-rw-r--r-- | src/messagein.h | 13 | ||||
-rw-r--r-- | src/messageout.cpp | 79 | ||||
-rw-r--r-- | src/messageout.h | 20 | ||||
-rw-r--r-- | src/packet.cpp | 13 | ||||
-rw-r--r-- | src/packet.h | 11 |
13 files changed, 268 insertions, 227 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 84f1c553..602d7f49 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -42,56 +42,57 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) Storage &store = Storage::instance("tmw"); - char type = message.readByte(); + int type = message.readByte(); switch(type) { case CMSG_LOGIN: - { - std::string username = message.readString(); - std::string password = message.readString(); - - // see if the account exists - Account *acc = store.getAccount(username); - if (!acc) { - // account doesn't exist -- send error to client - std::cout << "Account does not exist " << username << std::endl; - return; - } - - if (acc->getPassword() != password) { - // bad password -- send error to client - std::cout << "Bad password for " << username << std::endl; - return; - } - - // Login OK! (send an OK message or something) - std::cout << "Login OK by " << username << std::endl; - } break; + { + std::string username = message.readString(); + std::string password = message.readString(); + + // see if the account exists + Account *acc = store.getAccount(username); + if (!acc) { + // account doesn't exist -- send error to client + std::cout << "Account does not exist " << username + << std::endl; + return; + } + + if (acc->getPassword() != password) { + // bad password -- send error to client + std::cout << "Bad password for " << username << std::endl; + return; + } + + // Login OK! (send an OK message or something) + std::cout << "Login OK by " << username << std::endl; + } break; case CMSG_REGISTER: - { - std::string username = message.readString(); - std::string password = message.readString(); - std::string email = message.readString(); + { + std::string username = message.readString(); + std::string password = message.readString(); + std::string email = message.readString(); - AccountPtr acc(new Account(username, password, email)); - store.addAccount(acc); + AccountPtr acc(new Account(username, password, email)); + store.addAccount(acc); - std::cout << "Account registered" << std::endl; - store.flush(); // flush changes - } break; + std::cout << "Account registered" << std::endl; + store.flush(); // flush changes + } break; case CMSG_CHAR_CREATE: - { - std::string name = message.readString(); - // TODO: Finish this message type (should a player customize stats - // slightly?) - } break; + { + std::string name = message.readString(); + // TODO: Finish this message type (should a player customize + // stats slightly?) + } break; default: - std::cout << "Invalid message type" << std::endl; - break; + std::cout << "Invalid message type" << std::endl; + break; } debugCatch(result); @@ -118,13 +119,13 @@ int AccountHandler::loginMessage(NetComputer &computer, MessageIn &message) // HANDLE_STATUS_BANNED // RETURN TMW_ACCOUNTERROR_ALREADYASSIGNED if: the handle is already // assigned - + // Get the character within that handle that the player is requesting // RETURN TMW_ACCOUNTERROR_CHARNOTFOUND if: character not found - + // Assign the player to that character // RETURN TMW_ACCOUNTERROR_ASSIGNFAILED if: assignment not successful - + // return TMW_SUCCESS -- successful exit return TMW_SUCCESS; } @@ -143,9 +144,9 @@ AccountHandler::assignAccount(NetComputer &computer, tmwserv::Account *account) { // RETURN TMW_ACCOUNTERROR_ASSIGNFAILED if: the account was accessed before // being initalized. - + // Assign the handle - - + + return TMW_SUCCESS; } diff --git a/src/chathandler.cpp b/src/chathandler.cpp index 4d901f19..f7505894 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -27,24 +27,26 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) { - char type = message.readByte(); + int type = message.readShort(); switch (type) { case CMSG_SAY: - { - std::string text = message.readString(); - short channel = message.readShort(); - std::cout << "Say (" << channel << "): " << text << std::endl; - } break; + { + std::string text = message.readString(); + short channel = message.readShort(); + std::cout << "Say (" << channel << "): " << text << std::endl; + } + break; case CMSG_ANNOUNCE: - { - std::string text = message.readString(); - std::cout << "Announce: " << text << std::endl; - } break; + { + std::string text = message.readString(); + std::cout << "Announce: " << text << std::endl; + } + break; default: - std::cout << "Invalid message type" << std::endl; - break; + std::cout << "Invalid message type" << std::endl; + break; } } diff --git a/src/client.cpp b/src/client.cpp index e3b010b2..c9e4ed4e 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -62,11 +62,11 @@ int main(int argc, char *argv[]) msg.writeString("Hello World!"); msg.writeShort(0); */ - + // message hex /* for (unsigned int i = 0; i < msg.getPacket()->length; i++) { - printf("%x ", msg.getPacket()->data[i]); + printf("%x ", msg.getPacket()->data[i]); } printf("\n"); */ diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index 2b3b64d0..2a5d6c44 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -45,24 +45,27 @@ * This function may seem unoptimized, except it is this way to allow for * thread safety. */ -std::vector<std::string> stringSplit(const std::string &str, - const std::string &split) +std::vector<std::string> +stringSplit(const std::string &str, + const std::string &split) { std::vector<std::string> result; // temporary result unsigned int i; unsigned int last = 0; // iterate through string - for (i = 0; i < str.length(); i++) { - if (str.compare(i, split.length(), split.c_str(), split.length()) == 0) { - result.push_back(str.substr(last, i - last)); - last = i + 1; - } + for (i = 0; i < str.length(); i++) + { + if (str.compare(i, split.length(), split.c_str(), split.length()) == 0) + { + result.push_back(str.substr(last, i - last)); + last = i + 1; + } } - + // add remainder of string if (last < str.length()) { - result.push_back(str.substr(last, str.length())); + result.push_back(str.substr(last, str.length())); } return result; @@ -71,7 +74,8 @@ std::vector<std::string> stringSplit(const std::string &str, /** * Convert a IP4 address into its string representation */ -std::string ip4ToString(unsigned int ip4addr) +std::string +ip4ToString(unsigned int ip4addr) { std::stringstream ss; ss << (ip4addr & 0x000000ff) << "." @@ -92,7 +96,8 @@ ConnectionHandler::ConnectionHandler() { } -void ConnectionHandler::startListen(ListenThreadData *ltd) +void +ConnectionHandler::startListen(ListenThreadData *ltd) { // Allocate a socket set SDLNet_SocketSet set = SDLNet_AllocSocketSet(MAX_CLIENTS); @@ -158,46 +163,50 @@ void ConnectionHandler::startListen(ListenThreadData *ltd) // client //buffer[result] = 0; //LOG_INFO("Received: " << buffer << ", Length: " - // << result); + // << result); - LOG_INFO("Received length: " - << result); + LOG_INFO("Received length: " + << result); #ifdef SCRIPT_SUPPORT - // this could be good if you wanted to extend the - // server protocol using a scripting language. This - // could be attained by using allowing scripts to - // "hook" certain messages. + // this could be good if you wanted to extend the + // server protocol using a scripting language. This + // could be attained by using allowing scripts to + // "hook" certain messages. //script->message(buffer); #endif - // if the scripting subsystem didn't hook the message - // it will be handled by the default message handler. - - // convert the client IP address to string representation - std::string ipaddr = ip4ToString(SDLNet_TCP_GetPeerAddress(s)->host); - - // generate packet - Packet *packet = new Packet(buffer, result); - MessageIn msg(packet); // (MessageIn frees packet) - - // make sure that the packet is big enough - if (result >= 4) { - unsigned int messageType = (unsigned int)*packet->data; - if (handlers.find(messageType) != handlers.end()) { - // send message to appropriate handler - handlers[messageType]->receiveMessage(*comp, msg); - } else { - // bad message (no registered handler) - LOG_ERROR("Unhandled message received from " - << ipaddr); - } - } else { - LOG_ERROR("Message too short from " << ipaddr); - } - // + // if the scripting subsystem didn't hook the message + // it will be handled by the default message handler. + + // convert the client IP address to string + // representation + std::string ipaddr = ip4ToString( + SDLNet_TCP_GetPeerAddress(s)->host); + + // generate packet + Packet *packet = new Packet(buffer, result); + MessageIn msg(packet); // (MessageIn frees packet) + + // make sure that the packet is big enough + if (result >= 4) { + unsigned int messageType = + (unsigned int)*packet->data; + if (handlers.find(messageType) != handlers.end()) + { + // send message to appropriate handler + handlers[messageType]->receiveMessage( + *comp, msg); + } else { + // bad message (no registered handler) + LOG_ERROR("Unhandled message received from " + << ipaddr); + } + } else { + LOG_ERROR("Message too short from " << ipaddr); + } } } diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 51619007..38e61397 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -370,10 +370,9 @@ DALStorage::createTable(const std::string& tblName, alreadyExists += tblName; alreadyExists += "' already exists"; #elif defined (POSTGRESQL_SUPPORT) - std::string alreadyExists("table "); + std::string alreadyExists("table "); alreadyExists += tblName; alreadyExists += " already exists"; - #else // SQLITE_SUPPORT std::string alreadyExists("table "); alreadyExists += tblName; diff --git a/src/defines.h b/src/defines.h index 2119b9d7..4b1db479 100644 --- a/src/defines.h +++ b/src/defines.h @@ -59,46 +59,47 @@ typedef enum { */ enum { // Login/Register - CMSG_REGISTER = 0, - CMSG_ENCRYPTED_REGISTER, - SMSG_REGISTER_RESPONSE, - CMSG_LOGIN, - CMSG_ENCRYPTED_LOGIN, - SMSG_LOGIN_ERROR, - SMSG_LOGIN_CONFIRM, - CMSG_CHAR_CREATE, - SMSG_CHAR_CREATE_RESPONSE, + CMSG_REGISTER = 0x0000, + CMSG_ENCRYPTED_REGISTER = 0x0001, + SMSG_REGISTER_RESPONSE = 0x0002, + CMSG_LOGIN = 0x0010, + CMSG_ENCRYPTED_LOGIN = 0x0011, + SMSG_LOGIN_ERROR = 0x0012, + SMSG_LOGIN_CONFIRM = 0x0013, + CMSG_CHAR_CREATE = 0x0020, + SMSG_CHAR_CREATE_RESPONSE = 0x0021, // Objects - SMSG_NEW_OBJECT = 20, - SMSG_REMOVE_OBJECT, - SMSG_CHANGE_OBJECT, - CMSG_PICKUP, - CMSG_USER_OBJECT, + SMSG_NEW_OBJECT = 0x0100, + SMSG_REMOVE_OBJECT = 0x0101, + SMSG_CHANGE_OBJECT = 0x0102, + CMSG_PICKUP = 0x0110, + CMSG_USE_OBJECT = 0x0120, // Beings - SMSG_NEW_BEING = 30, - SMSG_REMOVE_BEING, - SMSG_INVENTORY_UPD, - SMSG_EQUIPMENT_UPD, - SMSG_ATTACK, - SMSG_PATH, - CMSG_TARGET, - CMSG_WALK, - CMSG_START_TRADE, - CMSG_START_TALK, - CMSG_REQ_TRADE, + SMSG_NEW_BEING = 0x0200, + SMSG_REMOVE_BEING = 0x0201, + + SMSG_INVENTORY_UPD = 0x0210, + SMSG_EQUIPMENT_UPD = 0x0220, + SMSG_ATTACK = 0x0230, + SMSG_PATH = 0x0240, + CMSG_TARGET = 0x0250, + CMSG_WALK = 0x0260, + CMSG_START_TRADE = 0x0270, + CMSG_START_TALK = 0x0280, + CMSG_REQ_TRADE = 0x0290, // Items - CMSG_USE_ITEM = 40, - CMSG_EQUIP, + CMSG_USE_ITEM = 0x0300, + CMSG_EQUIP = 0x0301, // Chat - SMSG_CHAT = 60, - SMSG_SYSTEM, - SMSG_ANNOUNCEMENT, - CMSG_SAY, - CMSG_ANNOUNCE, + SMSG_CHAT = 0x0400, + SMSG_SYSTEM = 0x0401, + SMSG_ANNOUNCEMENT = 0x0402, + CMSG_SAY = 0x0410, + CMSG_ANNOUNCE = 0x0411, }; diff --git a/src/messagehandler.h b/src/messagehandler.h index f838c60a..ee4eec65 100644 --- a/src/messagehandler.h +++ b/src/messagehandler.h @@ -37,6 +37,11 @@ class MessageHandler { public: /** + * Destructor. + */ + virtual ~MessageHandler() {}; + + /** * Called when a message is received with a message ID that corresponds * to an ID this message handler registered to handle. */ diff --git a/src/messagein.cpp b/src/messagein.cpp index f2faaf2c..6a27f280 100644 --- a/src/messagein.cpp +++ b/src/messagein.cpp @@ -24,28 +24,28 @@ #include "messagein.h" #include <SDL_net.h> -MessageIn::MessageIn(Packet *p) +MessageIn::MessageIn(Packet *packet): + mPacket(packet), + mPos(0) { - pos = 0; - packet = p; } MessageIn::~MessageIn() { - delete packet; // To be removed if the packet is deleted elsewhere. + delete mPacket; // To be removed if the packet is deleted elsewhere. } char MessageIn::readByte() { - if (packet) // if Packet exists + if (mPacket) { - if ( pos < packet->length ) // if there is enough to read + if ( mPos < mPacket->length ) // if there is enough to read { - return packet->data[pos++]; + return mPacket->data[mPos++]; } else { - pos = packet->length - 1; + mPos = mPacket->length - 1; return 0; } } @@ -54,12 +54,12 @@ char MessageIn::readByte() short MessageIn::readShort() { - if (packet) // if Packet exists + if (mPacket) { - if ( (pos + sizeof(short)) <= packet->length ) + if ( (mPos + sizeof(short)) <= mPacket->length ) { - pos += sizeof(short); - return (short) SDLNet_Read16(&(packet->data[pos - sizeof(short)])); + mPos += sizeof(short); + return (short) SDLNet_Read16(&(mPacket->data[mPos - sizeof(short)])); } else { @@ -72,12 +72,12 @@ short MessageIn::readShort() long MessageIn::readLong() { - if (packet) // if Packet exists + if (mPacket) { - if ( (pos + sizeof(long)) <= packet->length ) + if ( (mPos + sizeof(long)) <= mPacket->length ) { - pos += sizeof(long); - return (long) SDLNet_Read32(&(packet->data[pos - sizeof(long)])); + mPos += sizeof(long); + return (long) SDLNet_Read32(&(mPacket->data[mPos - sizeof(long)])); } else { @@ -93,30 +93,28 @@ std::string MessageIn::readString(int length) int stringLength = 0; std::string readString = ""; - if (packet) { - // get string length - if (length < 0) { - stringLength = (short) packet->data[pos]; - pos += sizeof(short); - } else { - stringLength = length; - } + if (mPacket) + { + // Get string length + if (length < 0) { + stringLength = readShort(); + } else { + stringLength = length; + } - // make sure the string isn't erroneus - if (pos + length > packet->length) { - return ""; - } + // Make sure the string isn't erroneus + if (mPos + length > mPacket->length) { + return ""; + } - // read the string - char *tmpString = new char[stringLength + 1]; - memcpy(tmpString, (void*)&packet->data[pos], stringLength); - tmpString[stringLength] = 0; - pos += stringLength; + // 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; - } else { - return ""; + readString = tmpString; + delete tmpString; } return readString; diff --git a/src/messagein.h b/src/messagein.h index beb20310..0b99c40f 100644 --- a/src/messagein.h +++ b/src/messagein.h @@ -28,7 +28,7 @@ #include <string> /** - * A helper class to + * Used for parsing an incoming message. */ class MessageIn { @@ -36,7 +36,7 @@ class MessageIn /** * Constructor. */ - MessageIn(Packet *p); + MessageIn(Packet *packet); /** * Destructor. @@ -56,13 +56,12 @@ class MessageIn std::string readString(int length = -1); private: - Packet *packet; - + Packet *mPacket; + /** - * Actual Position in the packet - * From 0 to p->length - 1. + * Actual Position in the packet. From 0 to packet->length - 1. */ - unsigned int pos; + unsigned int mPos; }; #endif diff --git a/src/messageout.cpp b/src/messageout.cpp index 9c3ea453..a2faf33e 100644 --- a/src/messageout.cpp +++ b/src/messageout.cpp @@ -23,59 +23,92 @@ #include "messageout.h" #include <iostream> -#include <cstdlib> +#include <SDL_net.h> MessageOut::MessageOut(): - packet(0) + mPacket(0), + mData(0), + mDataSize(0), + mPos(0) { - packet = new Packet(NULL, 0); } MessageOut::~MessageOut() { - if (packet) { - delete packet; + if (mPacket) { + delete mPacket; } + + if (mData) { + free(mData); + } +} + +void MessageOut::expand(size_t bytes) +{ + mData = (char*)realloc(mData, bytes); + mDataSize = bytes; } void MessageOut::writeByte(char value) { - packet->expand(sizeof(char)); - packet->data[packet->length] = value; - packet->length += sizeof(char); + expand(mPos + sizeof(char)); + mData[mPos] = value; + mPos += sizeof(char); } void MessageOut::writeShort(short value) { - packet->expand(sizeof(short)); - memcpy(&packet->data[packet->length], (void*)&value, sizeof(short)); - packet->length += sizeof(short); + expand(mPos + sizeof(short)); + SDLNet_Write16(value, &mData[mPos]); + mPos += sizeof(short); } void MessageOut::writeLong(long value) { - packet->expand(sizeof(long)); - memcpy(&packet->data[packet->length], (void*)&value, sizeof(long)); - packet->length += sizeof(long); + expand(mPos + sizeof(long)); + SDLNet_Write32(value, &mData[mPos]); + mPos += sizeof(long); } void MessageOut::writeString(const std::string &string, int length) { + std::string toWrite = string; + if (length < 0) - length = string.length(); + { + // Write the length at the start if not fixed + writeShort(string.length()); + std::string toWrite = string; - packet->expand(length + sizeof(unsigned short)); + expand(mPos + string.length()); + } + else if (length < (int)string.length()) + { + // Make sure the length of the string is no longer than specified + toWrite = string.substr(0, length); + + expand(mPos + length); + } - // length prefix - memcpy(&packet->data[packet->length], (void*)&length, sizeof(unsigned short)); - // actual string - memcpy(&packet->data[packet->length + sizeof(unsigned short)], - (void*)string.c_str(), length); + // Write the actual string + memcpy(&mData[mPos], (void*)toWrite.c_str(), toWrite.length()); + mPos += toWrite.length(); - packet->length += length + sizeof(unsigned short); + // Pad remaining space with zeros + if (length > (int)toWrite.length()) + { + memset(&mData[mPos], '\0', length - toWrite.length()); + mPos += length - toWrite.length(); + } } const Packet *MessageOut::getPacket() { - return packet; + if (!mPacket) + { + mPacket = new Packet(mData, mDataSize); + } + + return mPacket; } diff --git a/src/messageout.h b/src/messageout.h index d48cb899..7d1da2f8 100644 --- a/src/messageout.h +++ b/src/messageout.h @@ -28,6 +28,9 @@ #include "packet.h" +/** + * Used for building an outgoing message. + */ class MessageOut { public: @@ -53,12 +56,25 @@ class MessageOut /** * Returns an instance of Packet derived from the written data. Use for - * sending the packet. + * sending the packet. No more writing to the packet may be done after + * a call to this method. */ const Packet *getPacket(); private: - Packet *packet; /**< Created packet. */ + /** + * Expand the packet data to be able to hold more data. + * + * NOTE: For performance enhancements this method could allocate extra + * memory in advance instead of expanding size every time more data is + * added. + */ + void expand(unsigned int size); + + Packet *mPacket; /**< Created packet. */ + char *mData; /**< Data building up. */ + unsigned int mDataSize; /**< Size of data. */ + unsigned int mPos; /**< Position in the data. */ }; #endif diff --git a/src/packet.cpp b/src/packet.cpp index 6664235f..133c72a8 100644 --- a/src/packet.cpp +++ b/src/packet.cpp @@ -31,21 +31,10 @@ Packet::Packet(const char *data, int length): // Create a copy of the data this->data = new char[length]; memcpy(this->data, data, length); - size = length; } Packet::~Packet() { // Clean up the data - if (data) - delete[] data; -} - -void Packet::expand(unsigned int bytes) -{ - char *newData = (char*)malloc(length + bytes); - memcpy(newData, (void*)data, length); - delete []data; - data = newData; - size += bytes; + delete[] data; } diff --git a/src/packet.h b/src/packet.h index 6b276808..4e1d48c5 100644 --- a/src/packet.h +++ b/src/packet.h @@ -26,9 +26,6 @@ /** * A packet wraps a certain amount of bytes for sending and receiving. - * - * NOTE: For performance enhancements this class could allocate extra memory - * in advance instead of expanding size every time more data is added. */ class Packet { @@ -43,16 +40,8 @@ class Packet */ ~Packet(); - /** - * Expand the packet size - */ - void expand(unsigned int bytes); - char *data; /**< Packet data */ unsigned int length; /**< Length of data in bytes */ - - private: - unsigned int size; /**< Size of data */ }; #endif |