#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); }