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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
* xfirelib - C++ Library for the xfire protocol.
* Copyright (C) 2006 by
* Beat Wolf <asraniel@fryx.ch> / http://gfire.sf.net
* Herbert Poul <herbert.poul@gmail.com> / http://goim.us
* http://xfirelib.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "stdafx.h"
#include "xdebug.h"
#include "client.h"
#include "clientinformationpacket.h"
#include "clientversionpacket.h"
#include "xfirepacket.h"
#include "authpacket.h"
#include "clientloginpacket.h"
#include "buddylistnamespacket.h"
#include "messagepacket.h"
#include "sendmessagepacket.h"
#include "messageackpacket.h"
#include "recvoldversionpacket.h"
#include "sendkeepalivepacket.h"
#include "baseProtocol.h"
#include <iostream>
//#define XFIRE_HOST "cs.xfire.com"
#define XFIRE_HOST "206.220.42.147"
#define XFIRE_PORT 25999
extern BOOL mySleep(int ms,HANDLE evt);
extern HANDLE hConnectionClose;
extern WINBASEAPI
BOOL
WINAPI
ResetEvent(
__in HANDLE hEvent
);
//#define UINT_8 unsigned char
//#define UINT_32 unsigned long
namespace xfirelib {
using namespace std;
Client::Client() {
XDEBUG(("Client constructor...\n"));
gameResolver = NULL;
packetReader = new PacketReader(NULL);
packetReader->addPacketListener( this );
buddyList = new BuddyList( this );
socket=NULL;
#ifndef NO_PTHREAD
sendpingthread.p=NULL;
readthread.p=NULL;
#endif
}
Client::~Client(){
XDEBUG(("Client destructor...\n"));
delete username;
delete password;
delete buddyList;
delete packetReader;
delete socket;
}
void Client::connect( string username, string password, int useproxy, string proxyip, int proxyport) {
try {
this->gotBudduyList=FALSE;
this->username = new string(username);
this->password = new string(password);
socket = new Socket( XFIRE_HOST, XFIRE_PORT,useproxy,proxyip,proxyport );
//bevors losgeht, erstmal die localaddr sichern
struct sockaddr_in sa;
int iLen = sizeof(sa);
getsockname(socket->m_sock, (SOCKADDR*)&sa, &iLen);
strcpy(this->localaddr,inet_ntoa(sa.sin_addr));
this->llocaladdr=inet_addr(this->localaddr);
packetReader->setSocket(socket);
ResetEvent(hConnectionClose);
startThreads();
//packetReader->startListening();
socket->send("UA01");
XDEBUG(("Sent UA01\n"));
ClientInformationPacket *infoPacket = new ClientInformationPacket();
this->send( infoPacket );
delete infoPacket;
XINFO(("sent ClientInformationPacket\n"));
ClientVersionPacket *versionPacket = new ClientVersionPacket();
versionPacket->setProtocolVersion( protocolVersion);
this->send( versionPacket );
delete versionPacket;
XINFO(("sent ClientVersionPacket\n"));
this->connected=TRUE;
} catch( SocketException ex ) {
XERROR(("Socket Exception ?! %s \n",ex.description().c_str() ));
this->connected=FALSE;
}
}
XFireGameResolver *Client::getGameResolver() {
return gameResolver;
}
void Client::startThreads() {
XINFO(("About to start thread\n"));
#ifndef NO_PTHREAD
void* (*func)(void*) = &xfirelib::Client::startReadThread;
pthread_create( &readthread, NULL, func, (void*)this );
void* (*func2)(void*) = &xfirelib::Client::startSendPingThread;
pthread_create( &sendpingthread, NULL, func2, (void*)this );
#else
//mir_create!!!!
mir_forkthread(xfirelib::Client::startReadThread,(LPVOID)this);
mir_forkthread(xfirelib::Client::startSendPingThread,(LPVOID)this);
#endif
}
#ifndef NO_PTHREAD
void *Client::startReadThread(void *ptr) {
#else
void Client::startReadThread(LPVOID lParam) {
void* ptr=(void*)lParam;
#endif
if (ptr==NULL||((Client*)ptr)->packetReader==NULL)
#ifndef NO_PTHREAD
return NULL;
#else
return;
#endif
try {
((Client*)ptr)->packetReader->run();
} catch (SocketException ex) {
XERROR(("Socket Exception ?! %s \n",ex.description().c_str() ));
//miranda bescheid geben, wir haben verbindung verloren
if (ptr==NULL||((Client*)ptr)->connected) SetStatus(ID_STATUS_OFFLINE,NULL);
//((Client*)ptr)->disconnect();
}
#ifndef NO_PTHREAD
return NULL;
#else
return;
#endif
}
#ifndef NO_PTHREAD
void *Client::startSendPingThread(void *ptr) {
Client *me = (Client*)ptr;
#else
void Client::startSendPingThread(LPVOID lParam) {
Client *me = (Client*)lParam;
#endif
SendKeepAlivePacket packet;
while(1) {
#ifndef NO_PTHREAD
pthread_testcancel();
#endif
//Sleep(60000); // Sleep for 40 sek
if (mySleep(60000,hConnectionClose))
{
#ifndef NO_PTHREAD
return NULL;
#else
return;
#endif
}
#ifndef NO_PTHREAD
pthread_testcancel();
#endif
XDEBUG(( "Sending KeepAlivePacket\n" ));
if (!me->send( &packet )) {
XINFO(( "Could not send KeepAlivePacket... exiting thread.\n" ));
break;
}
}
#ifndef NO_PTHREAD
return NULL;
#else
return;
#endif
}
void Client::disconnect() {
this->connected=FALSE;
//socket vom packetreader auf NULL, damit die readschleife geschlossen wird
if (this->packetReader!=NULL)
this->packetReader->setSocket(NULL);
XDEBUG( "cancelling readthread... \n");
#ifndef NO_PTHREAD
if (readthread.p!=NULL) pthread_cancel (readthread);
readthread.p=NULL;
XDEBUG( "cancelling sendpingthread... \n");
if (sendpingthread.p!=NULL) pthread_cancel (sendpingthread);
sendpingthread.p=NULL;
#endif
XDEBUG( "deleting socket...\n" );
if (socket){
delete socket;
socket = NULL;
}
XDEBUG(( "done\n" ));
}
bool Client::send( XFirePacketContent *content ) {
if (!socket) {
XERROR(( "Trying to send content packet altough socket is NULL ! (ignored)\n" ));
return false;
}
XFirePacket *packet = new XFirePacket(content);
packet->sendPacket( socket );
delete packet;
return true;
}
void Client::addPacketListener( PacketListener *listener ) {
packetReader->addPacketListener( listener );
}
void Client::receivedPacket( XFirePacket *packet ) {
XDEBUG(("Client::receivedPacket\n"));
if ( packet == NULL ) {
XERROR(("packet is NULL !!!\n"));
return;
}
if ( packet->getContent() == NULL ) {
XERROR(("ERRRR getContent() returns null ?!\n"));
return;
}
XFirePacketContent *content = packet->getContent();
switch( content->getPacketId() ) {
case XFIRE_PACKET_AUTH_ID: {
XINFO(("Got Auth Packet .. Sending Login\n"));
AuthPacket *authPacket = (AuthPacket*)packet->getContent();
ClientLoginPacket *login = new ClientLoginPacket();
login->setSalt( authPacket->getSalt() );
login->setUsername( *username );
login->setPassword( *password );
send( login );
delete login;
break;
}
case XFIRE_MESSAGE_ID: {
XDEBUG(( "Got Message, sending ACK\n" ));
MessagePacket *message = (MessagePacket*)packet->getContent();
if (message->getMessageType() == 0){
MessageACKPacket *ack = new MessageACKPacket();
memcpy(ack->sid,message->getSid(),16);
ack->imindex = message->getImIndex();
send( ack );
delete ack;
}else if (message->getMessageType() == 2){
send(message);
}
break;
}
default:
//cout << "Nothing here... " << endl;
break;
}
}
};
|