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 
37 namespace Pt {
38 
39 namespace Net {
40 
43 class PT_NET_API TcpServerOptions
44 {
45  public:
48  explicit TcpServerOptions(int backlog = 5);
49 
53 
57 
60  TcpServerOptions& operator=(const TcpServerOptions& opts);
61 
66  int acceptDeferred() const
67  { return _deferAccept; }
68 
73  void setDeferAccept(int n)
74  { _deferAccept = n; }
75 
78  int backlog() const
79  { return _backlog; }
80 
83  void setBacklog(int backlog)
84  { _backlog = backlog; }
85 
86  private:
87  Pt::uint32_t _flags;
88  int _backlog;
89  int _deferAccept;
90  varint_t _r0;
91  varint_t _r1;
92  varint_t _r2;
93 };
94 
95 class TcpServerImpl;
96 
99 class PT_NET_API TcpServer : public System::Selectable
100 {
101  public:
104  TcpServer();
105 
108  explicit TcpServer(System::EventLoop& loop);
109 
112  explicit TcpServer(const Endpoint& ep);
113 
116  ~TcpServer();
117 
120  void listen(const Endpoint& ep);
121 
124  void listen(const Endpoint& ep, const TcpServerOptions& options);
125 
128  void beginAccept();
129 
132  void close();
133 
140  { return _connectionPending; }
141 
145  { return _loop; }
146 
148  const TcpServerImpl& impl() const
149  { return *_impl; }
150 
152  TcpServerImpl& impl()
153  { return *_impl; }
154 
155  protected:
156  // inherit doc
157  virtual void onAttach(System::EventLoop& loop);
158 
159  // inherit doc
160  virtual void onDetach(System::EventLoop& loop);
161 
162  // inherit doc
163  virtual void onCancel();
164 
165  // inherit doc
166  virtual bool onRun();
167 
168  private:
169  System::EventLoop* _loop;
170  TcpServerImpl* _impl;
171  Signal<TcpServer&> _connectionPending;
172 };
173 
174 } // namespace Net
175 
176 } // namespace Pt
177 
178 #endif // Pt_Net_TcpServer_h
int backlog() const
Returns the accept backlog size.
Definition: TcpServer.h:78
TCP server options.
Definition: TcpServer.h:43
Signal< TcpServer & > & connectionPending()
Notifies that a connection was accepted.
Definition: TcpServer.h:139
void setDeferAccept(int n)
Defer accept until data arrives.
Definition: TcpServer.h:73
Dispatches operations through an event loop.
Definition: Selectable.h:44
int acceptDeferred() const
Returns the max time for data to arrive.
Definition: TcpServer.h:66
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Represents a Network Host.
Definition: Endpoint.h:46
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
System::EventLoop * loop() const
Returns the parent event loop.
Definition: TcpServer.h:144
void setBacklog(int backlog)
Sets the accept backlog size.
Definition: TcpServer.h:83
TCP server socket.
Definition: TcpServer.h:99