Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Asynch_Queued_Message.cpp

Go to the documentation of this file.
00001 #include "tao_pch.h"
00002 #include "Asynch_Queued_Message.h"
00003 #include "debug.h"
00004 #include "ace/Malloc_T.h"
00005 #include "ace/Log_Msg.h"
00006 
00007 
00008 ACE_RCSID (tao,
00009            Asynch_Queued_Message,
00010            "$Id: Asynch_Queued_Message.cpp,v 1.1.1.2.2.2 2003/04/21 19:10:23 chad Exp $")
00011 
00012 
00013 
00014 TAO_Asynch_Queued_Message::    TAO_Asynch_Queued_Message (const ACE_Message_Block *contents,
00015                                ACE_Allocator *alloc,
00016                                int is_heap_allocated)
00017   : TAO_Queued_Message (alloc, is_heap_allocated)
00018   , offset_ (0)
00019 {
00020   this->size_ = contents->total_length ();
00021   // @@ Use a pool for these guys!!
00022   ACE_NEW (this->buffer_, char[this->size_]);
00023 
00024   size_t copy_offset = 0;
00025   for (const ACE_Message_Block *i = contents;
00026        i != 0;
00027        i = i->cont ())
00028     {
00029       ACE_OS::memcpy (this->buffer_ + copy_offset,
00030                       i->rd_ptr (),
00031                       i->length ());
00032       copy_offset += i->length ();
00033     }
00034 }
00035 
00036 TAO_Asynch_Queued_Message::TAO_Asynch_Queued_Message (char *buf,
00037                                                       size_t size,
00038                                                       ACE_Allocator *alloc)
00039   : TAO_Queued_Message (alloc)
00040   , size_ (size)
00041   , offset_ (0)
00042   , buffer_ (buf)
00043 {
00044 }
00045 
00046 TAO_Asynch_Queued_Message::~TAO_Asynch_Queued_Message (void)
00047 {
00048   // @@ Use a pool for these guys!
00049   delete[] this->buffer_;
00050 }
00051 
00052 size_t
00053 TAO_Asynch_Queued_Message::message_length (void) const
00054 {
00055   return this->size_ - this->offset_;
00056 }
00057 
00058 int
00059 TAO_Asynch_Queued_Message::all_data_sent (void) const
00060 {
00061   return this->size_ == this->offset_;
00062 }
00063 
00064 void
00065 TAO_Asynch_Queued_Message::fill_iov (int iovcnt_max,
00066                                      int &iovcnt,
00067                                      iovec iov[]) const
00068 {
00069   ACE_ASSERT (iovcnt_max > iovcnt);
00070   ACE_UNUSED_ARG (iovcnt_max); // not used if ACE_ASSERT() is empty
00071 
00072   iov[iovcnt].iov_base = this->buffer_ + this->offset_;
00073   iov[iovcnt].iov_len  = ACE_static_cast(u_long,
00074                                          this->size_ - this->offset_);
00075   iovcnt++;
00076 }
00077 
00078 void
00079 TAO_Asynch_Queued_Message::bytes_transferred (size_t &byte_count)
00080 {
00081   this->state_changed_i (TAO_LF_Event::LFS_ACTIVE);
00082 
00083   size_t remaining_bytes = this->size_ - this->offset_;
00084   if (byte_count > remaining_bytes)
00085     {
00086       this->offset_ = this->size_;
00087       byte_count -= remaining_bytes;
00088       return;
00089     }
00090   this->offset_ += byte_count;
00091   byte_count = 0;
00092 
00093   if (this->all_data_sent ())
00094     this->state_changed (TAO_LF_Event::LFS_SUCCESS);
00095 }
00096 
00097 
00098 TAO_Queued_Message *
00099 TAO_Asynch_Queued_Message::clone (ACE_Allocator *alloc)
00100 {
00101   char *buf = 0;
00102 
00103   // @@todo: Need to use a memory pool. But certain things need to
00104   // change a bit in this class for that. Till then.
00105 
00106   // Just allocate and copy data that needs to be sent, no point
00107   // copying the whole buffer.
00108   size_t sz = this->size_ - this->offset_;
00109 
00110   ACE_NEW_RETURN (buf,
00111                   char[sz],
00112                   0);
00113 
00114   ACE_OS::memcpy (buf,
00115                   this->buffer_ + this->offset_,
00116                   sz);
00117 
00118   TAO_Asynch_Queued_Message *qm = 0;
00119 
00120   if (alloc)
00121     {
00122       ACE_NEW_MALLOC_RETURN (qm,
00123                              ACE_static_cast (TAO_Asynch_Queued_Message *,
00124                                  alloc->malloc (sizeof (TAO_Asynch_Queued_Message))),
00125                              TAO_Asynch_Queued_Message (buf,
00126                                                         sz,
00127                                                         alloc),
00128                              0);
00129     }
00130   else
00131     {
00132       // No allocator, so use the common heap!
00133       if (TAO_debug_level == 4)
00134         {
00135           // This debug is for testing purposes!
00136           ACE_DEBUG ((LM_DEBUG,
00137                       "TAO (%P|%t) - Asynch_Queued_Message::clone\n",
00138                       "Using global pool for allocation \n"));
00139         }
00140 
00141       ACE_NEW_RETURN (qm,
00142                       TAO_Asynch_Queued_Message (buf,
00143                                                  sz),
00144                       0);
00145     }
00146 
00147   // Set the flag to indicate that <qm> is created on the heap.
00148   if (qm)
00149     qm->is_heap_created_ = 1;
00150 
00151   return qm;
00152 }
00153 
00154 void
00155 TAO_Asynch_Queued_Message::destroy (void)
00156 {
00157   if (this->is_heap_created_)
00158     {
00159       // If we have an allocator release the memory to the allocator
00160       // pool.
00161       if (this->allocator_)
00162         {
00163           ACE_DES_FREE (this,
00164                         this->allocator_->free,
00165                         TAO_Asynch_Queued_Message);
00166 
00167         }
00168       else // global release..
00169         {
00170           delete this;
00171         }
00172     }
00173 
00174 }

Generated on Mon Jun 16 13:48:01 2003 for TAO by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002