sender.cpp

Go to the documentation of this file.
00001 #include <QtGui>
00002 #include <QtNetwork>
00003 
00004 #include "sender.h"
00005 
00006 
00007 Sender::Sender(QWidget *parent)
00008     : QWidget(parent)
00009 {
00010     udpSocket = new QUdpSocket(this);
00011 
00012     connect(udpSocket, SIGNAL(connected()), SLOT(udp_connected()));
00013     connect(udpSocket, SIGNAL(disconnected()), SLOT(udp_disconnected()));
00014     connect(udpSocket, SIGNAL(readyRead()), SLOT(readPending()));
00015     connect(udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(udp_stateChanged(QAbstractSocket::SocketState)));
00016 }
00017 
00018 void Sender::udp_stateChanged( QAbstractSocket::SocketState state )
00019 {
00020     if (state != QAbstractSocket::UnconnectedState)
00021         emit connectionClose();
00022     if (state == QAbstractSocket::ConnectedState)
00023         emit connectionOpen();
00024 //  if (state == QAbstractSocket::BoundState)
00025 //      emit connectionBound();
00026 }
00027 
00028 void Sender::readPending()
00029 {
00030     while (udpSocket->hasPendingDatagrams()) 
00031     {
00032         QByteArray datagram;
00033         datagram.resize(udpSocket->pendingDatagramSize());
00034         
00035         udpSocket->readDatagram(datagram.data(), datagram.size() );
00036         emit received(datagram);
00037     }
00038 }
00039 
00040 bool Sender::isConnected()
00041 {
00042     QAbstractSocket::SocketState state = udpSocket->state();
00043     
00044     if (state == QAbstractSocket::ConnectedState)
00045         return true;
00046     
00047     return false;
00048 }
00049 
00050 void Sender::connectUrl(QUrl url)
00051 {
00052     udpSocket->connectToHost( url.host(), url.port() );
00053 }
00054 
00055 void Sender::sendText(QString text)
00056 {
00057     if ( isConnected() )
00058         udpSocket->write( text.toAscii() );
00059     else
00060         emit connectionClose();
00061 }
00062 
00063 void Sender::disconnect()
00064 {
00065     udpSocket->disconnectFromHost();
00066 }
00067 
00068 void Sender::udp_connected()
00069 {
00070     emit connectionOpen();
00071 }
00072 
00073 void Sender::udp_disconnected()
00074 {
00075     emit connectionClose();
00076 }
00077 

Generated on Sun Oct 28 22:51:29 2007 for BBoB by  doxygen 1.5.3