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.18 2004/11/11 07:34:50 csoutheren 00028 * Added #include <ptlib.h> 00029 * 00030 * Revision 1.17 2002/11/06 22:47:24 robertj 00031 * Fixed header comment (copyright etc) 00032 * 00033 * Revision 1.16 2002/09/16 01:08:59 robertj 00034 * Added #define so can select if #pragma interface/implementation is used on 00035 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00036 * 00037 * Revision 1.15 2001/09/26 09:37:04 robertj 00038 * Added virtual keyword to a lot of functions so can be overridden. 00039 * 00040 * Revision 1.14 1999/03/09 08:01:46 robertj 00041 * Changed comments for doc++ support (more to come). 00042 * 00043 * Revision 1.13 1999/02/16 08:07:10 robertj 00044 * MSVC 6.0 compatibility changes. 00045 * 00046 * Revision 1.12 1998/09/23 06:19:40 robertj 00047 * Added open source copyright license. 00048 * 00049 * Revision 1.11 1996/09/14 13:09:13 robertj 00050 * Major upgrade: 00051 * rearranged sockets to help support IPX. 00052 * added indirect channel class and moved all protocols to descend from it, 00053 * separating the protocol from the low level byte transport. 00054 * 00055 * Revision 1.10 1996/05/15 10:07:00 robertj 00056 * Added access function to set intercharacter line read timeout. 00057 * 00058 * Revision 1.9 1996/05/09 12:14:02 robertj 00059 * Rewrote the "unread" buffer usage and then used it to improve ReadLine() performance. 00060 * 00061 * Revision 1.8 1996/03/31 08:43:38 robertj 00062 * Added version of WriteCommand() and ExecteCommand() without argument string. 00063 * 00064 * Revision 1.7 1996/03/16 04:35:32 robertj 00065 * Added PString parameter version of UnRead(). 00066 * Changed lastResponseCode to an integer. 00067 * Added ParseReponse() for splitting reponse line into code and info. 00068 * 00069 * Revision 1.6 1996/02/13 12:57:05 robertj 00070 * Added access to the last response in an application socket. 00071 * 00072 * Revision 1.5 1996/02/03 11:33:16 robertj 00073 * Changed RadCmd() so can distinguish between I/O error and unknown command. 00074 * 00075 * Revision 1.4 1996/01/23 13:08:43 robertj 00076 * Major rewrite for HTTP support. 00077 * 00078 * Revision 1.3 1995/06/17 11:12:15 robertj 00079 * Documentation update. 00080 * 00081 * Revision 1.2 1995/06/17 00:39:53 robertj 00082 * More implementation. 00083 * 00084 * Revision 1.1 1995/06/04 13:17:16 robertj 00085 * Initial revision 00086 * 00087 */ 00088 00089 #ifndef _PINTERNETPROTOCOL 00090 #define _PINTERNETPROTOCOL 00091 00092 #ifdef P_USE_PRAGMA 00093 #pragma interface 00094 #endif 00095 00096 #include <ptlib.h> 00097 00098 class PSocket; 00099 class PIPSocket; 00100 00101 00121 class PInternetProtocol : public PIndirectChannel 00122 { 00123 PCLASSINFO(PInternetProtocol, PIndirectChannel) 00124 00125 protected: 00126 PInternetProtocol( 00127 const char * defaultServiceName, // Service name for the protocol. 00128 PINDEX cmdCount, // Number of command strings. 00129 char const * const * cmdNames // Strings for each command. 00130 ); 00131 // Create an unopened TCP/IP protocol socket channel. 00132 00133 00134 public: 00135 // Overrides from class PChannel. 00147 virtual BOOL Read( 00148 void * buf, // Pointer to a block of memory to receive the read bytes. 00149 PINDEX len // Maximum number of bytes to read into the buffer. 00150 ); 00151 00167 virtual BOOL Write( 00168 const void * buf, // Pointer to a block of memory to write. 00169 PINDEX len // Number of bytes to write. 00170 ); 00171 00175 void SetReadLineTimeout( 00176 const PTimeInterval & t 00177 ); 00178 00179 // New functions for class. 00185 virtual BOOL Connect( 00186 const PString & address, // Address of remote machine to connect to. 00187 WORD port = 0 // Port number to use for the connection. 00188 ); 00189 virtual BOOL Connect( 00190 const PString & address, // Address of remote machine to connect to. 00191 const PString & service // Service name to use for the connection. 00192 ); 00193 00199 virtual BOOL Accept( 00200 PSocket & listener // Address of remote machine to connect to. 00201 ); 00202 00209 const PString & GetDefaultService() const; 00210 00219 PIPSocket * GetSocket() const; 00220 00228 virtual BOOL WriteLine( 00229 const PString & line // String to write as a command line. 00230 ); 00231 00248 virtual BOOL ReadLine( 00249 PString & line, // String to receive a CR/LF terminated line. 00250 BOOL allowContinuation = FALSE // Flag to handle continued lines. 00251 ); 00252 00256 virtual void UnRead( 00257 int ch // Individual character to be returned. 00258 ); 00259 virtual void UnRead( 00260 const PString & str // String to be put back into data stream. 00261 ); 00262 virtual void UnRead( 00263 const void * buffer, // Characters to be put back into data stream. 00264 PINDEX len // Number of characters to be returned. 00265 ); 00266 00280 virtual BOOL WriteCommand( 00281 PINDEX cmdNumber // Number of command to write. 00282 ); 00283 virtual BOOL WriteCommand( 00284 PINDEX cmdNumber, // Number of command to write. 00285 const PString & param // Extra parameters required by the command. 00286 ); 00287 00305 virtual BOOL ReadCommand( 00306 PINDEX & num, 00307 // Number of the command parsed from the command line, or P_MAX_INDEX 00308 // if no match. 00309 PString & args // String to receive the arguments to the command. 00310 ); 00311 00328 virtual BOOL WriteResponse( 00329 unsigned numericCode, // Response code for command response. 00330 const PString & info // Extra information available after response code. 00331 ); 00332 virtual BOOL WriteResponse( 00333 const PString & code, // Response code for command response. 00334 const PString & info // Extra information available after response code. 00335 ); 00336 00355 virtual BOOL ReadResponse(); 00356 virtual BOOL ReadResponse( 00357 int & code, // Response code for command response. 00358 PString & info // Extra information available after response code. 00359 ); 00360 00372 virtual int ExecuteCommand( 00373 PINDEX cmdNumber // Number of command to write. 00374 ); 00375 virtual int ExecuteCommand( 00376 PINDEX cmdNumber, // Number of command to write. 00377 const PString & param // Extra parameters required by the command. 00378 ); 00379 00386 int GetLastResponseCode() const; 00387 00393 PString GetLastResponseInfo() const; 00394 00395 00396 protected: 00408 virtual PINDEX ParseResponse( 00409 const PString & line // Input response line to be parsed 00410 ); 00411 00412 00413 PString defaultServiceName; 00414 // Default Service name to use for the internet protocol socket. 00415 00416 PStringArray commandNames; 00417 // Names of each of the command codes. 00418 00419 PCharArray unReadBuffer; 00420 // Buffer for characters put back into the data stream. 00421 00422 PINDEX unReadCount; 00423 // Buffer count for characters put back into the data stream. 00424 00425 PTimeInterval readLineTimeout; 00426 // Time for characters in a line to be received. 00427 00428 enum StuffState { 00429 DontStuff, StuffIdle, StuffCR, StuffCRLF, StuffCRLFdot, StuffCRLFdotCR 00430 } stuffingState; 00431 // Do byte stuffing of '.' characters in output to the socket channel. 00432 00433 BOOL newLineToCRLF; 00434 // Translate \n characters to CR/LF pairs. 00435 00436 int lastResponseCode; 00437 PString lastResponseInfo; 00438 // Responses 00439 00440 private: 00441 BOOL AttachSocket(PIPSocket * socket); 00442 }; 00443 00444 00445 00446 #endif 00447 00448 00449 // End Of File ///////////////////////////////////////////////////////////////