GIOP_Message_State.cpp

Go to the documentation of this file.
00001 // $Id: GIOP_Message_State.cpp 79705 2007-09-24 07:45:37Z johnnyw $
00002 
00003 #include "tao/GIOP_Message_State.h"
00004 #include "tao/debug.h"
00005 #include "tao/GIOP_Message_Base.h"
00006 
00007 #include "ace/Log_Msg.h"
00008 
00009 #if !defined (__ACE_INLINE__)
00010 # include "tao/GIOP_Message_State.inl"
00011 #endif /* __ACE_INLINE__ */
00012 
00013 ACE_RCSID (tao,
00014            GIOP_Message_State,
00015            "$Id: GIOP_Message_State.cpp 79705 2007-09-24 07:45:37Z johnnyw $")
00016 
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 int
00020 TAO_GIOP_Message_State::parse_message_header (ACE_Message_Block &incoming)
00021 {
00022   if (incoming.length () >= TAO_GIOP_MESSAGE_HEADER_LEN)
00023     {
00024       // Parse the GIOP header
00025       return this->parse_message_header_i (incoming);
00026     }
00027 
00028   // One indicates that we didn't have enough data in the message to
00029   // parse the header
00030   return 1;
00031 }
00032 
00033 int
00034 TAO_GIOP_Message_State::parse_message_header_i (ACE_Message_Block &incoming)
00035 {
00036   if (TAO_debug_level > 8)
00037     {
00038       ACE_DEBUG ((LM_DEBUG,
00039                   ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::parse_message_header_i\n")
00040                   ));
00041     }
00042 
00043   char * const buf = incoming.rd_ptr ();
00044 
00045   if (this->parse_magic_bytes (buf) == -1        // Parse magic bytes first
00046       || this->get_version_info (buf) == -1      // Get version information
00047       || this->get_byte_order_info (buf) == -1)  // Get byte order information
00048     return -1;
00049 
00050   // Get the message type
00051   this->message_type_ = this->message_type (buf[TAO_GIOP_MESSAGE_TYPE_OFFSET]);
00052 
00053   // Get the size of the message..
00054   this->get_payload_size (buf);
00055 
00056   if (this->payload_size_ == 0)
00057     {
00058       switch (this->message_type_)
00059         {
00060         case GIOP::MessageError:
00061         case GIOP::CloseConnection:
00062           if (TAO_debug_level > 0)
00063             {
00064               char const * const which =
00065                 (this->message_type_ == GIOP::CloseConnection) ? "CloseConnection" :
00066                 (this->message_type_ == GIOP::MessageError) ? "MessageError" : "unknown";
00067               ACE_DEBUG ((LM_DEBUG,
00068                           ACE_TEXT ("TAO (%P|%t) - GIOP %s received \n"), which));
00069             }
00070           return 0;
00071         default:
00072           if (TAO_debug_level > 0)
00073             ACE_DEBUG ((LM_DEBUG,
00074                         ACE_TEXT ("TAO (%P|%t) - ")
00075                         ACE_TEXT ("TAO_GIOP_Message_State::parse_magic_bytes, ")
00076                         ACE_TEXT ("Message of size zero recd. \n")));
00077           return -1;
00078         }
00079     }
00080 
00081   return 0; // success
00082 }
00083 
00084 int
00085 TAO_GIOP_Message_State::parse_magic_bytes (char *buf)
00086 {
00087   // The values are hard-coded to support non-ASCII platforms.
00088   if (!(buf [0] == 0x47      // 'G'
00089         && buf [1] == 0x49   // 'I'
00090         && buf [2] == 0x4f   // 'O'
00091         && buf [3] == 0x50)) // 'P'
00092     {
00093       if (TAO_debug_level > 0)
00094         ACE_DEBUG ((LM_DEBUG,
00095                     ACE_TEXT ("TAO (%P|%t) - ")
00096                     ACE_TEXT ("TAO_GIOP_Message_State::parse_magic_bytes, ")
00097                     ACE_TEXT ("bad header: ")
00098                     ACE_TEXT ("magic word [%02x,%02x,%02x,%02x]\n"),
00099                     buf[0],
00100                     buf[1],
00101                     buf[2],
00102                     buf[3]));
00103       return -1;
00104     }
00105 
00106  return 0;
00107 }
00108 
00109 int
00110 TAO_GIOP_Message_State::get_version_info (char *buf)
00111 {
00112   if (TAO_debug_level > 8)
00113     {
00114       ACE_DEBUG ((LM_DEBUG,
00115                   "TAO (%P|%t) - GIOP_Message_State::get_version_info\n"));
00116     }
00117 
00118   // We have a GIOP message on hand. Get its revision numbers
00119   CORBA::Octet incoming_major = buf[TAO_GIOP_VERSION_MAJOR_OFFSET];
00120   CORBA::Octet incoming_minor = buf[TAO_GIOP_VERSION_MINOR_OFFSET];
00121 
00122   // Check the revision information
00123   if (TAO_GIOP_Message_Generator_Parser_Impl::check_revision (
00124           incoming_major,
00125           incoming_minor) == 0)
00126     {
00127       if (TAO_debug_level > 0)
00128         {
00129           ACE_DEBUG ((LM_DEBUG,
00130                       ACE_TEXT ("TAO (%P|%t) - bad version <%d.%d>\n"),
00131                       incoming_major, incoming_minor));
00132         }
00133 
00134       return -1;
00135     }
00136 
00137   // Set the version
00138   this->giop_version_.minor = incoming_minor;
00139   this->giop_version_.major = incoming_major;
00140 
00141   return 0;
00142 }
00143 
00144 int
00145 TAO_GIOP_Message_State::get_byte_order_info (char *buf)
00146 {
00147   if (TAO_debug_level > 8)
00148     {
00149       ACE_DEBUG ((LM_DEBUG,
00150                   ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::get_byte_order_info\n") ));
00151     }
00152 
00153     // Let us be specific that this is for 1.0
00154   if (this->giop_version_.minor == 0 &&
00155       this->giop_version_.major == 1)
00156     {
00157       this->byte_order_ =
00158         buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET];
00159 
00160       if (this->byte_order_ != 0 &&
00161           this->byte_order_ != 1)
00162         {
00163           if (TAO_debug_level > 2)
00164             {
00165               ACE_DEBUG ((LM_DEBUG,
00166                           ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::get_byte_order_info, ")
00167                           ACE_TEXT ("invalid byte order <%d> for version <1.0>\n"),
00168                           this->byte_order_));
00169             }
00170           return -1;
00171         }
00172     }
00173   else
00174     {
00175       // Read the byte ORDER
00176       this->byte_order_ =
00177         (CORBA::Octet) (buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET]& 0x01);
00178 
00179       // Read the fragment bit
00180       this->more_fragments_ =
00181         (CORBA::Octet) (buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET]& 0x02);
00182 
00183       if ((buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET] & ~0x3) != 0)
00184         {
00185           if (TAO_debug_level > 2)
00186             {
00187               ACE_DEBUG ((LM_DEBUG,
00188                           ACE_TEXT ("TAO (%P|%t) - invalid flags for <%d> ")
00189                           ACE_TEXT ("for version <%d %d> \n"),
00190                           buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET],
00191                           this->giop_version_.major,
00192                           this->giop_version_.minor));
00193             }
00194           return -1;
00195         }
00196     }
00197 
00198   return 0;
00199 }
00200 
00201 void
00202 TAO_GIOP_Message_State::get_payload_size (char *rd_ptr)
00203 {
00204   // Move the read pointer
00205   rd_ptr += TAO_GIOP_MESSAGE_SIZE_OFFSET;
00206 
00207   this->payload_size_ =  this->read_ulong (rd_ptr);
00208 }
00209 
00210 CORBA::ULong
00211 TAO_GIOP_Message_State::read_ulong (const char *rd_ptr) const
00212 {
00213   CORBA::ULong x = 0;
00214 
00215   // We don't need to do this sort of copy. But some compilers (read it
00216   // as SunCC) have a problem in deferencing from the
00217   // reinterpret_cast pointer of the <rd_ptr>, as the <rd_ptr> can be
00218   // on stack. So let us go ahead with this copying...
00219   char buf[] =
00220     {
00221       *rd_ptr,
00222       *(rd_ptr + 1),
00223       *(rd_ptr + 2),
00224       *(rd_ptr + 3)
00225     };
00226 
00227 #if !defined (ACE_DISABLE_SWAP_ON_READ)
00228   if (!(this->byte_order_ != ACE_CDR_BYTE_ORDER))
00229     {
00230       x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
00231     }
00232   else
00233     {
00234       ACE_CDR::swap_4 (buf, reinterpret_cast<char*> (&x));
00235     }
00236 #else
00237   x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
00238 #endif /* ACE_DISABLE_SWAP_ON_READ */
00239 
00240   return x;
00241 }
00242 
00243 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:37:52 2010 for TAO by  doxygen 1.4.7