Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / GIOP_Message_State.cpp
blob314932c5be60e1b1aa0645018198717278ce4ea9
1 #include "tao/GIOP_Message_State.h"
2 #include "tao/debug.h"
3 #include "tao/GIOP_Message_Base.h"
5 #include "ace/Log_Msg.h"
7 #if !defined (__ACE_INLINE__)
8 # include "tao/GIOP_Message_State.inl"
9 #endif /* __ACE_INLINE__ */
11 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
13 int
14 TAO_GIOP_Message_State::parse_message_header (ACE_Message_Block &incoming)
16 if (incoming.length () >= TAO_GIOP_MESSAGE_HEADER_LEN)
18 // Parse the GIOP header
19 return this->parse_message_header_i (incoming);
22 // One indicates that we didn't have enough data in the message to
23 // parse the header
24 return 1;
27 int
28 TAO_GIOP_Message_State::parse_message_header_i (ACE_Message_Block &incoming)
30 if (TAO_debug_level > 8)
32 TAOLIB_DEBUG ((LM_DEBUG,
33 ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::parse_message_header_i\n")
34 ));
37 char * const buf = incoming.rd_ptr ();
39 if (this->parse_magic_bytes (buf) == -1 // Parse magic bytes first
40 || this->get_version_info (buf) == -1 // Get version information
41 || this->get_byte_order_info (buf) == -1) // Get byte order information
43 return -1;
46 // Get the message type
47 this->message_type_ =
48 static_cast <GIOP::MsgType> (buf[TAO_GIOP_MESSAGE_TYPE_OFFSET]);
50 // Get the size of the message..
51 this->get_payload_size (buf);
53 if (this->payload_size_ == 0)
55 switch (this->message_type_)
57 case GIOP::MessageError:
58 case GIOP::CloseConnection:
59 if (TAO_debug_level > 0)
61 char const * const which =
62 (this->message_type_ == GIOP::CloseConnection) ? "CloseConnection" :
63 (this->message_type_ == GIOP::MessageError) ? "MessageError" : "unknown";
64 TAOLIB_DEBUG ((LM_DEBUG,
65 ACE_TEXT ("TAO (%P|%t) - GIOP %C received\n"), which));
67 return 0;
68 default:
69 if (TAO_debug_level > 0)
70 TAOLIB_DEBUG ((LM_DEBUG,
71 ACE_TEXT ("TAO (%P|%t) - ")
72 ACE_TEXT ("TAO_GIOP_Message_State::parse_magic_bytes, ")
73 ACE_TEXT ("Message of size zero recd.\n")));
74 return -1;
78 return 0; // success
81 int
82 TAO_GIOP_Message_State::parse_magic_bytes (char *buf)
84 if (!((buf [0] == 0x5A || buf [0] == 0x47) // 'Z' or 'G' (depending on compression)
85 && buf [1] == 0x49 // 'I'
86 && buf [2] == 0x4f // 'O'
87 && buf [3] == 0x50)) // 'P'
89 if (TAO_debug_level > 0)
90 TAOLIB_ERROR ((LM_ERROR,
91 ACE_TEXT ("TAO (%P|%t) - ")
92 ACE_TEXT ("TAO_GIOP_Message_State::parse_magic_bytes, ")
93 ACE_TEXT ("bad %cIOP header: ")
94 ACE_TEXT ("magic word [%02x,%02x,%02x,%02x]\n"),
95 buf[0],
96 buf[0],
97 buf[1],
98 buf[2],
99 buf[3]));
100 return -1;
102 return 0;
106 TAO_GIOP_Message_State::get_version_info (char *buf)
108 if (TAO_debug_level > 8)
110 TAOLIB_DEBUG ((LM_DEBUG,
111 "TAO (%P|%t) - GIOP_Message_State::get_version_info\n"));
114 // We have a GIOP message on hand. Get its revision numbers
115 CORBA::Octet const incoming_major = buf[TAO_GIOP_VERSION_MAJOR_OFFSET];
116 CORBA::Octet const incoming_minor = buf[TAO_GIOP_VERSION_MINOR_OFFSET];
118 // Check the revision information
119 if (TAO_GIOP_Message_Generator_Parser_Impl::check_revision (
120 incoming_major,
121 incoming_minor) == 0)
123 if (TAO_debug_level > 0)
125 TAOLIB_DEBUG ((LM_DEBUG,
126 ACE_TEXT ("TAO (%P|%t) - bad version <%d.%d>\n"),
127 incoming_major, incoming_minor));
130 return -1;
133 // Set the version
134 this->giop_version_.minor = incoming_minor;
135 this->giop_version_.major = incoming_major;
137 return 0;
141 TAO_GIOP_Message_State::get_byte_order_info (char *buf)
143 if (TAO_debug_level > 8)
145 TAOLIB_DEBUG ((LM_DEBUG,
146 ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::get_byte_order_info\n") ));
149 // Let us be specific that this is for 1.0
150 if (this->giop_version_.minor == 0 && this->giop_version_.major == 1)
152 this->byte_order_ =
153 buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET];
155 if (this->byte_order_ != 0 && this->byte_order_ != 1)
157 if (TAO_debug_level > 2)
159 TAOLIB_DEBUG ((LM_DEBUG,
160 ACE_TEXT ("TAO (%P|%t) - GIOP_Message_State::get_byte_order_info, ")
161 ACE_TEXT ("invalid byte order <%d> for version <1.0>\n"),
162 this->byte_order_));
164 return -1;
167 else
169 // Read the byte ORDER
170 this->byte_order_ =
171 (CORBA::Octet) (buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET]& 0x01);
173 // Read the fragment bit
174 this->more_fragments_ =
175 ((buf[TAO_GIOP_MESSAGE_FLAGS_OFFSET]& 0x02) == 2);
177 this->compressed_ = (buf[0] == 0x5A);
180 return 0;
183 void
184 TAO_GIOP_Message_State::get_payload_size (char *rd_ptr)
186 // Move the read pointer
187 rd_ptr += TAO_GIOP_MESSAGE_SIZE_OFFSET;
189 this->payload_size_ = this->read_ulong (rd_ptr);
192 CORBA::ULong
193 TAO_GIOP_Message_State::read_ulong (const char *rd_ptr) const
195 CORBA::ULong x = 0;
197 // We don't need to do this sort of copy. But some compilers (read it
198 // as SunCC) have a problem in deferencing from the
199 // reinterpret_cast pointer of the <rd_ptr>, as the <rd_ptr> can be
200 // on stack. So let us go ahead with this copying...
201 char buf[] =
203 *rd_ptr,
204 *(rd_ptr + 1),
205 *(rd_ptr + 2),
206 *(rd_ptr + 3)
209 #if !defined (ACE_DISABLE_SWAP_ON_READ)
210 if (!(this->byte_order_ != ACE_CDR_BYTE_ORDER))
212 ACE_CDR::ULong* pul = reinterpret_cast<ACE_CDR::ULong*> (buf);
213 x = *pul;
215 else
217 ACE_CDR::swap_4 (buf, reinterpret_cast<char*> (&x));
219 #else
220 x = *reinterpret_cast<ACE_CDR::ULong*> (buf);
221 #endif /* ACE_DISABLE_SWAP_ON_READ */
223 return x;
226 TAO_END_VERSIONED_NAMESPACE_DECL