summaryrefslogtreecommitdiff
path: root/TAO/tao/GIOP.cpp
blob: 024602f5d90767979597a4cd580a9ce622e28089 (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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// $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.
// @@ Some dependance on the specific underlying transport protocol used.
//    This must be removed in order to support pluggable protocols.
//    TAO_Connector and TAO_Transport objects will be introduced
//    to abstract away the specific transport protocol used.  We will
//    just expose behavior (methods) to all transport protocols that
//    can be used with GIOP!  fredk

#include "tao/GIOP.h"
#include "tao/Timeprobe.h"
#include "tao/CDR.h"
#include "tao/Pluggable.h"
#include "tao/debug.h"
#include "tao/ORB_Core.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_message - start",
    "GIOP::recv_message - 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_MESSAGE_START,
    TAO_GIOP_RECV_MESSAGE_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 [] =
{
  "CommunicationError",
  "EndOfFile",
  "ShortRead",
  "Request",
  "Reply",
  "CancelRequest",
  "LocateRequest",
  "LocateReply",
  "CloseConnection",
  "MessageError"
  "Fragment"
};

CORBA::ULong const TAO_GIOP::tao_specific_message_types = 3;

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",
                  label,
                  digits[ptr[4]],
                  digits[ptr[5]],
                  len - TAO_GIOP_HEADER_LEN,
                  (ptr[6] == TAO_ENCAP_BYTE_ORDER) ? "my" : "other",
                  (ptr[7] <= TAO_GIOP::Fragment) ? names [ptr[7] +
                                                         TAO_GIOP::tao_specific_message_types] : "UNKNOWN TYPE"));

      if (ptr[7] == TAO_GIOP::Request)
        {
          // @@ Only works if ServiceContextList is empty....
          const CORBA::ULong *request_id =
            ACE_reinterpret_cast(const CORBA::ULong*,
                                 ptr + TAO_GIOP_HEADER_LEN + 4);
          ACE_DEBUG ((LM_DEBUG, " = %d\n", *request_id));
        }
      else if (ptr[7] == TAO_GIOP::Reply)
        {
          const CORBA::ULong *request_id =
            ACE_reinterpret_cast(const CORBA::ULong*,
                                 ptr + TAO_GIOP_HEADER_LEN + 4);
          ACE_DEBUG ((LM_DEBUG, " = %d\n", *request_id));
        }
      else
        ACE_DEBUG ((LM_DEBUG, "\n"));

      if (TAO_debug_level >= 4)
        ACE_HEX_DUMP ((LM_DEBUG,
                       (const char*)ptr,
                       len,
                       "GIOP message"));
    }
}

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

CORBA::Boolean
operator>>(TAO_InputCDR &cdr,
           TAO_GIOP_ServiceContext &x)
{
  if (cdr >> x.context_id
      && cdr >> x.context_data)
    return 1;
  else
    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 ();
}

CORBA::Boolean
TAO_GIOP::send_request (TAO_Transport  *transport,
                        TAO_OutputCDR &stream,
                        TAO_ORB_Core *orb_core)
{

  TAO_FUNCTION_PP_TIMEPROBE (TAO_GIOP_SEND_REQUEST_START);

  char *buf = (char *) stream.buffer (); // ptr to first buffer
  size_t total_len = stream.total_length ();  // length of all buffers

  // 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 header_len = TAO_GIOP_HEADER_LEN;
  // Fred: this is actually a good start to think about pluggable
  //       "messaging protocols" (maybe this is a bad name, but I want
  //       to stress the difference with the pluggable "transport
  //       protocols" that you recently completed).
  //       For example: it seems that the transport layer needs to
  //       know the size of the header and somehow needs to determine
  //       the size of the message from that header, this could be
  //       virtual methods in the MessagingProtocol class.
  //       Just a wild thought..... Carlos
  //
  size_t offset = TAO_GIOP_MESSAGE_SIZE_OFFSET;
  if (orb_core->orb_params ()->use_lite_protocol ())
    {
      header_len = TAO_GIOP_LITE_HEADER_LEN;
      offset = TAO_GIOP_LITE_MESSAGE_SIZE_OFFSET;
    }

  CORBA::ULong bodylen = total_len - header_len;

#if !defined (ACE_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
    ACE_CDR::swap_4 (ACE_reinterpret_cast (char *,
                                           &bodylen),
                     buf + offset);
#endif /* ACE_ENABLE_SWAP_ON_WRITE */

  // 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.

  TAO_GIOP::dump_msg ("send",
                      ACE_reinterpret_cast (u_char *, buf),
                      stream.length ());

  // this guarantees to send all data (bytes) or return an error
  ssize_t n = transport->send (stream.begin ());

  if (n == -1)
    {
      if (TAO_orbdebug)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "TAO: (%P|%t) closing conn %d after fault %p\n",
                      transport->handle (),
                      "GIOP::send_request ()"));
      }
    transport->close_connection ();
    return 0;
  }

  // @@ Don't know about this one, when will we get a 0 from the write
  //    if we assume that there is data to write.  I would only expect
  //    a 0 if there was nothing to send or if nonblocking.
  if (n == 0)
    {
      if (TAO_orbdebug)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "TAO: (%P|%t) GIOP::send_request () "
                      "EOF, closing conn %d\n",
                      transport->handle()));
        }
      transport->close_connection ();
      return 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 CORBA::Octet
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_Transport *transport, 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 ("close_connection",
                      (const u_char *) close_message,
                      TAO_GIOP_HEADER_LEN);

  // @@ Carlos, can you please check the return value on this?
  ACE_HANDLE which = transport->handle ();
  if (transport->send ((const u_char *) close_message, TAO_GIOP_HEADER_LEN) == -1)
  {
    if (TAO_orbdebug)
        ACE_ERROR ((LM_ERROR,
                    "(%P|%t) error closing connection %d\n",
                    which));
  }

  transport->close_connection ();
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) shut down transport, handle %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_Transport *transport)
{
  // @@ How does this works with GIOP lite?

  TAO_GIOP::dump_msg ("send_error",
                      (const u_char *) error_message,
                      TAO_GIOP_HEADER_LEN);

  ACE_HANDLE which = transport->handle ();

  // @@ Carlos, can you please check to see if <send_n> should have
  // it's reply checked?
  if (transport->send ((const u_char *)error_message,
                       TAO_GIOP_HEADER_LEN) == -1)
  {
    if (TAO_orbdebug != 0)
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) error sending error to %d\n",
                  which));
  }

  if (TAO_orbdebug) {
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) aborted transport handle %d\n",
                transport->handle ()));
  }
  // @@ Why do we close the connection at this point?
  transport->close_connection ();
}

