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/api.h>
33 #include <cxxtools/iodevice.h>
34 #include <cxxtools/net/addrinfo.h>
35 #include <cxxtools/signal.h>
36 #include <string>
37 
38 namespace cxxtools {
39 
40 namespace net {
41 
42 class TcpServer;
43 class AddrInfo;
44 
46 {
47  class TcpSocketImpl* _impl;
48 
49  public:
50  // flags for accept method
51  enum { INHERIT = 1, DEFER_ACCEPT = 2 };
52 
53  TcpSocket();
54 
55  TcpSocket(const TcpServer& server, unsigned flags = 0);
56 
57  TcpSocket(const std::string& ipaddr, unsigned short int port);
58 
59  explicit TcpSocket(const AddrInfo& addrinfo);
60 
61  ~TcpSocket();
62 
63  std::string getSockAddr() const;
64 
65  std::string getPeerAddr() const;
66 
67  void setTimeout(std::size_t msecs);
68 
69  std::size_t timeout() const;
70 
71  std::size_t getTimeout() const
72  { return timeout(); }
73 
74  void accept(const TcpServer& server, unsigned flags = 0);
75 
76  void connect(const AddrInfo& addrinfo);
77 
78  void connect(const std::string& ipaddr, unsigned short int port)
79  { connect(AddrInfo(ipaddr, port)); }
80 
81  bool beginConnect(const AddrInfo& addrinfo);
82 
83  bool beginConnect(const std::string& ipaddr, unsigned short int port)
84  { return beginConnect(AddrInfo(ipaddr, port)); }
85 
86  void endConnect();
87 
88  Signal<TcpSocket&> connected;
89 
92  Signal<TcpSocket&> closed;
93 
94  bool isConnected() const;
95 
96  int getFd() const;
97 
98  short poll(short events) const;
99 
100  protected:
101  TcpSocket(TcpSocketImpl* impl)
102  : _impl(impl)
103  { }
104 
105  // inherit doc
106  virtual void onClose();
107 
108  // inherit doc
109  virtual bool onWait(std::size_t msecs);
110 
111  // inherit doc
112  virtual void onAttach(SelectorBase&);
113 
114  // inherit doc
115  virtual void onDetach(SelectorBase&);
116 
117  // inherit doc
118  virtual size_t onBeginRead(char* buffer, size_t n, bool& eof);
119 
120  // inherit doc
121  virtual size_t onEndRead(bool& eof);
122 
123  // inherit doc
124  virtual size_t onRead(char* buffer, size_t count, bool& eof);
125 
126  // inherit doc
127  virtual size_t onBeginWrite(const char* buffer, size_t n);
128 
129  // inherit doc
130  virtual size_t onEndWrite();
131 
132  // inherit doc
133  virtual size_t onWrite(const char* buffer, size_t count);
134 
135  virtual void onCancel();
136 
137  public:
138  // inherit doc
139  virtual SelectableImpl& simpl();
140 
141  // inherit doc
142  virtual IODeviceImpl& ioimpl();
143 };
144 
145 } // namespace net
146 
147 } // namespace cxxtools
148 
149 #endif