1 | // |
---|
2 | // DialogSocket.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/DialogSocket.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Sockets |
---|
8 | // Module: DialogSocket |
---|
9 | // |
---|
10 | // Definition of the DialogSocket 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_DialogSocket_INCLUDED |
---|
40 | #define Net_DialogSocket_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/StreamSocket.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | namespace Net { |
---|
49 | |
---|
50 | |
---|
51 | class Net_API DialogSocket: public StreamSocket |
---|
52 | /// DialogSocket is a subclass of StreamSocket that |
---|
53 | /// can be used for implementing request-response |
---|
54 | /// based client server connections. |
---|
55 | /// |
---|
56 | /// A request is always a single-line command terminated |
---|
57 | /// by CR-LF. |
---|
58 | /// |
---|
59 | /// A response can either be a single line of text terminated |
---|
60 | /// by CR-LF, or multiple lines of text in the format used |
---|
61 | /// by the FTP and SMTP protocols. |
---|
62 | /// |
---|
63 | /// Limited support for the TELNET protocol (RFC 854) is |
---|
64 | /// available. |
---|
65 | { |
---|
66 | public: |
---|
67 | DialogSocket(); |
---|
68 | /// Creates an unconnected stream socket. |
---|
69 | /// |
---|
70 | /// Before sending or receiving data, the socket |
---|
71 | /// must be connected with a call to connect(). |
---|
72 | |
---|
73 | explicit DialogSocket(const SocketAddress& address); |
---|
74 | /// Creates a stream socket and connects it to |
---|
75 | /// the socket specified by address. |
---|
76 | |
---|
77 | DialogSocket(const Socket& socket); |
---|
78 | /// Creates the DialogSocket with the SocketImpl |
---|
79 | /// from another socket. The SocketImpl must be |
---|
80 | /// a StreamSocketImpl, otherwise an InvalidArgumentException |
---|
81 | /// will be thrown. |
---|
82 | |
---|
83 | ~DialogSocket(); |
---|
84 | /// Destroys the DialogSocket. |
---|
85 | |
---|
86 | DialogSocket& operator = (const Socket& socket); |
---|
87 | /// Assignment operator. |
---|
88 | /// |
---|
89 | /// Releases the socket's SocketImpl and |
---|
90 | /// attaches the SocketImpl from the other socket and |
---|
91 | /// increments the reference count of the SocketImpl. |
---|
92 | |
---|
93 | void sendByte(unsigned char ch); |
---|
94 | /// Sends a single byte over the socket connection. |
---|
95 | |
---|
96 | void sendString(const char* str); |
---|
97 | /// Sends the given null-terminated string over |
---|
98 | /// the socket connection. |
---|
99 | |
---|
100 | void sendString(const std::string& str); |
---|
101 | /// Sends the given string over the socket connection. |
---|
102 | |
---|
103 | void sendMessage(const std::string& message); |
---|
104 | /// Appends a CR-LF sequence to the message and sends it |
---|
105 | /// over the socket connection. |
---|
106 | |
---|
107 | void sendMessage(const std::string& message, const std::string& arg); |
---|
108 | /// Concatenates message and arg, separated by a space, appends a |
---|
109 | /// CR-LF sequence, and sends the result over the socket connection. |
---|
110 | |
---|
111 | void sendMessage(const std::string& message, const std::string& arg1, const std::string& arg2); |
---|
112 | /// Concatenates message and args, separated by a space, appends a |
---|
113 | /// CR-LF sequence, and sends the result over the socket connection. |
---|
114 | |
---|
115 | bool receiveMessage(std::string& message); |
---|
116 | /// Receives a single-line message, terminated by CR-LF, |
---|
117 | /// from the socket connection and appends it to response. |
---|
118 | /// |
---|
119 | /// Returns true if a message has been read or false if |
---|
120 | /// the connection has been closed by the peer. |
---|
121 | |
---|
122 | int receiveStatusMessage(std::string& message); |
---|
123 | /// Receives a single-line or multi-line response from |
---|
124 | /// the socket connection. The format must be according to |
---|
125 | /// one of the response formats specified in the FTP (RFC 959) |
---|
126 | /// or SMTP (RFC 2821) specifications. |
---|
127 | /// |
---|
128 | /// The first line starts with a 3-digit status code. |
---|
129 | /// Following the status code is either a space character (' ' ) |
---|
130 | /// (in case of a single-line response) or a minus character ('-') |
---|
131 | /// in case of a multi-line response. The following lines can have |
---|
132 | /// a three-digit status code followed by a minus-sign and some |
---|
133 | /// text, or some arbitrary text only. The last line again begins |
---|
134 | /// with a three-digit status code (which must be the same as the |
---|
135 | /// one in the first line), followed by a space and some arbitrary |
---|
136 | /// text. All lines must be terminated by a CR-LF sequence. |
---|
137 | /// |
---|
138 | /// The response contains all response lines, separated by a newline |
---|
139 | /// character, including the status code. The status code is returned. |
---|
140 | /// If the response line does not contain a status code, 0 is returned. |
---|
141 | |
---|
142 | int get(); |
---|
143 | /// Reads one character from the connection. |
---|
144 | /// |
---|
145 | /// Returns -1 (EOF_CHAR) if no more characters are available. |
---|
146 | |
---|
147 | int peek(); |
---|
148 | /// Returns the character that would be returned by the next call |
---|
149 | /// to get(), without actually extracting the character from the |
---|
150 | /// buffer. |
---|
151 | /// |
---|
152 | /// Returns -1 (EOF_CHAR) if no more characters are available. |
---|
153 | |
---|
154 | void synch(); |
---|
155 | /// Sends a TELNET SYNCH signal over the connection. |
---|
156 | /// |
---|
157 | /// According to RFC 854, a TELNET_DM char is sent |
---|
158 | /// via sendUrgent(). |
---|
159 | |
---|
160 | void sendTelnetCommand(unsigned char command); |
---|
161 | /// Sends a TELNET command sequence (TELNET_IAC followed |
---|
162 | /// by the given command) over the connection. |
---|
163 | |
---|
164 | void sendTelnetCommand(unsigned char command, unsigned char arg); |
---|
165 | /// Sends a TELNET command sequence (TELNET_IAC followed |
---|
166 | /// by the given command, followed by arg) over the connection. |
---|
167 | |
---|
168 | enum TelnetCodes |
---|
169 | { |
---|
170 | TELNET_SE = 240, |
---|
171 | TELNET_NOP = 241, |
---|
172 | TELNET_DM = 242, |
---|
173 | TELNET_BRK = 243, |
---|
174 | TELNET_IP = 244, |
---|
175 | TELNET_AO = 245, |
---|
176 | TELNET_AYT = 246, |
---|
177 | TELNET_EC = 247, |
---|
178 | TELNET_EL = 248, |
---|
179 | TELNET_GA = 249, |
---|
180 | TELNET_SB = 250, |
---|
181 | TELNET_WILL = 251, |
---|
182 | TELNET_WONT = 252, |
---|
183 | TELNET_DO = 253, |
---|
184 | TELNET_DONT = 254, |
---|
185 | TELNET_IAC = 255 |
---|
186 | }; |
---|
187 | |
---|
188 | protected: |
---|
189 | void allocBuffer(); |
---|
190 | void refill(); |
---|
191 | bool receiveLine(std::string& line); |
---|
192 | int receiveStatusLine(std::string& line); |
---|
193 | |
---|
194 | private: |
---|
195 | enum |
---|
196 | { |
---|
197 | RECEIVE_BUFFER_SIZE = 1024, |
---|
198 | EOF_CHAR = -1 |
---|
199 | }; |
---|
200 | |
---|
201 | char* _pBuffer; |
---|
202 | char* _pNext; |
---|
203 | char* _pEnd; |
---|
204 | }; |
---|
205 | |
---|
206 | |
---|
207 | } } // namespace Poco::Net |
---|
208 | |
---|
209 | |
---|
210 | #endif // Net_DialogSocket_INCLUDED |
---|