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 "mythbaseexp.h"
13 
27 class PrivUdpSocket;
28 
30 {
34 };
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  explicit PrivTcpServer(QObject *parent = nullptr,
45  ~PrivTcpServer() override = default;
46 
47  PoolServerType GetServerType(void) { return m_serverType; }
48 
49  signals:
50  void newConnection(qintptr socket);
51 
52  protected:
53  void incomingConnection(qintptr 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) override;
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) const { return m_listening; }
93  int maxPendingConnections(void) const { return m_maxPendingConn; }
94  void setMaxPendingConnections(int n) { m_maxPendingConn = n; }
95  quint16 serverPort(void) const { 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(qintptr 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 
132 {
133  Q_OBJECT
134  public:
135  explicit MythServer(QObject *parent = nullptr) : ServerPool(parent) {}
136 
137  signals:
138  void newConnection(qintptr socket);
139 
140  protected slots:
141  void newTcpConnection(qintptr socket) override // ServerPool
142  {
143  emit newConnection(socket);
144  }
145 };
146 
147 #endif
kSSLServer
@ kSSLServer
Definition: serverpool.h:33
PrivTcpServer::m_serverType
PoolServerType m_serverType
Definition: serverpool.h:56
ServerPool::setMaxPendingConnections
void setMaxPendingConnections(int n)
Definition: serverpool.h:94
ServerPool
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
mythbaseexp.h
PrivUdpSocket
Definition: serverpool.cpp:32
ServerPool::isListening
bool isListening(void) const
Definition: serverpool.h:92
ServerPool::m_udpSockets
QList< PrivUdpSocket * > m_udpSockets
Definition: serverpool.h:127
MythServer::MythServer
MythServer(QObject *parent=nullptr)
Definition: serverpool.h:135
MythServer::newTcpConnection
void newTcpConnection(qintptr socket) override
Definition: serverpool.h:141
ServerPool::maxPendingConnections
int maxPendingConnections(void) const
Definition: serverpool.h:93
PrivTcpServer::GetServerType
PoolServerType GetServerType(void)
Definition: serverpool.h:47
force
bool force
Definition: mythcommflag.cpp:70
kUDPServer
@ kUDPServer
Definition: serverpool.h:32
ServerPool::newConnection
void newConnection(QTcpSocket *)
ServerPool::proxy
QNetworkProxy proxy(void)
Definition: serverpool.h:97
close
#define close
Definition: compat.h:43
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
PrivTcpServer
Definition: serverpool.h:39
MythServer
Definition: serverpool.h:131
PoolServerType
PoolServerType
Definition: serverpool.h:29
ServerPool::m_tcpServers
QList< PrivTcpServer * > m_tcpServers
Definition: serverpool.h:126
ServerPool::ServerPool
ServerPool(QObject *parent=nullptr)
Definition: serverpool.h:64
ServerPool::serverPort
quint16 serverPort(void) const
Definition: serverpool.h:95
ServerPool::setProxy
void setProxy(const QNetworkProxy &proxy)
Definition: serverpool.h:98
kTCPServer
@ kTCPServer
Definition: serverpool.h:31