summaryrefslogtreecommitdiff
path: root/TAO/tao/GIOP.cpp
blob: a5ff8b35850f9d6c7dd33c0c6f2b4c993429cb0f (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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
// $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/GIOP_Server_Request.h"
#include "tao/CDR.h"
#include "tao/Pluggable.h"
#include "tao/debug.h"
#include "tao/ORB_Core.h"
#include "tao/POA.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::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 [] =
{
  "Request",
  "Reply",
  "CancelRequest",
  "LocateRequest",
  "LocateReply",
  "CloseConnection",
  "MessageError"
  "Fragment"
};

TAO_GIOP_MessageHeader::TAO_GIOP_MessageHeader (void)
  : byte_order (TAO_ENCAP_BYTE_ORDER),
    message_type (TAO_GIOP::MessageError),
    message_size (0)
{
  // Note that we need to use the ASCII values so the code will be
  // portable to platforms that use different character sets, such as
  // MVS (which uses EBCDIC).
  this->magic[0] = 0x47;   // 'G'
  this->magic[1] = 0x49;   // 'I'
  this->magic[2] = 0x4f;   // 'O'
  this->magic[3] = 0x50;   // 'P'

  giop_version.major =
    TAO_GIOP_MessageHeader::MY_MAJOR;
  giop_version.minor =
    TAO_GIOP_MessageHeader::MY_MINOR;
}

void
TAO_GIOP::dump_msg (const char *label,
                    const u_char *ptr,
                    size_t len)
{
  if (TAO_debug_level >= 5)
    {
      const char *message_name = "UNKNOWN MESSAGE";
      // @@ Carlos, where does the magic number '7' come from?!
      u_long slot = ptr[7];
      if (slot < sizeof (names)/sizeof(names[0]))
        message_name = names [slot];
      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",
                  message_name));

      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 >= 10)
        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::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);
}

CORBA::Boolean
TAO_GIOP::write_request_header (const TAO_GIOP_ServiceContextList& svc_ctx,
                                CORBA::ULong request_id,
                                CORBA::Boolean is_roundtrip,
                                const TAO_opaque& key,
                                const char *opname,
                                CORBA::Principal_ptr principal,
                                TAO_OutputCDR &msg,
                                TAO_ORB_Core *orb_core)
{
  if (orb_core->orb_params ()->use_lite_protocol ())
    return TAO_GIOP::write_request_header_lite (svc_ctx,
                                                request_id,
                                                is_roundtrip,
                                                key,
                                                opname,
                                                principal,
                                                msg);
  else
    return TAO_GIOP::write_request_header_std (svc_ctx,
                                               request_id,
                                               is_roundtrip,
                                               key,
                                               opname,
                                               principal,
                                               msg);
}

// Write the GIOP locate request header.

CORBA::Boolean
TAO_GIOP::write_locate_request_header (CORBA::ULong request_id,
                                       const TAO_opaque &key,
                                       TAO_OutputCDR &msg)
{
  msg << request_id;
  msg << key;

  return 1;
}

int
TAO_GIOP::send_message (TAO_Transport *transport,
                        TAO_OutputCDR &stream,
                        TAO_ORB_Core *orb_core)
{

  TAO_FUNCTION_PP_TIMEPROBE (TAO_GIOP_SEND_REQUEST_START);

  // Ptr to first buffer.
  char *buf = (char *) stream.buffer (); 

  // Length of all buffers.
  size_t total_len =
    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 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 -1;
  }

  // EOF.
  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 -1;
    }

  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] =
{
  // The following works on non-ASCII platforms, such as MVS (which
  // uses EBCDIC).
  0x47, // 'G'
  0x49, // 'I'
  0x4f, // 'O' 
  0x50, // '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] =
{
  // The following works on non-ASCII platforms, such as MVS (which
  // uses EBCDIC).
  0x47, // 'G'
  0x49, // 'I'
  0x4f, // 'O' 
  0x50, // '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,
                    "TAO (%P|%t) error sending error to %d\n",
                    which));
    }
}

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 <= 0 && TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "TAO (%P|%t) - %p,\n"
                "              transport = %d, "
                "bytes = %d, len = %d\n",
                "TAO_GIOP::read_buffer",
                transport->handle (),
                bytes_read,
                len));

  if (bytes_read == -1 && errno == ECONNRESET)
    {
      // @@ Is this OK??

      // 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;
}

