TcpSocket.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00043
00044
00045
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
00063 socket->disconnect (this);
00064 if (socket->state() != QAbstractSocket::UnconnectedState)
00065 abort();
00066 socket->deleteLater();
00067 socket.release();
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 }