ssize_t
TAO_GIOP::read_buffer (TAO_Transport *transport,
                       char *buf,
                       size_t len)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_READ_BUFFER_START);

  ssize_t bytes_read = transport->recv (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?

// I am now making this call non-blocking. For reading the header it
// is not non-blocking. But for reading the rest of the message, it is
// non-blocking. Total size and the current offset of the incoming
// message is kept at the Transport class. Flag <block> is to force
// blocking for the full reply.  Useful <wait_on_Read> strategies.
TAO_GIOP::Message_Type
TAO_GIOP::recv_message (TAO_Transport *transport,
                        TAO_InputCDR &msg,
                        TAO_ORB_Core* orb_core,
                        TAO_GIOP_Version & /* version */,
                        int block)
{
  TAO_FUNCTION_PP_TIMEPROBE (TAO_GIOP_RECV_MESSAGE_START);

  // 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.

  // Default header length.
  ssize_t header_len = TAO_GIOP_HEADER_LEN;

  // @@ Alex&Carlos: we need to figure out what is the right value to
  //    initialize this thing...
  TAO_GIOP::Message_Type retval = TAO_GIOP::ShortRead;
  CORBA::ULong message_size;
  ssize_t len;
  char *header = 0;

  if (!transport->message_size ())
    {
      // This is the first read for this message.
      ACE_CDR::mb_align (&msg.start_);

      if (orb_core->orb_params ()->use_lite_protocol ())
        header_len = TAO_GIOP_LITE_HEADER_LEN;

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

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

      if (len != header_len)
        {
          switch (len)
            {
            case 0:
              if (TAO_orbdebug)
                ACE_DEBUG ((LM_DEBUG,
                            "TAO (%P|%t) GIOP::recv_message "
                            "end of connection, transport handle %d\n",
                            transport->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,
                            "TAO (%P|%t) GIOP::recv_message header %p\n",
                            "read_buffer"));
              break;
              /* NOTREACHED */

            default:
              if (TAO_orbdebug)
                ACE_DEBUG ((LM_ERROR,
                            "TAO (%P|%t) GIOP::recv_message header read failed, "
                            "only %d of %d bytes\n",
                            len,
                            header_len));
              break;
              /* NOTREACHED */
            }

          return TAO_GIOP::CommunicationError;
        }

      // NOTE: if message headers, or whole 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 (transport);
          // We didn't really receive anything useful here.
          return TAO_GIOP::CommunicationError;
        }

      // 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 (ACE_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);
      ACE_CDR::mb_align (&msg.start_);
      msg.start_.wr_ptr (header_len);
      msg.start_.wr_ptr (message_size);
      msg.start_.rd_ptr (header_len);

      // Keep the message_size with the Transport object.
      transport->message_size (message_size);
    }

  // Header is read already. Read the rest of this message into the
  // buffer.

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

  // @@ Handle the non-blocking case !!!. (Alex).
  len = TAO_GIOP::read_buffer (transport,
                               payload,
                               (size_t) transport->message_size () -
                               transport->message_offset ());

  if (len != (ssize_t) message_size)
    {
      switch (len)
        {
        case 0:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_DEBUG,
                        "(%t) End of connection, transport handle %d\n",
                        transport->handle ()));

          return TAO_GIOP::EndOfFile;
          /* NOTREACHED */

        case -1:
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_ERROR,
                        "(%P|%t) TAO_GIOP::recv_message - body %p\n",
                        "read_buffer"));
          break;
          /* NOTREACHED */

        default:
          // @@ This is ok in the non-blocking read. (Alex).
          if (TAO_orbdebug)
            ACE_DEBUG ((LM_ERROR,
                        "TAO: (%P|%t) GIOP::recv_message body read failed, "
                        "only %d of %d bytes\n",
                        len,
                        message_size));
          break;
          /* NOTREACHED */
        }
      return TAO_GIOP::CommunicationError;
    }


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

  transport->incr_message_offset (len);

  // If we have read the whole message, reset the states for the input
  // message.
  if (transport->message_size () == transport->message_offset ())
    {
      // Reset.
      transport->message_size (0);

      // Current message is received fully.
      transport->message_received (1);
    }

  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,
                  "TAO: (%P|%t) bad header, magic word [%c%c%c%c]\n",
                  header[0], header[1], header[2], header[3]));
      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,
                  "TAO: (%P|%t) bad header, version [%c%c]\n",
                  header[4], header[5]));
      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_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 &)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_LOCATE_REQUEST_HEADER_INIT_START);

  return (msg.read_ulong (this->request_id)
          && (msg >> this->object_key) != 0);
}

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_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 */