summaryrefslogblamecommitdiff
path: root/net/nethandler.cpp~
blob: d6f031ec4b9174889d9d09f25e3c53fdc675bedf (plain) (tree)






















































































































































































                                                                     
#include "nethandler.h"
#include "network.h"
#include "protocol.h"
#include "messageout.h"

#include "loginhandler.h"
#include "chathandler.h"
#include "charserverhandler.h"
#include "maploginhandler.h"

#include "../log.h"
#include "../main.h"
#include "../game.h"

static NetHandler *NetInstance = NULL;
static Network *mNetwork = NULL;
MessageHandler *mChatHandler = NULL;

LoginHandler loginHandler;
CharServerHandler charServerHandler;
MapLoginHandler mapLoginHandler;

NetHandler::NetHandler()
{
    NetInstance = this;
}

NetHandler::~NetHandler()
{
    delete mNetwork;

    if (mChatHandler != NULL)
        delete mChatHandler;

    delete NetInstance;
}

void NetHandler::clean_up()
{
    delete mNetwork;
    
    if (mChatHandler != NULL) // fix a crash caused by map server D/C
        delete mChatHandler;

    mNetwork = new Network();
}

NetHandler *NetHandler::getNetInstance()
{
    return NetInstance;
}

Network *NetHandler::getNetwork()
{
    if (mNetwork == NULL)
       mNetwork = new Network();

    return mNetwork;
}

void NetHandler::loadHandlers()
{
    mChatHandler = new ChatHandler();
    mNetwork->registerHandler(mChatHandler);
}

// All code related to login, from main.cpp

void NetHandler::accountLogin(LoginData *loginData)
{
    /*
    logger->log("Trying to connect to account server...");
    logger->log("Username is %s", loginData->username.c_str());*/
    mNetwork->connect(loginData->hostname, loginData->port);
    mNetwork->registerHandler(&loginHandler);
    loginHandler.setLoginData(loginData);

    // Send login infos
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(0x0064);
    outMsg.writeInt32(0); // client version
    outMsg.writeString(loginData->username, 24);
    outMsg.writeString(loginData->password, 24);
    outMsg.writeInt8(0); // unknown
}

void NetHandler::charLogin(LoginData *loginData)
{
    /*logger->log("Trying to connect to char server...");

    logger->log("Server: %s (%s:%d)",
                        server_info[0]->name.c_str(),
                        loginData->hostname.c_str(),
                        loginData->port);*/

    mNetwork->connect(loginData->hostname, loginData->port);
    mNetwork->registerHandler(&charServerHandler);
    charServerHandler.setLoginData(loginData);

    // Send login infos
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(0x0065);
    outMsg.writeInt32(loginData->account_ID);
    outMsg.writeInt32(loginData->session_ID1);
    outMsg.writeInt32(loginData->session_ID2);
    outMsg.writeInt16(0); // unknown
    outMsg.writeInt8(loginData->sex);

    // We get 4 useless bytes before the real answer comes in
    mNetwork->skip(4);
}

void NetHandler::mapLogin(LoginData *loginData)
{
    MessageOut outMsg(mNetwork);

    /*logger->log("Trying to connect to map server...");
    logger->log("Map: %s", map_path.c_str());*/

    mNetwork->connect(loginData->hostname, loginData->port);
    mNetwork->registerHandler(&mapLoginHandler);

    // Send login infos
    outMsg.writeInt16(0x0072);
    outMsg.writeInt32(loginData->account_ID);
    outMsg.writeInt32(charID[main_charno]);
    outMsg.writeInt32(loginData->session_ID1);
    outMsg.writeInt32(loginData->session_ID2);
    outMsg.writeInt8(loginData->sex);

    // We get 4 useless bytes before the real answer comes in
    mNetwork->skip(4);
}

// add a few of the packets that need to be sent.
void NetHandler::attemptCharSelect()
{
  /*logger->log("CharServer: Character Select: %s",
                        char_name.c_str());*/
    // Request character selection
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(0x0066);
    outMsg.writeInt8(main_charno);
}

void NetHandler::mapLoaded()
{
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(CMSG_MAP_LOADED);
}

void NetHandler::privateMessage(std::string name, std::string msg)
{
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(CMSG_CHAT_WHISPER);
    outMsg.writeInt16(static_cast<short>(msg.length() + 28));
    outMsg.writeString(name, 24);
    outMsg.writeString(msg, static_cast<int>(msg.length()));
}

void NetHandler::publicMessage(std::string msg)
{
    std::string mes = std::string("guild") + " : " + msg;
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(CMSG_CHAT_MESSAGE);
    outMsg.writeInt16(static_cast<short>(mes.length() + 4 + 1));
    outMsg.writeString(mes, static_cast<int>(mes.length() + 1));
}

void NetHandler::sit()
{
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(CMSG_PLAYER_CHANGE_ACT);
    outMsg.writeInt32(0);
    outMsg.writeInt8(2);
}

void NetHandler::ping(int tick)
{
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(CMSG_CLIENT_PING);
    outMsg.writeInt32(tick);
}