1//
2// SocketStream.h
3//
4// Library: Net
5// Package: Sockets
6// Module: SocketStream
7//
8// Definition of the SocketStream class.
9//
10// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Net_SocketStream_INCLUDED
18#define Net_SocketStream_INCLUDED
19
20
21#include "Poco/Net/Net.h"
22#include "Poco/Net/StreamSocket.h"
23#include "Poco/BufferedBidirectionalStreamBuf.h"
24#include <istream>
25#include <ostream>
26
27
28namespace Poco {
29namespace Net {
30
31
32class StreamSocketImpl;
33
34
35class Net_API SocketStreamBuf: public Poco::BufferedBidirectionalStreamBuf
36 /// This is the streambuf class used for reading from and writing to a socket.
37{
38public:
39 SocketStreamBuf(const Socket& socket);
40 /// Creates a SocketStreamBuf with the given socket.
41 ///
42 /// The socket's SocketImpl must be a StreamSocketImpl,
43 /// otherwise an InvalidArgumentException is thrown.
44
45 ~SocketStreamBuf();
46 /// Destroys the SocketStreamBuf.
47
48 StreamSocketImpl* socketImpl() const;
49 /// Returns the internal SocketImpl.
50
51protected:
52 int readFromDevice(char* buffer, std::streamsize length);
53 int writeToDevice(const char* buffer, std::streamsize length);
54
55private:
56 enum
57 {
58 STREAM_BUFFER_SIZE = 1024
59 };
60
61 StreamSocketImpl* _pImpl;
62};
63
64
65class Net_API SocketIOS: public virtual std::ios
66 /// The base class for SocketStream, SocketInputStream and
67 /// SocketOutputStream.
68 ///
69 /// This class is needed to ensure the correct initialization
70 /// order of the stream buffer and base classes.
71{
72public:
73 SocketIOS(const Socket& socket);
74 /// Creates the SocketIOS with the given socket.
75 ///
76 /// The socket's SocketImpl must be a StreamSocketImpl,
77 /// otherwise an InvalidArgumentException is thrown.
78
79 ~SocketIOS();
80 /// Destroys the SocketIOS.
81 ///
82 /// Flushes the buffer, but does not close the socket.
83
84 SocketStreamBuf* rdbuf();
85 /// Returns a pointer to the internal SocketStreamBuf.
86
87 void close();
88 /// Flushes the stream and closes the socket.
89
90 StreamSocket socket() const;
91 /// Returns the underlying socket.
92
93protected:
94 SocketStreamBuf _buf;
95};
96
97
98class Net_API SocketOutputStream: public SocketIOS, public std::ostream
99 /// An output stream for writing to a socket.
100{
101public:
102 explicit SocketOutputStream(const Socket& socket);
103 /// Creates the SocketOutputStream with the given socket.
104 ///
105 /// The socket's SocketImpl must be a StreamSocketImpl,
106 /// otherwise an InvalidArgumentException is thrown.
107
108 ~SocketOutputStream();
109 /// Destroys the SocketOutputStream.
110 ///
111 /// Flushes the buffer, but does not close the socket.
112};
113
114
115class Net_API SocketInputStream: public SocketIOS, public std::istream
116 /// An input stream for reading from a socket.
117 ///
118 /// When using formatted input from a SocketInputStream,
119 /// always ensure that a receive timeout is set for the
120 /// socket. Otherwise your program might unexpectedly
121 /// hang.
122 ///
123 /// However, using formatted input from a SocketInputStream
124 /// is not recommended, due to the read-ahead behavior of
125 /// istream with formatted reads.
126{
127public:
128 explicit SocketInputStream(const Socket& socket);
129 /// Creates the SocketInputStream with the given socket.
130 ///
131 /// The socket's SocketImpl must be a StreamSocketImpl,
132 /// otherwise an InvalidArgumentException is thrown.
133
134 ~SocketInputStream();
135 /// Destroys the SocketInputStream.
136};
137
138
139class Net_API SocketStream: public SocketIOS, public std::iostream
140 /// An bidirectional stream for reading from and writing to a socket.
141 ///
142 /// When using formatted input from a SocketStream,
143 /// always ensure that a receive timeout is set for the
144 /// socket. Otherwise your program might unexpectedly
145 /// hang.
146 ///
147 /// However, using formatted input from a SocketStream
148 /// is not recommended, due to the read-ahead behavior of
149 /// istream with formatted reads.
150{
151public:
152 explicit SocketStream(const Socket& socket);
153 /// Creates the SocketStream with the given socket.
154 ///
155 /// The socket's SocketImpl must be a StreamSocketImpl,
156 /// otherwise an InvalidArgumentException is thrown.
157
158 ~SocketStream();
159 /// Destroys the SocketStream.
160 ///
161 /// Flushes the buffer, but does not close the socket.
162};
163
164
165//
166// inlines
167//
168inline StreamSocketImpl* SocketStreamBuf::socketImpl() const
169{
170 return _pImpl;
171}
172
173
174} } // namespace Poco::Net
175
176
177#endif // Net_SocketStream_INCLUDED
178