MythTV  master
mythsocket.h
Go to the documentation of this file.
1 
2 #ifndef MYTH_SOCKET_H
3 #define MYTH_SOCKET_H
4 
5 #include <QHostAddress>
6 #include <QStringList>
7 #include <QAtomicInt>
8 #include <QMutex>
9 #include <QHash>
10 
11 #include "referencecounter.h"
12 #include "mythsocket_cb.h"
13 #include "mythbaseexp.h"
14 #include "mthread.h"
15 
16 class QTcpSocket;
17 
25 class MBASE_PUBLIC MythSocket : public QObject, public ReferenceCounter
26 {
27  Q_OBJECT
28 
29  friend class MythSocketManager;
30 
31  public:
32  explicit MythSocket(qintptr socket = -1, MythSocketCBs *cb = nullptr,
33  bool use_shared_thread = false);
34 
35  bool ConnectToHost(const QString &hostname, quint16 port);
36  bool ConnectToHost(const QHostAddress &address, quint16 port);
37  void DisconnectFromHost(void);
38 
39  bool Validate(std::chrono::milliseconds timeout = kMythSocketLongTimeout,
40  bool error_dialog_desired = false);
41  bool IsValidated(void) const { return m_isValidated; }
42 
43  bool Announce(const QStringList &new_announce);
44  QStringList GetAnnounce(void) const { return m_announce; }
45  void SetAnnounce(const QStringList &new_announce);
46  bool IsAnnounced(void) const { return m_isAnnounced; }
47 
48  void SetReadyReadCallbackEnabled(bool enabled)
49  { m_disableReadyReadCallback.fetchAndStoreOrdered((enabled) ? 0 : 1); }
50 
51  bool SendReceiveStringList(
52  QStringList &list, uint min_reply_length = 0,
53  std::chrono::milliseconds timeoutMS = kLongTimeout);
54 
55  bool ReadStringList(QStringList &list, std::chrono::milliseconds timeoutMS = kShortTimeout);
56  bool WriteStringList(const QStringList &list);
57 
58  bool IsConnected(void) const;
59  bool IsDataAvailable(void);
60 
61  QHostAddress GetPeerAddress(void) const;
62  int GetPeerPort(void) const;
63  int GetSocketDescriptor(void) const;
64 
65  // RemoteFile stuff
66  int Write(const char *data, int size);
67  int Read(char *data, int size, std::chrono::milliseconds max_wait);
68  void Reset(void);
69 
70  static constexpr std::chrono::milliseconds kShortTimeout { kMythSocketShortTimeout };
71  static constexpr std::chrono::milliseconds kLongTimeout { kMythSocketLongTimeout };
72 
73  private:
74  inline QString LOC()
75  {
76  return QString("MythSocket(%1:%2): ")
77  .arg((intptr_t)(this), 0, 16).arg(GetSocketDescriptor());
78  }
79 
80 
81  signals:
82  void CallReadyRead(void);
83 
84  protected slots:
85  void ConnectHandler(void);
86  void ErrorHandler(QAbstractSocket::SocketError err);
87  void AboutToCloseHandler(void);
88  void DisconnectHandler(void);
89  void ReadyReadHandler(void);
90  void CallReadyReadHandler(void);
91 
92  void ReadStringListReal(QStringList *list, std::chrono::milliseconds timeoutMS, bool *ret);
93  void WriteStringListReal(const QStringList *list, bool *ret);
94  void ConnectToHostReal(const QHostAddress& addr, quint16 port, bool *ret);
95  void DisconnectFromHostReal(void);
96 
97  void WriteReal(const char *data, int size, int *ret);
98  void ReadReal(char *data, int size, std::chrono::milliseconds max_wait_ms, int *ret);
99  void ResetReal(void);
100 
101  void IsDataAvailableReal(bool *ret) const;
102 
103  protected:
104  ~MythSocket() override; // force reference counting
105 
106  QTcpSocket *m_tcpSocket {nullptr}; // only set in ctor
107  MThread *m_thread {nullptr}; // only set in ctor
108  mutable QMutex m_lock;
109  qintptr m_socketDescriptor {-1}; // protected by m_lock
110  QHostAddress m_peerAddress; // protected by m_lock
111  int m_peerPort {-1}; // protected by m_lock
112  MythSocketCBs *m_callback {nullptr}; // only set in ctor
113  bool m_useSharedThread; // only set in ctor
114  QAtomicInt m_disableReadyReadCallback {0};
115  bool m_connected {false}; // protected by m_lock
118  mutable QAtomicInt m_dataAvailable {0};
119  bool m_isValidated {false}; // only set in thread using MythSocket
120  bool m_isAnnounced {false}; // only set in thread using MythSocket
121  QStringList m_announce; // only set in thread using MythSocket
122 
123  static const int kSocketReceiveBufferSize;
124 
125  static QMutex s_loopbackCacheLock;
126  static QHash<QString, QHostAddress::SpecialAddress> s_loopbackCache;
127 
128  static QMutex s_thread_lock;
129  static MThread *s_thread; // protected by s_thread_lock
130  static int s_thread_cnt; // protected by s_thread_lock
131 
132  private:
133  Q_DISABLE_COPY(MythSocket)
134 };
135 
136 #endif /* MYTH_SOCKET_H */
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
mythbaseexp.h
MythSocket::s_loopbackCacheLock
static QMutex s_loopbackCacheLock
Definition: mythsocket.h:125
MythSocket::IsAnnounced
bool IsAnnounced(void) const
Definition: mythsocket.h:46
MythSocket::IsValidated
bool IsValidated(void) const
Definition: mythsocket.h:41
MythSocket::kSocketReceiveBufferSize
static const int kSocketReceiveBufferSize
Definition: mythsocket.h:123
MythSocket::LOC
QString LOC()
Definition: mythsocket.h:74
mythsocket_cb.h
MythSocket::s_thread
static MThread * s_thread
Definition: mythsocket.h:129
MythSocket::m_useSharedThread
bool m_useSharedThread
Definition: mythsocket.h:113
m_callback
static void(* m_callback)(void *, QString &)
Definition: mytharchive.cpp:244
MythSocketManager
Definition: mythsocketmanager.h:21
MythSocket
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:25
kMythSocketShortTimeout
static constexpr std::chrono::milliseconds kMythSocketShortTimeout
Definition: mythsocket_cb.h:9
MythSocket::s_loopbackCache
static QHash< QString, QHostAddress::SpecialAddress > s_loopbackCache
Definition: mythsocket.h:126
kMythSocketLongTimeout
static constexpr std::chrono::milliseconds kMythSocketLongTimeout
Definition: mythsocket_cb.h:10
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MythSocket::s_thread_lock
static QMutex s_thread_lock
Definition: mythsocket.h:128
MythSocket::s_thread_cnt
static int s_thread_cnt
Definition: mythsocket.h:130
MythSocketCBs
Definition: mythsocket_cb.h:13
MythSocket::SetReadyReadCallbackEnabled
void SetReadyReadCallbackEnabled(bool enabled)
Definition: mythsocket.h:48
uint
unsigned int uint
Definition: compat.h:79
referencecounter.h
MythSocket::m_announce
QStringList m_announce
Definition: mythsocket.h:121
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
MythSocket::m_peerAddress
QHostAddress m_peerAddress
Definition: mythsocket.h:110
MythSocket::GetAnnounce
QStringList GetAnnounce(void) const
Definition: mythsocket.h:44
musicbrainzngs.caa.hostname
string hostname
Definition: caa.py:17
MythSocket::m_lock
QMutex m_lock
Definition: mythsocket.h:108
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26