summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmakeclient.sh4
-rw-r--r--src/accounthandler.cpp12
-rw-r--r--src/client.cpp11
-rw-r--r--src/connectionhandler.cpp13
-rw-r--r--src/messagein.cpp75
-rw-r--r--src/messageout.cpp47
-rw-r--r--src/messageout.h5
-rw-r--r--src/packet.cpp8
-rw-r--r--src/packet.h11
9 files changed, 125 insertions, 61 deletions
diff --git a/makeclient.sh b/makeclient.sh
index 7b5b9e21..72c4f50f 100755
--- a/makeclient.sh
+++ b/makeclient.sh
@@ -1,2 +1,4 @@
#!/bin/bash
-g++ -I/usr/include/SDL -D_REENTRANT -o client src/client.cpp -L/usr/lib -lSDL_net -lSDL -lpthread
+g++ -I/usr/include/SDL -D_REENTRANT -o client \
+ src/client.cpp src/messageout.cpp src/packet.cpp \
+ -L/usr/lib -lSDL_net -lSDL -lpthread
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp
index ec862532..1eb1ed12 100644
--- a/src/accounthandler.cpp
+++ b/src/accounthandler.cpp
@@ -23,6 +23,7 @@
#include "accounthandler.h"
#include "debug.h"
+#include <iostream>
/**
* Generic interface convention for getting a message and sending it to the
@@ -33,7 +34,9 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
int result = 0;
- // determine message type
+ // strip message type
+ message.readByte();
+
/*
switch(message.type)
{
@@ -43,6 +46,13 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
}
*/
+ /*
+ 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 99b1e3dd..f07ded2b 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -1,6 +1,8 @@
#include <SDL.h>
#include <SDL_net.h>
#include <stdlib.h>
+#include "defines.h"
+#include "messageout.h"
int main(int argc, char *argv[])
{
@@ -35,6 +37,15 @@ int main(int argc, char *argv[])
}
printf("Succesfully connected!\n");
+
+ // Send a login message
+ MessageOut msg;
+ msg.writeByte(MSG_LOGIN);
+ msg.writeString("nym");
+ msg.writeString("password");
+
+ SDLNet_TCP_Send(tcpsock, msg.getPacket()->data, msg.getPacket()->length);
+
SDLNet_TCP_Close(tcpsock);
return 0;
diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp
index 98d4a748..2b3b64d0 100644
--- a/src/connectionhandler.cpp
+++ b/src/connectionhandler.cpp
@@ -156,8 +156,13 @@ void ConnectionHandler::startListen(ListenThreadData *ltd)
else {
// Copy the incoming data to the in buffer of this
// client
- buffer[result] = 0;
- LOG_INFO("Received " << buffer);
+ //buffer[result] = 0;
+ //LOG_INFO("Received: " << buffer << ", Length: "
+ // << result);
+
+
+ LOG_INFO("Received length: "
+ << result);
#ifdef SCRIPT_SUPPORT
// this could be good if you wanted to extend the
@@ -175,11 +180,11 @@ void ConnectionHandler::startListen(ListenThreadData *ltd)
std::string ipaddr = ip4ToString(SDLNet_TCP_GetPeerAddress(s)->host);
// generate packet
- Packet *packet = new Packet(buffer, strlen(buffer));
+ Packet *packet = new Packet(buffer, result);
MessageIn msg(packet); // (MessageIn frees packet)
// make sure that the packet is big enough
- if (strlen(buffer) >= 4) {
+ if (result >= 4) {
unsigned int messageType = (unsigned int)*packet->data;
if (handlers.find(messageType) != handlers.end()) {
// send message to appropriate handler
diff --git a/src/messagein.cpp b/src/messagein.cpp
index aae45c2b..d916e717 100644
--- a/src/messagein.cpp
+++ b/src/messagein.cpp
@@ -90,58 +90,29 @@ long MessageIn::readLong()
std::string MessageIn::readString(int length)
{
- if (packet) // If there a good packet
- {
- int stringLength = 0;
- std::string readString = "";
+ int stringLength = 0;
+ std::string readString = "";
- if ( length != -1 ) // if the length isn't specified, read it in the packet
- {
- // If the length of the packet is sufficient for us to read the length of the string
- if ( (pos + sizeof(short)) <= packet->length )
- {
- // We first read the length of the string given by the short before it.
- stringLength = (short)SDLNet_Read16(&(packet->data[pos]));
- pos += sizeof(short);
- }
- else
- {
- // Place us to the end as the size is a short
- pos = packet->length;
- }
- }
- else // if the length is specified
- {
- stringLength = length;
- }
+ if (packet) {
+ // get string length
+ if (length < 0) {
+ stringLength = (short) packet->data[pos];
+ pos += sizeof(short);
+ } else {
+ stringLength = length;
+ }
- if ( stringLength > 0 )
- {
- if ( (pos + stringLength) <= packet->length ) // If there's enough length to copy
- {
- char tempChar[stringLength+1];
- memcpy(&tempChar, packet->data, stringLength);
- readString = tempChar; // We first copy the entire char array
- // And then, we copy from the pos
- // to the stringLength.
- readString = readString.substr(pos,stringLength);
- pos += stringLength;
- }
- else // if there isn't, copy till the end
- {
- char tempChar[packet->length +1];
- memcpy(&tempChar, packet->data, packet->length - 1);
- readString = tempChar; // We first copy the entire char array
- // And then, we copy from the pos
- // to the length - 1 of the string.
- readString = readString.substr(pos,packet->length - 1 - pos);
- pos = packet->length - 1;
- }
- }
- else
- {
- // The length of the string is zero ?
- }
- } // End if there is a packet
- return "";
+ // read the string
+ char *tmpString = new char[stringLength + 1];
+ memcpy(tmpString, (void*)&packet->data[pos], stringLength);
+ tmpString[stringLength] = 0;
+ pos += stringLength;
+
+ readString = tmpString;
+ delete tmpString;
+ } else {
+ return "";
+ }
+
+ return readString;
}
diff --git a/src/messageout.cpp b/src/messageout.cpp
index d9393667..2bdc52ec 100644
--- a/src/messageout.cpp
+++ b/src/messageout.cpp
@@ -22,10 +22,14 @@
*/
#include "messageout.h"
+#include <iostream>
+#include <cstdlib>
MessageOut::MessageOut():
- packet(0)
+ packet(0),
+ pos(0)
{
+ packet = new Packet(NULL, 0);
}
MessageOut::~MessageOut()
@@ -34,3 +38,44 @@ MessageOut::~MessageOut()
delete packet;
}
}
+
+void MessageOut::writeByte(char value)
+{
+ packet->expand(sizeof(char));
+ packet->data[packet->length] = value;
+ packet->length += sizeof(char);
+}
+
+void MessageOut::writeShort(unsigned short value)
+{
+ packet->expand(sizeof(unsigned short));
+ memcpy(&packet->data[packet->length], (void*)&value, sizeof(unsigned short));
+ packet->length += sizeof(unsigned short);
+}
+
+void MessageOut::writeLong(unsigned long value)
+{
+ packet->expand(sizeof(unsigned long));
+ memcpy(&packet->data[packet->length], (void*)&value, sizeof(unsigned long));
+ packet->length += sizeof(unsigned long);
+}
+
+void MessageOut::writeString(const std::string &string, int length)
+{
+ if (length < 0)
+ length = string.length();
+
+ packet->expand(length + sizeof(unsigned short));
+
+ // 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);
+ packet->length += length + sizeof(unsigned short);
+}
+
+const Packet *MessageOut::getPacket()
+{
+ return packet;
+}
diff --git a/src/messageout.h b/src/messageout.h
index d48cb899..df49881e 100644
--- a/src/messageout.h
+++ b/src/messageout.h
@@ -42,8 +42,8 @@ class MessageOut
~MessageOut();
void writeByte(char value); /**< Reads a byte. */
- void writeShort(short value); /**< Reads a short. */
- void writeLong(long value); /**< Reads a long. */
+ void writeShort(unsigned short value);/**< Reads a short. */
+ void writeLong(unsigned long value); /**< Reads a long. */
/**
* Writes a string. If a fixed length is not given (-1), it is stored
@@ -59,6 +59,7 @@ 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 a7cd00cb..9ab27d84 100644
--- a/src/packet.cpp
+++ b/src/packet.cpp
@@ -23,6 +23,7 @@
#include "packet.h"
#include <string.h>
+#include <cstdlib>
Packet::Packet(const char *data, int length):
length(length)
@@ -30,6 +31,7 @@ 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()
@@ -37,3 +39,9 @@ Packet::~Packet()
// Clean up the data
delete[] data;
}
+
+void Packet::expand(unsigned int bytes)
+{
+ realloc(data, length + bytes);
+ size += bytes;
+}
diff --git a/src/packet.h b/src/packet.h
index 4e1d48c5..6b276808 100644
--- a/src/packet.h
+++ b/src/packet.h
@@ -26,6 +26,9 @@
/**
* 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
{
@@ -40,8 +43,16 @@ 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