summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/accounthandler.cpp37
-rw-r--r--src/chathandler.cpp5
-rw-r--r--src/connectionhandler.cpp40
-rw-r--r--src/connectionhandler.h4
-rw-r--r--src/messagehandler.h4
-rw-r--r--src/messagein.cpp2
-rw-r--r--src/messagein.h4
-rw-r--r--src/netcomputer.h2
8 files changed, 52 insertions, 46 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp
index 6bc1b68d..0f5adddd 100644
--- a/src/accounthandler.cpp
+++ b/src/accounthandler.cpp
@@ -41,69 +41,70 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
Storage &store = Storage::instance("tmw");
- int type = message.readShort();
-
MessageOut result;
- switch(type)
+ switch (message.getId())
{
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;
-
+
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;
-
+
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
}
- } break;
-
+ }
+ 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;
-
+ }
+ break;
+
case CMSG_CHAR_CREATE:
{
std::string name = message.readString();
- char hairStyle = message.readByte();
- char hairColor = message.readByte();
- char sex = message.readByte();
+ //char hairStyle = message.readByte();
+ //char hairColor = message.readByte();
+ //char sex = message.readByte();
// TODO: Finish this message type (should a player customize stats
// slightly?)
result.writeShort(SMSG_CHAR_CREATE_RESPONSE);
result.writeShort(CREATE_OK);
- } break;
+ }
+ break;
default:
std::cout << "Invalid message type" << std::endl;
diff --git a/src/chathandler.cpp b/src/chathandler.cpp
index f7505894..f3bf38b1 100644
--- a/src/chathandler.cpp
+++ b/src/chathandler.cpp
@@ -27,9 +27,8 @@
void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
- int type = message.readShort();
-
- switch (type) {
+ switch (message.getId())
+ {
case CMSG_SAY:
{
std::string text = message.readString();
diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp
index 338913f8..ad42adcf 100644
--- a/src/connectionhandler.cpp
+++ b/src/connectionhandler.cpp
@@ -149,7 +149,8 @@ ConnectionHandler::startListen(ListenThreadData *ltd)
NetComputer *comp = *i;
TCPsocket s = (*i)->getSocket();
- if (SDLNet_SocketReady(s)) {
+ if (SDLNet_SocketReady(s))
+ {
char buffer[1024];
int result = SDLNet_TCP_Recv(s, buffer, 1024);
if (result <= 0) {
@@ -171,7 +172,7 @@ ConnectionHandler::startListen(ListenThreadData *ltd)
<< result);
#ifdef SCRIPT_SUPPORT
- // this could be good if you wanted to extend the
+ // 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.
@@ -179,33 +180,35 @@ ConnectionHandler::startListen(ListenThreadData *ltd)
//script->message(buffer);
#endif
- // if the scripting subsystem didn't hook the message
+ // 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
+ // 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)
+ {
+ Packet *packet = new Packet(buffer, result);
+ MessageIn msg(packet); // (MessageIn frees packet)
- // make sure that the packet is big enough
- if (result >= 4) {
- unsigned short messageType =
- SDLNet_Read16((void*)packet->data);
- if (handlers.find(messageType) != handlers.end())
+ short messageId = msg.getId();
+
+ if (handlers.find(messageId) != handlers.end())
{
// send message to appropriate handler
- handlers[messageType]->receiveMessage(
+ handlers[messageId]->receiveMessage(
*comp, msg);
- } else {
+ }
+ else {
// bad message (no registered handler)
- LOG_ERROR("Unhandled message (" << messageType
+ LOG_ERROR("Unhandled message (" << messageId
<< ") received from " << ipaddr);
}
- } else {
+ }
+ else {
LOG_ERROR("Message too short from " << ipaddr);
}
}
@@ -213,10 +216,7 @@ ConnectionHandler::startListen(ListenThreadData *ltd)
// Traverse to next client, possibly deleting current
if (comp == NULL) {
- std::list<NetComputer*>::iterator ii = i;
- ii++;
- clients.erase(i);
- i = ii;
+ i = clients.erase(i);
}
else {
i++;
diff --git a/src/connectionhandler.h b/src/connectionhandler.h
index 589838b7..a66c075b 100644
--- a/src/connectionhandler.h
+++ b/src/connectionhandler.h
@@ -106,7 +106,9 @@ class ConnectionHandler
private:
std::map<unsigned int, MessageHandler*> handlers;
- std::list<NetComputer*> clients;
+
+ typedef std::list<NetComputer*> NetComputers;
+ NetComputers clients;
};
#endif
diff --git a/src/messagehandler.h b/src/messagehandler.h
index ee4eec65..ccf2a8f1 100644
--- a/src/messagehandler.h
+++ b/src/messagehandler.h
@@ -45,8 +45,8 @@ class MessageHandler
* Called when a message is received with a message ID that corresponds
* to an ID this message handler registered to handle.
*/
- virtual void receiveMessage(
- NetComputer &computer, MessageIn &message) = 0;
+ virtual void
+ receiveMessage(NetComputer &computer, MessageIn &message) = 0;
};
diff --git a/src/messagein.cpp b/src/messagein.cpp
index 6a27f280..f5eaefed 100644
--- a/src/messagein.cpp
+++ b/src/messagein.cpp
@@ -28,6 +28,8 @@ MessageIn::MessageIn(Packet *packet):
mPacket(packet),
mPos(0)
{
+ // Read the message ID
+ mId = readShort();
}
MessageIn::~MessageIn()
diff --git a/src/messagein.h b/src/messagein.h
index 0b99c40f..b2682fc1 100644
--- a/src/messagein.h
+++ b/src/messagein.h
@@ -43,6 +43,7 @@ class MessageIn
*/
~MessageIn();
+ short getId() { return mId; } /**< Returns the message ID. */
char readByte(); /**< Reads a byte. */
short readShort(); /**< Reads a short. */
@@ -56,7 +57,8 @@ class MessageIn
std::string readString(int length = -1);
private:
- Packet *mPacket;
+ Packet *mPacket; /**< The packet being processed. */
+ short mId; /**< The message ID. */
/**
* Actual Position in the packet. From 0 to packet->length - 1.
diff --git a/src/netcomputer.h b/src/netcomputer.h
index 83382a7a..d3eb0a05 100644
--- a/src/netcomputer.h
+++ b/src/netcomputer.h
@@ -69,7 +69,7 @@ class NetComputer
*/
void send(const Packet *p);
//void send(Packet *p, bool reliable = true);
-
+
/**
* Return the socket
*/