00001 /* 00002 * inetprot.h 00003 * 00004 * Internet Protocol ancestor channel class 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-2002 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Contributor(s): ______________________________________. 00025 * 00026 * $Log: inetprot.h,v $ 00027 * Revision 1.17 2002/11/06 22:47:24 robertj 00028 * Fixed header comment (copyright etc) 00029 * 00030 * Revision 1.16 2002/09/16 01:08:59 robertj 00031 * Added #define so can select if #pragma interface/implementation is used on 00032 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00033 * 00034 * Revision 1.15 2001/09/26 09:37:04 robertj 00035 * Added virtual keyword to a lot of functions so can be overridden. 00036 * 00037 * Revision 1.14 1999/03/09 08:01:46 robertj 00038 * Changed comments for doc++ support (more to come). 00039 * 00040 * Revision 1.13 1999/02/16 08:07:10 robertj 00041 * MSVC 6.0 compatibility changes. 00042 * 00043 * Revision 1.12 1998/09/23 06:19:40 robertj 00044 * Added open source copyright license. 00045 * 00046 * Revision 1.11 1996/09/14 13:09:13 robertj 00047 * Major upgrade: 00048 * rearranged sockets to help support IPX. 00049 * added indirect channel class and moved all protocols to descend from it, 00050 * separating the protocol from the low level byte transport. 00051 * 00052 * Revision 1.10 1996/05/15 10:07:00 robertj 00053 * Added access function to set intercharacter line read timeout. 00054 * 00055 * Revision 1.9 1996/05/09 12:14:02 robertj 00056 * Rewrote the "unread" buffer usage and then used it to improve ReadLine() performance. 00057 * 00058 * Revision 1.8 1996/03/31 08:43:38 robertj 00059 * Added version of WriteCommand() and ExecteCommand() without argument string. 00060 * 00061 * Revision 1.7 1996/03/16 04:35:32 robertj 00062 * Added PString parameter version of UnRead(). 00063 * Changed lastResponseCode to an integer. 00064 * Added ParseReponse() for splitting reponse line into code and info. 00065 * 00066 * Revision 1.6 1996/02/13 12:57:05 robertj 00067 * Added access to the last response in an application socket. 00068 * 00069 * Revision 1.5 1996/02/03 11:33:16 robertj 00070 * Changed RadCmd() so can distinguish between I/O error and unknown command. 00071 * 00072 * Revision 1.4 1996/01/23 13:08:43 robertj 00073 * Major rewrite for HTTP support. 00074 * 00075 * Revision 1.3 1995/06/17 11:12:15 robertj 00076 * Documentation update. 00077 * 00078 * Revision 1.2 1995/06/17 00:39:53 robertj 00079 * More implementation. 00080 * 00081 * Revision 1.1 1995/06/04 13:17:16 robertj 00082 * Initial revision 00083 * 00084 */ 00085 00086 #ifndef _PINTERNETPROTOCOL 00087 #define _PINTERNETPROTOCOL 00088 00089 #ifdef P_USE_PRAGMA 00090 #pragma interface 00091 #endif 00092 00093 00094 class PSocket; 00095 class PIPSocket; 00096 00097 00117 class PInternetProtocol : public PIndirectChannel 00118 { 00119 PCLASSINFO(PInternetProtocol, PIndirectChannel) 00120 00121 protected: 00122 PInternetProtocol( 00123 const char * defaultServiceName, // Service name for the protocol. 00124 PINDEX cmdCount, // Number of command strings. 00125 char const * const * cmdNames // Strings for each command. 00126 ); 00127 // Create an unopened TCP/IP protocol socket channel. 00128 00129 00130 public: 00131 // Overrides from class PChannel. 00143 virtual BOOL Read( 00144 void * buf, // Pointer to a block of memory to receive the read bytes. 00145 PINDEX len // Maximum number of bytes to read into the buffer. 00146 ); 00147 00163 virtual BOOL Write( 00164 const void * buf, // Pointer to a block of memory to write. 00165 PINDEX len // Number of bytes to write. 00166 ); 00167 00171 void SetReadLineTimeout( 00172 const PTimeInterval & t 00173 ); 00174 00175 // New functions for class. 00181 virtual BOOL Connect( 00182 const PString & address, // Address of remote machine to connect to. 00183 WORD port = 0 // Port number to use for the connection. 00184 ); 00185 virtual BOOL Connect( 00186 const PString & address, // Address of remote machine to connect to. 00187 const PString & service // Service name to use for the connection. 00188 ); 00189 00195 virtual BOOL Accept( 00196 PSocket & listener // Address of remote machine to connect to. 00197 ); 00198 00205 const PString & GetDefaultService() const; 00206 00215 PIPSocket * GetSocket() const; 00216 00224 virtual BOOL WriteLine( 00225 const PString & line // String to write as a command line. 00226 ); 00227 00244 virtual BOOL ReadLine( 00245 PString & line, // String to receive a CR/LF terminated line. 00246 BOOL allowContinuation = FALSE // Flag to handle continued lines. 00247 ); 00248 00252 virtual void UnRead( 00253 int ch // Individual character to be returned. 00254 ); 00255 virtual void UnRead( 00256 const PString & str // String to be put back into data stream. 00257 ); 00258 virtual void UnRead( 00259 const void * buffer, // Characters to be put back into data stream. 00260 PINDEX len // Number of characters to be returned. 00261 ); 00262 00276 virtual BOOL WriteCommand( 00277 PINDEX cmdNumber // Number of command to write. 00278 ); 00279 virtual BOOL WriteCommand( 00280 PINDEX cmdNumber, // Number of command to write. 00281 const PString & param // Extra parameters required by the command. 00282 ); 00283 00301 virtual BOOL ReadCommand( 00302 PINDEX & num, 00303 // Number of the command parsed from the command line, or P_MAX_INDEX 00304 // if no match. 00305 PString & args // String to receive the arguments to the command. 00306 ); 00307 00324 virtual BOOL WriteResponse( 00325 unsigned numericCode, // Response code for command response. 00326 const PString & info // Extra information available after response code. 00327 ); 00328 virtual BOOL WriteResponse( 00329 const PString & code, // Response code for command response. 00330 const PString & info // Extra information available after response code. 00331 ); 00332 00351 virtual BOOL ReadResponse(); 00352 virtual BOOL ReadResponse( 00353 int & code, // Response code for command response. 00354 PString & info // Extra information available after response code. 00355 ); 00356 00368 virtual int ExecuteCommand( 00369 PINDEX cmdNumber // Number of command to write. 00370 ); 00371 virtual int ExecuteCommand( 00372 PINDEX cmdNumber, // Number of command to write. 00373 const PString & param // Extra parameters required by the command. 00374 ); 00375 00382 int GetLastResponseCode() const; 00383 00389 PString GetLastResponseInfo() const; 00390 00391 00392 protected: 00404 virtual PINDEX ParseResponse( 00405 const PString & line // Input response line to be parsed 00406 ); 00407 00408 00409 PString defaultServiceName; 00410 // Default Service name to use for the internet protocol socket. 00411 00412 PStringArray commandNames; 00413 // Names of each of the command codes. 00414 00415 PCharArray unReadBuffer; 00416 // Buffer for characters put back into the data stream. 00417 00418 PINDEX unReadCount; 00419 // Buffer count for characters put back into the data stream. 00420 00421 PTimeInterval readLineTimeout; 00422 // Time for characters in a line to be received. 00423 00424 enum StuffState { 00425 DontStuff, StuffIdle, StuffCR, StuffCRLF, StuffCRLFdot, StuffCRLFdotCR 00426 } stuffingState; 00427 // Do byte stuffing of '.' characters in output to the socket channel. 00428 00429 BOOL newLineToCRLF; 00430 // Translate \n characters to CR/LF pairs. 00431 00432 int lastResponseCode; 00433 PString lastResponseInfo; 00434 // Responses 00435 00436 private: 00437 BOOL AttachSocket(PIPSocket * socket); 00438 }; 00439 00440 00441 00442 #endif 00443 00444 00445 // End Of File ///////////////////////////////////////////////////////////////