Skip to content

Commit 639842f

Browse files
committed
simplify default TCP/HTTPServer construction
1 parent 5cde6d3 commit 639842f

7 files changed

Lines changed: 127 additions & 85 deletions

File tree

‎Net/include/Poco/Net/HTTPServer.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class Net_API HTTPServer: public TCPServer
7373
/// information about the HTTP protocol.
7474
{
7575
public:
76+
HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, Poco::UInt16 portNumber = 80, HTTPServerParams::Ptr pParams = new HTTPServerParams);
77+
/// Creates HTTPServer listening on the given port (default 80).
78+
///
79+
/// The server takes ownership of the HTTPRequstHandlerFactory
80+
/// and deletes it when it's no longer needed.
81+
///
82+
/// New threads are taken from the default thread pool.
83+
7684
HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams);
7785
/// Creates the HTTPServer, using the given ServerSocket.
7886
///
@@ -81,7 +89,7 @@ class Net_API HTTPServer: public TCPServer
8189
///
8290
/// The server also takes ownership of the HTTPServerParams object.
8391
///
84-
/// News threads are taken from the default thread pool.
92+
/// New threads are taken from the default thread pool.
8593

8694
HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, Poco::ThreadPool& threadPool, const ServerSocket& socket, HTTPServerParams::Ptr pParams);
8795
/// Creates the HTTPServer, using the given ServerSocket.
@@ -91,7 +99,7 @@ class Net_API HTTPServer: public TCPServer
9199
///
92100
/// The server also takes ownership of the HTTPServerParams object.
93101
///
94-
/// News threads are taken from the given thread pool.
102+
/// New threads are taken from the given thread pool.
95103

96104
~HTTPServer();
97105
/// Destroys the HTTPServer and its HTTPRequestHandlerFactory.

‎Net/include/Poco/Net/TCPServer.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ class Net_API TCPServer: public Poco::Runnable
9898
/// Already served connections, however, will continue being served.
9999
{
100100
public:
101+
TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::UInt16 portNumber = 0, TCPServerParams::Ptr pParams = 0);
102+
/// Creates the TCPServer, with ServerSocket listening on the given port.
103+
/// Default port is zero, allowing any availble port. The port number
104+
/// can be queried through TCPServer::port() member.
105+
///
106+
/// The server takes ownership of the TCPServerConnectionFactory
107+
/// and deletes it when it's no longer needed.
108+
///
109+
/// The server also takes ownership of the TCPServerParams object.
110+
/// If no TCPServerParams object is given, the server's TCPServerDispatcher
111+
/// creates its own one.
112+
///
113+
/// New threads are taken from the default thread pool.
114+
101115
TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams = 0);
102116
/// Creates the TCPServer, using the given ServerSocket.
103117
///
@@ -163,6 +177,9 @@ class Net_API TCPServer: public Poco::Runnable
163177
int refusedConnections() const;
164178
/// Returns the number of refused connections.
165179

180+
const ServerSocket& socket() const;
181+
/// Returns the underlying server socket.
182+
166183
Poco::UInt16 port() const;
167184
/// Returns the port the server socket listens on.
168185

@@ -188,6 +205,12 @@ class Net_API TCPServer: public Poco::Runnable
188205
};
189206

190207

