ServerSocket.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // ServerSocket.cpp
  3. //
  4. // Library: Net
  5. // Package: Sockets
  6. // Module: ServerSocket
  7. //
  8. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/Net/ServerSocket.h"
  14. #include "Poco/Net/ServerSocketImpl.h"
  15. #include "Poco/Exception.h"
  16. using Poco::InvalidArgumentException;
  17. namespace Poco {
  18. namespace Net {
  19. ServerSocket::ServerSocket(): Socket(new ServerSocketImpl)
  20. {
  21. }
  22. ServerSocket::ServerSocket(const Socket& socket): Socket(socket)
  23. {
  24. if (!dynamic_cast<ServerSocketImpl*>(impl()))
  25. throw InvalidArgumentException("Cannot assign incompatible socket");
  26. }
  27. ServerSocket::ServerSocket(const SocketAddress& address, int backlog): Socket(new ServerSocketImpl)
  28. {
  29. impl()->bind(address, true);
  30. impl()->listen(backlog);
  31. }
  32. ServerSocket::ServerSocket(Poco::UInt16 port, int backlog): Socket(new ServerSocketImpl)
  33. {
  34. IPAddress wildcardAddr;
  35. SocketAddress address(wildcardAddr, port);
  36. impl()->bind(address, true);
  37. impl()->listen(backlog);
  38. }
  39. ServerSocket::ServerSocket(SocketImpl* pImpl, bool /*ignore*/): Socket(pImpl)
  40. {
  41. }
  42. ServerSocket::~ServerSocket()
  43. {
  44. }
  45. ServerSocket& ServerSocket::operator = (const Socket& socket)
  46. {
  47. if (dynamic_cast<ServerSocketImpl*>(socket.impl()))
  48. Socket::operator = (socket);
  49. else
  50. throw InvalidArgumentException("Cannot assign incompatible socket");
  51. return *this;
  52. }
  53. void ServerSocket::bind(const SocketAddress& address, bool reuseAddress)
  54. {
  55. impl()->bind(address, reuseAddress);
  56. }
  57. void ServerSocket::bind(const SocketAddress& address, bool reuseAddress, bool reusePort)
  58. {
  59. impl()->bind(address, reuseAddress, reusePort);
  60. }
  61. void ServerSocket::bind(Poco::UInt16 port, bool reuseAddress)
  62. {
  63. IPAddress wildcardAddr;
  64. SocketAddress address(wildcardAddr, port);
  65. impl()->bind(address, reuseAddress);
  66. }
  67. void ServerSocket::bind(Poco::UInt16 port, bool reuseAddress, bool reusePort)
  68. {
  69. IPAddress wildcardAddr;
  70. SocketAddress address(wildcardAddr, port);
  71. impl()->bind(address, reuseAddress, reusePort);
  72. }
  73. void ServerSocket::bind6(const SocketAddress& address, bool reuseAddress, bool ipV6Only)
  74. {
  75. impl()->bind6(address, reuseAddress, ipV6Only);
  76. }
  77. void ServerSocket::bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only)
  78. {
  79. impl()->bind6(address, reuseAddress, reusePort, ipV6Only);
  80. }
  81. void ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool ipV6Only)
  82. {
  83. #if defined(POCO_HAVE_IPv6)
  84. IPAddress wildcardAddr(IPAddress::IPv6);
  85. SocketAddress address(wildcardAddr, port);
  86. impl()->bind6(address, reuseAddress, ipV6Only);
  87. #else
  88. throw Poco::NotImplementedException("No IPv6 support available");
  89. #endif // POCO_HAVE_IPv6
  90. }
  91. void ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool reusePort, bool ipV6Only)
  92. {
  93. #if defined(POCO_HAVE_IPv6)
  94. IPAddress wildcardAddr(IPAddress::IPv6);
  95. SocketAddress address(wildcardAddr, port);
  96. impl()->bind6(address, reuseAddress, reusePort, ipV6Only);
  97. #else
  98. throw Poco::NotImplementedException("No IPv6 support available");
  99. #endif // POCO_HAVE_IPv6
  100. }
  101. void ServerSocket::listen(int backlog)
  102. {
  103. impl()->listen(backlog);
  104. }
  105. StreamSocket ServerSocket::acceptConnection(SocketAddress& clientAddr)
  106. {
  107. return StreamSocket(impl()->acceptConnection(clientAddr));
  108. }
  109. StreamSocket ServerSocket::acceptConnection()
  110. {
  111. SocketAddress clientAddr;
  112. return StreamSocket(impl()->acceptConnection(clientAddr));
  113. }
  114. } } // namespace Poco::Net