summaryrefslogtreecommitdiff
path: root/src/net/nethandler.cpp~
blob: d6f031ec4b9174889d9d09f25e3c53fdc675bedf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#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);
}