MythTV  master
serverpool.h
Go to the documentation of this file.
1 #ifndef SERVERPOOL_H_
2 #define SERVERPOOL_H_
3 
4 #include <QList>
5 #include <QHostAddress>
6 #include <QNetworkProxy>
7 #include <QTcpServer>
8 #include <QSslSocket>
9 #include <QUdpSocket>
10 #include <QStringList>
11 
12 #include "mythqtcompat.h"
13 #include "mythbaseexp.h"
14 
27 class PrivUdpSocket;
28 
29 typedef enum PoolServerTypes
30 {
35 
36 // Making a 'Priv' server public is a contradiction, it was this or passing
37 // through the server type in the newConnection signal which would have required
38 // modifying a lot more places to handle or ignore the info
39 class MBASE_PUBLIC PrivTcpServer : public QTcpServer
40 {
41  Q_OBJECT
42  public:
43  PrivTcpServer(QObject *parent = nullptr,
45  ~PrivTcpServer() = default;
46 
47  PoolServerType GetServerType(void) { return m_serverType; }
48 
49  signals:
50  void newConnection(qt_socket_fd_t socket);
51 
52  protected:
53  void incomingConnection(qt_socket_fd_t socket) override; // QTcpServer
54 
55  private:
57 };
58 
59 class MBASE_PUBLIC ServerPool : public QObject
60 {
61  Q_OBJECT
62 
63  public:
64  explicit ServerPool(QObject *parent=nullptr)
65  : QObject(parent) {}
66  ~ServerPool(void);
67 
68  static void RefreshDefaultListen(void);
69  static QList<QHostAddress> DefaultListen(void);
70  static QList<QHostAddress> DefaultListenIPv4(void);
71  static QList<QHostAddress> DefaultListenIPv6(void);
72  static QList<QHostAddress> DefaultBroadcast(void);
73  static QList<QHostAddress> DefaultBroadcastIPv4(void);
74  static QList<QHostAddress> DefaultBroadcastIPv6(void);
75 
76  bool listen(QList<QHostAddress> addrs, quint16 port, bool requireall=true,
78  bool listen(QStringList addrs, quint16 port, bool requireall=true,
80  bool listen(quint16 port, bool requireall=true,
82 
83  bool bind(QList<QHostAddress> addrs, quint16 port, bool requireall=true);
84  bool bind(QStringList addrs, quint16 port, bool requireall=true);
85  bool bind(quint16 port, bool requireall=true);
86 
87  qint64 writeDatagram(const char * data, qint64 size,
88  const QHostAddress &addr, quint16 port);
89  qint64 writeDatagram(const QByteArray &datagram,
90  const QHostAddress &addr, quint16 port);
91 
92  bool isListening(void) { return m_listening; }
93  int maxPendingConnections(void) { return m_maxPendingConn; }
94  void setMaxPendingConnections(int n) { m_maxPendingConn = n; }
95  quint16 serverPort(void) { return m_port; }
96 
97  QNetworkProxy proxy(void) { return m_proxy; }
98  void setProxy(const QNetworkProxy &proxy) { m_proxy = proxy; }
99 
100  void close(void);
101 
102  int tryListeningPort(int baseport, int range = 1);
103  int tryBindingPort(int baseport, int range = 1);
104  // Utility functions
105  static int tryListeningPort(QTcpServer *server, int baseport,
106  int range = 1, bool *isipv6 = nullptr);
107  static int tryBindingPort(QUdpSocket *socket, int baseport,
108  int range = 1, bool *isipv6 = nullptr);
109 
110  signals:
111  void newConnection(QTcpSocket *);
112  void newDatagram(QByteArray, QHostAddress, quint16);
113 
114  protected slots:
115  virtual void newUdpDatagram(void);
116  virtual void newTcpConnection(qt_socket_fd_t socket);
117 
118  private:
119  static void SelectDefaultListen(bool force=false);
120 
121  bool m_listening {false};
122  int m_maxPendingConn {30};
123  quint16 m_port {0};
124  QNetworkProxy m_proxy {QNetworkProxy::NoProxy};
125 
126  QList<PrivTcpServer*> m_tcpServers;
127  QList<PrivUdpSocket*> m_udpSockets;
128  PrivUdpSocket *m_lastUdpSocket {nullptr};
129 };
130 
131 #endif
quint16 serverPort(void)
Definition: serverpool.h:95
unsigned int slots[4]
Definition: element.c:38
QNetworkProxy proxy(void)
Definition: serverpool.h:97
void setProxy(const QNetworkProxy &proxy)
Definition: serverpool.h:98
int maxPendingConnections(void)
Definition: serverpool.h:93
PoolServerType GetServerType(void)
Definition: serverpool.h:47
qintptr qt_socket_fd_t
Definition: mythqtcompat.h:4
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
QList< PrivTcpServer * > m_tcpServers
Definition: serverpool.h:126
#define close
Definition: compat.h:16
QList< PrivUdpSocket * > m_udpSockets
Definition: serverpool.h:127
PoolServerTypes
Definition: serverpool.h:29
PoolServerType m_serverType
Definition: serverpool.h:56
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
enum PoolServerTypes PoolServerType
void setMaxPendingConnections(int n)
Definition: serverpool.h:94
bool isListening(void)
Definition: serverpool.h:92
ServerPool(QObject *parent=nullptr)
Definition: serverpool.h:64