summaryrefslogtreecommitdiffstats
path: root/mimelib/doc/protocol.html
blob: 28bb81db61469cdafbae725ed463c1edb390df3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<HTML>
<HEAD>
  <TITLE> DwProtocolClient Man Page </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<H2>
  <FONT COLOR="navy"> NAME </FONT>
</H2>
<P>
DwProtocolClient -- Base class for all protocol clients
<H2>
  <FONT COLOR="navy"> SYNOPSIS </FONT>
</H2>
<PRE>class DW_EXPORT DwProtocolClient {

public:

    enum Failure {
        kFailNoFailure      = 0, // No failure
        kFailNoWinsock      = 1, // A usable Winsock DLL could not be found
        kFailNetDown        = 2, // The network is down
        kFailHostNotFound   = 3, // The server was not found
        kFailConnReset      = 4, // The connection was reset
        kFailNetUnreachable = 5, // The network is unreachable
        kFailTimedOut       = 6, // Timed out while waiting for an operation
                                 // to complete
        kFailConnDropped    = 7,
        kFailConnRefused    = 8,
        kFailNoResources    = 9
    };

    enum Error {
        kErrNoError = 0,
        kErrUnknownError = 0x4000,
        kErrBadParameter = 0x4001,
        kErrBadUsage     = 0x4002,
        kErrNoWinsock    = 0x4003,  // Win32
        kErrHostNotFound = 0x5000,  // UNIX
        kErrTryAgain     = 0x5001,  // UNIX
        kErrNoRecovery   = 0x5002,  // UNIX
        kErrNoData       = 0x5003,  // UNIX
        kErrNoAddress    = 0x5004,  // UNIX
    };

protected:

    <A HREF="protocol.html#DwProtocolClient">DwProtocolClient</A>();

public:

    virtual <A HREF="protocol.html#~DwProtocolClient">~DwProtocolClient</A>();
    virtual int <A HREF="protocol.html#Open">Open</A>(const char* aServer, DwUint16 aPort);
    DwBool <A HREF="protocol.html#IsOpen">IsOpen</A>() const;
    int <A HREF="protocol.html#Close">Close</A>();
    int <A HREF="protocol.html#SetReceiveTimeout">SetReceiveTimeout</A>(int aSecs);
    int <A HREF="protocol.html#LastCommand">LastCommand</A>() const;
    int <A HREF="protocol.html#LastFailure">LastFailure</A>() const;
    const char* <A HREF="protocol.html#LastFailureStr">LastFailureStr</A>() const;
    int <A HREF="protocol.html#LastError">LastError</A>() const;
    const char* <A HREF="protocol.html#LastErrorStr">LastErrorStr</A>() const;

protected:

    enum {
        kWSAStartup=1,  // Win32
        kgethostbyname,
        ksocket,
        ksetsockopt,
        kconnect,
        ksend,
        krecv,
        kclose,         // UNIX
        kclosesocket,   // Win32
        kselect
    };
    DwBool      mIsDllOpen;
    DwBool      mIsOpen;
    SOCKET      mSocket;
    DwUint16    mPort;
    char*       mServerName;
    int         mReceiveTimeout;
    int         mLastCommand;
    int         mFailureCode;
    const char* mFailureStr;
    int         mErrorCode;
    const char* mErrorStr;
    virtual void <A HREF="protocol.html#HandleError">HandleError</A>(int aErrorCode, int aSystemCall);
    int <A HREF="protocol.html#PSend">PSend</A>(const char* aBuf, int aBufLen);
    int <A HREF="protocol.html#PReceive">PReceive</A>(char* aBuf, int aBufSize);
};
</PRE>
<H2>
  <FONT COLOR="navy"> DESCRIPTION </FONT>
</H2>
<P>
<B><TT>DwProtocolClient</TT></B> is the base class for other classes that
implement specific protocols, such as SMTP, POP, and NNTP.
<B><TT>DwProtocolClient</TT></B> serves two purposes. First, It combines
operations common to all its derived classes, such as opening a TCP connection
to the server. Second, it provides a platform-independent interface to the
network services required by its subclasses.
<P>
There are two separate implementations of
<B><TT>DwProtocolClient</TT></B>: one for Berkeley sockets under UNIX, and
one for Winsock under Win32. The interface is the same for both implementations,
thus providing platform independence.
<P>
There are two platform-specific details that you should be aware of. First,
if you are writing a UNIX program, you should be sure to handle the SIGPIPE
signal. This signal is raised when a program tries to write to a TCP connection
that was shutdown by the remote host. The default action for this signal
is to terminate the program. To prevent this from happening in your program,
you should either catch the signal or tell the operating system to ignore
it. Second, if you are writing a Win32 application for Windows NT or Windows95,
you should be aware of the fact that the constructor calls the Winsock function
<B><TT>WSAStartup()</TT></B> to initialize the Winsock DLL. (The destructor
calls <B><TT>WSACleanup()</TT></B>.) Because it is possible for
<B><TT>WSAStartup()</TT></B> to fail, it is also possible that the constructor
may fail. To verify that the constructor has succeeded, call the member function
<B><TT>LastError()</TT></B> and check that it returns zero.
<P>
To open a connection to a server, call <B><TT>Open()</TT></B> with the server
name and TCP port number as arguments. <B><TT>Open()</TT></B> is declared
virtual; derived classes may override this member function.
<B><TT>Open()</TT></B> may fail, so you should check the return value to
verify that it succeeded. To close the connection, call
<B><TT>Close()</TT></B>. To check if a connection is open, call
<B><TT>IsOpen()</TT></B>. <B><TT>IsOpen()</TT></B> returns a value that indicates
whether or not a call to <B><TT>Open()</TT></B> was successful; it will not
detect failure in the network or a close operation by the remote host.
<P>
<B><TT>DwProtocolClient</TT></B> sets a timeout on receive operations on
the TCP connection. The default value of the timeout period is 90 seconds.
To change the default value, call <B><TT>SetReceiveTimeout()</TT></B> and
pass the new value as an argument.
<P>
Whenever <B><TT>DwProtocolClient</TT></B> cannot complete an operation, it
is because an error has occurred. Most member functions indicate that an
error has occurred via their return values. For most member functions, a
return value of -1 indicates an error. To get the specific error that has
occurred, call <B><TT>LastError()</TT></B>, which returns either the system
error code or a MIME++ defined error code. To get a text string that describes
the error, call <B><TT>LastErrorStr()</TT></B>.
<P>
Some errors are also considered "failures." A failure occurs when an operation
cannot be completed because of conditions external to the program. For example,
a failure occurs when the network is down or when an application's user enters
bad input. Errors that occur because of programmer error are not considered
failures. If an error occurs, you should call <B><TT>LastError()</TT></B>
to determine the error, but you should also call
<B><TT>LastFailure()</TT></B> to determine if a failure occurred. In interactive
applications, failures should always be reported to the application's user.
To get a text string that describes a failure, call
<B><TT>LastFailureStr()</TT></B>.
<P>
It is possible to translate the error and failure message strings to a language
other than English. To do this, you may override the virtual function
<B><TT>HandleError()</TT></B>.
<H2>
  <FONT COLOR="navy"> Public Member Functions </FONT>