void
TAO_GIOP_LocateRequestHeader::init (TAO_InputCDR &msg,
                                    CORBA::Environment &ACE_TRY_ENV)
{
  ACE_FUNCTION_TIMEPROBE (TAO_GIOP_LOCATE_REQUEST_HEADER_INIT_START);

  if (msg.read_ulong (this->request_id) == 0
      || (msg >> this->object_key) == 0)
    ACE_THROW (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE,
                               CORBA::COMPLETED_NO));
}

TAO_GIOP_ReplyStatusType
TAO_GIOP::convert_CORBA_to_GIOP_exception (CORBA::exception_type 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;
    }
}

int
TAO_GIOP::read_header (TAO_Transport *transport,
                       TAO_ORB_Core *orb_core,
                       TAO_GIOP_MessageHeader &header,
                       CORBA::ULong &header_size,
                       TAO_InputCDR &input)
{
  // Default header length.
  header_size = TAO_GIOP_HEADER_LEN;
  if (orb_core->orb_params ()->use_lite_protocol ())
    header_size = TAO_GIOP_LITE_HEADER_LEN;

  if (input.grow (header_size) == -1)
    return -1;

  // Read until all the header is received.  There should be no
  // problems with locking, the header is only a few bytes so they
  // should all be available on the socket, otherwise there is a
  // problem with the underlying transport, in which case we have more
  // problems than just this small loop.

  char *buf = input.rd_ptr ();
  size_t n;
  
  for (int t = header_size;
       t != 0;
       t -= n)
    {
      n = transport->recv (buf, t);
      if (n == -1)
        return -1;
      else if (n == 0 && errno != EWOULDBLOCK)
        return -1;
      buf += n;
    }

  if (TAO_GIOP::parse_header (orb_core, 
                              input,
                              header) == -1)
    return -1;
  else
    return header_size;
}

int
TAO_GIOP::handle_input (TAO_Transport *transport,
                        TAO_ORB_Core *orb_core,
                        TAO_GIOP_MessageHeader &header,
                        CORBA::ULong &current_offset,
                        TAO_InputCDR& input)
{
  if (header.message_size == 0)
    {
      current_offset = 0;
      CORBA::ULong header_size;
      if (TAO_GIOP::read_header (transport,
                                 orb_core,
                                 header,
                                 header_size,
                                 input) == -1)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n",
                        "TAO_GIOP::handle_input, read_header"));
          return -1;
        }

      if (input.grow (header_size +
                      header.message_size) == -1)
        // @@ Carlos, shouldn't we only print out an error message if
        // TAO_debug_level is > 0?
        ACE_ERROR_RETURN ((LM_ERROR,
                           "TAO (%P|%t) - %p\n",
                           "TAO_GIOP::handle_input, ACE_CDR::grow"),
                          -1);

      // Growing the buffer may have reset the rd_ptr(), but we want
      // to leave it just after the GIOP header (that was parsed
      // already);
      input.skip_bytes (header_size);
    }

  size_t missing_data =
    header.message_size - current_offset;
  ssize_t n =
    TAO_GIOP::read_buffer (transport,
                           input.rd_ptr () + current_offset,
                           missing_data);
  if (n == -1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - %p\n",
                    "TAO_GIOP::handle_input, read_buffer[1]"));
      return -1;
    }
  else if (n == 0)
    {
      if (errno == EWOULDBLOCK)
        return 0;
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - %p\n",
                    "TAO_GIOP::handle_input, read_buffer[2]"));
      return -1;
    }

  current_offset += n;

  if (current_offset == header.message_size)
    {
      if (TAO_debug_level >= 5)
        {
          size_t header_len = TAO_GIOP_HEADER_LEN;
          if (orb_core->orb_params ()->use_lite_protocol ())
            header_len = TAO_GIOP_LITE_HEADER_LEN;

          char *buf = input.rd_ptr ();
          buf -= header_len;
          size_t msg_len = input.length () + header_len;
          TAO_GIOP::dump_msg ("recv",
                              ACE_reinterpret_cast (u_char *,
                                                    buf),
                              msg_len);
        }
      return 1;
    }
  return 0;
}

