From b7a8b597b00eedde277836eb8530ba742edcad5d Mon Sep 17 00:00:00 2001 From: Treeki Date: Wed, 25 Dec 2013 09:00:59 +0100 Subject: commit initial bits --- server/client.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 server/client.cpp (limited to 'server/client.cpp') 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 + +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); +} -- cgit v1.2.3