diff options
author | Aaron Marks <nymacro@gmail.com> | 2005-07-17 03:18:31 +0000 |
---|---|---|
committer | Aaron Marks <nymacro@gmail.com> | 2005-07-17 03:18:31 +0000 |
commit | 98f41d64a9a1628dd132b356487415762b1409a8 (patch) | |
tree | c2e784d778c0285f589d555f6acb4395485bb406 /src | |
parent | 49bc551cc81f23c6e5243e1ddb0ddf6d0159c544 (diff) | |
download | manaserv-98f41d64a9a1628dd132b356487415762b1409a8.tar.gz manaserv-98f41d64a9a1628dd132b356487415762b1409a8.tar.bz2 manaserv-98f41d64a9a1628dd132b356487415762b1409a8.tar.xz manaserv-98f41d64a9a1628dd132b356487415762b1409a8.zip |
Added server->client communications.
Updated MessageHandler's to use short for message type.
Diffstat (limited to 'src')
-rw-r--r-- | src/accounthandler.cpp | 56 | ||||
-rw-r--r-- | src/client.cpp | 22 | ||||
-rw-r--r-- | src/connectionhandler.cpp | 24 | ||||
-rw-r--r-- | src/connectionhandler.h | 5 | ||||
-rw-r--r-- | src/defines.h | 30 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/netcomputer.cpp | 30 | ||||
-rw-r--r-- | src/netcomputer.h | 25 |
8 files changed, 158 insertions, 36 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 602d7f49..a23ac666 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -25,6 +25,7 @@ #include "debug.h" #include "storage.h" #include "account.h" +#include "messageout.h" #include <iostream> using tmwserv::Account; @@ -38,11 +39,11 @@ using tmwserv::Storage; */ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { - int result = 0; - Storage &store = Storage::instance("tmw"); - int type = message.readByte(); + int type = message.readShort(); + + MessageOut result; switch(type) { @@ -50,52 +51,63 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { 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) { + std::cout << "Account does not exist " << username << std::endl; + + result.writeShort(SMSG_LOGIN_ERROR); + result.writeByte(LOGIN_INVALID_USERNAME); + } else if (acc->getPassword() != password) { // bad password -- send error to client std::cout << "Bad password for " << username << std::endl; - return; + + result.writeShort(SMSG_LOGIN_ERROR); + result.writeByte(LOGIN_INVALID_PASSWORD); + } else { + // Login OK! (send an OK message or something) + std::cout << "Login OK by " << username << std::endl; + + result.writeShort(SMSG_LOGIN_CONFIRM); + // TODO: Return information about available characters } - - // 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(); - + AccountPtr acc(new Account(username, password, email)); store.addAccount(acc); - + + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_OK); + 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?) + // TODO: Finish this message type (should a player customize stats + // slightly?) + result.writeShort(LOGIN_UNKNOWN); } break; - + default: std::cout << "Invalid message type" << std::endl; + result.writeShort(SMSG_LOGIN_ERROR); + result.writeByte(LOGIN_UNKNOWN); break; } - debugCatch(result); + // return result + computer.send(result.getPacket()); } /* ----Login Message---- diff --git a/src/client.cpp b/src/client.cpp index c9e4ed4e..e1bbc546 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) // Register /* - msg.writeByte(CMSG_REGISTER); + msg.writeShort(CMSG_REGISTER); msg.writeString("test"); msg.writeString("password"); msg.writeString("test@email.addr"); @@ -51,28 +51,40 @@ int main(int argc, char *argv[]) // Login ///* - msg.writeByte(CMSG_LOGIN); + msg.writeShort(CMSG_LOGIN); msg.writeString("test"); msg.writeString("password"); //*/ // Chat /* - msg.writeByte(CMSG_SAY); + msg.writeShort(CMSG_SAY); 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("\n"); - */ + //*/ SDLNet_TCP_Send(tcpsock, msg.getPacket()->data, msg.getPacket()->length); + char data[1024]; + int recvLength = SDLNet_TCP_Recv(tcpsock, data, 1024); + printf("Received:\n"); + if (recvLength != -1) + for (unsigned int i = 0; i < recvLength; i++) { + printf("%x ", data[i]); + } + else + printf("ERROR!"); + printf("\n"); + + SDLNet_TCP_Close(tcpsock); return 0; diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index 2a5d6c44..d0a0d234 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -24,6 +24,7 @@ #include <iostream> #include <vector> #include <sstream> +#include <SDL_net.h> #include "connectionhandler.h" #include "netsession.h" @@ -192,8 +193,8 @@ ConnectionHandler::startListen(ListenThreadData *ltd) // make sure that the packet is big enough if (result >= 4) { - unsigned int messageType = - (unsigned int)*packet->data; + unsigned short messageType = + SDLNet_Read16((void*)packet->data); if (handlers.find(messageType) != handlers.end()) { // send message to appropriate handler @@ -201,8 +202,8 @@ ConnectionHandler::startListen(ListenThreadData *ltd) *comp, msg); } else { // bad message (no registered handler) - LOG_ERROR("Unhandled message received from " - << ipaddr); + LOG_ERROR("Unhandled message (" << messageType + << ") received from " << ipaddr); } } else { LOG_ERROR("Message too short from " << ipaddr); @@ -244,3 +245,18 @@ void ConnectionHandler::registerHandler( { handlers[msgId] = handler; } + +void ConnectionHandler::sendPackets() +{ + for (std::map<NetComputer*, TCPsocket>::iterator i = clients.begin(); + i != clients.end(); i++) { + NetComputer *computer = i->first; + + while (computer->size() > 0) { + // Send queued packet + Packet *packet = computer->front(); + SDLNet_TCP_Send(i->second, packet->data, packet->length); + delete packet; + } + } +} diff --git a/src/connectionhandler.h b/src/connectionhandler.h index cf93e6af..0f114765 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -99,6 +99,11 @@ class ConnectionHandler */ void registerHandler(unsigned int msgId, MessageHandler *handler); + /** + * Send queued packets to client + */ + void sendPackets(); + private: std::map<unsigned int, MessageHandler*> handlers; std::map<NetComputer*, TCPsocket> clients; diff --git a/src/defines.h b/src/defines.h index 4b1db479..7fb33107 100644 --- a/src/defines.h +++ b/src/defines.h @@ -79,7 +79,6 @@ enum { // Beings SMSG_NEW_BEING = 0x0200, SMSG_REMOVE_BEING = 0x0201, - SMSG_INVENTORY_UPD = 0x0210, SMSG_EQUIPMENT_UPD = 0x0220, SMSG_ATTACK = 0x0230, @@ -102,6 +101,35 @@ enum { CMSG_ANNOUNCE = 0x0411, }; +// Login return values +enum { + LOGIN_OK = 0, + LOGIN_INVALID_USERNAME, + LOGIN_INVALID_PASSWORD, + LOGIN_INVALID_VERSION, + LOGIN_SERVER_FULL, + LOGIN_ACCOUNT_BANNED, + LOGIN_ACCOUNT_REVIEW, + LOGIN_UNKNOWN +}; + +enum { + REGISTER_OK = 0, + REGISTER_INVALID_USERNAME, + REGISTER_INVALID_PASSWORD, + REGISTER_INVALID_EMAIL, + REGISTER_EXISTS_USERNAME, + REGISTER_EXISTS_EMAIL +}; + +// Character creation return values +enum { + CREATE_INVALID_NAME = 0, + CREATE_INVALID_HAIR, + CREATE_INVALID_SEX, + CREATE_EXISTS_USERNAME, + CREATE_EXISTS_EMAIL +}; #endif // _TMWSERV_DEFINES_H_ diff --git a/src/main.cpp b/src/main.cpp index 992f6574..a4c20670 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -239,6 +239,8 @@ int main(int argc, char *argv[]) // - Update all active objects/beings updateWorld(); + // Send data to client + connectionHandler->sendPackets(); } else if (event.type == SDL_QUIT) { running = false; diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp index 79110fd4..bffe7b86 100644 --- a/src/netcomputer.cpp +++ b/src/netcomputer.cpp @@ -22,15 +22,43 @@ */ #include "netcomputer.h" - +#include <cstdlib> +#include <iostream> NetComputer::NetComputer(ConnectionHandler *handler): handler(handler) { } +NetComputer::~NetComputer() +{ + // delete unsent messages + while (queue.size() > 0) { + delete queue.front(); + queue.pop(); + } +} + void NetComputer::disconnect(const std::string &reason) { // Somehow notify the netsession listener about the disconnect after // sending this computer a disconnect message containing the reason. } + +void NetComputer::send(const Packet *p) +{ + // Copy the packet + Packet *newPacket = new Packet(NULL, 0); + newPacket->data = new char[p->length]; + memcpy(newPacket->data, (void*) p->data, p->length); + newPacket->length = p->length; + + queue.push(newPacket); +} + +Packet *NetComputer::front() +{ + Packet *ret = queue.front(); + queue.pop(); + return ret; +} diff --git a/src/netcomputer.h b/src/netcomputer.h index 1a73d675..046c2a69 100644 --- a/src/netcomputer.h +++ b/src/netcomputer.h @@ -27,6 +27,8 @@ #include "packet.h" #include <SDL_net.h> #include <string> +#include <queue> +#include <list> // Forward declaration class ConnectionHandler; @@ -44,6 +46,11 @@ class NetComputer NetComputer(ConnectionHandler *handler); /** + * Destructor + */ + ~NetComputer(); + + /** * Returns <code>true</code> if this computer is disconnected. */ bool isDisconnected(); @@ -54,17 +61,29 @@ class NetComputer void disconnect(const std::string &reason); /** - * Sends a packet to this computer. - * + * Queues (FIFO) a packet 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. */ - void send(Packet *p); + void send(const Packet *p); //void send(Packet *p, bool reliable = true); + /** + * Get next message + */ + Packet *front(); + + /** + * Number of messages in queue + */ + unsigned int size() { return queue.size(); } + private: ConnectionHandler *handler; + + std::queue<Packet*> queue; /**< Message Queue (FIFO) */ }; #endif |