int
TAO_GIOP::parse_reply (TAO_Transport *transport,
                       TAO_ORB_Core *orb_core,
                       TAO_InputCDR &input,
                       const TAO_GIOP_MessageHeader &header,
                       TAO_GIOP_ServiceContextList &reply_ctx,
                       CORBA::ULong &request_id,
                       CORBA::ULong &reply_status)
{
  switch (header.message_type)
    {
    case TAO_GIOP::Request:
      // In GIOP 1.0 and GIOP 1.1 this is an error, but it is
      // *possible* to receive requests in GIOP 1.2.  Don't handle
      // this on the firt iteration, leave it for the nearby future...
      // ERROR too.  @@ this->reply_handler_->error ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         "TAO (%P|%t) %N:%l handle_client_input: "
                         "request.\n"),
                        -1);

    case TAO_GIOP::CancelRequest:
    case TAO_GIOP::LocateRequest:
    case TAO_GIOP::CloseConnection:
    default:
      // @@ Errors for the time being.
      // @@ this->reply_handler_->error ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         "TAO (%P|%t) %N:%l handle_client_input: "
                         "wrong message.\n"),
                        -1);

    case TAO_GIOP::LocateReply:
    case TAO_GIOP::Reply:
      // Handle after the switch.
      break;
    }

  // For GIOP 1.0 and 1.1 the reply_ctx comes first:
  // @@ Use <header.giop_version> to make this work with GIOP 1.2
  if ((input >> reply_ctx) == 0)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) TAO_GIOP::parse_reply, "
                    "extracting context\n"));
      return -1;
    }

  // Read the request id
  if (!input.read_ulong (request_id))
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) : TAO_GIOP::parse_reply, "
                    "extracting request id"));
      return -1;
    }

  // and the reply status type.  status can be NO_EXCEPTION,
  // SYSTEM_EXCEPTION, USER_EXCEPTION, LOCATION_FORWARD or (on GIOP
  // 1.2) LOCATION_FORWARD_PERM
  if (!input.read_ulong (reply_status))
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) : TAO_GIOP::parse_reply, "
                    "extracting reply status\n"));
      return -1;
    }
  return 0;
}

// @@ Carlos, this method is rather large.  Is it possible to break it
// up a bit?
void
TAO_GIOP::process_server_message (TAO_Transport *transport,
                                  TAO_ORB_Core *orb_core,
                                  TAO_InputCDR &input,
                                  const TAO_GIOP_MessageHeader &header)
{
  char repbuf[ACE_CDR::DEFAULT_BUFSIZE];
#if defined(ACE_HAS_PURIFY)
  (void) ACE_OS::memset (repbuf,
                         '\0',
                         sizeof repbuf);
#endif /* ACE_HAS_PURIFY */
  TAO_OutputCDR output (repbuf, 
                        sizeof repbuf,
                        TAO_ENCAP_BYTE_ORDER,
                        orb_core->output_cdr_buffer_allocator (),
                        orb_core->output_cdr_dblock_allocator (),
                        orb_core->orb_params ()->cdr_memcpy_tradeoff (),
                        orb_core->to_iso8859 (),
                        orb_core->to_unicode ());

  CORBA::Boolean response_required = 0;
  CORBA::ULong request_id = 0;

  CORBA::Environment &ACE_TRY_ENV = TAO_default_environment ();
  ACE_TRY
    {
      TAO_MINIMAL_TIMEPROBE (TAO_SERVER_CONNECTION_HANDLER_RECEIVE_REQUEST_END);

      switch (header.message_type)
        {
        case TAO_GIOP::Request:
          // The following two routines will either raise an exception
          // or successfully write the response into <output>
          TAO_GIOP::process_server_request (transport,
                                            orb_core,
                                            input,
                                            output,
                                            response_required,
                                            request_id,
                                            ACE_TRY_ENV);
          ACE_TRY_CHECK;
          break;

        case TAO_GIOP::LocateRequest:
          TAO_GIOP::process_server_locate (transport,
                                           orb_core,
                                           input,
                                           output,
                                           response_required,
                                           request_id,
                                           ACE_TRY_ENV);
          ACE_TRY_CHECK;
          break;

          // These messages should never be sent to the server; it's an
          // error if the peer tries.  Set the environment accordingly,
          // as it's not yet been reported as an error.
        case TAO_GIOP::Reply:
        case TAO_GIOP::LocateReply:
        case TAO_GIOP::CloseConnection:
        default:   // Unknown message
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) Illegal message received by server\n"));
          TAO_GIOP::send_error (transport);
          break;

        case TAO_GIOP::MessageError:
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) MessageError received by server\n"));
          break;
        }
    }
  // Only CORBA exceptions are caught here.
  ACE_CATCHANY
    {
      if (response_required)
        {
          if (TAO_GIOP::send_reply_exception (transport,
                                              orb_core,
                                              request_id,
                                              &ACE_ANY_EXCEPTION) == -1)
            {
              if (TAO_debug_level > 0)
                ACE_ERROR ((LM_ERROR,
                            "TAO: (%P|%t) %p: cannot send exception\n",
                            "TAO_GIOP::process_server_message"));
              ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                                   "TAO: ");
            }

          return;
        }
      else if (TAO_debug_level > 0)
        {
          // It is unfotunate that an exception (probably a system
          // exception) was thrown by the upcall code (even by the
          // user) when the client was not expecting a response.
          // However, in this case, we cannot close the connection
          // down, since it really isn't the client's fault.

          ACE_ERROR ((LM_ERROR,
                      "(%P|%t) exception thrown "
                      "but client is not waiting a response\n"));
          ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                               "TAO: ");
        }
      return;
    }
#if defined (TAO_HAS_EXCEPTIONS)
  ACE_CATCHALL
    {
      // @@ TODO some c++ exception or another, but what do we do with
      //    it?
      // We are supposed to map it into a CORBA::UNKNOWN exception.
      // BTW, this cannot be detected if using the <env> mapping.  If
      // we have native exceptions but no support for them in the ORB
      // we should still be able to catch it.  If we don't have native
      // exceptions it couldn't have been raised in the first place!
      if (response_required)
        {
          CORBA::UNKNOWN exception 
            (CORBA::SystemException::minor_code_tao_ 
             (TAO_UNHANDLED_SERVER_CXX_EXCEPTION, 0),
             CORBA::COMPLETED_MAYBE);

          if (TAO_GIOP::send_reply_exception (transport,
                                              orb_core,
                                              request_id,
                                              &exception) == -1)
            {
              if (TAO_debug_level > 0)
                ACE_ERROR ((LM_ERROR,
                            "TAO: (%P|%t) %p: cannot send exception\n",
                            "TAO_GIOP::process_server_message"));
              ACE_PRINT_EXCEPTION (exception,
                                   "TAO: ");
            }
        }
      else if (TAO_debug_level > 0)
        // It is unfotunate that an exception (probably a system
        // exception) was thrown by the upcall code (even by the user)
        // when the client was not expecting a response.  However, in
        // this case, we cannot close the connection down, since it
        // really isn't the client's fault.
        ACE_ERROR ((LM_ERROR,
                    "(%P|%t) exception thrown "
                    "but client is not waiting a response\n"));
      return;
    }
#endif /* TAO_HAS_EXCEPTIONS */
  ACE_ENDTRY;

  if (response_required
      && TAO_GIOP::send_message (transport,
                                 output,
                                 orb_core) == -1
      && TAO_debug_level > 0)
    // No exception but some kind of error, yet a response is
    // required.
    ACE_ERROR ((LM_ERROR,
                "TAO: (%P|%t) %p: cannot send reply\n",
                "TAO_GIOP::process_server_message"));

  TAO_MINIMAL_TIMEPROBE (TAO_SERVER_CONNECTION_HANDLER_HANDLE_INPUT_END);
}

void
TAO_GIOP::process_server_request (TAO_Transport *transport,
                                  TAO_ORB_Core* orb_core,
                                  TAO_InputCDR &input,
                                  TAO_OutputCDR &output,
                                  CORBA::Boolean &response_required,
                                  CORBA::ULong &request_id,
                                  CORBA::Environment &ACE_TRY_ENV)
{
  // This will extract the request header, set <response_required> as
  // appropriate.
  TAO_GIOP_ServerRequest request (input,
                                  output,
                                  orb_core,
                                  ACE_TRY_ENV);
  ACE_CHECK;

  // The request_id_ field in request will be 0 if something went
  // wrong before it got a chance to read it out.
  request_id = request.request_id ();

  response_required = request.response_expected ();

#if !defined (TAO_NO_IOR_TABLE)
  const CORBA::Octet *object_key =
    request.object_key ().get_buffer ();

  if (ACE_OS::memcmp (object_key,
                      &TAO_POA::objectkey_prefix[0],
                      TAO_POA::TAO_OBJECTKEY_PREFIX_SIZE) != 0)
    {
      ACE_CString object_id (ACE_reinterpret_cast (const char *,
                                                   object_key),
                             request.object_key ().length (),
                             0,
                             0);

      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "Simple Object key %s. Doing the Table Lookup ...\n",
                    object_id.c_str ()));

      CORBA::Object_ptr object_reference;

      // Do the Table Lookup.
      int status =
        orb_core->orb ()->_tao_find_in_IOR_table (object_id,
                                                  object_reference);

      // If ObjectID not in table or reference is nil raise
      // OBJECT_NOT_EXIST.

      if (CORBA::is_nil (object_reference)
          || status == -1)
        ACE_THROW (CORBA::OBJECT_NOT_EXIST ());

      // ObjectID present in the table with an associated NON-NULL
      // reference.  Throw a forward request exception.

      CORBA::Object_ptr dup =
        CORBA::Object::_duplicate (object_reference);

      // @@ We could simply write the response at this point...
      ACE_THROW (PortableServer::ForwardRequest (dup));
    }

#endif /* TAO_NO_IOR_TABLE */

  orb_core->object_adapter ()->dispatch_servant (request.object_key (),
                                                 request,
                                                 0,
                                                 ACE_TRY_ENV);
  ACE_CHECK;
}

void
TAO_GIOP::process_server_locate (TAO_Transport *transport,
                                 TAO_ORB_Core* orb_core,
                                 TAO_InputCDR &input,
                                 TAO_OutputCDR &output,
                                 CORBA::Boolean &response_required,
                                 CORBA::ULong &request_id,
                                 CORBA::Environment &ACE_TRY_ENV)
{
  //  TAO_FUNCTION_PP_TIMEPROBE (TAO_SERVER_CONNECTION_HANDLER_HANDLE_LOCATE_START);

  // This will extract the request header, set <response_required> as
  // appropriate.
  TAO_GIOP_LocateRequestHeader locateRequestHeader;

  TAO_GIOP_LocateStatusType status;
  CORBA::Object_var forward_location_var;

  ACE_TRY
    {
      locateRequestHeader.init (input, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      request_id = locateRequestHeader.request_id;
      response_required = 1;

#if !defined (TAO_NO_IOR_TABLE)

      const CORBA::Octet *object_key =
        locateRequestHeader.object_key.get_buffer ();

      if (ACE_OS::memcmp (object_key,
                          &TAO_POA::objectkey_prefix[0],
                          TAO_POA::TAO_OBJECTKEY_PREFIX_SIZE) != 0)
        {
          ACE_CString object_id (ACE_reinterpret_cast (const char *,
                                                       object_key),
                                 locateRequestHeader.object_key.length (),
                                 0,
                                 0);

          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "Simple Object key %s. Doing the Table Lookup ...\n",
                        object_id.c_str ()));

          CORBA::Object_ptr object_reference;

          // Do the Table Lookup.
          int find_status =
            orb_core->orb ()->_tao_find_in_IOR_table (object_id,
                                                      object_reference);

          // If ObjectID not in table or reference is nil raise
          // OBJECT_NOT_EXIST.

          if (CORBA::is_nil (object_reference) 
              || find_status == -1)
            ACE_TRY_THROW (CORBA::OBJECT_NOT_EXIST ());

          // ObjectID present in the table with an associated NON-NULL
          // reference.  Throw a forward request exception.

          CORBA::Object_ptr dup =
            CORBA::Object::_duplicate (object_reference);

          // @@ We could simply write the response at this point...
          ACE_TRY_THROW (PortableServer::ForwardRequest (dup));
        }
#endif /* TAO_NO_IOR_TABLE */

      // Execute a fake request to find out if the object is there or
      // if the POA can activate it on demand...
      char repbuf[ACE_CDR::DEFAULT_BUFSIZE];
      TAO_OutputCDR dummy_output (repbuf,
                                  sizeof repbuf);
      // This output CDR is not used!

      TAO_ObjectKey tmp_key (locateRequestHeader.object_key.length (),
                             locateRequestHeader.object_key.length (),
                             locateRequestHeader.object_key.get_buffer (),
                             0);

      TAO_GIOP_ServerRequest serverRequest (locateRequestHeader.request_id,
                                            response_required,
                                            tmp_key,
                                            "_non_existent",
                                            dummy_output,
                                            orb_core,
                                            ACE_TRY_ENV);

      orb_core->object_adapter ()->dispatch_servant (serverRequest.object_key (),
                                                     serverRequest,
                                                     0,
                                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (serverRequest.exception_type () == TAO_GIOP_NO_EXCEPTION)
        {
          // We got no exception, so the object is here.
          status = TAO_GIOP_OBJECT_HERE;
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO: (%P|%t) handle_locate() : found\n"));
        }
      else if (serverRequest.exception_type () != TAO_GIOP_NO_EXCEPTION)
        {
          forward_location_var = serverRequest.forward_location ();
          if (!CORBA::is_nil (forward_location_var.in ()))
            {
              status = TAO_GIOP_OBJECT_FORWARD;
              ACE_DEBUG ((LM_DEBUG,
                          "handle_locate has been called: forwarding\n"));
            }
          else
            {
              // Normal exception, so the object is not here
              status = TAO_GIOP_UNKNOWN_OBJECT;
              ACE_DEBUG ((LM_DEBUG,
                          "handle_locate has been called: not here\n"));
            }
        }

    }
#if !defined (TAO_HAS_MINIMUM_CORBA)
  ACE_CATCH (PortableServer::ForwardRequest, forward_request)
    {
      status = TAO_GIOP_OBJECT_FORWARD;
      forward_location_var =
        forward_request.forward_reference;
      ACE_DEBUG ((LM_DEBUG,
                  "handle_locate has been called: forwarding\n"));
    }
#endif /* TAO_HAS_MINIMUM_CORBA */
  ACE_CATCHANY
    {
      // Normal exception, so the object is not here
      status = TAO_GIOP_UNKNOWN_OBJECT;
      ACE_DEBUG ((LM_DEBUG,
                  "handle_locate has been called: not here\n"));
    }
  ACE_ENDTRY;

  // Create the response.
  TAO_GIOP::start_message (TAO_GIOP::LocateReply,
                           output,
                           orb_core);
  output.write_ulong (locateRequestHeader.request_id);
  output.write_ulong (status);

  if (status == TAO_GIOP_OBJECT_FORWARD)
    {
      CORBA::Object_ptr object_ptr = forward_location_var.in ();
      if ((output << object_ptr) == 0)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "Server_Connection_Handler::handle_locate - "
                        "error marshaling forwarded reference\n"));
          ACE_THROW (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE,
                                     CORBA::COMPLETED_YES));
        }
    }
}

int
TAO_GIOP::send_reply_exception (TAO_Transport *transport,
                                TAO_ORB_Core* orb_core,
                                CORBA::ULong request_id,
                                CORBA::Exception *x)
{
  // Create a new output CDR stream

  char repbuf[ACE_CDR::DEFAULT_BUFSIZE];
#if defined(ACE_HAS_PURIFY)
  (void) ACE_OS::memset (repbuf,
                         '\0',
                         sizeof repbuf);
#endif /* ACE_HAS_PURIFY */
  TAO_OutputCDR output (repbuf, 
                        sizeof repbuf,
                        TAO_ENCAP_BYTE_ORDER,
                        orb_core->output_cdr_buffer_allocator (),
                        orb_core->output_cdr_dblock_allocator (),
                        orb_core->orb_params ()->cdr_memcpy_tradeoff (),
                        orb_core->to_iso8859 (),
                        orb_core->to_unicode ());

  // Construct a REPLY header.
  TAO_GIOP::start_message (TAO_GIOP::Reply,
                           output,
                           orb_core);

  // A new try/catch block, but if something goes wrong now we have no
  // hope, just abort.
  ACE_TRY_NEW_ENV
    {
      // create and write a dummy context
      TAO_GIOP_ServiceContextList resp_ctx;
      resp_ctx.length (0);
      output << resp_ctx;

      // Write the request ID
      output.write_ulong (request_id);

#if !defined (TAO_HAS_MINIMUM_CORBA)

      // @@ TODO This is the place to conditionally compile
      // forwarding. It certainly seems easy to strategize too, just
      // invoke an strategy to finish marshalling the response.

      // Now we check for Forwarding ***************************

      // Try to narrow to ForwardRequest
      PortableServer::ForwardRequest_ptr forward_request_ptr =
        PortableServer::ForwardRequest::_narrow (x);

      // If narrowing of exception succeeded
      if (forward_request_ptr != 0
          && !CORBA::is_nil (forward_request_ptr->forward_reference.in ()))
        {
          // write the reply_status
          output.write_ulong (TAO_GIOP_LOCATION_FORWARD);

          // write the object reference into the stream
          CORBA::Object_ptr object_ptr =
            forward_request_ptr->forward_reference.in();

          output << object_ptr;
        }
      // end of the forwarding code ****************************
      else

#endif /* TAO_HAS_MINIMUM_CORBA */
        {
          // Write the exception
          CORBA::TypeCode_ptr except_tc = x->_type ();

          CORBA::exception_type extype =
            CORBA::USER_EXCEPTION;

          if (CORBA::SystemException::_narrow (x) != 0)
            extype = CORBA::SYSTEM_EXCEPTION;

          // write the reply_status
          output.write_ulong (TAO_GIOP::convert_CORBA_to_GIOP_exception (extype));

          // @@ Any way to implement this without interpretive
          //    marshaling???
          output.encode (except_tc,
                         x,
                         0,
                         ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
    }
  ACE_CATCH (CORBA_Exception, ex)
    {
      // Now we know that while handling the error an other error
      // happened -> no hope, close connection.

      // Close the handle.
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) cannot marshal exception %p\n",
                  transport->handle (),
                  "TAO_GIOP::send_reply_exception"));
      return -1;
    }
  ACE_ENDTRY;

  return TAO_GIOP::send_message (transport,
                                 output,
                                 orb_core);
}

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[] =
  {
    // The following works on non-ASCII platforms, such as MVS (which
    // uses EBCDIC).
    0x47, // 'G'
    0x49, // 'I'
    0x4f, // 'O' 
    0x50, // '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...
  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::write_request_header_std (const TAO_GIOP_ServiceContextList& svc_ctx,
                                    CORBA::ULong request_id,
                                    CORBA::Boolean is_roundtrip,
                                    const TAO_opaque& key,
                                    const char* opname,
                                    CORBA::Principal_ptr principal,
                                    TAO_OutputCDR &out_stream)
{
  out_stream << svc_ctx;
  out_stream << request_id;
  out_stream << CORBA::Any::from_boolean (is_roundtrip);
  out_stream << key;
  out_stream << opname;
  out_stream << principal;
  return 1;
}

CORBA::Boolean
TAO_GIOP::write_request_header_lite (const TAO_GIOP_ServiceContextList&,
                                     CORBA::ULong request_id,
                                     CORBA::Boolean is_roundtrip,
                                     const TAO_opaque &key,
                                     const char* opname,
                                     CORBA::Principal_ptr,
                                     TAO_OutputCDR &out_stream)
{
  out_stream << request_id;
  out_stream << CORBA::Any::from_boolean (is_roundtrip);
  out_stream << key;
  out_stream << opname;
  return 1;
}

int
TAO_GIOP::parse_header (TAO_ORB_Core *orb_core,
                        TAO_InputCDR &input,
                        TAO_GIOP_MessageHeader& header)
{
  if (orb_core->orb_params ()->use_lite_protocol ())
    return TAO_GIOP::parse_header_lite (input,
                                        header);
  else
    return TAO_GIOP::parse_header_std (input,
                                       header);
}

int
TAO_GIOP::parse_header_std (TAO_InputCDR &input,
                            TAO_GIOP_MessageHeader &header)
{
  char *buf = input.rd_ptr ();

  // The values are hard-coded to support non-ASCII platforms.
  if (!(buf [0] == 0x47      // 'G'
        && buf [1] == 0x49   // 'I'
        && buf [2] == 0x4f   // 'O'
        && buf [3] == 0x50)) // 'P'
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) bad header, magic word [%c%c%c%c]\n",
                    buf[0],
                    buf[1],
                    buf[2],
                    buf[3]));
      return -1;
    }

#if 0
  // @@ Nobody uses this magic number, no sense in wasting time here.
  header.magic[0] = 0x47; // 'G'
  header.magic[1] = 0x49; // 'I'
  header.magic[2] = 0x4f; // 'O'
  header.magic[3] = 0x50; // 'P'
#endif /* 0 */
  header.giop_version.major = buf[4];
  header.giop_version.minor = buf[5];
  header.byte_order = buf[6];
  header.message_type = buf[7];

  input.reset_byte_order (header.byte_order);
  input.skip_bytes (TAO_GIOP_MESSAGE_SIZE_OFFSET);
  input.read_ulong (header.message_size);

  if (TAO_debug_level > 2)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "TAO (%P|%t) Parsed header = <%d,%d,%d,%d,%d>\n",
                  header.giop_version.major,
                  header.giop_version.minor,
                  header.byte_order,
                  header.message_type,
                  header.message_size));
    }
  return 0;
}

int
TAO_GIOP::parse_header_lite (TAO_InputCDR &input,
                             TAO_GIOP_MessageHeader &header)
{
  char *buf = input.rd_ptr ();

#if 0
  // @@ Nobody uses this magic number, no sense in wasting time here.
  header.magic[0] = 0x47; // 'G'
  header.magic[1] = 0x49; // 'I'
  header.magic[2] = 0x4f; // 'O'
  header.magic[3] = 0x50; // 'P'
#endif /* 0 */
  header.giop_version.major = 1;
  header.giop_version.minor = 0;
  header.byte_order = TAO_ENCAP_BYTE_ORDER;
  header.message_type = buf[4];

  input.reset_byte_order (header.byte_order);
  input.read_ulong (header.message_size);
  return 0;
}

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