Advertisement
Guest User

Untitled

a guest
Apr 30th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. diff --git a/mythtv/libs/libmythbase/serverpool.cpp b/mythtv/libs/libmythbase/serverpool.cpp
  2. index 7ff6df8..8cccc05 100644
  3. --- a/mythtv/libs/libmythbase/serverpool.cpp
  4. +++ b/mythtv/libs/libmythbase/serverpool.cpp
  5. @@ -452,3 +452,131 @@ void ServerPool::newUdpDatagram(void)
  6. emit newDatagram(buffer, sender, senderPort);
  7. }
  8. }
  9. +
  10. +/**
  11. + * tryListeningPort
  12. + *
  13. + * Description:
  14. + * Tells the server to listen for incoming connections on port port.
  15. + * The server will attempt to listen on all IPv6 and IPv4 interfaces.
  16. + * If IPv6 isn't available, the server will listen on all IPv4 network interfaces.
  17. + *
  18. + * Usage:
  19. + * server: QTcpServer object to use
  20. + * baseport: port to listen on. If port is 0, a port is chosen automatically.
  21. + * range: range of ports to try (default 1)
  22. + * isipv6: is set to true if IPv6 was successful (default NULL)
  23. + *
  24. + * Returns port used on success; otherwise returns -1.
  25. + */
  26. +int ServerPool::tryListeningPort(QTcpServer *server, int baseport,
  27. + int range, bool *isipv6)
  28. +{
  29. + bool ipv6 = true;
  30. + // try a few ports in case the first is in use
  31. + int port = baseport;
  32. + while (port < baseport + range)
  33. + {
  34. + if (ipv6)
  35. + {
  36. + if (server->listen(QHostAddress::AnyIPv6, port))
  37. + {
  38. + break;
  39. + }
  40. + else
  41. + {
  42. + // did we fail because IPv6 isn't available?
  43. + QAbstractSocket::SocketError err = server->serverError();
  44. + if (err == QAbstractSocket::UnsupportedSocketOperationError)
  45. + {
  46. + ipv6 = false;
  47. + }
  48. + }
  49. + }
  50. + if (!ipv6)
  51. + {
  52. + if (server->listen(QHostAddress::Any, port))
  53. + {
  54. + break;
  55. + }
  56. + }
  57. + port++;
  58. + }
  59. +
  60. + if (isipv6)
  61. + {
  62. + *isipv6 = ipv6;
  63. + }
  64. +
  65. + if (port >= baseport + range)
  66. + {
  67. + return -1;
  68. + }
  69. + if (port == 0)
  70. + {
  71. + port = server->serverPort();
  72. + }
  73. + return port;
  74. +}
  75. +
  76. +/**
  77. + * tryBindingPort
  78. + *
  79. + * Description:
  80. + * Binds this socket for incoming connections on port port.
  81. + * The socket will attempt to bind on all IPv6 and IPv4 interfaces.
  82. + * If IPv6 isn't available, the socket will be bound to all IPv4 network interfaces.
  83. + *
  84. + * Usage:
  85. + * socket: QUdpSocket object to use
  86. + * baseport: port to bind to.
  87. + * range: range of ports to try (default 1)
  88. + * isipv6: is set to true if IPv6 was successful (default NULL)
  89. + *
  90. + * Returns port used on success; otherwise returns -1.
  91. + */
  92. +int ServerPool::tryBindingPort(QUdpSocket *socket, int baseport,
  93. + int range, bool *isipv6)
  94. +{
  95. + bool ipv6 = true;
  96. + // try a few ports in case the first is in use
  97. + int port = baseport;
  98. + while (port < baseport + range)
  99. + {
  100. + if (ipv6)
  101. + {
  102. + if (socket->bind(QHostAddress::AnyIPv6, port))
  103. + {
  104. + break;
  105. + }
  106. + else
  107. + {
  108. + // did we fail because IPv6 isn't available?
  109. + QAbstractSocket::SocketError err = socket->error();
  110. + if (err == QAbstractSocket::UnsupportedSocketOperationError)
  111. + {
  112. + ipv6 = false;
  113. + }
  114. + }
  115. + }
  116. + if (!ipv6)
  117. + {
  118. + if (socket->bind(QHostAddress::Any, port))
  119. + {
  120. + break;
  121. + }
  122. + }
  123. + port++;
  124. + }
  125. +
  126. + if (isipv6)
  127. + {
  128. + *isipv6 = ipv6;
  129. + }
  130. +
  131. + if (port >= baseport + range)
  132. + {
  133. + return -1;
  134. + }
  135. + return port;
  136. +}
  137. diff --git a/mythtv/libs/libmythbase/serverpool.h b/mythtv/libs/libmythbase/serverpool.h
  138. index 1eabeda..3351f37 100644
  139. --- a/mythtv/libs/libmythbase/serverpool.h
  140. +++ b/mythtv/libs/libmythbase/serverpool.h
  141. @@ -76,7 +76,13 @@ class MBASE_PUBLIC ServerPool : public QObject
  142.  
  143. void close(void);
  144.  
  145. - signals:
  146. + // Utility functions
  147. + static int tryListeningPort(QTcpServer *server, int baseport,
  148. + int range = 1, bool *isipv6 = NULL);
  149. + static int tryBindingPort(QUdpSocket *socket, int baseport,
  150. + int range = 1, bool *isipv6 = NULL);
  151. +
  152. +signals:
  153. void newConnection(QTcpSocket *);
  154. void newDatagram(QByteArray, QHostAddress, quint16);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement