summaryrefslogtreecommitdiff
path: root/TAO/tao/GIOP.cpp
blob: a3026b34b843a38a70dec14a8d2511fcc75444fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// $Id$

// @(#)giop.cpp 1.10 95/09/21
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// GIOP:        Utility routines for sending, receiving GIOP messages
//
// Note that the Internet IOP is just the TCP-specific mapping of the
// General IOP.  Areas where other protocols may map differently
// include use of record streams (TCP has none), orderly disconnect
// (TCP has it), endpoint addressing (TCP uses host + port), security
// (Internet security should be leveraged by IIOP) and more.
//
// NOTE: There are a few places where this code knows that it's really
// talking IIOP instead of GIOP.  No rush to fix this so long as we
// are really not running atop multiple connection protocols.
//
// THREADING NOTE: currently, the connection manager eliminates tricky
// threading issues by providing this code with the same programming
// model both in threaded and unthreaded environments.  Since the GIOP
// APIs were all designed to be reentrant, this makes threading rather
// simple!
//
// That threading model is that the thread making (or handling) a call
// is given exclusive access to a connection for the duration of a
// call, so that no multiplexing or demultiplexing is needed.  That
// is, locking is at the "connection level" rather than "message
// level".
//
// The down side of this simple threading model is that utilization of
// system resources (mostly connections, but to some extent network
// I/O) in some kinds of environments can be inefficient.  However,
// simpler threading models are much easier to get properly debugged,
// and often perform better.  Also, such environments haven't been
// seen to be any kind of problem; the model can be changed later if
// needed, it's just an internal implementation detail.  Any portable
// ORB client is not allowed to rely on semantic implications of such
// a model.
//
// @@ there is lots of unverified I/O here.  In all cases, if an
// error is detected when marshaling or unmarshaling, it should be
// reported.

#include "tao/corba.h"
#include "tao/Timeprobe.h"

#if !defined (__ACE_INLINE__)
# include "tao/GIOP.i"
#endif /* ! __ACE_INLINE__ */

ACE_RCSID(tao, GIOP, "$Id$")

#if defined (ACE_ENABLE_TIMEPROBES)

static const char *TAO_GIOP_Timeprobe_Description[] =
{
  "GIOP::send_request - start",
  "GIOP::send_request - end",

  "GIOP::recv_request - start",
  "GIOP::recv_request - end",

  "GIOP::read_buffer - start",
  "GIOP::read_buffer - end",

  "GIOP::LocateRequestHeader_init - start",
  "GIOP::LocateRequestHeader_init - end"
};

enum
{
  // Timeprobe description table start key
  TAO_GIOP_SEND_REQUEST_START = 100,
  TAO_GIOP_SEND_REQUEST_END,

  TAO_GIOP_RECV_REQUEST_START,
  TAO_GIOP_RECV_REQUEST_END,

  TAO_GIOP_READ_BUFFER_START,
  TAO_GIOP_READ_BUFFER_END,

  TAO_GIOP_LOCATE_REQUEST_HEADER_INIT_START,
  TAO_GIOP_LOCATE_REQUEST_HEADER_INIT_END
};

// Setup Timeprobes
ACE_TIMEPROBE_EVENT_DESCRIPTIONS (TAO_GIOP_Timeprobe_Description,
                                  TAO_GIOP_SEND_REQUEST_START);

#endif /* ACE_ENABLE_TIMEPROBES */

static const char digits [] = "0123456789ABCD";
static const char *names [] =
{
  "Request",
  "Reply",
  "CancelRequest",
  "LocateRequest",
  "LocateReply",
  "CloseConnection",
  "MessageError",
  "EndOfFile"
};

void
TAO_GIOP::dump_msg (const char *label,
                    const u_char *ptr,
                    size_t len)
{
  if (TAO_debug_level >= 2)
    {
      ACE_DEBUG ((LM_DEBUG, "%s GIOP v%c.%c msg, %d data bytes, %s endian, %s\n",
                  label,
                  digits[ptr[4]],
                  digits[ptr[5]],
                  len - TAO_GIOP_HEADER_LEN,
                  (ptr[6] == TAO_ENCAP_BYTE_ORDER) ? "my" : "other",
                  (ptr[7] <= TAO_GIOP::MessageError) ? names [ptr[7]] : "UNKNOWN TYPE"));

      if (TAO_debug_level >= 4)
        ACE_HEX_DUMP ((LM_DEBUG, (const char*)ptr, len, "(%P|%t) data bytes\n"));
    }
}

