tcpsocket.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2009 by Marc Boris Duerner, Tommi Maekitalo
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 CXXTOOLS_net_TcpSocket_h
30 #define CXXTOOLS_net_TcpSocket_h
31 
32 #include <cxxtools/iodevice.h>
33 #include <cxxtools/net/addrinfo.h>
34 #include <cxxtools/signal.h>
35 #include <string>
36 
37 namespace cxxtools {
38 
39 namespace net {
40 
41 class TcpServer;
42 class AddrInfo;
43 
44 class TcpSocket : public IODevice
45 {
46  class TcpSocketImpl* _impl;
47 
48  public:
49  // flags for accept method
50  enum { INHERIT = 1, DEFER_ACCEPT = 2 };
51 
52  TcpSocket();
53 
54  TcpSocket(const TcpServer& server, unsigned flags = 0);
55 
56  TcpSocket(const std::string& ipaddr, unsigned short int port);
57 
58  explicit TcpSocket(const AddrInfo& addrinfo);
59 
60  ~TcpSocket();
61 
62  std::string getSockAddr() const;
63 
64  std::string getPeerAddr() const;
65 
67 
68  Milliseconds timeout() const;
69 
71  { return timeout(); }
72 
73  void accept(const TcpServer& server, unsigned flags = 0);
74 
75  void connect(const AddrInfo& addrinfo);
76 
77  void connect(const std::string& ipaddr, unsigned short int port)
78  { connect(AddrInfo(ipaddr, port)); }
79 
80  bool beginConnect(const AddrInfo& addrinfo);
81 
82  bool beginConnect(const std::string& ipaddr, unsigned short int port)
83  { return beginConnect(AddrInfo(ipaddr, port)); }
84 
85  void endConnect();
86 
87  Signal<TcpSocket&> connected;
88  Signal<TcpSocket&> sslAccepted;
89  Signal<TcpSocket&> sslConnected;
90  Signal<TcpSocket&> sslClosed;
91 
94  Signal<TcpSocket&> closed;
95 
96  bool isConnected() const;
97 
98  int getFd() const;
99 
100  short poll(short events) const;
101 
102  void loadSslCertificateFile(const std::string& certFile, const std::string& privateKeyFile = std::string());
103 
105  void beginSslConnect();
106  void endSslConnect();
107 
109  void sslConnect();
110 
112  void beginSslAccept();
113  void endSslAccept();
114 
116  void sslAccept();
117 
119  void beginSslShutdown();
120  void endSslShutdown();
121 
123  void sslShutdown();
124 
125  protected:
126  TcpSocket(TcpSocketImpl* impl)
127  : _impl(impl)
128  { }
129 
130  // inherit doc
131  virtual void onClose();
132 
133  // inherit doc
134  virtual bool onWait(Timespan timeout);
135 
136  // inherit doc
137  virtual void onAttach(SelectorBase&);
138 
139  // inherit doc
140  virtual void onDetach(SelectorBase&);
141 
142  // inherit doc
143  virtual size_t onBeginRead(char* buffer, size_t n, bool& eof);
144 
145  // inherit doc
146  virtual size_t onEndRead(bool& eof);
147 
148  // inherit doc
149  virtual size_t onRead(char* buffer, size_t count, bool& eof);
150 
151  // inherit doc
152  virtual size_t onBeginWrite(const char* buffer, size_t n);
153 
154  // inherit doc
155  virtual size_t onEndWrite();
156 
157  // inherit doc
158  virtual size_t onWrite(const char* buffer, size_t count);
159 
160  virtual void onCancel();
161 
162  public:
163  // inherit doc
164  virtual SelectableImpl& simpl();
165 
166  // inherit doc
167  virtual IODeviceImpl& ioimpl();
168 };
169 
170 } // namespace net
171 
172 } // namespace cxxtools
173 
174 #endif