UdpConnection.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 "Imports.h"
00025 #include "UdpConnection.h"
00026 #include "UdpSwitch.h"
00027
00028 namespace Protocols {
00029 namespace Transports {
00030
00031
00032
00033 class UdpConnectionPrivate
00034 {
00035 REFERENCE_OBJECT (UdpConnectionPrivate)
00036
00037 public:
00038 UdpSwitch *udpSwitch;
00039 QQueue <QByteArray> readQueue;
00040 qint64 headDatagramReadBytes;
00041
00042 UdpConnectionPrivate (UdpSwitch *udpSwitch)
00043 : udpSwitch (udpSwitch), readQueue(), headDatagramReadBytes (0)
00044 {}
00045 };
00046
00047 }
00048 }
00049
00050 using namespace Protocols::Transports;
00051
00052 UdpConnection::UdpConnection (UdpSwitch *udpSwitch)
00053 : p (new UdpConnectionPrivate (udpSwitch))
00054 {
00055
00056 }
00057
00058 UdpConnection::~UdpConnection()
00059 {
00060
00061 }
00062
00063 void UdpConnection::doConnectToNode (const NodeAddress &nodeAddress)
00064 {
00065 Q_ASSERT (!nodeAddress.isNull());
00066
00067 if (p->udpSwitch->openChannel (this, nodeAddress)) {
00068 setState (Connection::ConnectedState);
00069 setOpenMode (QIODevice::ReadWrite | QIODevice::Unbuffered);
00070
00071 emit connectionEstablished();
00072 } else {
00073 emit connectionFailed();
00074 }
00075 }
00076
00077 void UdpConnection::doDisconnectFromNode()
00078 {
00079 p->udpSwitch->closeChannel (this);
00080 setOpenMode (QIODevice::NotOpen);
00081
00082 emit connectionClosed();
00083 }
00084
00085 void UdpConnection::datagramRead (const QByteArray &datagram)
00086 {
00087 p->readQueue.enqueue (datagram);
00088 emit readyRead();
00089 }
00090
00091 qint64 UdpConnection::readData (char * data, qint64 maxSize)
00092 {
00093
00094
00095
00096 if (p->readQueue.isEmpty())
00097 return 0;
00098
00099 if (p->readQueue.head().length() - p->headDatagramReadBytes >= maxSize) {
00100 memcpy (data, p->readQueue.head().data() + p->headDatagramReadBytes, maxSize);
00101 p->headDatagramReadBytes += maxSize;
00102 if (p->headDatagramReadBytes == p->readQueue.head().length()) {
00103 p->headDatagramReadBytes = 0;
00104 p->readQueue.dequeue();
00105 }
00106 return maxSize;
00107 } else {
00108
00109 return -1;
00110 }
00111 }
00112
00113 qint64 UdpConnection::writeData (const char * data, qint64 maxSize)
00114 {
00115 if (p->udpSwitch->writeDatagram (this, data, maxSize)) {
00116
00117 QMetaObject::invokeMethod (this, "bytesWritten", Qt::QueuedConnection,
00118 QGenericReturnArgument(),
00119 Q_ARG (qint64, maxSize));
00120 return maxSize;
00121 } else {
00122
00123 return -1;
00124 }
00125 }