3 /** @file Mem_Map_Stream.h
5 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
6 * @author Krishnakumar B <kitty@cs.wustl.edu>
9 #ifndef _ACEXML_MEM_MAP_STREAM_H
10 #define _ACEXML_MEM_MAP_STREAM_H
12 #include /**/ "ace/pre.h"
13 #include "ACEXML/common/ACEXML_Export.h"
15 #if !defined (ACE_LACKS_PRAGMA_ONCE)
17 #endif /* ACE_LACKS_PRAGMA_ONCE */
19 #include "ace/SOCK_Stream.h"
20 #include "ace/Mem_Map.h"
21 #include "ace/SOCK_Connector.h"
22 #include "ace/Connector.h"
23 #include "ace/Svc_Handler.h"
24 #include "ACEXML/common/XML_Types.h"
26 typedef ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
> Svc_Handler
;
27 typedef ACE_Connector
<Svc_Handler
, ACE_SOCK_CONNECTOR
> Connector
;
30 * @class ACEXML_Mem_Map_Stream
32 * @brief Provides a memory-mapped stream abstraction to simplify parsing
35 * This class makes it possible to treat an connection as a stream of
36 * bytes, similar to the C library stdio streams. The contents of the
37 * connection are buffered incrementally in a memory-mapped file. This
38 * class maintains pointers to two positions in the stream:
40 * 1. The <recv> position, which keeps track of the beginning of a
41 * token that is in the stream.
43 * 2. The <get> position, which moves along character-by-character
44 * until the end of the token is reached.
46 * Once a token has been located, it can be extracted from the stream by
47 * calling the <recv>. The length of the token, i.e., the <recv_len>, is
48 * the length in bytes between the <get> position and the <recv> position.
49 * Once the token has been extracted, the <recv> and <get> positions can be
50 * updated by the <seek> method.
52 class ACEXML_Export ACEXML_Mem_Map_Stream
55 /// Default constructor
56 ACEXML_Mem_Map_Stream ();
58 /// Initialize this object.
59 virtual int open (Connector
*connector
,
60 const ACE_INET_Addr
&);
63 virtual ~ACEXML_Mem_Map_Stream ();
65 /// Returns the underlying <ACE_SOCK_Stream>.
66 ACE_SOCK_Stream
&stream ();
69 * Send <size> bytes in <buf> to the connected peer. This is a
70 * completely unbuffered call.
72 virtual ssize_t
send_n (const void *buf
,
74 ACE_Time_Value
*tv
= 0);
77 * Return the next character in the stream and advance the <get>
78 * position. Returns EOF when the <get> position reaches the end of the
81 virtual int get_char ();
84 * Returns a pointer to array of at most <len> characters starting at
85 * the <recv> position. If the <recv> position + <len> extends past the
86 * EOF then <len> is set to the number of characters between the <recv>
87 * position and the EOF and both the <get> and <recv> positions are
88 * advanced by <len>. Returns 0 if the <recv> position is at the EOF.
90 virtual const char *recv (size_t &len
);
93 * Returns a pointer to array of characters starting at the <recv>
96 virtual const char *recv () const;
99 * Returns the length in bytes between the <get> position and the <recv>
102 virtual size_t recv_len () const;
105 * Returns the no. of bytes available in the stream.
107 virtual size_t available () const;
110 * Resets the <get> and <recv> positions to the beginning of the stream.
111 * This works since all the data has been cached in the memory-mapped
114 virtual void rewind ();
117 * Returns the nth character <offset> from the <get> position in the
118 * stream without advancing the <get> position. Automatically extends
119 * the backing store if necessary. Returns EOF if <offset> is past the
122 virtual int peek_char (size_t offset
);
125 * Return a pointer to an array of <size> characters starting at
126 * <offset> characters from the <get> position in the stream without
127 * advancing the <get> position. Automatically extends the backing store
128 * if necessary. Returns 0 if <offset> or <offset + size> is past the
131 virtual const char *peek_str (size_t offset
, size_t size
);
134 * Sets the <get> and <recv> positions as follows:
135 * o If <whence> is <SEEK_SET>, the positions are set to <offset>
136 * bytes from the start of the stream.
137 * o If <whence> is <SEEK_CUR>, the positions are set to the
138 * current <get> position plus <offset>.
139 * o If <whence> is <SEEK_END>, the positions are set to the size
140 * of the stream plus <offset>.
142 * If offset is greater than EOF, both <get> and <recv> are set to EOF.
143 * Note special return value is returned to indicate this condition.
145 virtual ACE_OFF_T
seek (ACE_OFF_T offset
, int whence
= SEEK_CUR
);
147 /// Returns 1 if we're at the end of the HTTP stream, else 0.
148 virtual int eof () const;
150 /// Returns the underlying service handler.
151 Svc_Handler
*svc_handler ();
155 * Grow the file by reading another chunk from the HTTP socket and
156 * extend the mapping to cover this chunk. Returns -1 on failure or
159 int grow_file_and_remap ();
162 * Connection to peer. The granularity is at the Svc_Handler level.
163 * The Svc_Handler has an SOCK_Stream.
165 Svc_Handler
*svc_handler_
;
167 /// Memory-mapped file that we're iterating over.
168 ACE_Mem_Map mem_map_
;
170 /// Pointer to the address where the next <recv> method will start.
174 * Pointer to the address where the next <get_char> method will
179 /// Address at the end of the file mapping.
180 char *end_of_mapping_plus1_
;
184 #include /**/ "ace/post.h"
187 #endif /* _ACEXML_MEM_MAP_STREAM_H */