Browse files

Add two network utility methods to ServerPool

Will be used by Airplay and RAOP
  • Loading branch information...
1 parent 5d1dc99 commit c45628d75939337f99eaa8b19c4a7c22f3435dde @jyavenard jyavenard committed May 2, 2012
Showing with 68 additions and 0 deletions.
  1. +66 −0 mythtv/libs/libmythbase/serverpool.cpp
  2. +2 −0 mythtv/libs/libmythbase/serverpool.h
View
66 mythtv/libs/libmythbase/serverpool.cpp
@@ -539,6 +539,72 @@ void ServerPool::newUdpDatagram(void)
*
* Description:
* Tells the server to listen for incoming connections on port port.
+ * The server will attempt to listen on all local interfaces.
+ *
+ * Usage:
+ * baseport: port to listen on.
+ * range: range of ports to try (default 1)
+ *
+ * Returns port used on success; otherwise returns -1.
+ */
+int ServerPool::tryListeningPort(int baseport, int range)
+{
+ // try a few ports in case the first is in use
+ int port = baseport;
+ while (port < baseport + range)
+ {
+ if (listen(port))
+ {
+ break;
+ }
+ port++;
+ }
+
+ if (port >= baseport + range)
+ {
+ return -1;
+ }
+ return port;
+}
+
+/**
+ * tryBindingPort
+ *
+ * Description:
+ * Binds this socket for incoming connections on port port.
+ * The socket will attempt to bind on all local interfaces.
+ *
+ * Usage:
+ * baseport: port to bind to.
+ * range: range of ports to try (default 1)
+ *
+ * Returns port used on success; otherwise returns -1.
+ */
+int ServerPool::tryBindingPort(int baseport, int range)
+{
+ // try a few ports in case the first is in use
+ int port = baseport;
+ while (port < baseport + range)
+ {
+ if (bind(port))
+ {
+ break;
+ }
+ port++;
+ }
+
+ if (port >= baseport + range)
+ {
+ return -1;
+ }
+ return port;
+}
+
+/**
+ * tryListeningPort
+ *
+ * Description:
+ * Tells the server to listen for incoming connections on port port.
* The server will attempt to listen on all IPv6 and IPv4 interfaces.
* If IPv6 isn't available, the server will listen on all IPv4 network interfaces.
*
View
2 mythtv/libs/libmythbase/serverpool.h
@@ -78,6 +78,8 @@ class MBASE_PUBLIC ServerPool : public QObject
void close(void);
+ int tryListeningPort(int baseport, int range = 1);
+ int tryBindingPort(int baseport, int range = 1);
// Utility functions
static int tryListeningPort(QTcpServer *server, int baseport,
int range = 1, bool *isipv6 = NULL);

0 comments on commit c45628d

Please sign in to comment.