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 | |
14 | |
15 | #include "Poco/Net/ServerSocket.h" |
16 | #include "Poco/Net/ServerSocketImpl.h" |
17 | #include "Poco/Exception.h" |
18 | |
19 | |
20 | using Poco::InvalidArgumentException; |
21 | |
22 | |
23 | namespace Poco { |
24 | namespace Net { |
25 | |
26 | |
27 | ServerSocket::ServerSocket(): Socket(new ServerSocketImpl) |
28 | { |
29 | } |
30 | |
31 | |
32 | ServerSocket::ServerSocket(const Socket& socket): Socket(socket) |
33 | { |
34 | if (!dynamic_cast<ServerSocketImpl*>(impl())) |
35 | throw InvalidArgumentException("Cannot assign incompatible socket" ); |
36 | } |
37 | |
38 | |
39 | ServerSocket::ServerSocket(const SocketAddress& address, int backlog): Socket(new ServerSocketImpl) |
40 | { |
41 | impl()->bind(address, true); |
42 | impl()->listen(backlog); |
43 | } |
44 | |
45 | |
46 | ServerSocket::ServerSocket(Poco::UInt16 port, int backlog): Socket(new ServerSocketImpl) |
47 | { |
48 | IPAddress wildcardAddr; |
49 | SocketAddress address(wildcardAddr, port); |
50 | impl()->bind(address, true); |
51 | impl()->listen(backlog); |
52 | } |
53 | |
54 | |
55 | ServerSocket::ServerSocket(SocketImpl* pImpl, bool /*ignore*/): Socket(pImpl) |
56 | { |
57 | } |
58 | |
59 | |
60 | ServerSocket::~ServerSocket() |
61 | { |
62 | } |
63 | |
64 | |
65 | ServerSocket& ServerSocket::operator = (const Socket& socket) |
66 | { |
67 | if (dynamic_cast<ServerSocketImpl*>(socket.impl())) |
68 | Socket::operator = (socket); |
69 | else |
70 | throw InvalidArgumentException("Cannot assign incompatible socket" ); |
71 | return *this; |
72 | } |
73 | |
74 | |
75 | void ServerSocket::bind(const SocketAddress& address, bool reuseAddress) |
76 | { |
77 | impl()->bind(address, reuseAddress); |
78 | } |
79 | |
80 | |
81 | void ServerSocket::bind(const SocketAddress& address, bool reuseAddress, bool reusePort) |
82 | { |
83 | impl()->bind(address, reuseAddress, reusePort); |
84 | } |
85 | |
86 | |
87 | void ServerSocket::bind(Poco::UInt16 port, bool reuseAddress) |
88 | { |
89 | IPAddress wildcardAddr; |
90 | SocketAddress address(wildcardAddr, port); |
91 | impl()->bind(address, reuseAddress); |
92 | } |
93 | |
94 | |
95 | void ServerSocket::bind(Poco::UInt16 port, bool reuseAddress, bool reusePort) |
96 | { |
97 | IPAddress wildcardAddr; |
98 | SocketAddress address(wildcardAddr, port); |
99 | impl()->bind(address, reuseAddress, reusePort); |
100 | } |
101 | |
102 | |
103 | void ServerSocket::bind6(const SocketAddress& address, bool reuseAddress, bool ipV6Only) |
104 | { |
105 | impl()->bind6(address, reuseAddress, ipV6Only); |
106 | } |
107 | |
108 | |
109 | void ServerSocket::bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only) |
110 | { |
111 | impl()->bind6(address, reuseAddress, reusePort, ipV6Only); |
112 | } |
113 | |
114 | |
115 | void ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool ipV6Only) |
116 | { |
117 | #if defined(POCO_HAVE_IPv6) |
118 | IPAddress wildcardAddr(IPAddress::IPv6); |
119 | SocketAddress address(wildcardAddr, port); |
120 | impl()->bind6(address, reuseAddress, ipV6Only); |
121 | #else |
122 | throw Poco::NotImplementedException("No IPv6 support available" ); |
123 | #endif // POCO_HAVE_IPv6 |
124 | } |
125 | |
126 | |
127 | void ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool reusePort, bool ipV6Only) |
128 | { |
129 | #if defined(POCO_HAVE_IPv6) |
130 | IPAddress wildcardAddr(IPAddress::IPv6); |
131 | SocketAddress address(wildcardAddr, port); |
132 | impl()->bind6(address, reuseAddress, reusePort, ipV6Only); |
133 | #else |
134 | throw Poco::NotImplementedException("No IPv6 support available" ); |
135 | #endif // POCO_HAVE_IPv6 |
136 | } |
137 | |
138 | |
139 | void ServerSocket::listen(int backlog) |
140 | { |
141 | impl()->listen(backlog); |
142 | } |
143 | |
144 | |
145 | StreamSocket ServerSocket::acceptConnection(SocketAddress& clientAddr) |
146 | { |
147 | return StreamSocket(impl()->acceptConnection(clientAddr)); |
148 | } |
149 | |
150 | |
151 | StreamSocket ServerSocket::acceptConnection() |
152 | { |
153 | SocketAddress clientAddr; |
154 | return StreamSocket(impl()->acceptConnection(clientAddr)); |
155 | } |
156 | |
157 | |
158 | } } // namespace Poco::Net |
159 | |