208+
inline const ServerSocket& TCPServer::socket() const
209+
{
210+
return _socket;
211+
}
212+
213+
191214
inline Poco::UInt16 TCPServer::port() const
192215
{
193216
return _socket.address().port();

‎Net/samples/HTTPTimeServer/HTTPTimeServer_vs110.vcxproj‎

Lines changed: 70 additions & 67 deletions
Large diffs are not rendered by default.

‎Net/src/HTTPServer.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ namespace Poco {
4242
namespace Net {
4343

4444

45+
HTTPServer::HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, Poco::UInt16 portNumber, HTTPServerParams::Ptr pParams):
46+
TCPServer(new HTTPServerConnectionFactory(pParams, pFactory), portNumber, pParams),
47+
_pFactory(pFactory)
48+
{
49+
}
50+
51+
4552
HTTPServer::HTTPServer(HTTPRequestHandlerFactory::Ptr pFactory, const ServerSocket& socket, HTTPServerParams::Ptr pParams):
4653
TCPServer(new HTTPServerConnectionFactory(pParams, pFactory), socket, pParams),
4754
_pFactory(pFactory)

‎Net/src/TCPServer.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ namespace Poco {
5050
namespace Net {
5151

5252

53+
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, Poco::UInt16 portNumber, TCPServerParams::Ptr pParams):
54+
_socket(ServerSocket(portNumber)),
55+
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),
56+
_thread(threadName(_socket)),
57+
_stopped(true)
58+
{
59+
}
60+
61+
5362
TCPServer::TCPServer(TCPServerConnectionFactory::Ptr pFactory, const ServerSocket& socket, TCPServerParams::Ptr pParams):
5463
_socket(socket),
5564
_pDispatcher(new TCPServerDispatcher(pFactory, Poco::ThreadPool::defaultPool(), pParams)),

‎Net/testsuite/src/HTTPServerTest.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,10 @@ void HTTPServerTest::testClosedRequest()
253253

254254
void HTTPServerTest::testIdentityRequestKeepAlive()
255255
{
256-
ServerSocket svs(0);
257-
HTTPServerParams* pParams = new HTTPServerParams;
258-
pParams->setKeepAlive(true);
259-
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
256+
HTTPServer srv(new RequestHandlerFactory);
260257
srv.start();
261258

262-
HTTPClientSession cs("localhost", svs.address().port());
259+
HTTPClientSession cs("localhost", srv.socket().address().port());
263260
cs.setKeepAlive(true);
264261
std::string body(5000, 'x');
265262
HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
@@ -322,13 +319,10 @@ void HTTPServerTest::testChunkedRequestKeepAlive()
322319

323320
void HTTPServerTest::testClosedRequestKeepAlive()
324321
{
325-
ServerSocket svs(0);
326-
HTTPServerParams* pParams = new HTTPServerParams;
327-
pParams->setKeepAlive(true);
328-
HTTPServer srv(new RequestHandlerFactory, svs, pParams);
322+
HTTPServer srv(new RequestHandlerFactory);
329323
srv.start();
330324

331-
HTTPClientSession cs("localhost", svs.address().port());
325+
HTTPClientSession cs("localhost", srv.socket().address().port());//svs.address().port());
332326
std::string body(5000, 'x');
333327
HTTPRequest request("POST", "/echoBody");
334328
request.setContentType("text/plain");

‎Net/testsuite/src/TCPServerTest.cpp‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,14 @@ TCPServerTest::~TCPServerTest()
9797

9898
void TCPServerTest::testOneConnection()
9999
{
100-
ServerSocket svs(0);
101-
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
100+
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>());
102101
srv.start();
103102
assert (srv.currentConnections() == 0);
104103
assert (srv.currentThreads() == 0);
105104
assert (srv.queuedConnections() == 0);
106105
assert (srv.totalConnections() == 0);
107106

108-
SocketAddress sa("localhost", svs.address().port());
107+
SocketAddress sa("localhost", srv.socket().address().port());
109108
StreamSocket ss1(sa);
110109
std::string data("hello, world");
111110
ss1.sendBytes(data.data(), (int) data.size());
@@ -125,15 +124,14 @@ void TCPServerTest::testOneConnection()
125124

126125
void TCPServerTest::testTwoConnections()
127126
{
128-
ServerSocket svs(0);
129-
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
127+
TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>());
130128
srv.start();
131129
assert (srv.currentConnections() == 0);
132130
assert (srv.currentThreads() == 0);
133131
assert (srv.queuedConnections() == 0);
134132
assert (srv.totalConnections() == 0);
135133

136-
SocketAddress sa("localhost", svs.address().port());
134+
SocketAddress sa("localhost", srv.socket().address().port());
137135
StreamSocket ss1(sa);
138136
StreamSocket ss2(sa);
139137
std::string data("hello, world");

0 commit comments

Comments
 (0)