Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
coretransfer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 by the Quassel Project *
3  * devel@quassel-irc.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) version 3. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include <QtEndian>
22 
23 #include <QCoreApplication>
24 #include <QTcpSocket>
25 
26 #include "coretransfer.h"
27 
28 const qint64 chunkSize = 16 * 1024;
29 
31 
32 CoreTransfer::CoreTransfer(Direction direction, const QString &nick, const QString &fileName, const QHostAddress &address, quint16 port, quint64 fileSize, QObject *parent)
33  : Transfer(direction, nick, fileName, address, port, fileSize, parent),
34  _socket(0),
35  _pos(0),
36  _reading(false)
37 {
38 
39 }
40 
41 
43 {
44  if (_socket) {
45  _socket->close();
46  _socket->deleteLater();
47  _socket = 0;
48  }
49 
50  _buffer.clear();
51  _reading = false;
52 }
53 
54 
56 {
57  if (state() == Connecting || state() == Transferring) {
58  setError(tr("Socket closed while still transferring!"));
59  }
60  else
61  cleanUp();
62 }
63 
64 
65 void CoreTransfer::onSocketError(QAbstractSocket::SocketError error)
66 {
67  Q_UNUSED(error)
68 
69  if (state() == Connecting || state() == Transferring) {
70  setError(tr("DCC connection error: %1").arg(_socket->errorString()));
71  }
72 }
73 
74 
76 {
77  if (_peer || !peer || state() != New)
78  return; // transfer was already accepted
79 
80  _peer = peer;
82 
83  emit accepted(peer);
84 
85  // FIXME temporary until we have queueing
86  start();
87 }
88 
89 
91 {
92  if (_peer || state() != New)
93  return;
94 
95  _peer = peer;
97 
98  emit rejected(peer);
99 }
100 
101 
103 {
104  if (!_peer || state() != Pending || direction() != Receive)
105  return;
106 
108 }
109 
110 
112 {
113  if (port() == 0) {
114  setError(tr("Reverse DCC not supported yet!"));
115  return;
116  }
117 
119 
120  _socket = new QTcpSocket(this);
121  connect(_socket, SIGNAL(connected()), SLOT(startReceiving()));
122  connect(_socket, SIGNAL(disconnected()), SLOT(onSocketDisconnected()));
123  connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError)));
124  connect(_socket, SIGNAL(readyRead()), SLOT(onDataReceived()));
125 
126  _socket->connectToHost(address(), port());
127 }
128 
129 
131 {
133 }
134 
135 
137 {
138  if (_reading) // since we're spinning the event loop, we may get another readyRead() and thus reentrancy
139  return;
140  _reading = true;
141 
142  while (_socket->bytesAvailable()) {
143  QByteArray data = _socket->read(chunkSize);
144  _pos += data.size();
145  if (!relayData(data, true))
146  return;
147 
148  QCoreApplication::processEvents(); // don't block the rest of the core/client communication
149  if (!_socket) // just in case something happened during spinning the event loop that killed our socket
150  return;
151  }
152 
153  // Send ack to sender. The DCC protocol only specifies 32 bit values, but modern clients (i.e. those who can send files
154  // larger than 4 GB) will ignore this anyway...
155  quint32 ack = qToBigEndian((quint32)_pos);// qDebug() << Q_FUNC_INFO << _pos;
156  _socket->write((char *)&ack, 4);
157 
158  if (_pos > fileSize()) {
159  qWarning() << "DCC Receive: Got more data than expected!";
160  setError(tr("DCC Receive: Got more data than expected!"));
161  }
162  else if (_pos == fileSize()) {
163  qDebug() << "DCC Receive: Transfer finished";
164  if (relayData(QByteArray(), false)) // empty buffer
166  }
167 
168  _reading = false;
169 }
170 
171 
172 bool CoreTransfer::relayData(const QByteArray &data, bool requireChunkSize)
173 {
174  // safeguard against a disconnecting quasselclient
175  if (!_peer) {
176  setError(tr("DCC Receive: Quassel Client disconnected during transfer!"));
177  return false;
178  }
179  _buffer.append(data);
180 
181  // we only want to send data to the client once we have reached the chunksize
182  if (_buffer.size() > 0 && (_buffer.size() >= chunkSize || !requireChunkSize)) {
184  _buffer.clear();
185  }
186 
187  return true;
188 }