Skip to content

Commit

Permalink
Add two network utility methods to ServerPool
Browse files Browse the repository at this point in the history
Will be used by Airplay and RAOP
  • Loading branch information
jyavenard committed May 1, 2012
1 parent 34c46f1 commit 1d375db
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
66 changes: 66 additions & 0 deletions mythtv/libs/libmythbase/serverpool.cpp
Expand Up @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions mythtv/libs/libmythbase/serverpool.h
Expand Up @@ -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);
Expand Down

0 comments on commit 1d375db

Please sign in to comment.