TcpServer.h
1/*
2 * Copyright (C) 2006-2013 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_Net_TcpServer_h
30#define Pt_Net_TcpServer_h
31
32#include <Pt/Net/Api.h>
33#include <Pt/System/Selectable.h>
34#include <Pt/Signal.h>
35#include <Pt/Types.h>
36
37namespace Pt {
38
39namespace Net {
40
45class PT_NET_API TcpServerOptions
46{
47 public:
50 explicit TcpServerOptions(int backlog = 5);
51
55
59
63
66 int acceptDeferred() const
67 { return _deferAccept; }
68
71 void setDeferAccept(int n)
72 { _deferAccept = n; }
73
76 int backlog() const
77 { return _backlog; }
78
82 { _backlog = backlog; }
83
84 private:
85 Pt::uint32_t _flags;
86 int _backlog;
87 int _deferAccept;
88 varint_t _r0;
89 varint_t _r1;
90 varint_t _r2;
91};
92
93class TcpServerImpl;
94
158class PT_NET_API TcpServer : public System::Selectable
159{
160 public:
164
168
173 explicit TcpServer(const Endpoint& ep);
174
178
183 void listen(const Endpoint& ep);
184
189 void listen(const Endpoint& ep, const TcpServerOptions& options);
190
196
199 void close();
200
204 { return _connectionPending; }
205
209 { return _loop; }
210
212 const TcpServerImpl& impl() const
213 { return *_impl; }
214
216 TcpServerImpl& impl()
217 { return *_impl; }
218
219 protected:
220 // inherit doc
222
223 // inherit doc
225
226 // inherit doc
227 virtual void onCancel();
228
229 // inherit doc
230 virtual bool onRun();
231
232 private:
233 System::EventLoop* _loop;
234 TcpServerImpl* _impl;
235 Signal<TcpServer&> _connectionPending;
236};
237
238} // namespace Net
239
240} // namespace Pt
241
242#endif // Pt_Net_TcpServer_h
Host and service address.
Definition Endpoint.h:77
TCP server listen options.
Definition TcpServer.h:46
TcpServerOptions(int backlog=5)
Creates options with accept backlog backlog.
int acceptDeferred() const
Returns the deferred-accept timeout in seconds, or -1 if unset.
Definition TcpServer.h:66
void setDeferAccept(int n)
Sets deferred accept to wait at most n seconds for data.
Definition TcpServer.h:71
~TcpServerOptions()
Destroys the options.
TcpServerOptions(const TcpServerOptions &opts)
Copies the options.
int backlog() const
Returns the accept backlog size.
Definition TcpServer.h:76
TcpServerOptions & operator=(const TcpServerOptions &opts)
Assigns the options.
void setBacklog(int backlog)
Sets the accept backlog size.
Definition TcpServer.h:81
void beginAccept()
Begins accepting a connection.
TcpServer(const Endpoint &ep)
Creates a server and listens on ep.
virtual void onCancel()
Blocks until operation has cancelled.
~TcpServer()
Destroys the server.
void close()
Closes the server and stops listening.
TcpServer(System::EventLoop &loop)
Creates a server and attaches it to loop.
void listen(const Endpoint &ep, const TcpServerOptions &options)
Listens on ep with options.
virtual bool onRun()
Check if ready and run.
virtual void onDetach(System::EventLoop &loop)
Detached from loop.
System::EventLoop * loop() const
Returns the parent event loop.
Definition TcpServer.h:208
virtual void onAttach(System::EventLoop &loop)
Attached to loop.
TcpServer()
Creates a closed server.
void listen(const Endpoint &ep)
Listens on local endpoint ep.
Signal< TcpServer & > & connectionPending()
Returns the signal that a connection is pending.
Definition TcpServer.h:203
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
Operation that runs through an event loop.
Definition Selectable.h:60
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
TCP and UDP network sockets.
Definition Client.h:45
Core module.
Definition Allocator.h:33