summaryrefslogtreecommitdiff
path: root/TAO/tao/Strategies/GIOP_Message_NonReactive_Handler.cpp
blob: 7f3b35b9f62de62688f40713e5d1a54a87fa3539 (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
#include "GIOP_Message_NonReactive_Handler.h"
#include "GIOP_Message_NonReactive_Base.h"
#include "tao/ORB_Core.h"
#include "tao/CDR.h"
#include "tao/debug.h"
#include "tao/Transport.h"

#if !defined (__ACE_INLINE__)
# include "GIOP_Message_NonReactive_Handler.inl"
#endif /* __ACE_INLINE__ */

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


TAO_GIOP_Message_NonReactive_Handler::TAO_GIOP_Message_NonReactive_Handler (TAO_ORB_Core *orb_core,
                                                                            TAO_GIOP_Message_NonReactive_Base *mesg_base,
                                                                            size_t cdr_size)
  : TAO_GIOP_Message_Reactive_Handler (orb_core,
                                       mesg_base,
                                       cdr_size),
    mesg_base_ (mesg_base),
    input_cdr_ (orb_core->data_block_for_message_block (cdr_size))
{
}

int
TAO_GIOP_Message_NonReactive_Handler::read_parse_message(TAO_Transport *transport,
                                                         ACE_Time_Value *max_wait_time)
{
  // Reset the input_cdr before we do anything with it
  this->input_cdr_.reset_contents ();

  // Before we do this let us reset the
  char *buf = this->input_cdr_.rd_ptr ();

  // This is of the following pieces:
  //  - We first read the GIOP header and parse the information. We
  //    also get the size of the incoming message. We grow the size of
  //    the CDR if needed
  //  - We read the information/message in toto.
  if (this->read_message (transport,
                          buf,
                          TAO_GIOP_MESSAGE_HEADER_LEN,
                          max_wait_time) == -1)
    {
      if (TAO_debug_level > 1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%P|%t) : (%N | %l) \n")
                    ACE_TEXT ("Error during header read \n")));
        return -1;
    }

  // Parse the message header
  if (this->parse_message_header_i (buf) == -1)
    {
      if (TAO_debug_level > 2)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%P|%t) : (%N|%l)\n")
                      ACE_TEXT ("Error while parsing GIOP header \n")));
        }
      return -1;
    }
  // Reset the byte order now
  this->input_cdr_.reset_byte_order (this->message_state_.byte_order);

  // Calculate the message size to grow the CDR
  size_t msg_size =
    this->message_state_.message_size +
    TAO_GIOP_MESSAGE_HEADER_LEN;

  // With GIOP 1.2 we should get a fragment header
  if (this->message_state_.more_fragments &&
      this->message_state_.giop_version.minor == 2)
    {
      msg_size += TAO_GIOP_MESSAGE_FRAGMENT_HEADER;
    }

  // Grow the CDR
  if (this->input_cdr_.grow (msg_size) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%P|%t) Failure to grow the CDR buffers \n")
                  ACE_TEXT ("(%P|%t) to the right size")));
      return -1;
    }

  // Growing the CDR would have reset our read pointer, so set it back
  // to the right position
  if (this->input_cdr_.skip_bytes (TAO_GIOP_MESSAGE_HEADER_LEN) == 0)
    {
      if (TAO_debug_level> 3)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%P|%t) Failure to skip the right bytes  \n")
                    ACE_TEXT ("(%P|%t) in the CDR stream")));
      return -1;
    }

  buf = this->input_cdr_.rd_ptr ();

  // Read the rest of the message
  if (this->read_message (transport,
                          buf,
                          msg_size - TAO_GIOP_MESSAGE_HEADER_LEN,
                          max_wait_time) == -1)
    {
      if (TAO_debug_level > 1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%P|%t) : (%N | %l) \n")
                    ACE_TEXT ("Error during message read \n")));
        return -1;
    }

  // Parse the GIOP fragment header.
  if (this->parse_fragment_header (this->input_cdr_.rd_ptr (),
                                   this->input_cdr_.length ()))
    {
      // We had a GIOP fragment header, so skip few more bytes.
      if (this->input_cdr_.skip_bytes (TAO_GIOP_MESSAGE_FRAGMENT_HEADER) == 0)
        {
          if (TAO_debug_level> 3)
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P|%t) Failure to skip the right bytes  \n")
                        ACE_TEXT ("(%P|%t) in the CDR stream [2]")));
          return -1;
        }
    }

  // Now we have parsed almost every thing that is part of the GIOP
  // header. Return a succes value

  return 0;
}


int
TAO_GIOP_Message_NonReactive_Handler::read_message (TAO_Transport *transport,
                                                    char *buf,
                                                    size_t size,
                                                    ACE_Time_Value *max_wait_time)
{
  ssize_t bytes = 0;

  // As this used for transports where things are available in one
  // shot this looping should not create any problems.
  for (size_t n = size;
       n != 0;
       n -= bytes)
    {
      // We would have liked to use something like a recv_n ()
      // here. But at the time when the code was written, the MEM_Stream
      // classes had poor support  for recv_n (). Till a day when we
      // get proper recv_n (), let us stick with this. The other
      // argument that can be said against this is that, this is the
      // bad layer in which this is being done ie. recv_n is
      // simulated. But...
      bytes = transport->recv (buf,
                               n,
                               max_wait_time);

      // @@ Do we need to check for errno != EWOULDBLOCK?? and errno ==
      // @@ ECONNRESET. Does such things make sense here??
      if (bytes == 0 ||
          bytes == -1)
        return bytes;
      buf += bytes;
    }

  return 1;
}