Permalink
Browse files

Add two network utility methods to ServerPool

Will be used by Airplay and RAOP
  • Loading branch information...
jyavenard committed May 1, 2012
1 parent 5d1dc99 commit c45628d75939337f99eaa8b19c4a7c22f3435dde
Showing with 68 additions and 0 deletions.
  1. +66 −0 mythtv/libs/libmythbase/serverpool.cpp
  2. +2 −0 mythtv/libs/libmythbase/serverpool.h
@@ -534,6 +534,72 @@ void ServerPool::newUdpDatagram(void)
}
}
+/**
+ * tryListeningPort
+ *
+ * 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
*
@@ -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.