00001 /* -*- C++ -*- */ 00002 00003 /** @file Mem_Map_Stream.h 00004 * 00005 * Mem_Map_Stream.h,v 1.8 2003/11/09 04:12:06 dhinton Exp 00006 * 00007 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00008 * @author Krishnakumar B <kitty@cs.wustl.edu> 00009 * 00010 */ 00011 00012 #ifndef _ACEXML_MEM_MAP_STREAM_H 00013 #define _ACEXML_MEM_MAP_STREAM_H 00014 00015 #include /**/ "ace/pre.h" 00016 #include "ACEXML/common/ACEXML_Export.h" 00017 00018 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00019 #pragma once 00020 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00021 00022 #include "ace/SOCK_Stream.h" 00023 #include "ace/Mem_Map.h" 00024 #include "ace/SOCK_Connector.h" 00025 #include "ace/Connector.h" 00026 #include "ace/Svc_Handler.h" 00027 #include "ACEXML/common/XML_Types.h" 00028 00029 typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> Svc_Handler; 00030 typedef ACE_Connector<Svc_Handler, ACE_SOCK_CONNECTOR> Connector; 00031 00032 /** 00033 * @class ACEXML_Mem_Map_Stream Mem_Map_Stream.h "ACEXML/common/Mem_Map_Stream.h" 00034 * 00035 * @brief Provides a memory-mapped stream abstraction to simplify parsing 00036 * of tokens. 00037 * 00038 * This class makes it possible to treat an connection as a stream of 00039 * bytes, similar to the C library stdio streams. The contents of the 00040 * connection are buffered incrementally in a memory-mapped file. This 00041 * class maintains pointers to two positions in the stream: 00042 * 00043 * 1. The <recv> position, which keeps track of the beginning of a 00044 * token that is in the stream. 00045 * 00046 * 2. The <get> position, which moves along character-by-character 00047 * until the end of the token is reached. 00048 * 00049 * Once a token has been located, it can be extracted from the stream by 00050 * calling the <recv>. The length of the token, i.e., the <recv_len>, is 00051 * the length in bytes between the <get> position and the <recv> position. 00052 * Once the token has been extracted, the <recv> and <get> positions can be 00053 * updated by the <seek> method. 00054 */ 00055 class ACEXML_Export ACEXML_Mem_Map_Stream 00056 { 00057 public: 00058 /// Default constructor 00059 ACEXML_Mem_Map_Stream (void); 00060 00061 /// Initialize this object. 00062 virtual int open (Connector *connector, 00063 const ACE_INET_Addr &); 00064 00065 /// Destructor. 00066 virtual ~ACEXML_Mem_Map_Stream (void); 00067 00068 /// Returns the underlying <ACE_SOCK_Stream>. 00069 ACE_SOCK_Stream &stream (void); 00070 00071 /** 00072 * Send <size> bytes in <buf> to the connected peer. This is a 00073 * completely unbuffered call. 00074 */ 00075 virtual ssize_t send_n (const void *buf, 00076 size_t size, 00077 ACE_Time_Value *tv = 0); 00078 00079 /** 00080 * Return the next character in the stream and advance the <get> 00081 * position. Returns EOF when the <get> position reaches the end of the 00082 * HTTP stream. 00083 */ 00084 virtual int get_char (void); 00085 00086 /** 00087 * Returns a pointer to array of at most <len> characters starting at 00088 * the <recv> position. If the <recv> position + <len> extends past the 00089 * EOF then <len> is set to the number of characters between the <recv> 00090 * position and the EOF and both the <get> and <recv> positions are 00091 * advanced by <len>. Returns 0 if the <recv> position is at the EOF. 00092 */ 00093 virtual const char *recv (size_t &len); 00094 00095 /** 00096 * Returns a pointer to array of characters starting at the <recv> 00097 * position. 00098 */ 00099 virtual const char *recv (void) const; 00100 00101 /** 00102 * Returns the length in bytes between the <get> position and the <recv> 00103 * position. 00104 */ 00105 virtual size_t recv_len (void) const; 00106 00107 /** 00108 * Returns the no. of bytes available in the stream. 00109 */ 00110 virtual size_t available (void) const; 00111 00112 /** 00113 * Resets the <get> and <recv> positions to the beginning of the stream. 00114 * This works since all the data has been cached in the memory-mapped 00115 * backing store. 00116 */ 00117 virtual void rewind (void); 00118 00119 /** 00120 * Returns the nth character <offset> from the <get> position in the 00121 * stream without advancing the <get> position. Automatically extends 00122 * the backing store if necessary. Returns EOF if <offset> is past the 00123 * end of the stream. 00124 */ 00125 virtual int peek_char (size_t offset); 00126 00127 /** 00128 * Return a pointer to an array of <size> characters starting at 00129 * <offset> characters from the <get> position in the stream without 00130 * advancing the <get> position. Automatically extends the backing store 00131 * if necessary. Returns 0 if <offset> or <offset + size> is past the 00132 * end of the stream. 00133 */ 00134 virtual const char *peek_str (size_t offset, size_t size); 00135 00136 /** 00137 * Sets the <get> and <recv> positions as follows: 00138 * o If <whence> is <SEEK_SET>, the positions are set to <offset> 00139 * bytes from the start of the stream. 00140 * o If <whence> is <SEEK_CUR>, the positions are set to the 00141 * current <get> position plus <offset>. 00142 * o If <whence> is <SEEK_END>, the positions are set to the size 00143 * of the stream plus <offset>. 00144 * 00145 * If offset is greater than EOF, both <get> and <recv> are set to EOF. 00146 * Note special return value is returned to indicate this condition. 00147 */ 00148 virtual off_t seek (off_t offset, int whence = SEEK_CUR); 00149 00150 /// Returns 1 if we're at the end of the HTTP stream, else 0. 00151 virtual int eof (void) const; 00152 00153 /// Returns the underlying service handler. 00154 Svc_Handler *svc_handler (void); 00155 00156 private: 00157 /** 00158 * Grow the file by reading another chunk from the HTTP socket and 00159 * extend the mapping to cover this chunk. Returns -1 on failure or 00160 * EOF, else 0. 00161 */ 00162 int grow_file_and_remap (void); 00163 00164 /** 00165 * Connection to peer. The granularity is at the Svc_Handler level. 00166 * The Svc_Handler has an SOCK_Stream. 00167 */ 00168 Svc_Handler *svc_handler_; 00169 00170 /// Memory-mapped file that we're iterating over. 00171 ACE_Mem_Map mem_map_; 00172 00173 /// Pointer to the address where the next <recv> method will start. 00174 char *recv_pos_; 00175 00176 /** 00177 * Pointer to the address where the next <get_char> method will 00178 * start. 00179 */ 00180 char *get_pos_; 00181 00182 /// Address at the end of the file mapping. 00183 char *end_of_mapping_plus1_; 00184 00185 }; 00186 00187 00188 #include /**/ "ace/post.h" 00189 00190 00191 #endif /* _ACEXML_MEM_MAP_STREAM_H */