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