1 | // |
---|
2 | // SocketStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/SocketStream.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Sockets |
---|
8 | // Module: SocketStream |
---|
9 | // |
---|
10 | // Definition of the SocketStream class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_SocketStream_INCLUDED |
---|
40 | #define Net_SocketStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/StreamSocket.h" |
---|
45 | #include "Poco/BufferedBidirectionalStreamBuf.h" |
---|
46 | #include <istream> |
---|
47 | #include <ostream> |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | namespace Net { |
---|
52 | |
---|
53 | |
---|
54 | class StreamSocketImpl; |
---|
55 | |
---|
56 | |
---|
57 | class Net_API SocketStreamBuf: public Poco::BufferedBidirectionalStreamBuf |
---|
58 | /// This is the streambuf class used for reading from and writing to a socket. |
---|
59 | { |
---|
60 | public: |
---|
61 | SocketStreamBuf(const Socket& socket); |
---|
62 | /// Creates a SocketStreamBuf with the given socket. |
---|
63 | /// |
---|
64 | /// The socket's SocketImpl must be a StreamSocketImpl, |
---|
65 | /// otherwise an InvalidArgumentException is thrown. |
---|
66 | |
---|
67 | ~SocketStreamBuf(); |
---|
68 | /// Destroys the SocketStreamBuf. |
---|
69 | |
---|
70 | StreamSocketImpl* socketImpl() const; |
---|
71 | /// Returns the internal SocketImpl. |
---|
72 | |
---|
73 | protected: |
---|
74 | int readFromDevice(char* buffer, std::streamsize length); |
---|
75 | int writeToDevice(const char* buffer, std::streamsize length); |
---|
76 | |
---|
77 | private: |
---|
78 | enum |
---|
79 | { |
---|
80 | STREAM_BUFFER_SIZE = 1024 |
---|
81 | }; |
---|
82 | |
---|
83 | StreamSocketImpl* _pImpl; |
---|
84 | }; |
---|
85 | |
---|
86 | |
---|
87 | class Net_API SocketIOS: public virtual std::ios |
---|
88 | /// The base class for SocketStream, SocketInputStream and |
---|
89 | /// SocketOutputStream. |
---|
90 | /// |
---|
91 | /// This class is needed to ensure the correct initialization |
---|
92 | /// order of the stream buffer and base classes. |
---|
93 | { |
---|
94 | public: |
---|
95 | SocketIOS(const Socket& socket); |
---|
96 | /// Creates the SocketIOS with the given socket. |
---|
97 | /// |
---|
98 | /// The socket's SocketImpl must be a StreamSocketImpl, |
---|
99 | /// otherwise an InvalidArgumentException is thrown. |
---|
100 | |
---|
101 | ~SocketIOS(); |
---|
102 | /// Destroys the SocketIOS. |
---|
103 | /// |
---|
104 | /// Flushes the buffer, but does not close the socket. |
---|
105 | |
---|
106 | SocketStreamBuf* rdbuf(); |
---|
107 | /// Returns a pointer to the internal SocketStreamBuf. |
---|
108 | |
---|
109 | void close(); |
---|
110 | /// Flushes the stream and closes the socket. |
---|
111 | |
---|
112 | StreamSocket socket() const; |
---|
113 | /// Returns the underlying socket. |
---|
114 | |
---|
115 | protected: |
---|
116 | SocketStreamBuf _buf; |
---|
117 | }; |
---|
118 | |
---|
119 | |
---|
120 | class Net_API SocketOutputStream: public SocketIOS, public std::ostream |
---|
121 | /// An output stream for writing to a socket. |
---|
122 | { |
---|
123 | public: |
---|
124 | explicit SocketOutputStream(const Socket& socket); |
---|
125 | /// Creates the SocketOutputStream with the given socket. |
---|
126 | /// |
---|
127 | /// The socket's SocketImpl must be a StreamSocketImpl, |
---|
128 | /// otherwise an InvalidArgumentException is thrown. |
---|
129 | |
---|
130 | ~SocketOutputStream(); |
---|
131 | /// Destroys the SocketOutputStream. |
---|
132 | /// |
---|
133 | /// Flushes the buffer, but does not close the socket. |
---|
134 | }; |
---|
135 | |
---|
136 | |
---|
137 | class Net_API SocketInputStream: public SocketIOS, public std::istream |
---|
138 | /// An input stream for reading from a socket. |
---|
139 | /// |
---|
140 | /// When using formatted input from a SocketInputStream, |
---|
141 | /// always ensure that a receive timeout is set for the |
---|
142 | /// socket. Otherwise your program might unexpectedly |
---|
143 | /// hang. |
---|
144 | /// |
---|
145 | /// However, using formatted input from a SocketInputStream |
---|
146 | /// is not recommended, due to the read-ahead behavior of |
---|
147 | /// istream with formatted reads. |
---|
148 | { |
---|
149 | public: |
---|
150 | explicit SocketInputStream(const Socket& socket); |
---|
151 | /// Creates the SocketInputStream with the given socket. |
---|
152 | /// |
---|
153 | /// The socket's SocketImpl must be a StreamSocketImpl, |
---|
154 | /// otherwise an InvalidArgumentException is thrown. |
---|
155 | |
---|
156 | ~SocketInputStream(); |
---|
157 | /// Destroys the SocketInputStream. |
---|
158 | }; |
---|
159 | |
---|
160 | |
---|
161 | class Net_API SocketStream: public SocketIOS, public std::iostream |
---|
162 | /// An bidirectional stream for reading from and writing to a socket. |
---|
163 | /// |
---|
164 | /// When using formatted input from a SocketStream, |
---|
165 | /// always ensure that a receive timeout is set for the |
---|
166 | /// socket. Otherwise your program might unexpectedly |
---|
167 | /// hang. |
---|
168 | /// |
---|
169 | /// However, using formatted input from a SocketStream |
---|
170 | /// is not recommended, due to the read-ahead behavior of |
---|
171 | /// istream with formatted reads. |
---|
172 | { |
---|
173 | public: |
---|
174 | explicit SocketStream(const Socket& socket); |
---|
175 | /// Creates the SocketStream with the given socket. |
---|
176 | /// |
---|
177 | /// The socket's SocketImpl must be a StreamSocketImpl, |
---|
178 | /// otherwise an InvalidArgumentException is thrown. |
---|
179 | |
---|
180 | ~SocketStream(); |
---|
181 | /// Destroys the SocketStream. |
---|
182 | /// |
---|
183 | /// Flushes the buffer, but does not close the socket. |
---|
184 | }; |
---|
185 | |
---|
186 | |
---|
187 | // |
---|
188 | // inlines |
---|
189 | // |
---|
190 | inline StreamSocketImpl* SocketStreamBuf::socketImpl() const |
---|
191 | { |
---|
192 | return _pImpl; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | } } // namespace Poco::Net |
---|
197 | |
---|
198 | |
---|
199 | #endif // Net_SocketStream_INCLUDED |
---|