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
357
358
359
360
361
362
363
364
365
|
/*
kirctransfer.cpp - IRC transfer.
Copyright (c) 2003-2004 by Michel Hermier <[email protected]>
Kopete (c) 2003-2004 by the Kopete developers <[email protected]>
*************************************************************************
* *
* 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. *
* *
*************************************************************************
*/
#include <kdebug.h>
#include <kextsock.h>
#include <klocale.h>
#include <tqfile.h>
#include <tqtimer.h>
#include "kirctransfer.h"
using namespace KIRC;
Transfer::Transfer( Engine *engine, TQString nick,// TQString nick_peer_adress
Type type,
TQObject *parent, const char *name )
: TQObject( parent, name ),
m_engine(engine), m_nick(nick),
m_type(type), m_socket(0),
m_initiated(false),
m_file(0), m_fileName(TQString()), m_fileSize(0), m_fileSizeCur(0), m_fileSizeAck(0),
m_receivedBytes(0), m_receivedBytesLimit(0), m_sentBytes(0), m_sentBytesLimit(0)
{
}
Transfer::Transfer( Engine *engine, TQString nick,// TQString nick_peer_adress
Transfer::Type type,
TQString fileName, TQ_UINT32 fileSize, // put this in a TQVariant ?
TQObject *parent, const char *name )
: TQObject( parent, name ),
m_engine(engine), m_nick(nick),
m_type(type), m_socket(0),
m_initiated(false),
m_file(0), m_fileName(fileName), m_fileSize(fileSize), m_fileSizeCur(0), m_fileSizeAck(0),
m_receivedBytes(0), m_receivedBytesLimit(0), m_sentBytes(0), m_sentBytesLimit(0)
{
}
Transfer::Transfer( Engine *engine, TQString nick,// TQString nick_peer_adress
TQHostAddress hostAdress, TQ_UINT16 port, // put this in a TQVariant ?
Transfer::Type type,
TQString fileName, TQ_UINT32 fileSize, // put this in a TQVariant ?
TQObject *parent, const char *name )
: TQObject( parent, name ),
m_engine(engine), m_nick(nick),
m_type(type), m_socket(0),
m_initiated(false),
m_file(0), m_fileName(fileName), m_fileSize(fileSize), m_fileSizeCur(0), m_fileSizeAck(0),
m_receivedBytes(0), m_receivedBytesLimit(0), m_sentBytes(0), m_sentBytesLimit(0)
{
setSocket(new KExtendedSocket(hostAdress.toString(), port));
}
/*
Transfer::Transfer( Engine *engine, TQString nick,// TQString nick_peer_adress
Transfer::Type type, TQVariant properties,
TQObject *parent, const char *name )
: TQObject( parent, name ),
m_engine(engine), m_nick(nick),
m_type(type), m_socket(properties[socket]),
m_initiated(false),
m_file(0), m_fileName(properties[fileName]), m_fileSize(properties[fileSize]), m_fileSizeCur(0), m_fileSizeAck(0),
m_receivedBytes(0), m_receivedBytesLimit(0), m_sentBytes(0), m_sentBytesLimit(0)
{
if(!properites["socket"].isNull())
setSocket(properites["socket"]);
else if(!properites["hostAddress"].isNull() && !properites["hostPort"].isNull())
setSocket(new KExtendedSocket(properites["hostAddress"], properites["hostPort"]));
connect(this, TQT_SIGNAL(complete()),
this, TQT_SLOT(closeSocket()));
connect(this, TQT_SIGNAL(abort(TQString)),
this, TQT_SLOT(closeSocket()));
}
*/
Transfer::~Transfer()
{
closeSocket();
// m_file is automatically closed on destroy.
}
Transfer::tqStatus Transfer::status() const
{
if(m_socket)
{
// return (Transfer::tqStatus)m_socket->socketStatus();
return Connected;
}
return Error_NoSocket;
}
void Transfer::slotError( int error )
{
// Connection in progress.. This is a signal fired wrong
if (m_socket->socketStatus () != KExtendedSocket::connecting)
{
abort(KExtendedSocket::strError(m_socket->status(), m_socket->systemError()));
// closeSocket();
}
}
bool Transfer::initiate()
{
TQTimer *timer = 0;
if(m_initiated)
{
kdDebug(14121) << k_funcinfo << "Transfer allready initiated" << endl;
return false;
}
if(!m_socket)
{
kdDebug(14121) << k_funcinfo << "Socket not set" << endl;
return false;
}
m_initiated = true;
m_file.setName(m_fileName);
connect(this, TQT_SIGNAL(complete()),
this, TQT_SLOT(closeSocket()));
connect(this, TQT_SIGNAL(abort(TQString)),
this, TQT_SLOT(closeSocket()));
// connect(m_socket, TQT_SIGNAL(connectionClosed()),
// this, TQT_SLOT(slotConnectionClosed()));
// connect(m_socket, TQT_SIGNAL(delayedCloseFinished()),
// this, TQT_SLOT(slotConnectionClosed()));
connect(m_socket, TQT_SIGNAL(error(int)), // FIXME: connection failed: No such signal KExtendedSocket::error(int)
this, TQT_SLOT(slotError(int)));
switch( m_type )
{
case Chat:
kdDebug(14121) << k_funcinfo << "Stting up a chat." << endl;
connect(m_socket, TQT_SIGNAL(readyRead()),
this, TQT_SLOT(readyReadFileIncoming()));
break;
case FileIncoming:
kdDebug(14121) << k_funcinfo << "Stting up an incoming file transfer." << endl;
m_file.open(IO_WriteOnly);
connect(m_socket, TQT_SIGNAL(readyRead()),
this, TQT_SLOT(readyReadFileIncoming()));
break;
case FileOutgoing:
kdDebug(14121) << k_funcinfo << "Stting up an outgoing file transfer." << endl;
m_file.open(IO_ReadOnly);
connect(m_socket, TQT_SIGNAL(readyRead()),
this, TQT_SLOT(readyReadFileOutgoing()));
// timer = new TQTimer(this);
// connect(timer, TQT_SIGNAL(timeout()),
// this, TQT_SLOT(writeFileOutgoing()));
// timer->start(1000, false);
writeFileOutgoing(); // send a first packet.
break;
default:
kdDebug(14121) << k_funcinfo << "Closing transfer: Unknown extra initiation for type:" << m_type << endl;
m_socket->close();
return false;
break;
}
// if(status()==Idle)
if(m_socket->status()==KExtendedSocket::nothing)
m_socket->connect();
m_socket->enableRead(true);
m_socket->enableWrite(true);
m_socketDataStream.setDevice(m_socket);
// I wonder if calling this is really necessary
// As far as I understand, buffer (socket buffer at least) should be flushed while event-looping.
// But I'm not really sure of this, so I force the flush.
timer = new TQTimer(this);
connect(timer, TQT_SIGNAL(timeout()),
this, TQT_SLOT(flush()));
timer->start(1000, FALSE); // flush the streams at every seconds
return true;
}
bool Transfer::setSocket( KExtendedSocket *socket )
{
if (!m_socket)
{
m_socket = socket;
return true;
}
else
kdDebug(14121) << k_funcinfo << "Socket allready set" << endl;
return false;
}
void Transfer::closeSocket()
{
if(m_socket)
{
m_socket->close();
// m_socket->reset();
m_socket->deleteLater();
}
m_socket = 0;
}
/*
* This slot ensure that all the stream are flushed.
* This slot is called periodically internaly.
*/
void Transfer::flush()
{
/*
* Enure the incoming file content in case of a crash.
*/
if(m_file.isOpen() && m_file.isWritable())
m_file.flush();
/*
* Ensure that non interactive streams outputs (i.e file transfer acknowledge by example)
* are sent (Don't stay in a local buffer).
*/
if(m_socket && status() == Connected)
m_socket->flush();
}
void Transfer::userAbort(TQString msg)
{
emit abort(msg);
}
void Transfer::setCodec( TQTextCodec *codec )
{
switch( m_type )
{
case Chat:
m_socket_textStream.setCodec( codec );
break;
default:
// operation not permitted on this type.
break;
}
}
void Transfer::writeLine( const TQString &line )
{
switch( m_type )
{
case Chat:
// m_socket.flush();
break;
default:
// operation not permitted on this type.
break;
}
}
void Transfer::readyReadLine()
{
if( m_socket->canReadLine() )
{
TQString msg = m_socket_textStream.readLine();
emit readLine(msg);
}
}
void Transfer::readyReadFileIncoming()
{
kdDebug(14121) << k_funcinfo << endl;
m_bufferLength = m_socket->readBlock(m_buffer, sizeof(m_buffer));
if(m_bufferLength > 0)
{
int written = m_file.writeBlock(m_buffer, m_bufferLength);
if(m_bufferLength == written)
{
m_fileSizeCur += written;
m_fileSizeAck = m_fileSizeCur;
m_socketDataStream << m_fileSizeAck;
checkFileTransferEnd(m_fileSizeAck);
return;
}
else
// Something bad happened while writting.
abort(m_file.errorString());
}
else if(m_bufferLength == -1)
abort("Error while reading socket.");
}
void Transfer::readyReadFileOutgoing()
{
kdDebug(14121) << k_funcinfo << "Available bytes:" << m_socket->bytesAvailable() << endl;
bool hadData = false;
TQ_UINT32 fileSizeAck = 0;
// if (m_socket->bytesAvailable() >= sizeof(fileSizeAck)) // BUGGY: bytesAvailable() that allways return 0 on unbuffered sockets.
{
m_socketDataStream >> fileSizeAck;
hadData = true;
}
if (hadData)
{
checkFileTransferEnd(fileSizeAck);
writeFileOutgoing();
}
}
void Transfer::writeFileOutgoing()
{
kdDebug(14121) << k_funcinfo << endl;
if (m_fileSizeAck < m_fileSize)
{
m_bufferLength = m_file.readBlock(m_buffer, sizeof(m_buffer));
if (m_bufferLength > 0)
{
TQ_UINT32 read = m_socket->writeBlock(m_buffer, m_bufferLength); // should check written == read
// if(read != m_buffer_length)
// buffer is not cleared still
m_fileSizeCur += read;
// m_socket->flush(); // Should think on using this
emit fileSizeCurrent( m_fileSizeCur );
}
else if(m_bufferLength == -1)
abort("Error while reading file.");
}
}
void Transfer::checkFileTransferEnd(TQ_UINT32 fileSizeAck)
{
kdDebug(14121) << k_funcinfo << "Acknowledged:" << fileSizeAck << endl;
m_fileSizeAck = fileSizeAck;
emit fileSizeAcknowledge(m_fileSizeAck);
if(m_fileSizeAck > m_fileSize)
abort(i18n("Acknowledge size is greater than the expected file size"));
if(m_fileSizeAck == m_fileSize)
emit complete();
}
#include "kirctransfer.moc"
|