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 {
49  class TcpStream : public IOStream, public Connectable
50  {
51  void init(cxxtools::Timespan timeout);
52 
53  public:
55  explicit TcpStream(unsigned bufsize = 0, cxxtools::Milliseconds timeout = Selectable::WaitInfinite)
56  : IOStream(bufsize > 0 ? bufsize : 8192, bufsize == 0)
57  {
58  init(timeout);
59  }
60 
62  TcpStream(const std::string& ipaddr, unsigned short int port,
63  unsigned bufsize = 0, cxxtools::Milliseconds timeout = Selectable::WaitInfinite)
64  : IOStream(bufsize > 0 ? bufsize : 8192, bufsize == 0)
65  , _socket(ipaddr, port)
66  {
67  init(timeout);
68  }
69 
71  explicit TcpStream(const AddrInfo& addrinfo,
72  unsigned bufsize = 0, cxxtools::Milliseconds timeout = Selectable::WaitInfinite)
73  : IOStream(bufsize > 0 ? bufsize : 8192, bufsize == 0)
74  , _socket(addrinfo)
75  {
76  init(timeout);
77  }
78 
80  TcpStream(const char* ipaddr, unsigned short int port,
81  unsigned bufsize = 8192, cxxtools::Milliseconds timeout = Selectable::WaitInfinite)
82  : IOStream(bufsize, true)
83  , _socket(ipaddr, port)
84  {
85  init(timeout);
86  }
87 
89  explicit TcpStream(TcpServer& server, unsigned bufsize = 8192,
90  unsigned flags = 0, cxxtools::Milliseconds timeout = Selectable::WaitInfinite)
91  : IOStream(bufsize, true)
92  , _socket(server, flags)
93  {
94  init(timeout);
95  }
96 
98  void setTimeout(Milliseconds timeout)
99  { _socket.setTimeout(timeout); }
100 
103  { return _socket.getTimeout(); }
104 
105  void close()
106  { _socket.close(); }
107 
108  bool beginConnect(const AddrInfo& addrinfo)
109  { return _socket.beginConnect(addrinfo); }
110 
111  bool beginConnect(const std::string& ipaddr, unsigned short int port)
112  { return _socket.beginConnect(ipaddr, port); }
113 
114  void endConnect()
115  { _socket.endConnect(); }
116 
117  void connect(const AddrInfo& addrinfo)
118  { _socket.connect(addrinfo); }
119 
120  void connect(const std::string& ipaddr, unsigned short int port)
121  { _socket.connect(ipaddr, port); }
122 
123  void accept(const TcpServer& server, unsigned flags = 0)
124  { _socket.accept(server, flags); }
125 
126  std::string getSockAddr() const
127  { return _socket.getSockAddr(); }
128 
129  std::string getPeerAddr() const
130  { return _socket.getPeerAddr(); }
131 
132  bool isConnected() const
133  { return _socket.isConnected(); }
134 
135  int getFd() const
136  { return _socket.getFd(); }
137 
138  // initiates a ssl connection on the socket
139  void sslConnect()
140  { _socket.sslConnect(); }
141 
142  // accept a ssl connection from the peer
143  void sslAccept()
144  { _socket.sslAccept(); }
145 
146  // terminates ssl
147  void sslShutdown()
148  { _socket.sslShutdown(); }
149 
151  { return _socket; }
152 
162  Signal<TcpStream&> inputReady;
163 
170  Signal<TcpStream&> outputReady;
171 
174  Signal<TcpStream&> connected;
175 
178  Signal<TcpStream&> closed;
179 
180  private:
181  TcpSocket _socket;
182 
183  void onInput(IODevice&);
184  void onOutput(IODevice&);
185  void onConnected(TcpSocket&);
186  void onClosed(TcpSocket&);
187  };
188 
190 
191 } // namespace net
192 
193 } // namespace cxxtools
194 
195 #endif // CXXTOOLS_NET_TCPSTREAM_H