Provides a memory-mapped stream abstraction to simplify parsing of tokens. More...
#include <ACEXML/common/Mem_Map_Stream.h>
Public Member Functions | |
ACEXML_Mem_Map_Stream (void) | |
Default constructor. | |
virtual int | open (Connector *connector, const ACE_INET_Addr &) |
Initialize this object. | |
virtual | ~ACEXML_Mem_Map_Stream (void) |
Destructor. | |
ACE_SOCK_Stream & | stream (void) |
Returns the underlying <ACE_SOCK_Stream>. | |
virtual ssize_t | send_n (const void *buf, size_t size, ACE_Time_Value *tv=0) |
virtual int | get_char (void) |
virtual const char * | recv (size_t &len) |
virtual const char * | recv (void) const |
virtual size_t | recv_len (void) const |
virtual size_t | available (void) const |
virtual void | rewind (void) |
virtual int | peek_char (size_t offset) |
virtual const char * | peek_str (size_t offset, size_t size) |
virtual ACE_OFF_T | seek (ACE_OFF_T offset, int whence=SEEK_CUR) |
virtual int | eof (void) const |
Returns 1 if we're at the end of the HTTP stream, else 0. | |
Svc_Handler * | svc_handler (void) |
Returns the underlying service handler. | |
Private Member Functions | |
int | grow_file_and_remap (void) |
Private Attributes | |
Svc_Handler * | svc_handler_ |
ACE_Mem_Map | mem_map_ |
Memory-mapped file that we're iterating over. | |
char * | recv_pos_ |
Pointer to the address where the next <recv> method will start. | |
char * | get_pos_ |
char * | end_of_mapping_plus1_ |
Address at the end of the file mapping. |
Provides a memory-mapped stream abstraction to simplify parsing of tokens.
This class makes it possible to treat an connection as a stream of bytes, similar to the C library stdio streams. The contents of the connection are buffered incrementally in a memory-mapped file. This class maintains pointers to two positions in the stream:
1. The <recv> position, which keeps track of the beginning of a token that is in the stream.
2. The <get> position, which moves along character-by-character until the end of the token is reached.
Once a token has been located, it can be extracted from the stream by calling the <recv>. The length of the token, i.e., the <recv_len>, is the length in bytes between the <get> position and the <recv> position. Once the token has been extracted, the <recv> and <get> positions can be updated by the <seek> method.
Definition at line 55 of file Mem_Map_Stream.h.
ACEXML_Mem_Map_Stream::ACEXML_Mem_Map_Stream | ( | void | ) |
ACEXML_Mem_Map_Stream::~ACEXML_Mem_Map_Stream | ( | void | ) | [virtual] |
Destructor.
Definition at line 261 of file Mem_Map_Stream.cpp.
{ // Remove the mapping and the file. this->mem_map_.remove (); delete this->svc_handler_; }
size_t ACEXML_Mem_Map_Stream::available | ( | void | ) | const [virtual] |
Returns the no. of bytes available in the stream.
Definition at line 140 of file Mem_Map_Stream.cpp.
{ return this->end_of_mapping_plus1_ - this->get_pos_; }
int ACEXML_Mem_Map_Stream::eof | ( | void | ) | const [virtual] |
Returns 1 if we're at the end of the HTTP stream, else 0.
Definition at line 29 of file Mem_Map_Stream.cpp.
{ return this->get_pos_ >= this->end_of_mapping_plus1_; }
int ACEXML_Mem_Map_Stream::get_char | ( | void | ) | [virtual] |
Return the next character in the stream and advance the <get> position. Returns EOF when the <get> position reaches the end of the HTTP stream.
Definition at line 35 of file Mem_Map_Stream.cpp.
{ if (this->eof () && this->grow_file_and_remap () == -1) return EOF; return *this->get_pos_++; }
int ACEXML_Mem_Map_Stream::grow_file_and_remap | ( | void | ) | [private] |
Grow the file by reading another chunk from the HTTP socket and extend the mapping to cover this chunk. Returns -1 on failure or EOF, else 0.
Definition at line 191 of file Mem_Map_Stream.cpp.
{ char buf[8192]; // Copy the next chunk of bytes from the socket into the temporary // file. ACE_Time_Value tv (ACE_DEFAULT_TIMEOUT); ssize_t bytes = 0; ssize_t n = 0; while (1) { n = this->svc_handler_->peer ().recv (buf, sizeof buf, 0, &tv); if (n < 0) { if (errno != EWOULDBLOCK) { ACE_ERROR ((LM_ERROR, "%p\n", "recv")); } return -1; } bytes += n; if (n == 0 && !bytes) return -1; else if (n == 0) break; else if (ACE::write_n (this->mem_map_.handle (), buf, n) != n) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "write_n"), -1); } // ssize_t n = this->svc_handler_->peer ().recv (buf, sizeof buf, 0, &tv); // if (n == -1) // { // ACE_ERROR ((LM_ERROR, "%p\n", "recv")); // return -1; // } // else if (n == 0) // return -1; // else if (ACE::write_n (this->mem_map_.handle (), buf, n) != n) // ACE_ERROR_RETURN ((LM_ERROR, // "%p\n", // "write_n"), // -1); // Grow the memory-mapping to encompass the entire temporary file. if (this->mem_map_.map (static_cast<size_t> (-1), PROT_RDWR, ACE_MAP_PRIVATE, (void*)0) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "map"), -1); // MAP_FAILED is used as a "first time in" flag. if (this->recv_pos_ == MAP_FAILED) { this->recv_pos_ = reinterpret_cast<char *> (this->mem_map_.addr ()); this->get_pos_ = this->recv_pos_; } this->end_of_mapping_plus1_ = reinterpret_cast<char *> (this->mem_map_.addr ()) + this->mem_map_.size (); return 0; }
int ACEXML_Mem_Map_Stream::open | ( | Connector * | connector, | |
const ACE_INET_Addr & | addr | |||
) | [virtual] |
Initialize this object.
Definition at line 146 of file Mem_Map_Stream.cpp.
{ svc_handler_ = 0; // Connect to the server at <addr>. If the handler has to be // connected to the server again, the Caching strategy takes care // and uses the same connection. if (connector->connect (svc_handler_, addr) == -1) { ACE_ERROR_RETURN ((LM_ERROR, "%p %s %d\n", "Connect failed", addr.get_host_name (), addr.get_port_number ()), -1); } // Create a temporary filename. ACE_FILE_Addr file (ACE_sap_any_cast (ACE_FILE_Addr &)); // Create the temporary file via the <ACE_Mem_Map> class API. if (this->mem_map_.open (file.get_path_name (), O_RDWR | O_CREAT | O_APPEND, ACE_DEFAULT_FILE_PERMS) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1); // Make sure to unlink this right away so that if this process // crashes these files will be removed automatically. else if (ACE_OS::unlink (file.get_path_name ()) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "unlink"), -1); else // Initialize all the position pointers to 0. this->rewind (); return 0; }
int ACEXML_Mem_Map_Stream::peek_char | ( | size_t | offset | ) | [virtual] |
Returns the nth character <offset> from the <get> position in the stream without advancing the <get> position. Automatically extends the backing store if necessary. Returns EOF if <offset> is past the end of the stream.
Definition at line 51 of file Mem_Map_Stream.cpp.
{ // We may need to iterate if the size of <n> is large. while (this->get_pos_ + offset >= this->end_of_mapping_plus1_) if (this->grow_file_and_remap () == -1) return EOF; return this->get_pos_[offset]; }
const char * ACEXML_Mem_Map_Stream::peek_str | ( | size_t | offset, | |
size_t | size | |||
) | [virtual] |
Return a pointer to an array of <size> characters starting at <offset> characters from the <get> position in the stream without advancing the <get> position. Automatically extends the backing store if necessary. Returns 0 if <offset> or <offset + size> is past the end of the stream.
Definition at line 88 of file Mem_Map_Stream.cpp.
{ // We will iterate if the size of <offset> is large. while (this->get_pos_ + (offset + size) > this->end_of_mapping_plus1_) if (this->grow_file_and_remap () == -1) return 0; return &this->get_pos_[offset]; }
const char * ACEXML_Mem_Map_Stream::recv | ( | size_t & | len | ) | [virtual] |
Returns a pointer to array of at most <len> characters starting at the <recv> position. If the <recv> position + <len> extends past the EOF then <len> is set to the number of characters between the <recv> position and the EOF and both the <get> and <recv> positions are advanced by <len>. Returns 0 if the <recv> position is at the EOF.
Definition at line 68 of file Mem_Map_Stream.cpp.
const char * ACEXML_Mem_Map_Stream::recv | ( | void | ) | const [virtual] |
Returns a pointer to array of characters starting at the <recv> position.
Definition at line 62 of file Mem_Map_Stream.cpp.
{ return this->recv_pos_; }
size_t ACEXML_Mem_Map_Stream::recv_len | ( | void | ) | const [virtual] |
Returns the length in bytes between the <get> position and the <recv> position.
Definition at line 82 of file Mem_Map_Stream.cpp.
void ACEXML_Mem_Map_Stream::rewind | ( | void | ) | [virtual] |
Resets the <get> and <recv> positions to the beginning of the stream. This works since all the data has been cached in the memory-mapped backing store.
Definition at line 43 of file Mem_Map_Stream.cpp.
Sets the <get> and <recv> positions as follows: o If <whence> is <SEEK_SET>, the positions are set to <offset> bytes from the start of the stream. o If <whence> is <SEEK_CUR>, the positions are set to the current <get> position plus <offset>. o If <whence> is <SEEK_END>, the positions are set to the size of the stream plus <offset>.
If offset is greater than EOF, both <get> and <recv> are set to EOF. Note special return value is returned to indicate this condition.
Definition at line 100 of file Mem_Map_Stream.cpp.
{ switch (whence) { case SEEK_SET: this->get_pos_ = reinterpret_cast<char *> (this->mem_map_.addr ()) + offset; break; case SEEK_CUR: this->get_pos_ += offset; break; case SEEK_END: this->get_pos_ = this->end_of_mapping_plus1_ + offset; // @@ Not sure how to implement this (yet). ACE_NOTSUP_RETURN (-1); } // Make sure that the backing store will cover this. while (this->get_pos_ > this->end_of_mapping_plus1_) if (this->grow_file_and_remap () == -1) this->get_pos_ = this->end_of_mapping_plus1_; this->recv_pos_ = this->get_pos_; return ACE_Utils::truncate_cast<ACE_OFF_T> ( this->recv_pos_ - reinterpret_cast<char *> (this->mem_map_.addr ())); }
ssize_t ACEXML_Mem_Map_Stream::send_n | ( | const void * | buf, | |
size_t | size, | |||
ACE_Time_Value * | tv = 0 | |||
) | [virtual] |
Send <size> bytes in <buf> to the connected peer. This is a completely unbuffered call.
Definition at line 22 of file Mem_Map_Stream.cpp.
{ return svc_handler_->peer ().send_n (buf, size, 0, tv); }
ACE_SOCK_Stream & ACEXML_Mem_Map_Stream::stream | ( | void | ) |
Returns the underlying <ACE_SOCK_Stream>.
Definition at line 16 of file Mem_Map_Stream.cpp.
{ return svc_handler_->peer (); }
Svc_Handler * ACEXML_Mem_Map_Stream::svc_handler | ( | void | ) |
Returns the underlying service handler.
Definition at line 134 of file Mem_Map_Stream.cpp.
{ return this->svc_handler_; }
char* ACEXML_Mem_Map_Stream::end_of_mapping_plus1_ [private] |
Address at the end of the file mapping.
Definition at line 183 of file Mem_Map_Stream.h.
char* ACEXML_Mem_Map_Stream::get_pos_ [private] |
Pointer to the address where the next <get_char> method will start.
Definition at line 180 of file Mem_Map_Stream.h.
ACE_Mem_Map ACEXML_Mem_Map_Stream::mem_map_ [private] |
Memory-mapped file that we're iterating over.
Definition at line 171 of file Mem_Map_Stream.h.
char* ACEXML_Mem_Map_Stream::recv_pos_ [private] |
Pointer to the address where the next <recv> method will start.
Definition at line 174 of file Mem_Map_Stream.h.
Svc_Handler* ACEXML_Mem_Map_Stream::svc_handler_ [private] |
Connection to peer. The granularity is at the Svc_Handler level. The Svc_Handler has an SOCK_Stream.
Definition at line 168 of file Mem_Map_Stream.h.