TcpConnection.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "Imports.h"
00025 #include "TcpConnection.h"
00026 
00027 namespace Protocols {
00028 namespace Transports {
00029 
00030 enum Constants
00031 {
00032     ConnectTimeout = 10000
00033 };
00034 
00035 } // namespace Transports;
00036 } // namespace Protocols;
00037 
00038 using namespace Protocols::Transports;
00039 
00040 TcpConnection::TcpConnection (QTcpSocket *s)
00041  :  socket (s), timer (0)
00042 {
00043     qDebug() << "TcpConnection::TcpConnection";
00044 
00045     if (socket == 0)
00046         socket = new QTcpSocket;
00047 
00048     if (socket->state() == QAbstractSocket::UnconnectedState)
00049         setState (DisconnectedState);
00050     else if (socket->state() == QAbstractSocket::ConnectingState
00051                 || socket->state() == QAbstractSocket::HostLookupState)
00052         setState (Connection::ConnectingState);
00053     else if (socket->state() == QAbstractSocket::ConnectedState) {
00054         // \todo Incoming connections (from QTcpServer) should also be unbuffered.
00055         setState (Connection::ConnectedState);
00056         setOpenMode (QIODevice::ReadWrite | QIODevice::Unbuffered);
00057     } else
00058         setState (Connection::DisconnectingState);
00059     // \todo We should never get a socket that is bound or listening.
00060 
00061     setRemoteNodeAddress (NodeAddress (socket->peerAddress(), socket->peerPort()));
00062 
00063     QObject::connect (socket,   SIGNAL  (connected()),
00064                       this,     SLOT    (slotConnected()));
00065     QObject::connect (socket,   SIGNAL  (disconnected()),
00066                       this,     SLOT    (slotDisconnected()));
00067     QObject::connect (socket,   SIGNAL  (error(QAbstractSocket::SocketError)),
00068                       this,     SLOT    (slotError(QAbstractSocket::SocketError)));
00069     QObject::connect (socket,   SIGNAL  (readyRead()),
00070                       this,     SIGNAL  (readyRead()));
00071     QObject::connect (socket,   SIGNAL  (bytesWritten (qint64)),
00072                       this,     SIGNAL  (bytesWritten (qint64)));
00073 
00074     qDebug() << "TcpConnection::TcpConnection exit";
00075 }
00076 
00077 TcpConnection::~TcpConnection()
00078 {
00079     qDebug() << "TcpConnection::~TcpConnection";
00080     //Q_ASSERT (state() == DisconnectedState);
00081 
00082     if (socket)
00083         socket->disconnect (this);
00084     delete socket;
00085     delete timer;
00086     qDebug() << "TcpConnection::~TcpConnection exit";
00087 }
00088 
00089 void TcpConnection::startTimer()
00090 {
00091     timer = new QTimer;
00092     connect (timer, SIGNAL (timeout()), this, SLOT (connectTimeout()));
00093     timer->setSingleShot (true);
00094     timer->start (ConnectTimeout);
00095 }
00096 
00097 void TcpConnection::stopTimer()
00098 {
00099     delete timer;
00100     timer = 0;
00101 }
00102 
00103 void TcpConnection::doConnectToNode (const NodeAddress &nodeAddress)
00104 {
00105     Q_ASSERT (!nodeAddress.isNull());
00106 
00107     socket->connectToHost (nodeAddress.hostAddress(), nodeAddress.hostPort(),
00108                            QIODevice::ReadWrite | QIODevice::Unbuffered);
00109     startTimer();
00110 }
00111 
00112 void TcpConnection::doDisconnectFromNode()
00113 {
00114     socket->disconnectFromHost();
00115 }
00116 
00117 void TcpConnection::slotConnected()
00118 {
00119     stopTimer();
00120 
00121     setState (ConnectedState);
00122     setOpenMode (QIODevice::ReadWrite | QIODevice::Unbuffered);
00123 
00124     if (remoteNodeAddress() == NodeAddress::Null) {
00125         NodeAddress address (socket->peerAddress(), socket->peerPort());
00126         setRemoteNodeAddress (address);
00127     }
00128     emit connectionEstablished();
00129 }
00130 
00131 void TcpConnection::connectTimeout()
00132 {
00133     qDebug() << "TcpConnection::connectTimeout";
00134     Q_ASSERT (state() == ConnectingState);
00135 
00136     setState (TimeoutState);
00137     socket->abort();
00138     stopTimer();
00139 
00140     qDebug() << "TcpConnection::connectTimeout exit";
00141 }
00142 
00143 void TcpConnection::slotDisconnected()
00144 {
00145     qDebug() << "TcpConnection::slotDisconnected";
00146     if (state() == TimeoutState)
00147         emit connectionFailed();
00148     else {
00149         setState (DisconnectedState);
00150         emit connectionClosed();
00151     }
00152     setOpenMode (QIODevice::NotOpen);
00153     qDebug() << "TcpConnection::slotDisconnectedExit";
00154 }
00155 
00156 void TcpConnection::slotError(QTcpSocket::SocketError)
00157 {
00158     qDebug() << "TcpConnection::slotError";
00159     socket->close();
00160     if (state() == ConnectingState) {
00161         setState (DisconnectedState);
00162         // Stop the connection timeout timer:
00163         stopTimer();
00164         emit connectionFailed();
00165     } /* \todo That is the Disconnecting... bug!
00166       else if (socket->error() == QTcpSocket::RemoteHostClosedError) {
00167         slotDisconnected();
00168     }*/
00169     setOpenMode (QIODevice::NotOpen);
00170     qDebug() << "TcpConnection::slotError exit";
00171 }
00172 
00173 qint64 TcpConnection::readData (char * data, qint64 maxSize)
00174 {
00175     return socket->read (data, maxSize);
00176 }
00177 
00178 qint64 TcpConnection::writeData (const char * data, qint64 maxSize)
00179 {
00180     return socket->write (data, maxSize);
00181 }