File indexing completed on 2024-04-28 07:54:08

0001 
0002 /***************************************************************************
0003                    jabberbytestream.cpp  -  Byte Stream for Jabber
0004                              -------------------
0005     begin                : Wed Jul 7 2004
0006     copyright            : (C) 2004 by Till Gerken <till@tantalo.net>
0007 
0008                Kopete (C) 2004 Kopete developers <kopete-devel@kde.org>
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *   This program is free software; you can redistribute it and/or modify  *
0014  *   it under the terms of the GNU Lesser General Public License as        *
0015  *   published by the Free Software Foundation; either either version 2
0016    of the License, or (at your option) any later version.1 of the  *
0017  *   License, or (at your option) any later version.                       *
0018  *                                                                         *
0019  ***************************************************************************/
0020 
0021 #include <QObject>
0022 #include "jabber_protocol_debug.h"
0023 #include "jabberbytestream.h"
0024 // #include "jabberprotocol.h"
0025 
0026 JabberByteStream::JabberByteStream ( QObject *parent )
0027  : ByteStream ( parent )
0028 {
0029     qCDebug(JABBER_PROTOCOL_LOG) << "Instantiating new Jabber byte stream.";
0030 
0031     // reset close tracking flag
0032     mClosing = false;
0033 
0034     mSocket = nullptr;
0035 }
0036 
0037 void JabberByteStream::connect ( QString host, int port )
0038 {
0039     qCDebug(JABBER_PROTOCOL_LOG) << Q_FUNC_INFO << "Connecting to " << host << ", port " << port ;
0040 
0041     mClosing = false;
0042 
0043     mSocket = std::make_shared<QTcpSocket>(new QTcpSocket());
0044   mSocket->connectToHost(host, port);
0045 
0046     QObject::connect ( mSocket.get(), SIGNAL (error(QAbstractSocket::SocketError)), this, SLOT (slotError(QAbstractSocket::SocketError)) );
0047     QObject::connect ( mSocket.get(), &QAbstractSocket::connected, this, &JabberByteStream::slotConnected );
0048     QObject::connect ( mSocket.get(), &QAbstractSocket::disconnected, this, &JabberByteStream::slotConnectionClosed );
0049     QObject::connect ( mSocket.get(), &QIODevice::readyRead, this, &JabberByteStream::slotReadyRead );
0050     QObject::connect ( mSocket.get(), &QIODevice::bytesWritten, this, &JabberByteStream::slotBytesWritten );
0051 }
0052 
0053 bool JabberByteStream::isOpen () const
0054 {
0055 
0056     // determine if socket is open
0057     return socket()->isOpen ();
0058 
0059 }
0060 
0061 void JabberByteStream::close ()
0062 {
0063     qCDebug(JABBER_PROTOCOL_LOG) << "Closing stream.";
0064 
0065     // close the socket and set flag that we are closing it ourselves
0066     mClosing = true;
0067         if (mSocket) {
0068              qCDebug(JABBER_PROTOCOL_LOG) << Q_FUNC_INFO << "socket is not null";
0069          mSocket->close();
0070              qCDebug(JABBER_PROTOCOL_LOG) << Q_FUNC_INFO << "socket closed";
0071              mSocket=nullptr;
0072         }
0073 }
0074 
0075 int JabberByteStream::tryWrite ()
0076 {
0077 
0078     // send all data from the buffers to the socket
0079     QByteArray writeData = takeWrite();
0080     socket()->write ( writeData.data (), writeData.size () );
0081 
0082     return writeData.size ();
0083 
0084 }
0085 
0086 QTcpSocket *JabberByteStream::socket () const
0087 {
0088 
0089     return mSocket.get();
0090 
0091 }
0092 
0093 JabberByteStream::~JabberByteStream ()
0094 {
0095 
0096 }
0097 
0098 void JabberByteStream::slotConnected ()
0099 {
0100 
0101     emit connected ();
0102 
0103 }
0104 
0105 void JabberByteStream::slotConnectionClosed ()
0106 {
0107     qCDebug(JABBER_PROTOCOL_LOG) << "Socket has been closed.";
0108 
0109     // depending on who closed the socket, emit different signals
0110     if ( !mClosing )
0111     {
0112         emit connectionClosed ();
0113     }
0114     else
0115     {
0116         emit delayedCloseFinished ();
0117     }
0118 
0119     mClosing = false;
0120 
0121 }
0122 
0123 void JabberByteStream::slotReadyRead ()
0124 {
0125     qCDebug(JABBER_PROTOCOL_LOG) << "called:  available: " << socket()->bytesAvailable ();
0126     appendRead ( socket()->readAll() );
0127 
0128     emit readyRead ();
0129 
0130 }
0131 
0132 void JabberByteStream::slotBytesWritten ( qint64 bytes )
0133 {
0134 
0135     emit bytesWritten ( bytes );
0136 
0137 }
0138 
0139 void JabberByteStream::slotError ( QAbstractSocket::SocketError code )
0140 {
0141     qCDebug(JABBER_PROTOCOL_LOG) << "Socket error '" <<  mSocket->errorString() <<  "' - Code : " << code;
0142     emit error ( code );
0143 }
0144 
0145 #include "moc_jabberbytestream.cpp"