1 #include "GIOP_Buffer.h"
2 #include "ace/OS_NS_string.h"
3 #include "ace/Log_Msg.h"
5 static const char *size_leadin_1_5
= "GIOP v1."; //"x msg, ";
6 static size_t leadin_len_1_5
= 15;
7 static const char *size_leadin_1_6
= "GIOP message v1."; //2, ";
8 static size_t leadin_len_1_6
= 19;
11 const char *GIOP_Buffer::size_leadin
= 0;
12 size_t GIOP_Buffer::leadin_len
= 0;
13 const size_t GIOP_Buffer::giop_header_len
= 12;
16 GIOP_Buffer::init_leadin (int version
)
22 size_leadin
= size_leadin_1_5
;
23 leadin_len
= leadin_len_1_5
;
27 size_leadin
= size_leadin_1_6
;
28 leadin_len
= leadin_len_1_6
;
33 // GIOP 1.2 header: 12 bytes
37 // type: 1 // req/repl/lf/excp
40 // Request 1.2 header:
44 // Address disposition: 4
45 // target: <4 len, + len> [0-3 pad]
46 // opname: <4 len, + len>
48 // GIOP 1.0 header: 12 bytes
52 // type: 1 // req/repl/lf/excp
55 // Request 1.0 header:
57 struct RequestHeader_1_0 { // Renamed from RequestHeader
58 IOP::ServiceContextList service_context;
59 unsigned long request_id;
60 boolean response_expected;
61 sequence <octet> object_key;
63 CORBA::OctetSeq requesting_principal;
66 // service_context: 4(count)+contexts.
68 // content blob: 4(len) + len octets + (0-3)pad
72 // Address disposition: 4
73 // target: <4 len, + len> [0-3 pad]
74 // opname: <4 len, + len>
77 struct ReplyHeader_1_0 { // Renamed from ReplyHeader
78 IOP::ServiceContextList service_context;
79 unsigned long request_id;
80 ReplyStatusType_1_0 reply_status;
83 typedef ReplyHeader_1_0 ReplyHeader_1_1;
84 // Same Header contents for 1.0 and 1.1
87 enum ReplyStatusType_1_2 {
92 LOCATION_FORWARD_PERM,// new value for 1.2
93 NEEDS_ADDRESSING_MODE // new value for 1.2
95 struct ReplyHeader_1_2 {
96 unsigned long request_id;
97 ReplyStatusType_1_2 reply_status;
98 IOP::ServiceContextList service_context;
104 //static const size_t target_offset_12 = GIOP_Buffer::giop_header_len + 12;
106 // 12 = req_id + flags + RESVD + addr disp.
108 GIOP_Buffer::GIOP_Buffer()
121 buffer_lost_ (false),
132 header_parsed_ (false),
138 GIOP_Buffer::GIOP_Buffer(const char *text
,
146 time_(ACE_Time_Value::zero
),
154 buffer_lost_ (false),
165 header_parsed_ (false),
169 const char *size_str
= ACE_OS::strstr(text
, size_leadin
);
172 size_str
+= leadin_len
; //ACE_OS::strlen (size_leadin);
174 this->expected_size_
= ACE_OS::strtol(size_str
, 0, 10);
175 const char *id
= ACE_OS::strchr(size_str
, '[') + 1;
176 this->expected_req_id_
= ACE_OS::strtol(id
, 0, 10);
178 this->sending_
= ACE_OS::strstr(text
,"send") ? 0 : 1;
179 this->expected_type_
= ACE_OS::strstr(text
,"Request") ? 0 : 1;
183 GIOP_Buffer::owner (Invocation
*owner
)
185 this->owner_
= owner
;
189 GIOP_Buffer::owner ()
195 GIOP_Buffer::init_buf (const char *text
, size_t offset
)
197 // GIOP message - HEXDUMP
198 this->log_offset_
= offset
;
199 const char * size_str
= ACE_OS::strstr (text
,"HEXDUMP ") + 8;
200 this->buffer_size_
= ACE_OS::strtol (size_str
, 0, 10);
201 this->payload_size_
= this->buffer_size_
- 12;
202 size_str
= ACE_OS::strstr (text
,"showing first ");
206 this->buffer_size_
= ACE_OS::strtol (size_str
, 0, 10);
208 if (this->octets_
!= 0)
210 delete [] this->octets_
;
212 this->octets_
= new char [this->buffer_size_
];
213 ACE_OS::memset (this->octets_
, 0, this->buffer_size_
);
214 this->wr_pos_
= this->octets_
;
217 GIOP_Buffer::~GIOP_Buffer()
219 delete [] this->octets_
;
223 GIOP_Buffer::add_octets (const char *text
, size_t offset
)
225 if (this->octets_
== 0)
227 this->init_buf(text
, offset
);
231 const char *c
= text
;
234 count
< 16 && this->cur_size() < this->buffer_size_
;
239 int o
= ::strtol(c
, &err
, 16);
240 if (err
== c
|| *err
== 0)
242 *this->wr_pos_
++ = o
;
245 size_t cs
= this->cur_size();
247 if (!this->header_parsed_
)
249 this->header_parsed_
= this->parse_header();
250 if (this->header_parsed_
)
253 if (cs
== this->buffer_size_
)
255 char vmaj
= this->octets_
[4];
256 char order
= this->octets_
[6];
259 this->cdr_
= new ACE_InputCDR (this->octets_
,
261 order
, vmaj
, this->ver_minor_
);
262 this->cdr_
->skip_bytes (this->payload_start_
- this->octets_
);
269 GIOP_Buffer::sending () const
271 return this->sending_
;
275 GIOP_Buffer::is_full () const
277 return this->buffer_size_
> 0 && this->cur_size() == this->buffer_size_
;
281 GIOP_Buffer::type () const
283 if (this->octets_
== 0)
285 return this->octets_
[7];
289 GIOP_Buffer::expected_type () const
291 return this->expected_type_
;
295 GIOP_Buffer::minor_version () const
297 return this->ver_minor_
;
301 GIOP_Buffer::reply_status () const
303 return this->reply_status_
;
307 GIOP_Buffer::num_contexts () const
309 return this->num_contexts_
;
313 GIOP_Buffer::is_oneway ()
315 if (this->octets_
== 0)
320 if (!this->header_parsed_
)
321 this->header_parsed_
= this->parse_header();
323 return (resp_exp_
&1) == 0;
327 GIOP_Buffer::log_posn () const
329 return this->log_offset_
;
333 GIOP_Buffer::thread ()
338 const ACE_Time_Value
&
339 GIOP_Buffer::time () const
345 GIOP_Buffer::time (const ACE_Time_Value
&t
)
351 GIOP_Buffer::preamble () const
353 return this->preamble_
;
357 GIOP_Buffer::expected_size () const
359 return this->expected_size_
;
363 GIOP_Buffer::msg_size ()
365 if (this->cur_size() < 12)
367 if (!this->header_parsed_
)
368 this->header_parsed_
= this->parse_header();
369 return (size_leadin
== size_leadin_1_5
) ? this->payload_size_
: this->msg_size_
;
373 GIOP_Buffer::expected_req_id () const
375 return this->expected_req_id_
;
379 GIOP_Buffer::actual_req_id ()
381 if (this->octets_
== 0)
384 if (!this->header_parsed_
)
385 this->header_parsed_
= this->parse_header();
387 return this->req_id_
;
391 GIOP_Buffer::cur_size () const
393 return this->wr_pos_
- this->octets_
;
397 GIOP_Buffer::parse_svc_contexts ()
400 ACE_CDR::ULong num_svc_cont
;
401 if ( !(*this->cdr_
>> num_svc_cont
))
403 this->num_contexts_
= static_cast<size_t>(num_svc_cont
);
404 while (num_svc_cont
> 0)
406 if (!(*this->cdr_
>> temp
)) // tag really
408 if (!(*this->cdr_
>> temp
))
410 if ((this->cur_size() == this->buffer_size_
) && (this->cdr_
->length() < temp
))
412 return this->cdr_
->skip_bytes (this->cdr_
->length());
414 if (!this->cdr_
->skip_bytes(temp
))
416 return this->cur_size() == this->buffer_size_
;
424 GIOP_Buffer::parse_header ()
426 if (this->octets_
== 0 || this->cur_size() < 12)
431 char vmaj
= this->octets_
[4];
432 this->ver_minor_
= this->octets_
[5];
433 char order
= this->octets_
[6];
435 char mtype
= this->octets_
[7];
436 if (mtype
> 1) // not a request or reply
441 this->cdr_
= new ACE_InputCDR (this->octets_
,
443 order
, vmaj
, this->ver_minor_
);
444 this->cdr_
->skip_bytes (8);
445 this->payload_start_
= this->cdr_
->rd_ptr();
446 ACE_CDR::ULong len_ulong
;
447 if (!(*this->cdr_
>> len_ulong
))
449 this->msg_size_
= len_ulong
;
451 if (this->ver_minor_
< 2)
453 if (!this->parse_svc_contexts())
459 if (!(*this->cdr_
>> len_ulong
))
463 this->req_id_
= static_cast<size_t>(len_ulong
);
467 if (!(*this->cdr_
>> this->resp_exp_
))
471 if (this->ver_minor_
> 1 &&
472 !(*this->cdr_
>> len_ulong
)) // address disposition
476 if (!(*this->cdr_
>> len_ulong
))
480 this->oid_len_
= static_cast<size_t>(len_ulong
);
481 this->oid_
= this->cdr_
->rd_ptr();
482 if (!this->cdr_
->skip_bytes(len_ulong
))
486 if (!(*this->cdr_
>> len_ulong
))
490 this->opname_
= this->cdr_
->rd_ptr();
491 if (!this->cdr_
->skip_bytes(len_ulong
))
497 if (!(*this->cdr_
>> len_ulong
))
501 this->reply_status_
= static_cast<size_t>(len_ulong
);
506 if (this->ver_minor_
> 1)
508 if (!this->parse_svc_contexts())
511 this->cdr_
->align_read_ptr (8);
512 this->payload_start_
= this->cdr_
->rd_ptr();
517 GIOP_Buffer::target_oid (size_t &len
)
519 if (this->octets_
== 0)
524 if (!this->header_parsed_
)
525 this->header_parsed_
= this->parse_header();
530 len
= this->oid_len_
;
535 GIOP_Buffer::operation ()
540 if (!this->header_parsed_
)
541 this->header_parsed_
= this->parse_header();
543 return this->opname_
;
547 GIOP_Buffer::payload ()
549 if (octets_
!= 0 && !this->header_parsed_
)
550 this->header_parsed_
= this->parse_header();
555 GIOP_Buffer::has_octets () const
557 return (octets_
!= 0);
561 GIOP_Buffer::validate ()
564 this->expected_req_id_
== this->actual_req_id() &&
565 this->expected_type_
== this->type() &&
566 this->expected_size_
== this->msg_size();
570 GIOP_Buffer::matches (GIOP_Buffer
*other
) const
572 if (other
->header_parsed_
)
574 return this->expected_req_id_
== other
->actual_req_id() &&
575 this->expected_type_
== other
->type() &&
576 (this->expected_size_
== other
->msg_size() ||
577 this->expected_size_
== other
->msg_size() + 4);
579 return this->expected_req_id_
== other
->expected_req_id() &&
580 this->expected_type_
== other
->expected_type() &&
581 this->sending_
== other
->sending() &&
582 this->expected_size_
== other
->expected_size();
586 GIOP_Buffer::reset ()
590 this->buffer_size_
= 0;
591 this->payload_size_
= 0;
592 this->buffer_lost_
= true;
593 this->header_parsed_
= false;
598 GIOP_Buffer::transfer_from (GIOP_Buffer
*other
)
600 delete this->octets_
;
601 this->octets_
= other
->octets_
;
602 this->wr_pos_
= other
->wr_pos_
;
603 this->buffer_size_
= other
->buffer_size_
;
604 this->payload_size_
= other
->payload_size_
;
605 this->header_parsed_
= false;
610 GIOP_Buffer::swap (GIOP_Buffer
*other
)
612 char *tmp_octets
= this->octets_
;
613 char *tmp_wr_pos
= this->wr_pos_
;
614 size_t tmp_bsize
= this->buffer_size_
;
615 size_t tmp_psize
= this->payload_size_
;
617 this->octets_
= other
->octets_
;
618 this->wr_pos_
= other
->wr_pos_
;
619 this->buffer_size_
= other
->buffer_size_
;
620 this->payload_size_
= other
->payload_size_
;
622 other
->octets_
= tmp_octets
;
623 other
->wr_pos_
= tmp_wr_pos
;
624 other
->buffer_size_
= tmp_bsize
;
625 other
->payload_size_
= tmp_psize
;