CORBA_Boolean
operator<<(TAO_OutputCDR& cdr, const TAO_GIOP_ServiceContext& x)
{
  if ( (cdr << x.context_id)
       && (cdr << x.context_data) )
    return 1;
  return 0;
}

CORBA_Boolean

operator>>(TAO_InputCDR& cdr, TAO_GIOP_ServiceContext& x)
{
  if ( (cdr >> x.context_id)
       && (cdr >> x.context_data) )
    return 1;
  return 0;
}

CORBA_Boolean
operator<<(TAO_OutputCDR& cdr, const TAO_GIOP_ServiceContextList& x)
{
  CORBA::ULong length = x.length ();
  cdr.write_ulong (length);
  for (CORBA::ULong i = 0; i < length && cdr.good_bit (); ++i)
    cdr << x[i];
  return cdr.good_bit ();
}

CORBA_Boolean
operator>>(TAO_InputCDR& cdr, TAO_GIOP_ServiceContextList& x)
{
  CORBA::ULong length;
  cdr.read_ulong (length);
  if (cdr.good_bit ())
    {
      x.length (length);
      for (CORBA::ULong i = 0; i < length && cdr.good_bit (); ++i)
        cdr >> x[i];
    }
  return cdr.good_bit ();
}

// @@ TODO: this is a good candidate for an ACE routine, even more,
// all the code to write a Message_Block chain could be encapsulated
// in ACE.
static ssize_t
writev_n (ACE_HANDLE h, iovec *iov, int iovcnt)
{
  ssize_t writelen = 0;
  int s = 0;
  while (s < iovcnt)
    {
      ssize_t n = ACE_OS::sendv (h, iov + s, iovcnt - s);

      if (n == -1)
        {
          return n;
        }
      else
        {
          writelen += n;
          while (s < iovcnt && n >= ACE_static_cast (ssize_t, iov[s].iov_len))
            {
              n -= iov[s].iov_len;
              s++;
            }
          if (n != 0)
            {
              char* base = ACE_reinterpret_cast (char*, iov[s].iov_base);

              iov[s].iov_base = base + n;
              iov[s].iov_len = iov[s].iov_len - n;
            }
        }
    }
  return writelen;
}


CORBA::Boolean
TAO_GIOP::send_request (TAO_SVC_HANDLER *handler,
                        TAO_OutputCDR &stream,
                        TAO_ORB_Core* orb_core)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_SEND_REQUEST_START);

  char *buf = (char *) stream.buffer ();
  size_t buflen = stream.total_length ();

  // assert (buflen == (stream.length - stream.remaining));

  // Patch the message length in the GIOP header; it's always at the
  // same eight byte offset into the message.
  //
  // NOTE: Here would also be a fine place to calculate a digital
  // signature for the message and place it into a preallocated slot
  // in the "ServiceContext".  Similarly, this is a good spot to
  // encrypt messages (or just the message bodies) if that's needed in
  // this particular environment and that isn't handled by the
  // networking infrastructure (e.g. IPSEC).

  size_t offset = 8;
  size_t header_len = TAO_GIOP_HEADER_LEN;
  if (orb_core->orb_params ()->use_IIOP_lite_protocol ())
    {
      offset = 0;
      header_len = 5;
    }

  CORBA::ULong bodylen = buflen - header_len;
  

#if !defined (TAO_ENABLE_SWAP_ON_WRITE)
  *ACE_reinterpret_cast(CORBA::ULong*,buf + offset) = bodylen;
#else
  if (!stream->do_byte_swap ())
    {
      *ACE_reinterpret_cast(CORBA::ULong*, buf + offset) = bodylen;
    }
  else
    {
      CDR::swap_4 (ACE_reinterpret_cast(char*,&bodylen), buf + offset);
    }
#endif

  // Strictly speaking, should not need to loop here because the
  // socket never gets set to a nonblocking mode ... some Linux
  // versions seem to need it though.  Leaving it costs little.

#if 0
  TAO_GIOP::dump_msg ("send",
                      ACE_reinterpret_cast (u_char *, buf),
                      buflen);
#endif

  TAO_SOCK_Stream &peer = handler->peer ();

  const int TAO_WRITEV_MAX = 16;
  iovec iov[TAO_WRITEV_MAX];
  int iovcnt = 0;
  for (const ACE_Message_Block* i = stream.begin ();
       i != stream.end ();
       i = i->cont ())
    {
      iov[iovcnt].iov_base = i->rd_ptr ();
      iov[iovcnt].iov_len  = i->length ();
      iovcnt++;

      // The buffer is full make a OS call.
      // @@ TODO this should be optimized on a per-platform basis, for
      // instance, some platforms do not implement writev() there we
      // should copy the data into a buffer and call send_n(). In
      // other cases there may be some limits on the size of the
      // iovec, there we should set TAO_WRITEV_MAX to that limit.
      if (iovcnt == TAO_WRITEV_MAX)
        {
          ssize_t n = writev_n (peer.get_handle (), iov, iovcnt);
          if (n == -1)
            {
              if (TAO_orbdebug)
                ACE_DEBUG ((LM_DEBUG,
                            "(%P|%t) closing conn %d after fault %p\n",
                            peer.get_handle (), "GIOP::send_request"));
              handler->handle_close ();
              return 0;
            }
          else if (n == 0)
            {
              if (TAO_orbdebug)
                ACE_DEBUG ((LM_DEBUG,
                            "(%P|%t) GIOP::send_request (): "
                            "EOF, closing conn %d\n",
                            peer.get_handle ()));
              handler->handle_close ();
              return 0;
            }
          iovcnt = 0;
        }
    }

  if (iovcnt != 0)
    {
      ssize_t n = writev_n (peer.get_handle (), iov, iovcnt);
      if (n == -1)
        {
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_DEBUG,
                        "(%P|%t) closing conn %d after fault %p\n",
                        peer.get_handle (), "GIOP::send_request"));
          handler->handle_close ();
          return 0;
        }
      else if (n == 0)
        {
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_DEBUG,
                        "(%P|%t) GIOP::send_request (): "
                        "EOF, closing conn %d\n",
                        peer.get_handle ()));
          handler->handle_close ();
          return 0;
        }
      iovcnt = 0;
    }

  return 1;
}

// Server sends an "I'm shutting down now, any requests you've sent me
// can be retried" message to the server.  The message is prefab, for
// simplicity.
//
// NOTE: this is IIOP-specific though it doesn't look like it is.  It
// relies on a TCP-ism: orderly disconnect, which doesn't exist in all
// transport protocols.  Versions of GIOP atop some transport that's
// lacking orderly disconnect must define some transport-specific
// handshaking (e.g. the XNS/SPP handshake convention) in order to
// know that the same transport semantics are provided when shutdown
// is begun with messages "in flight". (IIOP doesn't report false
// errors in the case of "clean shutdown", because it relies on
// orderly disconnect as provided by TCP.  This quality of service is
// required to write robust distributed systems.)

static const char
close_message [TAO_GIOP_HEADER_LEN] =
{
  'G', 'I', 'O', 'P',
  TAO_GIOP_MessageHeader::MY_MAJOR,
  TAO_GIOP_MessageHeader::MY_MINOR,
  TAO_ENCAP_BYTE_ORDER,
  TAO_GIOP::CloseConnection,
  0, 0, 0, 0
};

void
TAO_GIOP::close_connection (TAO_Client_Connection_Handler *&handler,
                            void *)
{
  // It's important that we use a reliable shutdown after we send this
  // message, so we know it's received.
  //
  // @@ should recv and discard queued data for portability; note
  // that this won't block (long) since we never set SO_LINGER

  TAO_GIOP::dump_msg ("send",
                      (const u_char *) close_message,
                      TAO_GIOP_HEADER_LEN);

  handler->peer ().send (close_message, TAO_GIOP_HEADER_LEN);
  ACE_HANDLE which = handler->peer ().get_handle ();
  handler->handle_close ();
  handler = 0;
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) shut down socket %d\n", which));
}


// Send an "I can't understand you" message -- again, the message is
// prefabricated for simplicity.  This implies abortive disconnect (at
// the application level, if not at the level of TCP).
//
// NOTE that IIOP will still benefit from TCP's orderly disconnect.

static const char
error_message [TAO_GIOP_HEADER_LEN] =
{
  'G', 'I', 'O', 'P',
  TAO_GIOP_MessageHeader::MY_MAJOR,
  TAO_GIOP_MessageHeader::MY_MINOR,
  TAO_ENCAP_BYTE_ORDER,
  TAO_GIOP::MessageError,
  0, 0, 0, 0
};

void
TAO_GIOP::send_error (TAO_SVC_HANDLER *&handler)
{
  TAO_GIOP::dump_msg ("send",
                      (const u_char *) error_message,
                      TAO_GIOP_HEADER_LEN);
  handler->peer ().send_n (error_message, TAO_GIOP_HEADER_LEN);
  if (TAO_orbdebug)
    ACE_DEBUG ((LM_DEBUG, "(%P|%t) aborted socket %d\n", handler->peer ().get_handle ()));
//   handler->handle_close ();
  handler = 0;
}

ssize_t
TAO_GIOP::read_buffer (TAO_SOCK_Stream &peer,
                       char *buf,
                       size_t len)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_READ_BUFFER_START);

  ssize_t bytes_read = peer.recv_n (buf, len);

  if (bytes_read == -1 && errno == ECONNRESET)
    {
      // We got a connection reset (TCP RSET) from the other side,
      // i.e., they didn't initiate a proper shutdown.
      //
      // Make it look like things are OK to the upper layer.
      bytes_read = 0;
      errno = 0;
    }

  return bytes_read;
}

// Read the message header, plus any data part of the message, setting
// stuff up so that CDR byteswaps data as appropriate.  Errors are
// reported to be MessageError messages.
//
// NOTE: this code is structured to issue two read () calls for each
// incoming message.  Alternative structures (e.g. with a user-space
// buffer per connection, or networking code handing off entire GIOP
// messages) can reduce the overhead of these calls to the networking
// code; correctness and simplicity drove this implementation more
// than efficiency.
//
// NOTE: as always, counting system calls associated with I/O gives
// you a good basic understanding of the tuning issues.  On the server
// side, there is normally select/read/read/write per invocation.  The
// call to select () can be omitted by allocating a thread to each
// connection; in some cases, that alone has almost doubled
// performance.  The two read () calls can be made into one by fancy
// buffering.  How fast could it be with both optimizations applied?

TAO_GIOP::Message_Type
TAO_GIOP::recv_request (TAO_SVC_HANDLER *&handler,
                        TAO_InputCDR &msg,
			TAO_ORB_Core* orb_core)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_RECV_REQUEST_START);

  TAO_GIOP::Message_Type retval;
  CORBA::ULong message_size;
  TAO_SOCK_Stream &connection = handler->peer ();

  // Read the message header off the wire.
  //
  // THREADING NOTE: the connection manager handed us this connection
  // for exclusive use, so we need not worry about having two threads
  // interleave reads of partial messages.  This model is excellent
  // for "lightly threaded" systems (as will be the majority in the
  // near future) but makes less effective use of connection resources
  // as the "duty factor" goes down because of either long calls or
  // bursty contention during numerous short calls to the same server.

  CDR::mb_align (&msg.start_);

  ssize_t header_len = TAO_GIOP_HEADER_LEN;
  if (orb_core->orb_params ()->use_IIOP_lite_protocol ())
    header_len = 5;

  if (CDR::grow (&msg.start_, header_len) == -1)
    return TAO_GIOP::CommunicationError; // This should probably be an exception.

  char *header = msg.start_.rd_ptr ();
  ssize_t len = TAO_GIOP::read_buffer (connection,
                                       header,
                                       header_len);
  // Read the header into the buffer.

  if (len != header_len)
    {
      switch (len)
        {
        case 0:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_DEBUG,
                        "(%t) End of connection %d\n",
                        connection.get_handle ()));
          return TAO_GIOP::EndOfFile;
          // @@ should probably find some way to report this without
          // an exception, since for most servers it's not an error.
          // Is it _never_ an error?  Not sure ...
          /* NOTREACHED */
        case -1: // error
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_ERROR,
                        "(%P|%t) GIOP::recv_request header socket error %p\n",
                        "read_buffer"));
          break;
          /* NOTREACHED */
        default:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_ERROR,
                        "(%P|%t) GIOP::recv_request header read failed, only %d of %d bytes\n",
                        len, header_len));
          break;
          /* NOTREACHED */
        }

      return TAO_GIOP::CommunicationError;
    }

  // NOTE: if message headers, or whome messages, get encrypted in
  // application software (rather than by the network infrastructure)
  // they should be decrypted here ...

  // First make sure it's a GIOP message of any version.

  if (TAO_GIOP::parse_header (msg,
			      msg.do_byte_swap_,
			      retval,
			      message_size,
			      orb_core) == -1)
    {
      TAO_GIOP::send_error (handler);
      return TAO_GIOP::EndOfFile; // We didn't really receive 
                                  // anything useful here.
    }

  // Make sure we have the full length in memory, growing the buffer
  // if needed.
  //
  // NOTE: We could overwrite these few bytes of header... they're
  // left around for now as a debugging aid.

  assert (message_size <= UINT_MAX);

  if (CDR::grow (&msg.start_, header_len + message_size) == -1)
    return TAO_GIOP::CommunicationError;

  // Growing the buffer may have reset the rd_ptr(), but we want to
  // leave it just after the GIOP header (that was parsed already);
  CDR::mb_align (&msg.start_);
  msg.start_.wr_ptr (header_len);
  msg.start_.wr_ptr (message_size);
  msg.start_.rd_ptr (header_len);

  char* payload = msg.start_.rd_ptr ();

  // Read the rest of this message into the buffer.

  len = TAO_GIOP::read_buffer (connection,
                               payload,
                               (size_t) message_size);

  if (len != (ssize_t) message_size)
    {
      switch (len)
        {
        case 0:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_DEBUG,
                        "(%P|%t) TAO_GIOP::recv_request body, EOF on handle %d\n",
                        connection.get_handle ()));
          break;
          /* NOTREACHED */
        case -1:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_ERROR,
                        "(%P|%t) TAO_GIOP::recv_request () body %p\n",
                        "read_buffer"));
          break;
          /* NOTREACHED */
        default:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_ERROR,
                        "(%P|%t) short read, only %d of %d bytes\n", len, message_size));
          break;
          /* NOTREACHED */
        }

      // clean up, and ...
      if (TAO_orbdebug)
        ACE_DEBUG ((LM_DEBUG, "couldn't read rest of message\n"));
      return TAO_GIOP::CommunicationError;
    }

  TAO_GIOP::dump_msg ("recv",
                      ACE_reinterpret_cast (u_char *, header),
                      message_size + header_len);
  return retval;
}

int
TAO_GIOP::parse_header_std (TAO_InputCDR &cdr,
			    int& do_byte_swap,
			    TAO_GIOP::Message_Type& message_type,
			    CORBA::ULong& message_size)
{
  char *header = cdr.start_.rd_ptr ();
  
  if (!(header [0] == 'G'
        && header [1] == 'I'
        && header [2] == 'O'
        && header [3] == 'P'))
    {
      ACE_DEBUG ((LM_DEBUG, "bad header, magic word\n"));
      return -1;
    }

  // Then make sure the major version is ours, and the minor version
  // is one that we understand.

  if (!(header [4] == TAO_GIOP_MessageHeader::MY_MAJOR
        && header [5] <= TAO_GIOP_MessageHeader::MY_MINOR))
    {
      ACE_DEBUG ((LM_DEBUG, "bad header, version\n"));
      return -1;
    }

  // Get the message type out and adjust the buffer's records to record
  // that we've read everything except the length.

  message_type = (TAO_GIOP::Message_Type) header[7];

  do_byte_swap = (header [6] != TAO_ENCAP_BYTE_ORDER);

  // Make sure byteswapping is done if needed, and then read the
  // message size (appropriately byteswapped).

  cdr.start_.rd_ptr (8);
  cdr.read_ulong (message_size);

  return 0;
}

int
TAO_GIOP::parse_header_lite (TAO_InputCDR &cdr,
			     int& do_byte_swap,
			     TAO_GIOP::Message_Type& message_type,
			     CORBA::ULong& message_size)
{
  do_byte_swap = 0;

  char *header = cdr.start_.rd_ptr ();
  
  // Get the message type out and adjust the buffer's records to
  // record that we've read everything except the length.
  message_type = (TAO_GIOP::Message_Type) header[4];

  cdr.read_ulong (message_size);

  cdr.start_.rd_ptr (1);

  return 0;
}

