summaryrefslogtreecommitdiff
path: root/server/client.cpp
diff options
context:
space:
mode:
authorTreeki <treeki@gmail.com>2013-12-25 09:00:59 +0100
committerTreeki <treeki@gmail.com>2013-12-25 09:00:59 +0100
commitb7a8b597b00eedde277836eb8530ba742edcad5d (patch)
tree1b959fa0eec02ff4e22e168aa4379b2b64575ce3 /server/client.cpp
downloadbounce_qt-master.tar.gz
bounce_qt-master.zip
commit initial bitsHEADmaster
Diffstat (limited to 'server/client.cpp')
-rw-r--r--server/client.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/server/client.cpp b/server/client.cpp
new file mode 100644
index 0000000..3952297
--- /dev/null
+++ b/server/client.cpp
@@ -0,0 +1,73 @@
+#include "client.h"
+#include "packetreader.h"
+#include "packetwriter.h"
+#include <QTcpSocket>
+
+Client::Client(QObject *parent) :
+ Socket(parent)
+{
+ setup(new QTcpSocket(this));
+
+ memset(m_sessionKey, 0, sizeof(m_sessionKey));
+}
+
+
+void Client::connectToHost(const QString &hostname, int port) {
+ socket()->connectToHost(hostname, port);
+}
+
+
+void Client::handlePacket(int type, PacketReader &reader) {
+ switch (type) {
+ case B2C_OOB_LoginSuccess:
+ reader.readBytes(m_sessionKey, SessionKeySize);
+ qDebug("Logged in successfully!");
+
+ m_handshakeStatus = 1;
+ flushPacketCache();
+
+ break;
+
+ case B2C_OOB_LoginFailed:
+ qDebug("Login failed: code %u", reader.readU32());
+ unlinkSocket(true);
+ emit finished();
+ break;
+
+ case B2C_ConsoleOutput:
+ emit consoleOutput(reader.readString(MaxConsoleStringSize));
+ break;
+ }
+}
+
+
+void Client::socketConnected() {
+ qDebug("Client connected!");
+
+ sendLoginPacket("testtest", "optimal", m_sessionKey);
+}
+
+
+void Client::sendLoginPacket(const QString &username, const QString &password, const quint8 *sessionKey) {
+ int size = 12 + SessionKeySize +
+ (username.length() * 2) + (password.length() * 2);
+
+ PacketWriter writer(size);
+ writer.writeU32(1);
+ writer.writeU32(lastReceivedPacketID());
+ writer.writeBytes(sessionKey, SessionKeySize);
+ writer.writeString(username);
+ writer.writeString(password);
+
+ Packet packet(C2B_OOB_Login, writer.buffer());
+ sendPacket(packet, /*isHandshakePacket=*/true);
+}
+
+
+void Client::consoleInput(const QString &message) {
+ PacketWriter writer(4 + (message.size() * 2));
+ writer.writeString(message);
+
+ Packet packet(C2B_ConsoleInput, writer.buffer());
+ sendPacket(packet);
+}