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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
transport.cpp - Peer to peer transport
Copyright (c) 2005 by Gregg Edghill <gregg.edghill@gmail.com>
Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
*************************************************************************
* *
* 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; version 2 of the License. *
* *
*************************************************************************
*/
#include "transport.h"
#include "messageformatter.h"
//BEGIN QT Includes
//END
//BEGIN KDE Includes
#include <kclientsocketbase.h>
#include <kdebug.h>
#include <kstreamsocket.h>
//END
//BEGIN Using Directives
using namespace KNetwork;
//END
#include "msnswitchboardsocket.h"
namespace PeerToPeer {
Transport::Transport(TQObject* parent, const char* name)
: TQObject(parent, name)
{
mFormatter = new PeerToPeer::MessageFormatter(this);
}
Transport::~Transport()
{
}
//BEGIN Public Methods
TransportBridge* Transport::getBridge (const TQString& to, TQ_UINT16 port, TransportBridgeType type, const TQString& identifier)
{
TransportBridge *bridge = 0l;
KInetSocketAddress address;
if (mAddresses.contains(to))
{
address = mAddresses[to];
}
else
{
address = KInetSocketAddress(KIpAddress(to), port);
mAddresses[to] = address;
}
if (PeerToPeer::Tcp == type){
bridge = new TcpTransportBridge(address, mFormatter, this, identifier.ascii());
}
if (PeerToPeer::Udp == type){
// TODO Add class UdpTransportBridge
// bridge = new UdpTransportBridge(address, this, mFormatter, identifier.ascii());
}
if (bridge != 0l)
{
TQObject::connect(bridge, TQT_SIGNAL(readyRead(const TQByteArray&)), TQT_SLOT(slotOnReceive(const TQByteArray&)));
}
return 0l;
}
void Transport::setDefaultBridge(MSNSwitchBoardSocket* mss)
{
mDefaultBridge = mss;
TQObject::connect((MSNSwitchBoardSocket*)mDefaultBridge, TQT_SIGNAL(messageReceived(const TQString&, const TQByteArray&)), TQT_SLOT(slotOnReceive(const TQString&, const TQByteArray&)));
}
//END
//BEGIN Private Slot Methods
// void Transport::slotOnReceive(Message& message)
// {
// }
void Transport::slotOnReceive(const TQString& contact, const TQByteArray& bytes)
{
kdDebug (14140) << k_funcinfo << " >> RECEIVED " << bytes.size() << " bytes." << endl;
// Message message = mFormatter->readMessage(bytes);
}
//END
TransportBridge::TransportBridge(const KNetwork::KInetSocketAddress& to, MessageFormatter* formatter, TQObject* parent, const char* name)
: TQObject(parent, name)
{
mAddress = to;
mFormatter = formatter;
}
TransportBridge::TransportBridge(KNetwork::KClientSocketBase* socket, MessageFormatter* formatter, TQObject* parent, const char* name)
: TQObject(parent, name)
{
mSocket = socket;
mAddress = mSocket->peerAddress();
}
TransportBridge::~TransportBridge()
{
}
//BEGIN Public Methods
void TransportBridge::connect()
{
slotOnConnect();
}
void TransportBridge::disconnect()
{
slotOnDisconnect();
}
//END
//BEGIN Protected Slot Methods
void TransportBridge::slotOnConnect()
{
}
void TransportBridge::slotOnDisconnect()
{
}
void TransportBridge::slotOnError(int)
{
}
void TransportBridge::slotOnSocketClose()
{
}
void TransportBridge::slotOnSocketConnect()
{
}
void TransportBridge::slotOnSocketReceive()
{
}
//END
TcpTransportBridge::TcpTransportBridge(const KNetwork::KInetSocketAddress& to, MessageFormatter* formatter, TQObject* parent, const char* name)
: TransportBridge(to, formatter, parent, name)
{
mSocket = new KStreamSocket(mAddress.ipAddress().toString(), TQString::number(mAddress.port()), this);
mSocket->setBlocking(false);
TQObject::connect(mSocket, TQT_SIGNAL(connected(const KResolverEntry&)), TQT_SLOT(slotOnSocketConnect()));
TQObject::connect(mSocket, TQT_SIGNAL(gotError(int)), TQT_SLOT(slotOnError(int)));
mConnected = false;
}
TcpTransportBridge::TcpTransportBridge(KNetwork::KClientSocketBase* socket, MessageFormatter* formatter, TQObject* parent, const char* name)
: TransportBridge(socket, formatter, parent, name)
{
mConnected = (mSocket->state() == KStreamSocket::Open) ? true : false;
mSocket->setBlocking(false);
}
TcpTransportBridge::~TcpTransportBridge()
{
}
//BEGIN Protected Slot Methods
void TcpTransportBridge::slotOnConnect()
{
if (mConnected)
{
kdDebug(14140) << k_funcinfo << "Bridge (" << name() << ") ALREADY CONNECTED " << mSocket->peerAddress().toString() << " <-> " << mSocket->localAddress().toString() << endl;
return;
}
KStreamSocket *socket = static_cast<KStreamSocket*>(mSocket);
socket->setTimeout(5000);
TQObject::connect(socket, TQT_SIGNAL(timeOut()), TQT_SLOT(slotOnSocketConnectTimeout()));
mSocket->connect();
}
void TcpTransportBridge::slotOnDisconnect()
{
if (mConnected){
mSocket->close();
}
}
void TcpTransportBridge::slotOnError(int errorCode)
{
kdDebug(14140) << k_funcinfo << "Bridge (" << name() << ") ERROR occurred on {" << mSocket->localAddress().toString() << " <-> " << mSocket->peerAddress().toString() << "} - " << mSocket->errorString() << endl;
emit bridgeError(TQString("Bridge ERROR %1: %2").arg(errorCode).arg(mSocket->errorString()));
if (mConnected){
mSocket->disconnect();
mConnected = false;
}
mSocket->deleteLater();
mSocket = 0l;
}
void TcpTransportBridge::slotOnSocketClose()
{
mSocket->disconnect();
kdDebug(14140) << k_funcinfo << "Bridge (" << name() << ") DISCONNECTED {" << mSocket->peerAddress().toString() << " <-> " << mSocket->localAddress().toString() << "}" << endl;
mConnected = false;
mSocket->deleteLater();
mSocket = 0l;
emit bridgeDisconnect();
}
void TcpTransportBridge::slotOnSocketConnect()
{
kdDebug(14140) << k_funcinfo << "Bridge (" << name() << ") CONNECTED to " << mSocket->peerAddress().toString() << " from "
<< mSocket->localAddress().toString() << endl;
mConnected = true;
TQObject::connect(mSocket, TQT_SIGNAL(readyRead()), TQT_SLOT(slotOnSocketReceive()));
TQObject::connect(mSocket, TQT_SIGNAL(closed()), TQT_SLOT(slotOnSocketClose()));
mVerified = true;
TQString foo = "foo\0";
mSocket->writeBlock(foo.ascii(), foo.length());
foo = TQString();
emit bridgeConnect();
}
void TcpTransportBridge::slotOnSocketReceive()
{
kdDebug (14140) << k_funcinfo << "Bridge (" << name() << ") RECEIVED " << mSocket->bytesAvailable() << " bytes." << endl;
TQByteArray bytes(mSocket->bytesAvailable());
mSocket->readBlock(bytes.data(), bytes.size());
// Write the data to the buffer.
mBuffer.write(bytes);
if (mVerified == false && mBuffer.size() >= 4)
{
TQByteArray foo = mBuffer.read(4);
if (TQString(foo) == "foo"){
kdDebug (14140) << k_funcinfo << "Bridge (" << name() << ") CONNECTION verified." << endl;
mVerified = true;
}
}
while(mBuffer.size() > 0)
{
if (mBuffer.size() >= 4 && mLength == 0)
{
TQByteArray array = mBuffer.read(4);
for (int i=0; i < 4; i++){
((char*)mLength)[i] = array[i];
}
}
if (mLength > 0 && mBuffer.size() >= mLength)
{
kdDebug (14140) << k_funcinfo << "Bridge (" << name() << ") read " << mLength << " bytes." << endl;
bytes = mBuffer.read(mLength);
mLength = 0;
// Message message = mFormatter->readMessage(bytes, true);
// emit messageReceived(message);
}
else
{
kdDebug (14140) << k_funcinfo << "Bridge (" << name() << ") waiting for " << mLength << " bytes." << endl;
break;
}
}
}
//END
//BEGIN Private Slot Methods
void TcpTransportBridge::slotOnSocketConnectTimeout()
{
kdDebug (14140) << k_funcinfo << "Bridge (" << name() << ") CONNECT timeout." << endl;
emit bridgeConnectTimeout();
mSocket->deleteLater();
mSocket = 0l;
}
//END
TcpTransportBridge::Buffer::Buffer(TQ_UINT32 length)
: TQByteArray(length)
{
}
TcpTransportBridge::Buffer::~Buffer()
{
}
//BEGIN Public Methods
void TcpTransportBridge::Buffer::write(const TQByteArray& bytes)
{
resize(size() + bytes.size());
for (uint i=0; i < bytes.size(); i++){
(*this)[size() + i] = bytes[i];
}
}
TQByteArray TcpTransportBridge::Buffer::read(TQ_UINT32 length)
{
if (length >= size()) return TQByteArray();
TQByteArray buffer;
buffer.duplicate(data(), length);
char *bytes = new char[size() - length];
for(uint i=0; i < size() - length; i++){
bytes[i] = data()[length + i];
}
duplicate(bytes, size() - length);
delete[] bytes;
return buffer;
}
//END
}
#include "transport.moc"
|