TcpSocket.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 "TcpSocket.h"
00025 #include "Imports.cpp"
00026 
00028 
00036 TcpSocket::TcpSocket (auto_ptr <QTcpSocket> socket_, SocketStatus *status_)
00037     : socket (socket_), status (status_)
00038 {
00039     Q_ASSERT (socket->state() == QAbstractSocket::UnconnectedState
00040               || socket->state() == QAbstractSocket::ConnectedState);
00041 
00042     // localhost connections on BSD will be establihsed directly and we don't
00043     // want our socketConnected() called from within connectToNode() because in
00044     // this case we could emit connected() from withing connectToNode() and that
00045     // is not the specified behavior. Therefore we use Qt::QueuedConnection.
00046     QObject::connect (socket.get(), SIGNAL  (connected()),
00047                       this,         SLOT    (slotConnected()),
00048                       Qt::QueuedConnection);
00049     QObject::connect (socket.get(), SIGNAL  (disconnected()),
00050                       this,         SLOT    (slotDisconnected()));
00051     QObject::connect (socket.get(), SIGNAL  (error(QAbstractSocket::SocketError)),
00052                       this,         SLOT    (slotError()));
00053     QObject::connect (socket.get(), SIGNAL  (readyRead()),
00054                       this,         SLOT    (slotRead()));
00055     QObject::connect (socket.get(), SIGNAL  (bytesWritten (qint64)),
00056                       this,         SLOT    (slotWritten()));
00057 }
00058 
00060 TcpSocket::~TcpSocket()
00061 {
00062     // Disconnects our slots, we want no slots sending status notifications now:
00063     socket->disconnect (this);
00064     if (socket->state() != QAbstractSocket::UnconnectedState)
00065         abort();
00066     socket->deleteLater();
00067     socket.release(); // Qt will delete the object later.
00068 }
00069 
00071 void TcpSocket::connectToHost (const QHostAddress &address, quint16 port)
00072 {
00073     Q_ASSERT (socket->state() == QAbstractSocket::UnconnectedState);
00074     socket->connectToHost (address, port,
00075                            QIODevice::ReadWrite | QIODevice::Unbuffered);
00076 }
00077 
00079 
00084 void TcpSocket::disconnectFromHost()
00085 {
00086     Q_ASSERT (socket->state() == QAbstractSocket::ConnectedState);
00087     QMetaObject::invokeMethod (this, "delayedDisconnectFromHost",
00088                                Qt::QueuedConnection);
00089 }
00090 
00092 void TcpSocket::abort()
00093 {
00094     socket->abort();
00095 }
00096 
00098 
00108 qint64 TcpSocket::read (char *destination, qint64 size)
00109 {
00110     Q_ASSERT (destination != 0 && size >= 0);
00111     return socket->read (destination, size);
00112 }
00113 
00115 
00125 qint64 TcpSocket::write (const char *source, qint64 size)
00126 {
00127     Q_ASSERT (source != 0 && size >= 0);
00128     return socket->write (source, size);
00129 }
00130 
00132 void TcpSocket::slotConnected()
00133 {
00134     status->socketConnected();
00135 }
00136 
00138 void TcpSocket::slotDisconnected()
00139 {
00140     status->socketDisconnected();
00141 }
00142 
00144 
00150 void TcpSocket::slotError()
00151 {
00152     QMetaObject::invokeMethod (this, "delayedError", Qt::QueuedConnection);
00153 }
00154 
00156 void TcpSocket::slotRead()
00157 {
00158     status->socketRead();
00159 }
00160 
00162 void TcpSocket::slotWritten()
00163 {
00164     status->socketWritten();
00165 }
00166 
00168 void TcpSocket::delayedError()
00169 {
00170     status->socketError();
00171 }
00172 
00174 
00178 void TcpSocket::delayedDisconnectFromHost()
00179 {
00180     socket->disconnectFromHost();
00181 }