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 
62 
63  int keepAlive() const
64  { return _keepAlive.i; }
65 
66  void setKeepAlive(int n)
67  { _keepAlive.i = n; }
68 
69  private:
70  Pt::uint32_t _flags;
71  int _sndbufSize;
72  varint_t _keepAlive;
73  varint_t _r1;
74  varint_t _r2;
75 };
76 
77 
80 class PT_NET_API TcpSocket : public System::IODevice
81 {
82  public:
85 
87  explicit TcpSocket(System::EventLoop& loop);
88 
90  explicit TcpSocket(TcpServer& server);
91 
96  explicit TcpSocket(const Endpoint& ep);
97 
100 
102  void accept(TcpServer& server);
103 
105  void accept(TcpServer& server, const TcpSocketOptions& o);
106 
111  void connect(const Endpoint& ep);
112 
117  void connect(const Endpoint& ep, const TcpSocketOptions& o);
118 
123  void beginConnect(const Endpoint& ep);
124 
129  void beginConnect(const Endpoint& ep, const TcpSocketOptions& o);
130 
135  void endConnect();
136 
143  { return _connected; }
144 
147  bool isConnected() const
148  { return _isConnected; }
149 
152  void localEndpoint(Endpoint& ep) const;
153 
156  void remoteEndpoint(Endpoint& ep) const;
157 
158  protected:
159  // inherit doc
160  virtual void onClose();
161 
162  // inherit doc
163  virtual void onSetTimeout(std::size_t timeout);
164 
165  // inherit doc
166  virtual bool onRun();
167 
168  // inherit doc
169  virtual std::size_t onBeginRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
170 
171  // inherit doc
172  virtual std::size_t onEndRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
173 
174  // inherit doc
175  virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof);
176 
177  // inherit doc
178  virtual std::size_t onBeginWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
179 
180  // inherit doc
181  virtual std::size_t onEndWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
182 
183  // inherit doc
184  virtual std::size_t onWrite(const char* buffer, std::size_t count);
185 
186  // inherit doc
187  virtual void onCancel();
188 
189  private:
191  class TcpSocketImpl* _impl;
192 
194  Signal<TcpSocket&> _connected;
195 
197  bool _connecting;
198 
200  bool _isConnected;
201 };
202 
203 } // namespace Net
204 
205 } // namespace Pt
206 
207 #endif // Pt_Net_TcpSocket_h
Core module.
Definition: pt-gfx-images.dox:14
TcpSocket(const Endpoint &ep)
Connects to a host.
bool isConnected() const
Returns true if connected.
Definition: TcpSocket.h:147
TCP client socket.
Definition: TcpSocket.h:81
virtual bool onRun()
Check if ready and run.
TcpSocketOptions(const TcpSocketOptions &opts)
Copy Constructor.
void accept(TcpServer &server)
Accepts a connection.
TcpSocketOptions & operator=(const TcpSocketOptions &opts)
Assignment operator.
TCP server socket.
Definition: TcpServer.h:100
void connect(const Endpoint &ep, const TcpSocketOptions &o)
Connect to a host.
Multicast Signal to call multiple slots.
Definition: Signal.h:110
void accept(TcpServer &server, const TcpSocketOptions &o)
Accepts a connection.
TcpSocket(System::EventLoop &loop)
Construct with event loop.
TcpSocket()
Default Constructor.
virtual void onCancel()
Blocks until operation has cancelled.
~TcpSocket()
Destructor.
void beginConnect(const Endpoint &ep)
Begin connecting to a host.
TcpSocket(TcpServer &server)
Accepts a connection.
Represents a Network Host.
Definition: Endpoint.h:47
Signal< TcpSocket & > & connected()
Notifies that the socket was connected.
Definition: TcpSocket.h:142
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
Endpoint for I/O operations.
Definition: IODevice.h:56
TcpSocketOptions()
Default Constructor.
void remoteEndpoint(Endpoint &ep) const
Gets the remote endpoint.
void connect(const Endpoint &ep)
Connect to a host.
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
void endConnect()
Ends connecting to a host.
void localEndpoint(Endpoint &ep) const
Gets the local endpoint.
TCP socket options.
Definition: TcpSocket.h:49
void beginConnect(const Endpoint &ep, const TcpSocketOptions &o)
Begin connecting to a host.
~TcpSocketOptions()
Destructor.