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  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*, int size);
68  int Read(char*, 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);
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*, int size, int *ret);
91  void ReadReal(char*, int size, int max_wait_ms, int *ret);
92  void ResetReal(void);
93 
94  void IsDataAvailableReal(bool *ret) const;
95 
96  protected:
97  ~MythSocket(); // 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 */
static QMutex s_loopbackCacheLock
Definition: mythsocket.h:118
bool m_useSharedThread
Definition: mythsocket.h:106
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
unsigned int slots[4]
Definition: element.c:38
QStringList GetAnnounce(void) const
Definition: mythsocket.h:45
static const uint kLongTimeout
Definition: mythsocket.h:72
#define kMythSocketLongTimeout
Definition: mythsocket_cb.h:8
General purpose reference counter.
void SetReadyReadCallbackEnabled(bool enabled)
Definition: mythsocket.h:49
static int s_thread_cnt
Definition: mythsocket.h:123
static void(* m_callback)(void *, QString &)
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
QStringList m_announce
Definition: mythsocket.h:114
static MThread * s_thread
Definition: mythsocket.h:122
QHostAddress m_peerAddress
Definition: mythsocket.h:103
QMutex m_lock
Definition: mythsocket.h:101
qintptr qt_socket_fd_t
Definition: mythqtcompat.h:4
string hostname
Definition: caa.py:17
unsigned int uint
Definition: compat.h:140
static const int kSocketReceiveBufferSize
Definition: mythsocket.h:116
static QMutex s_thread_lock
Definition: mythsocket.h:121
bool IsValidated(void) const
Definition: mythsocket.h:42
static const uint kShortTimeout
Definition: mythsocket.h:71
static QHash< QString, QHostAddress::SpecialAddress > s_loopbackCache
Definition: mythsocket.h:119
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
static int ErrorHandler(Display *d, XErrorEvent *xeev)
bool IsAnnounced(void) const
Definition: mythsocket.h:47