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  signals:
74  void CallReadyRead(void);
75 
76  protected slots:
77  void ConnectHandler(void);
78  void ErrorHandler(QAbstractSocket::SocketError err);
79  void AboutToCloseHandler(void);
80  void DisconnectHandler(void);
81  void ReadyReadHandler(void);
82  void CallReadyReadHandler(void);
83 
84  void ReadStringListReal(QStringList *list, std::chrono::milliseconds timeoutMS, bool *ret);
85  void WriteStringListReal(const QStringList *list, bool *ret);
86  void ConnectToHostReal(const QHostAddress& addr, quint16 port, bool *ret);
87  void DisconnectFromHostReal(void);
88 
89  void WriteReal(const char *data, int size, int *ret);
90  void ReadReal(char *data, int size, std::chrono::milliseconds max_wait_ms, int *ret);
91  void ResetReal(void);
92 
93  void IsDataAvailableReal(bool *ret) const;
94 
95  protected:
96  ~MythSocket() override; // force reference counting
97 
98  QTcpSocket *m_tcpSocket {nullptr}; // only set in ctor
99  MThread *m_thread {nullptr}; // only set in ctor
100  mutable QMutex m_lock;
101  qintptr m_socketDescriptor {-1}; // protected by m_lock
102  QHostAddress m_peerAddress; // protected by m_lock
103  int m_peerPort {-1}; // protected by m_lock
104  MythSocketCBs *m_callback {nullptr}; // only set in ctor
105  bool m_useSharedThread; // only set in ctor
106  QAtomicInt m_disableReadyReadCallback {false};
107  bool m_connected {false}; // protected by m_lock
110  mutable QAtomicInt m_dataAvailable {0};
111  bool m_isValidated {false}; // only set in thread using MythSocket
112  bool m_isAnnounced {false}; // only set in thread using MythSocket
113  QStringList m_announce; // only set in thread using MythSocket
114 
115  static const int kSocketReceiveBufferSize;
116 
117  static QMutex s_loopbackCacheLock;
118  static QHash<QString, QHostAddress::SpecialAddress> s_loopbackCache;
119 
120  static QMutex s_thread_lock;
121  static MThread *s_thread; // protected by s_thread_lock
122  static int s_thread_cnt; // protected by s_thread_lock
123 
124  private:
125  Q_DISABLE_COPY(MythSocket)
126 };
127 
128 #endif /* MYTH_SOCKET_H */
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
m_callback
static void(* m_callback)(void *, QString &)
Definition: mythplugins/mytharchive/mytharchive/main.cpp:244
mythbaseexp.h
MythSocket::s_loopbackCacheLock
static QMutex s_loopbackCacheLock
Definition: mythsocket.h:117
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:115
mythsocket_cb.h
MythSocket::s_thread
static MThread * s_thread
Definition: mythsocket.h:121
MythSocket::m_useSharedThread
bool m_useSharedThread
Definition: mythsocket.h:105
MythSocketManager
Definition: mythsocketmanager.h:33
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:118
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:120
MythSocket::s_thread_cnt
static int s_thread_cnt
Definition: mythsocket.h:122
MythSocketCBs
Definition: mythsocket_cb.h:13
MythSocket::SetReadyReadCallbackEnabled
void SetReadyReadCallbackEnabled(bool enabled)
Definition: mythsocket.h:48
uint
unsigned int uint
Definition: compat.h:140
referencecounter.h
MythSocket::m_announce
QStringList m_announce
Definition: mythsocket.h:113
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:102
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:100
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26