• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdenetwork
  • Sitemap
  • Contact Us
 

kopete/protocols/messenger/libpapillon

networkstream.cpp

Go to the documentation of this file.
00001 //
00002 // NetworkStream
00003 //
00004 // Authors:
00005 //   Gregg Edghill (Gregg.Edghill@gmail.com)
00006 //
00007 // Copyright (C) 2007, Kopete (http://kopete.kde.org)
00008 //
00009 // The above copyright notice and this permission notice shall be
00010 // included in all copies or substantial portions of this software.
00011 //
00012 // THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
00013 // MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC
00014 // LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER
00015 // VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
00016 //
00017 
00018 #include "Papillon/Network/NetworkStream"
00019 #include <QtNetwork/QTcpSocket>
00020 #include <QtDebug>
00021 
00022 namespace Papillon
00023 {
00024 
00025 class NetworkStream::NetworkStreamPrivate
00026 {
00027     public:
00028         NetworkStreamPrivate() : closed(false) {}
00029 
00030         bool closed;
00031         bool ownsSocket;
00032         QByteArray rbuffer;
00033         QTcpSocket *socket;
00034 };
00035 
00036 NetworkStream::NetworkStream(QTcpSocket *socket, bool ownsSocket, QObject *parent) : ByteStreamBase(parent), d(new NetworkStreamPrivate())
00037 {
00038     Q_ASSERT(socket != 0l);
00039 
00040     d->socket = socket;
00041     // Connect the signal/slot
00042     QObject::connect(d->socket, SIGNAL(readyRead()), this,
00043     SLOT(socket_OnRead()));
00044 
00045     d->ownsSocket = ownsSocket;
00046 }
00047 
00048 NetworkStream::~NetworkStream()
00049 {
00050     delete d;
00051     d = 0l;
00052 }
00053 
00054 qint64 NetworkStream::bytesAvailable() const
00055 {
00056     return d->rbuffer.size();
00057 }
00058 
00059 bool NetworkStream::isOpen() const
00060 {
00061     return d->closed != true;
00062 }
00063 
00064 void NetworkStream::close()
00065 {
00066     // Determine whether the network stream
00067     // owns the underlying socket.
00068     if (d->ownsSocket)
00069     {
00070         // If so, close the socket.
00071         d->socket->close();
00072     }
00073 
00074     d->closed = true;
00075 }
00076 
00077 QByteArray NetworkStream::read(qint64 count)
00078 {
00079     if (isOpen() == false)
00080     {
00081         qDebug("%s: stream not open", Q_FUNC_INFO);
00082         return QByteArray();
00083     }
00084 
00085     // Read count bytes from the buffer.
00086     QByteArray bytes = d->rbuffer.left(count);
00087     // Remove the read bytes from the buffer.
00088     d->rbuffer.remove(0, count);
00089 
00090     qDebug("%s: read %lld bytes", Q_FUNC_INFO, count);
00091     return bytes;
00092 }
00093 
00094 QByteArray NetworkStream::readAll()
00095 {
00096     // Return a byte array with all the available
00097     // data from the read buffer.
00098     return read(bytesAvailable());
00099 }
00100 
00101 qint64 NetworkStream::write(const QByteArray & buffer)
00102 {
00103     qint64 count = -1;
00104     if (isOpen() == false)
00105     {
00106         qDebug("%s: stream not open", Q_FUNC_INFO);
00107         return count;
00108     }
00109 
00110     // Write the contents of the buffer to the socket.
00111     count = d->socket->write(buffer);
00112 
00113     qDebug("%s: sent %lld bytes", Q_FUNC_INFO, count);
00114     return count;
00115 }
00116 
00117 //BEGIN Socket Event Handling Functions
00118 
00119 void NetworkStream::socket_OnRead()
00120 {
00121     const qint64 count = d->socket->bytesAvailable();
00122     qDebug("%s: %lld bytes available", Q_FUNC_INFO, count);
00123 
00124     // If there is data to be read, write all received
00125     // data into the internal buffer.
00126     if (count > 0)
00127     {
00128         // Read all the data available from the socket.
00129         QByteArray bytes = d->socket->read(count);
00130         qDebug("%s: %i bytes read", Q_FUNC_INFO, bytes.size());
00131 
00132         // Append the read bytes to the buffer.
00133         d->rbuffer.append(bytes);
00134         // Signal that there is data to be read.
00135         emit readyRead();
00136     }
00137 }
00138 
00139 //END
00140 
00141 }
00142 
00143 #include "networkstream.moc"

kopete/protocols/messenger/libpapillon

Skip menu "kopete/protocols/messenger/libpapillon"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal