tcpstream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 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_TCPSTREAM_H
30 #define CXXTOOLS_NET_TCPSTREAM_H
31 
32 #include <string>
33 #include <cxxtools/net/tcpserver.h>
34 #include <cxxtools/net/tcpsocket.h>
35 #include <cxxtools/net/addrinfo.h>
36 #include <cxxtools/iostream.h>
37 
38 namespace cxxtools
39 {
40 
41 namespace net
42 {
43  class TcpStream : public IOStream, public Connectable
44  {
45  void init(std::size_t timeout);
46 
47  public:
48  explicit TcpStream(unsigned bufsize = 8192, std::size_t timeout = Selectable::WaitInfinite)
49  : IOStream(bufsize)
50  {
51  init(timeout);
52  }
53 
54  TcpStream(const std::string& ipaddr, unsigned short int port,
55  unsigned bufsize = 8192, std::size_t timeout = Selectable::WaitInfinite)
56  : IOStream(bufsize)
57  , _socket(ipaddr, port)
58  {
59  init(timeout);
60  }
61 
62  explicit TcpStream(const AddrInfo& addrinfo,
63  unsigned bufsize = 8192, std::size_t timeout = Selectable::WaitInfinite)
64  : IOStream(bufsize)
65  , _socket(addrinfo)
66  {
67  init(timeout);
68  }
69 
70  TcpStream(const char* ipaddr, unsigned short int port,
71  unsigned bufsize = 8192, std::size_t timeout = Selectable::WaitInfinite)
72  : IOStream(bufsize)
73  , _socket(ipaddr, port)
74  {
75  init(timeout);
76  }
77 
78  explicit TcpStream(TcpServer& server, unsigned bufsize = 8192,
79  unsigned flags = 0, std::size_t timeout = Selectable::WaitInfinite)
80  : IOStream(bufsize)
81  , _socket(server, flags)
82  {
83  init(timeout);
84  }
85 
87  void setTimeout(std::size_t timeout)
88  { _socket.setTimeout(timeout); }
89 
91  std::size_t getTimeout() const
92  { return _socket.getTimeout(); }
93 
94  void close()
95  { _socket.close(); }
96 
97  bool beginConnect(const AddrInfo& addrinfo)
98  { return _socket.beginConnect(addrinfo); }
99 
100  bool beginConnect(const std::string& ipaddr, unsigned short int port)
101  { return _socket.beginConnect(ipaddr, port); }
102 
103  void endConnect()
104  { _socket.endConnect(); }
105 
106  void connect(const AddrInfo& addrinfo)
107  { _socket.connect(addrinfo); }
108 
109  void connect(const std::string& ipaddr, unsigned short int port)
110  { _socket.connect(ipaddr, port); }
111 
112  void accept(const TcpServer& server, unsigned flags = 0)
113  { _socket.accept(server, flags); }
114 
115  std::string getSockAddr() const
116  { return _socket.getSockAddr(); }
117 
118  std::string getPeerAddr() const
119  { return _socket.getPeerAddr(); }
120 
121  bool isConnected() const
122  { return _socket.isConnected(); }
123 
124  int getFd() const
125  { return _socket.getFd(); }
126 
128  { return _socket; }
129 
139  Signal<TcpStream&> inputReady;
140 
147  Signal<TcpStream&> outputReady;
148 
151  Signal<TcpStream&> connected;
152 
155  Signal<TcpStream&> closed;
156 
157  private:
158  TcpSocket _socket;
159 
160  void onInput(IODevice&);
161  void onOutput(IODevice&);
162  void onConnected(TcpSocket&);
163  void onClosed(TcpSocket&);
164  };
165 
167 
168 } // namespace net
169 
170 } // namespace cxxtools
171 
172 #endif // CXXTOOLS_NET_TCPSTREAM_H