TcpSocket.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_TcpSocket_h
30 #define Pt_Net_TcpSocket_h
31 
32 #include <Pt/Net/Api.h>
33 #include <Pt/Net/Endpoint.h>
34 #include <Pt/System/IODevice.h>
35 #include <Pt/Types.h>
36 #include <cstddef>
37 
38 namespace Pt {
39 
40 namespace Net {
41 
42 // TCP_NODELAY
43 // SO_KEEPALIVE
44 // SO_SNDBUF
45 
48 class PT_NET_API TcpSocketOptions
49 {
50  public:
53 
56 
59 
61  TcpSocketOptions& operator=(const TcpSocketOptions& opts);
62 
63  private:
64  Pt::uint32_t _flags;
65  int _sndbufSize;
66  varint_t _r0;
67  varint_t _r1;
68  varint_t _r2;
69 };
70 
71 
74 class PT_NET_API TcpSocket : public System::IODevice
75 {
76  public:
78  TcpSocket();
79 
81  explicit TcpSocket(System::EventLoop& loop);
82 
84  explicit TcpSocket(TcpServer& server);
85 
90  explicit TcpSocket(const Endpoint& ep);
91 
93  ~TcpSocket();
94 
96  void accept(TcpServer& server);
97 
99  void accept(TcpServer& server, const TcpSocketOptions& o);
100 
105  void connect(const Endpoint& ep);
106 
111  void connect(const Endpoint& ep, const TcpSocketOptions& o);
112 
117  void beginConnect(const Endpoint& ep);
118 
123  void beginConnect(const Endpoint& ep, const TcpSocketOptions& o);
124 
129  void endConnect();
130 
137  { return _connected; }
138 
141  bool isConnected() const
142  { return _isConnected; }
143 
146  void localEndpoint(Endpoint& ep) const;
147 
150  void remoteEndpoint(Endpoint& ep) const;
151 
152  protected:
153  // inherit doc
154  virtual void onClose();
155 
156  // inherit doc
157  virtual void onSetTimeout(std::size_t timeout);
158 
159  // inherit doc
160  virtual bool onRun();
161 
162  // inherit doc
163  virtual std::size_t onBeginRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
164 
165  // inherit doc
166  virtual std::size_t onEndRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
167 
168  // inherit doc
169  virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof);
170 
171  // inherit doc
172  virtual std::size_t onBeginWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
173 
174  // inherit doc
175  virtual std::size_t onEndWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
176 
177  // inherit doc
178  virtual std::size_t onWrite(const char* buffer, std::size_t count);
179 
180  // inherit doc
181  virtual void onCancel();
182 
183  private:
185  class TcpSocketImpl* _impl;
186 
188  Signal<TcpSocket&> _connected;
189 
191  bool _connecting;
192 
194  bool _isConnected;
195 };
196 
197 } // namespace Net
198 
199 } // namespace Pt
200 
201 #endif // Pt_Net_TcpSocket_h
TCP socket options.
Definition: TcpSocket.h:48
Signal< TcpSocket & > & connected()
Notifies that the socket was connected.
Definition: TcpSocket.h:136
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Endpoint for I/O operations.
Definition: IODevice.h:55
Represents a Network Host.
Definition: Endpoint.h:46
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
bool isConnected() const
Returns true if connected.
Definition: TcpSocket.h:141
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
TCP client socket.
Definition: TcpSocket.h:74
TCP server socket.
Definition: TcpServer.h:99