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
38namespace Pt {
39
40namespace Net {
41
42// TCP_NODELAY
43// SO_KEEPALIVE
44// SO_SNDBUF
45
50class PT_NET_API TcpSocketOptions
51{
52 public:
56
60
64
68
71 int keepAlive() const
72 { return _keepAlive.i; }
73
78 void setKeepAlive(int n)
79 { _keepAlive.i = n; }
80
81 private:
82 Pt::uint32_t _flags;
83 int _sndbufSize;
84 varint_t _keepAlive;
85 varint_t _r1;
86 varint_t _r2;
87};
88
89
140class PT_NET_API TcpSocket : public System::IODevice
141{
142 public:
146
150
153 explicit TcpSocket(TcpServer& server);
154
159 explicit TcpSocket(const Endpoint& ep);
160
164
167 void accept(TcpServer& server);
168
171 void accept(TcpServer& server, const TcpSocketOptions& o);
172
177 void connect(const Endpoint& ep);
178
183 void connect(const Endpoint& ep, const TcpSocketOptions& o);
184
191 void beginConnect(const Endpoint& ep);
192
199 void beginConnect(const Endpoint& ep, const TcpSocketOptions& o);
200
206
210 { return _connected; }
211
214 bool isConnected() const
215 { return _isConnected; }
216
219 void localEndpoint(Endpoint& ep) const;
220
223 void remoteEndpoint(Endpoint& ep) const;
224
225 protected:
226 // inherit doc
227 virtual void onClose();
228
229 // inherit doc
230 virtual void onSetTimeout(std::size_t timeout);
231
232 // inherit doc
233 virtual bool onRun();
234
235 // inherit doc
236 virtual std::size_t onBeginRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
237
238 // inherit doc
239 virtual std::size_t onEndRead(System::EventLoop& loop, char* buffer, std::size_t n, bool& eof);
240
241 // inherit doc
242 virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof);
243
244 // inherit doc
245 virtual std::size_t onBeginWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
246
247 // inherit doc
248 virtual std::size_t onEndWrite(System::EventLoop& loop, const char* buffer, std::size_t n);
249
250 // inherit doc
251 virtual std::size_t onWrite(const char* buffer, std::size_t count);
252
253 // inherit doc
254 virtual void onCancel();
255
256 private:
258 class TcpSocketImpl* _impl;
259
261 Signal<TcpSocket&> _connected;
262
264 bool _connecting;
265
267 bool _isConnected;
268};
269
270} // namespace Net
271
272} // namespace Pt
273
274#endif // Pt_Net_TcpSocket_h
Host and service address.
Definition Endpoint.h:77
Listens for TCP connections.
Definition TcpServer.h:159
TCP socket connection options.
Definition TcpSocket.h:51
TcpSocketOptions()
Creates default options.
~TcpSocketOptions()
Destroys the options.
TcpSocketOptions(const TcpSocketOptions &opts)
Copies the options.
TcpSocketOptions & operator=(const TcpSocketOptions &opts)
Assigns the options.
void setKeepAlive(int n)
Sets the keep-alive interval in seconds.
Definition TcpSocket.h:78
int keepAlive() const
Returns the keep-alive interval in seconds, or a negative value if unset.
Definition TcpSocket.h:71
void endConnect()
Ends connecting to a host.
~TcpSocket()
Destroys the socket.
Signal< TcpSocket & > & connected()
Returns the signal that the socket was connected.
Definition TcpSocket.h:209
virtual void onCancel()
Blocks until operation has cancelled.
TcpSocket(TcpServer &server)
Creates a socket and accepts a connection from server.
void beginConnect(const Endpoint &ep, const TcpSocketOptions &o)
Begins connecting to ep with o.
void remoteEndpoint(Endpoint &ep) const
Writes the remote endpoint into ep.
TcpSocket(System::EventLoop &loop)
Creates a socket and attaches it to loop.
virtual bool onRun()
Check if ready and run.
bool isConnected() const
Returns true if the socket is connected.
Definition TcpSocket.h:214
void beginConnect(const Endpoint &ep)
Begins connecting to ep.
void accept(TcpServer &server)
Accepts a connection from server.
TcpSocket()
Creates a closed socket.
void accept(TcpServer &server, const TcpSocketOptions &o)
Accepts a connection from server with o.
void localEndpoint(Endpoint &ep) const
Writes the local endpoint into ep.
void connect(const Endpoint &ep, const TcpSocketOptions &o)
Connects to ep with o.
void connect(const Endpoint &ep)
Connects to ep.
TcpSocket(const Endpoint &ep)
Creates a socket and connects to ep.
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
Endpoint for I/O operations.
Definition IODevice.h:96
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:240
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