</H2>
<P>
<FONT COLOR="teal"><B> virtual
<A NAME="~DwProtocolClient">~DwProtocolClient</A>() </B></FONT>
<P>
Frees the resources used by this object. In a Win32 environment, the destructor
calls <B><TT>WSACleanup()</TT></B>.
<P>
<FONT COLOR="teal"><B> virtual int <A NAME="Open">Open</A>(const char* aServer,
DwUint16 aPort) </B></FONT>
<P>
Opens a TCP connection to the server <B><TT>aServer</TT></B> at port
<B><TT>aPort</TT></B>. <B><TT>aServer</TT></B> may be either a host name,
such as "smtp.acme.com" or an IP number in dotted decimal format, such as
"147.81.64.59". If the connection attempt succeeds,
<B><TT>Open()</TT></B> returns 0; othewise, it returns -1. To determine what
error occurred when the connection attempt fails, call the member function
<B><TT>LastError()</TT></B>. To determine if a failure also occurred, call
the member function <B><TT>LastFailure()</TT></B>.
<P>
<FONT COLOR="teal"><B> DwBool <A NAME="IsOpen">IsOpen</A>() const </B></FONT>
<P>
Returns true value if a connection to the server is open.
<B><TT>IsOpen()</TT></B> will return a true value if a call to
<B><TT>Open()</TT></B> was successful; it will not detect failure in the
network or a close operation by the remote host.
<P>
<FONT COLOR="teal"><B> int <A NAME="Close">Close</A>() </B></FONT>
<P>
Closes the connection to the server. Returns 0 if successful, or returns
-1 if unsuccessful.
<P>
<FONT COLOR="teal"><B> int
<A NAME="SetReceiveTimeout">SetReceiveTimeout</A>(int aSecs) </B></FONT>
<P>
Changes the default timeout for receive operations on the socket to
<B><TT>aSecs</TT></B> seconds. The default value is 90 seconds.
<P>
<FONT COLOR="teal"><B> int <A NAME="LastCommand">LastCommand</A>() const
</B></FONT>
<P>
Returns an enumerated value indicating the last command sent to the server.
Enumerated values are defined in subclasses of
<B><TT>DwProtocolClient</TT></B>.
<P>
<FONT COLOR="teal"><B> int <A NAME="LastFailure">LastFailure</A>() const
</B></FONT>
<P>
Returns an enumerated value indicating what failure last occurred.
<P>
<FONT COLOR="teal"><B> const char*
<A NAME="LastFailureStr">LastFailureStr</A>() const </B></FONT>
<P>
Returns a failure message string associated with the failure code returned
by <B><TT>LastFailure()</TT></B>.
<P>
<FONT COLOR="teal"><B> int <A NAME="LastError">LastError</A>() const
</B></FONT>
<P>
Returns an error code for the last error that occurred. Normally, the error
code returned is an error code returned by a system call;
<B><TT>DwProtocolClient</TT></B> does no translation of error codes returned
by system calls. In some cases, an error code defined by MIME++ may returned
to indicate improper use of the <B><TT>DwProtocolClient</TT></B> class.
<P>
<FONT COLOR="teal"><B> const char* <A NAME="LastErrorStr">LastErrorStr</A>()
const </B></FONT>
<P>
Returns an error message string associated with the error code returned by
<B><TT>LastError()</TT></B>.
<H2>
  <FONT COLOR="navy"> Protected Member Functions </FONT>
</H2>
<P>
<B><FONT COLOR="teal"> <A NAME="DwProtocolClient">DwProtocolClient</A>()
</FONT></B>
<P>
Initializes the <B><TT>DwProtocolClient</TT></B> object. In a Win32 environment,
this constructor calls <B><TT>WSAStartup()</TT></B> to initialize the Winsock
DLL. To verify that the DLL was initialized successfully, call the member
function <B><TT>LastError()</TT></B> and verify that it returns zero.
<P>
<B><FONT COLOR="teal"> virtual void <A NAME="HandleError">HandleError</A>(int
aErrorCode, int aSystemCall) </FONT></B>
<P>
Interprets error codes. <B><TT>aErrorCode</TT></B> is an error code, which
may be a system error code, or an error code defined by
<B><TT>DwProtocolClient</TT></B>. <B><TT>aSystemCall</TT></B> is an enumerated
value defined by <B><TT>DwProtocolClient</TT></B> that indicates the last
system call made, which should be the system call that set the error code.
<B><TT>HandleError()</TT></B> sets values for <B><TT>mErrorStr</TT></B>,
<B><TT>mFailureCode</TT></B>, and <B><TT>mFailureStr</TT></B>.
<P>
<B><FONT COLOR="teal"> int <A NAME="PSend">PSend</A>(const char* aBuf, int
aBufLen) </FONT></B>
<P>
Sends <B><TT>aBufLen</TT></B> characters from the buffer
<B><TT>aBuf</TT></B>. Returns the number of characters sent. If the number
of characters sent is less than the number of characters specified in
<B><TT>aBufLen</TT></B>, the caller should call
<B><TT>LastError()</TT></B> to determine what, if any, error occurred. To
determine if a failure also occurred, call the member function
<B><TT>LastFailure()</TT></B>.
<P>
<B><FONT COLOR="teal"> int <A NAME="PReceive">PReceive</A>(char* aBuf, int
aBufSize) </FONT></B>
<P>
Receives up to <B><TT>aBufSize</TT></B> characters into the buffer
<B><TT>aBuf</TT></B>. Returns the number of characters received. If zero
is returned, the caller should call the member function
<B><TT>LastError()</TT></B> to determine what, if any, error occurred. To
determine if a failure also occurred, call the member function
<B><TT>LastFailure()</TT></B>.
</BODY></HTML>