int
TAO_GIOP::parse_header (TAO_InputCDR &cdr,
			int& do_byte_swap,
			TAO_GIOP::Message_Type& message_type,
			CORBA::ULong& message_size,
			TAO_ORB_Core* orb_core)
{
  if (orb_core->orb_params ()->use_IIOP_lite_protocol ())
    return TAO_GIOP::parse_header_lite (cdr,
					do_byte_swap,
					message_type,
					message_size);
  else
    return TAO_GIOP::parse_header_std (cdr,
				       do_byte_swap,
				       message_type,
				       message_size);
}

CORBA::Boolean
TAO_GIOP_LocateRequestHeader::init (TAO_InputCDR &msg,
                                    CORBA::Environment &env)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_LOCATE_REQUEST_HEADER_INIT_START);

  return (msg.read_ulong (this->request_id)
          && msg.decode (TC_opaque,
                         &this->object_key,
                         0,
                         env));
}

CORBA::Boolean
TAO_GIOP::start_message_std (TAO_GIOP::Message_Type type,
			     TAO_OutputCDR &msg)
{
  msg.reset ();

  // if (msg.size () < TAO_GIOP_HEADER_LEN)
  // return 0;

  static CORBA::Octet header[] = {
    'G', 'I', 'O', 'P',
    TAO_GIOP_MessageHeader::MY_MAJOR,
    TAO_GIOP_MessageHeader::MY_MINOR,
    TAO_ENCAP_BYTE_ORDER
  };
  static int header_size = sizeof(header)/sizeof(header[0]);
  msg.write_octet_array (header, header_size);
  msg.write_octet (type);

  // Write a dummy <size> later it is set to the right value...
  // @@ TODO Maybe we should store the OutputCDR status in
  CORBA::ULong size = 0;
  msg.write_ulong (size);

  return 1;
}

CORBA::Boolean
TAO_GIOP::start_message_lite (TAO_GIOP::Message_Type type,
			      TAO_OutputCDR &msg)
{
  msg.reset ();

  // Write a dummy <size> later it is set to the right value...
  // @@ TODO Maybe we should store the OutputCDR status in
  CORBA::ULong size = 0;
  msg.write_ulong (size);

  msg.write_octet (type);

  return 1;
}

CORBA::Boolean
TAO_GIOP::start_message (TAO_GIOP::Message_Type type,
			 TAO_OutputCDR &msg,
			 TAO_ORB_Core* orb_core)
{
  if (orb_core->orb_params ()->use_IIOP_lite_protocol ())
    return TAO_GIOP::start_message_lite (type, msg);
  else
    return TAO_GIOP::start_message_std (type, msg);
}

const char * 
TAO_GIOP::message_name (TAO_GIOP::Message_Type which) 
{ 
  static const char *msgnames[] = 
  { 
    "EndOfFile (nonstd)", 
    "Request (client)", 
    "Reply (server)", 
    "CancelRequest (client)", 
    "LocateRequest (client)", 
    "LocateReply (server)", 
    "CloseConnection (server)", 
    "MessageError (either)" 
  }; 
 
  int i = (int) which + 1; 
  // Add one since EndOfFile is -1. 
 
  if (i > (int) (sizeof (msgnames) / sizeof (msgnames[0]))) 
    return "<Bad Value!>"; 
  else 
    return msgnames[i]; 
} 
 
TAO_GIOP_ReplyStatusType
TAO_GIOP::convert_CORBA_to_GIOP_exception (CORBA::ExceptionType corba_type)
{
  switch (corba_type)
    {
    case CORBA::NO_EXCEPTION:
      return TAO_GIOP_NO_EXCEPTION;

    case CORBA::SYSTEM_EXCEPTION:
      return TAO_GIOP_SYSTEM_EXCEPTION;

    case CORBA::USER_EXCEPTION:
      return TAO_GIOP_USER_EXCEPTION;

    default:
      // Don't know what to do here??
      return TAO_GIOP_SYSTEM_EXCEPTION;
    }
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class TAO_Unbounded_Sequence<TAO_GIOP_ServiceContext>;
template class TAO_Unbounded_Sequence<TAO_IOP_TaggedComponent>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate TAO_Unbounded_Sequence<TAO_GIOP_ServiceContext>
#pragma instantiate TAO_Unbounded_Sequence<TAO_IOP_TaggedComponent>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */