PageRenderTime 103ms CodeModel.GetById 41ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 0ms

/src/libtomahawk/network/Connection.h

http://github.com/tomahawk-player/tomahawk
C Header | 149 lines | 82 code | 32 blank | 35 comment | 0 complexity | f01e5541c5d4caad4279467b78c4c35a MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *   Copyright 2013,      Uwe L. Korn <uwelk@xhochy.com>
  6 *
  7 *   Tomahawk is free software: you can redistribute it and/or modify
  8 *   it under the terms of the GNU General Public License as published by
  9 *   the Free Software Foundation, either version 3 of the License, or
 10 *   (at your option) any later version.
 11 *
 12 *   Tomahawk is distributed in the hope that it will be useful,
 13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 *   GNU General Public License for more details.
 16 *
 17 *   You should have received a copy of the GNU General Public License
 18 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20
 21#ifndef CONNECTION_H
 22#define CONNECTION_H
 23
 24#include "Typedefs.h"
 25#include "DllMacro.h"
 26
 27#include <QHostAddress>
 28#include <QPointer>
 29#include <QString>
 30#include <QTcpSocket>
 31#include <QVariant>
 32
 33class ConnectionPrivate;
 34class Servent;
 35
 36class DLLEXPORT Connection : public QObject
 37{
 38Q_OBJECT
 39
 40public:
 41
 42    Connection( Servent* parent );
 43    virtual ~Connection();
 44    virtual Connection* clone() = 0;
 45
 46    QString id() const;
 47    void setId( const QString& );
 48
 49    QString nodeId() const;
 50    void setNodeId( const QString& );
 51
 52    void setFirstMessage( const QVariant& m );
 53    void setFirstMessage( msg_ptr m );
 54    msg_ptr firstMessage() const;
 55
 56    const QPointer<QTcpSocket>& socket() const;
 57
 58    void setOutbound( bool o );
 59    bool outbound() const;
 60
 61    Servent* servent() const;
 62
 63    /**
 64     * Get public port of remote peer.
 65     */
 66    int peerPort() const;
 67    void setPeerPort( int p );
 68
 69    void markAsFailed();
 70
 71    void setName( const QString& n );
 72    QString name() const;
 73
 74    void setOnceOnly( bool b );
 75    bool onceOnly() const;
 76
 77    bool isReady() const;
 78    bool isRunning() const;
 79
 80    qint64 bytesSent() const;
 81    qint64 bytesReceived() const;
 82
 83    void setMsgProcessorModeOut( quint32 m );
 84    void setMsgProcessorModeIn( quint32 m );
 85
 86    const QHostAddress peerIpAddress() const;
 87
 88    QString bareName() const;
 89signals:
 90    /**
 91     * Emitted if the authentication of this connection finally failed.
 92     */
 93    void authFailed();
 94
 95    /**
 96     * Emitted if this connection was authenticated so that we can start talking to the other peer.
 97     */
 98    void authSuccessful();
 99
100    /**
101     * Emiited if the authentication could not be processed in a given timespan.
102     *
103     * Though this does implicate a permanent problem in establishing a connection to the other peer,
104     * this connection will be shutdown. In most cases this signal is emitted because we are waiting
105     * for a user decision which has not yet be given.
106     */
107    void authTimeout();
108    void ready();
109    void failed();
110    void finished();
111    void statsTick( qint64 tx_bytes_sec, qint64 rx_bytes_sec );
112    void socketClosed();
113    void socketErrored( QAbstractSocket::SocketError );
114
115protected:
116    virtual void setup() = 0;
117
118protected slots:
119    virtual void handleMsg( msg_ptr msg ) = 0;
120    virtual void authCheckTimeout();
121
122public slots:
123    virtual void start( QTcpSocket* sock );
124    void sendMsg( QVariant );
125    void sendMsg( msg_ptr );
126
127    void shutdown( bool waitUntilSentAll = false );
128
129private slots:
130    void handleIncomingQueueEmpty();
131    void sendMsg_now( msg_ptr );
132    void socketDisconnected();
133    void socketDisconnectedError( QAbstractSocket::SocketError );
134    void readyRead();
135    void doSetup();
136    void checkACL();
137    void aclDecision( Tomahawk::ACLStatus::Type status );
138    void bytesWritten( qint64 );
139    void calcStats();
140
141private:
142    Q_DECLARE_PRIVATE( Connection )
143    ConnectionPrivate* d_ptr;
144
145    void handleReadMsg();
146    void actualShutdown();
147};
148
149#endif // CONNECTION_H