Protocols::Generics::TcpSocket Class Reference

#include <TcpSocket.h> [code]

Inherits QObject, and Protocols::Generics::Socket.

Inheritance diagram for Protocols::Generics::TcpSocket:

Inheritance graph
[legend]
Collaboration diagram for Protocols::Generics::TcpSocket:

Collaboration graph
[legend]
List of all members.

Detailed Description

A thin wrapper around QTcpSocket.

This class implements the Socket interface on top of QTcpSocket. It converts the Qt signals emitted by QTcpSockets to calls to the SocketStatus interface. It also makes sure that its notifications are sent synchronously or asynchronously in a consistent manner:

Todo:
do not send socketRead() or socketWritten() from within read() or write(). These should be sent only when the buffers have been empty/full and that has changed.

Definition at line 49 of file TcpSocket.h.

Public Member Functions

 TcpSocket (auto_ptr< QTcpSocket > socket, SocketStatus *status)
 Constructs a new TcpSocket object and attaches it to socket.
 ~TcpSocket ()
 Destroys a TcpSocket object. If not disconnected abort() is called.
void connectToHost (const QHostAddress &address, quint16 port)
 Tries to establish a connection to the remote host.
void disconnectFromHost ()
 Disconnects the socket from the remote Host.
void abort ()
 Aborts all ongoing operations and closes the socket.
qint64 read (char *destination, qint64 size)
 Reads data from socket.
qint64 write (const char *source, qint64 size)
 Writes data to socket.

Private Slots

void slotConnected ()
 Sends the socketConnected() status notification.
void slotDisconnected ()
 Sends the socketDisconnected() status notification.
void slotError ()
 Schedules a delayed socketError() status notification.
void slotRead ()
 Sends the socketRead() status notification.
void slotWritten ()
 Sends the socketWritten() status notification.
void delayedError ()
 A slot called after the slotError() gets called to report an error.
void delayedDisconnectFromHost ()
 A slot called after disconnectFromHost() was called.

Private Attributes

auto_ptr< QTcpSocket > socket
SocketStatusstatus


Constructor & Destructor Documentation

TcpSocket::TcpSocket auto_ptr< QTcpSocket >  socket_,
SocketStatus status_
 

Constructs a new TcpSocket object and attaches it to socket.

socket_ will be owned by the TcpSocket object. status_ is the required interface over which send status notifications.

Note:
When passing in a connected QTcpSocket socket to the ctor, it should be opened in unbuffered mode bypassing both QIODevice and QAbstractSocket buffers!

Definition at line 36 of file TcpSocket.cpp.

TcpSocket::~TcpSocket  ) 
 

Destroys a TcpSocket object. If not disconnected abort() is called.

Definition at line 60 of file TcpSocket.cpp.


Member Function Documentation

void TcpSocket::abort  )  [virtual]
 

Aborts all ongoing operations and closes the socket.

Implements Protocols::Generics::Socket.

Definition at line 92 of file TcpSocket.cpp.

void TcpSocket::connectToHost const QHostAddress &  address,
quint16  port
[virtual]
 

Tries to establish a connection to the remote host.

Implements Protocols::Generics::Socket.

Definition at line 71 of file TcpSocket.cpp.

void TcpSocket::delayedDisconnectFromHost  )  [private, slot]
 

A slot called after disconnectFromHost() was called.

This is to ensure that the slotDisconnected() is not entered from within the TcpSocket::disconnectFromHost() call.

Definition at line 178 of file TcpSocket.cpp.

void TcpSocket::delayedError  )  [private, slot]
 

A slot called after the slotError() gets called to report an error.

Definition at line 168 of file TcpSocket.cpp.

void TcpSocket::disconnectFromHost  )  [virtual]
 

Disconnects the socket from the remote Host.

socket->disconnectFromHost() could emit disconnected() emidiately in case the write buffer is empty! We want our status socketDisconnected() to always be sent asynchronously, so we invoke the socket method with a delay.

Implements Protocols::Generics::Socket.

Definition at line 84 of file TcpSocket.cpp.

qint64 TcpSocket::read char *  destination,
qint64  size
[virtual]
 

Reads data from socket.

destination is the starting address of the buffer where the read bytes should be copied to. size is the size of the destination buffer. At most size bytes will be copied to it.

Returns:
the number of bytes actually read from the socket and copied to destination.
Precondition:
destination != 0 && size >= 0

Implements Protocols::Generics::Socket.

Definition at line 108 of file TcpSocket.cpp.

void TcpSocket::slotConnected  )  [private, slot]
 

Sends the socketConnected() status notification.

Definition at line 132 of file TcpSocket.cpp.

void TcpSocket::slotDisconnected  )  [private, slot]
 

Sends the socketDisconnected() status notification.

Definition at line 138 of file TcpSocket.cpp.

void TcpSocket::slotError  )  [private, slot]
 

Schedules a delayed socketError() status notification.

Because the user is likely to abort() upon an error, we don't want to abort() from within a failing read/write operation, therefore we use Qt::QueuedConnection to schedule a delayed call to delayedError(), which will send the socketError() notification.

Definition at line 150 of file TcpSocket.cpp.

void TcpSocket::slotRead  )  [private, slot]
 

Sends the socketRead() status notification.

Definition at line 156 of file TcpSocket.cpp.

void TcpSocket::slotWritten  )  [private, slot]
 

Sends the socketWritten() status notification.

Definition at line 162 of file TcpSocket.cpp.

qint64 TcpSocket::write const char *  source,
qint64  size
[virtual]
 

Writes data to socket.

source is the starting address of the buffer from which the bytes should be written to the socket. size is the size of the source buffer. At most size bytes will be written from it.

Returns:
the number of bytes actually read from source and writtne to the socket.
Precondition:
source != 0 && size >= 0

Implements Protocols::Generics::Socket.

Definition at line 125 of file TcpSocket.cpp.


Member Data Documentation

auto_ptr<QTcpSocket> Protocols::Generics::TcpSocket::socket [private]
 

Definition at line 76 of file TcpSocket.h.

SocketStatus* Protocols::Generics::TcpSocket::status [private]
 

Definition at line 77 of file TcpSocket.h.


The documentation for this class was generated from the following files: