/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. #ifndef CONNECTION_H
  21. #define CONNECTION_H
  22. #include "Typedefs.h"
  23. #include "DllMacro.h"
  24. #include <QHostAddress>
  25. #include <QPointer>
  26. #include <QString>
  27. #include <QTcpSocket>
  28. #include <QVariant>
  29. class ConnectionPrivate;
  30. class Servent;
  31. class DLLEXPORT Connection : public QObject
  32. {
  33. Q_OBJECT
  34. public:
  35. Connection( Servent* parent );
  36. virtual ~Connection();
  37. virtual Connection* clone() = 0;
  38. QString id() const;
  39. void setId( const QString& );
  40. QString nodeId() const;
  41. void setNodeId( const QString& );
  42. void setFirstMessage( const QVariant& m );
  43. void setFirstMessage( msg_ptr m );
  44. msg_ptr firstMessage() const;
  45. const QPointer<QTcpSocket>& socket() const;
  46. void setOutbound( bool o );
  47. bool outbound() const;
  48. Servent* servent() const;
  49. /**
  50. * Get public port of remote peer.
  51. */
  52. int peerPort() const;
  53. void setPeerPort( int p );
  54. void markAsFailed();
  55. void setName( const QString& n );
  56. QString name() const;
  57. void setOnceOnly( bool b );
  58. bool onceOnly() const;
  59. bool isReady() const;
  60. bool isRunning() const;
  61. qint64 bytesSent() const;
  62. qint64 bytesReceived() const;
  63. void setMsgProcessorModeOut( quint32 m );
  64. void setMsgProcessorModeIn( quint32 m );
  65. const QHostAddress peerIpAddress() const;
  66. QString bareName() const;
  67. signals:
  68. /**
  69. * Emitted if the authentication of this connection finally failed.
  70. */
  71. void authFailed();
  72. /**
  73. * Emitted if this connection was authenticated so that we can start talking to the other peer.
  74. */
  75. void authSuccessful();
  76. /**
  77. * Emiited if the authentication could not be processed in a given timespan.
  78. *
  79. * Though this does implicate a permanent problem in establishing a connection to the other peer,
  80. * this connection will be shutdown. In most cases this signal is emitted because we are waiting
  81. * for a user decision which has not yet be given.
  82. */
  83. void authTimeout();
  84. void ready();
  85. void failed();
  86. void finished();
  87. void statsTick( qint64 tx_bytes_sec, qint64 rx_bytes_sec );
  88. void socketClosed();
  89. void socketErrored( QAbstractSocket::SocketError );
  90. protected:
  91. virtual void setup() = 0;
  92. protected slots:
  93. virtual void handleMsg( msg_ptr msg ) = 0;
  94. virtual void authCheckTimeout();
  95. public slots:
  96. virtual void start( QTcpSocket* sock );
  97. void sendMsg( QVariant );
  98. void sendMsg( msg_ptr );
  99. void shutdown( bool waitUntilSentAll = false );
  100. private slots:
  101. void handleIncomingQueueEmpty();
  102. void sendMsg_now( msg_ptr );
  103. void socketDisconnected();
  104. void socketDisconnectedError( QAbstractSocket::SocketError );
  105. void readyRead();
  106. void doSetup();
  107. void checkACL();
  108. void aclDecision( Tomahawk::ACLStatus::Type status );
  109. void bytesWritten( qint64 );
  110. void calcStats();
  111. private:
  112. Q_DECLARE_PRIVATE( Connection )
  113. ConnectionPrivate* d_ptr;
  114. void handleReadMsg();
  115. void actualShutdown();
  116. };
  117. #endif // CONNECTION_H