summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Marks <nymacro@gmail.com>2005-07-16 03:55:54 +0000
committerAaron Marks <nymacro@gmail.com>2005-07-16 03:55:54 +0000
commit34e887895276242efaf2e0b5f1700c1ab1d6b3db (patch)
tree8e2805ad893678364cde6ec1a37146c1bbecf075
parentca0ffbbacfcba5e0f019c83fd0b759752b192ca0 (diff)
downloadmanaserv-34e887895276242efaf2e0b5f1700c1ab1d6b3db.tar.gz
manaserv-34e887895276242efaf2e0b5f1700c1ab1d6b3db.tar.bz2
manaserv-34e887895276242efaf2e0b5f1700c1ab1d6b3db.tar.xz
manaserv-34e887895276242efaf2e0b5f1700c1ab1d6b3db.zip
Fixed problem with memory expanding in Packet.
Server now handles register requests.
-rw-r--r--src/accounthandler.cpp67
-rw-r--r--src/client.cpp23
-rw-r--r--src/defines.h7
-rw-r--r--src/main.cpp2
-rw-r--r--src/messagein.cpp9
-rw-r--r--src/messagein.h4
-rw-r--r--src/messageout.cpp4
-rw-r--r--src/messageout.h1
-rw-r--r--src/packet.cpp8
9 files changed, 98 insertions, 27 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp
index 1eb1ed12..266dfddf 100644
--- a/src/accounthandler.cpp
+++ b/src/accounthandler.cpp
@@ -23,8 +23,14 @@
#include "accounthandler.h"
#include "debug.h"
+#include "storage.h"
+#include "account.h"
#include <iostream>
+using tmwserv::Account;
+using tmwserv::AccountPtr;
+using tmwserv::Storage;
+
/**
* Generic interface convention for getting a message and sending it to the
* correct subroutines. Account handler takes care of determining the
@@ -34,24 +40,57 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
int result = 0;
- // strip message type
+ // strip message type byte
message.readByte();
- /*
- switch(message.type)
+ Storage &store = Storage::instance("tmw");
+
+ // get message type
+ char type = message.readByte();
+
+ switch(type)
{
- case TYPE_LOGIN:
- result = loginMessage(computer, message);
- break;
+ case MSG_ACCOUNT_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;
+
+ case MSG_ACCOUNT_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);
+
+ std::cout << "Account registered" << std::endl;
+ store.flush(); // flush changes
+ } break;
+
+ default:
+ std::cout << "Invalid message type" << std::endl;
+ break;
}
- */
-
- /*
- std::cout << "Username: " << message.readString() << ", Password: "
- << message.readString() << std::endl;
- */
- std::cout << "Data: " << message.readString() << std::endl;
- std::cout << "Data: " << message.readString() << std::endl;
+
debugCatch(result);
}
diff --git a/src/client.cpp b/src/client.cpp
index f07ded2b..19e0fa46 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -40,9 +40,28 @@ int main(int argc, char *argv[])
// Send a login message
MessageOut msg;
- msg.writeByte(MSG_LOGIN);
- msg.writeString("nym");
+
+ // Login
+ ///*
+ msg.writeByte(MSG_ACCOUNT);
+ msg.writeByte(MSG_ACCOUNT_LOGIN);
+ msg.writeString("test");
+ msg.writeString("password");
+ //*/
+
+ // Register
+ /*
+ msg.writeByte(MSG_ACCOUNT);
+ msg.writeByte(MSG_ACCOUNT_REGISTER);
+ msg.writeString("test");
msg.writeString("password");
+ msg.writeString("test@email.addr");
+ */
+
+ 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);
diff --git a/src/defines.h b/src/defines.h
index bc5dc9b4..a0d3273a 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -58,7 +58,7 @@ typedef enum {
* Enumerated type for received server messages
*/
enum {
- MSG_LOGIN = 0,
+ MSG_ACCOUNT = 0,
MSG_MOVE,
MSG_ATTACK,
MSG_PICKUP,
@@ -67,6 +67,11 @@ enum {
MSG_CHAT
};
+enum {
+ MSG_ACCOUNT_LOGIN = 0,
+ MSG_ACCOUNT_REGISTER
+};
+
// NOTE: Maybe it would be better to reuse some enumerated types with both
// server and client?
diff --git a/src/main.cpp b/src/main.cpp
index 10357c76..815d6b4b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -197,7 +197,7 @@ int main(int argc, char *argv[])
//
// Register message handlers
- connectionHandler->registerHandler(MSG_LOGIN, accountHandler);
+ connectionHandler->registerHandler(MSG_ACCOUNT, accountHandler);
//LOG_INFO("The Mana World Server v" << PACKAGE_VERSION) PACKAGE_VERSION undeclared
session->startListen(connectionHandler.get(), SERVER_PORT);
diff --git a/src/messagein.cpp b/src/messagein.cpp
index d916e717..065c7052 100644
--- a/src/messagein.cpp
+++ b/src/messagein.cpp
@@ -52,7 +52,7 @@ char MessageIn::readByte()
return -1;
}
-short MessageIn::readShort()
+unsigned short MessageIn::readShort()
{
if (packet) // if Packet exists
{
@@ -70,7 +70,7 @@ short MessageIn::readShort()
return -1;
}
-long MessageIn::readLong()
+unsigned long MessageIn::readLong()
{
if (packet) // if Packet exists
{
@@ -102,6 +102,11 @@ std::string MessageIn::readString(int length)
stringLength = length;
}
+ // make sure the string isn't erroneus
+ if (pos + length > packet->length) {
+ return "";
+ }
+
// read the string
char *tmpString = new char[stringLength + 1];
memcpy(tmpString, (void*)&packet->data[pos], stringLength);
diff --git a/src/messagein.h b/src/messagein.h
index e46392b4..0de64d46 100644
--- a/src/messagein.h
+++ b/src/messagein.h
@@ -45,8 +45,8 @@ class MessageIn
char readByte(); /**< Reads a byte. */
- short readShort(); /**< Reads a short. */
- long readLong(); /**< Reads a long. */
+ unsigned short readShort(); /**< Reads a short. */
+ unsigned long readLong(); /**< Reads a long. */
/**
* Reads a string. If a length is not given (-1), it is assumed
diff --git a/src/messageout.cpp b/src/messageout.cpp
index 2bdc52ec..2aca011a 100644
--- a/src/messageout.cpp
+++ b/src/messageout.cpp
@@ -26,8 +26,7 @@
#include <cstdlib>
MessageOut::MessageOut():
- packet(0),
- pos(0)
+ packet(0)
{
packet = new Packet(NULL, 0);
}
@@ -72,6 +71,7 @@ void MessageOut::writeString(const std::string &string, int length)
// actual string
memcpy(&packet->data[packet->length + sizeof(unsigned short)],
(void*)string.c_str(), length);
+
packet->length += length + sizeof(unsigned short);
}
diff --git a/src/messageout.h b/src/messageout.h
index df49881e..fd7d1752 100644
--- a/src/messageout.h
+++ b/src/messageout.h
@@ -59,7 +59,6 @@ class MessageOut
private:
Packet *packet; /**< Created packet. */
- unsigned int pos; /**< Current position in packet */
};
#endif
diff --git a/src/packet.cpp b/src/packet.cpp
index 9ab27d84..6664235f 100644
--- a/src/packet.cpp
+++ b/src/packet.cpp
@@ -37,11 +37,15 @@ Packet::Packet(const char *data, int length):
Packet::~Packet()
{
// Clean up the data
- delete[] data;
+ if (data)
+ delete[] data;
}
void Packet::expand(unsigned int bytes)
{
- realloc(data, length + bytes);
+ char *newData = (char*)malloc(length + bytes);
+ memcpy(newData, (void*)data, length);
+ delete []data;
+ data = newData;
size += bytes;
}