summaryrefslogtreecommitdiff
path: root/ACE/apps/JAWS3/jaws3/Asynch_IO.cpp
blob: 0113e73da39a0751fec93e7e4478d8c444cffc5f (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
// $Id$

#ifndef JAWS_BUILD_DLL
#define JAWS_BUILD_DLL
#endif /*JAWS_BUILD_DLL*/

#include "ace/config-lite.h"

#include "jaws3/IO.h"
#include "jaws3/Asynch_IO.h"
#include "jaws3/Event_Completer.h"
#include "jaws3/Event_Result.h"

#if defined (ACE_HAS_AIO_CALLS) || defined (ACE_WIN32)

#include "jaws3/Asynch_IO_Helpers.h"

void
JAWS_Asynch_IO::send ( ACE_HANDLE handle
                     , ACE_Message_Block *mb
                     , JAWS_Event_Completer *completer
                     , void *act
                     )
{
  JAWS_EC_AH_Adapter *jecaha;
  jecaha = JAWS_EC_AH_Adapter::make (completer);

  ACE_Asynch_Write_Stream asynch_write_stream;

  if (jecaha == 0
      || asynch_write_stream.open (*jecaha, handle) == -1
      || asynch_write_stream.write (*mb, mb->length (), act) == -1)
    {
      delete jecaha;
      JAWS_Event_Result io_result ( 0
                                  , JAWS_Event_Result::JE_ERROR
                                  , JAWS_Event_Result::JE_SEND_FAIL
                                  );

      if (completer)
        completer->output_complete (io_result, act);
    }
}


void
JAWS_Asynch_IO::recv ( ACE_HANDLE handle
                     , ACE_Message_Block *mb
                     , JAWS_Event_Completer *completer
                     , void *act
                     )
{
  JAWS_EC_AH_Adapter *jecaha;
  jecaha = JAWS_EC_AH_Adapter::make (completer);

  ACE_Asynch_Read_Stream asynch_read_stream;

  if (jecaha == 0
      || asynch_read_stream.open (*jecaha, handle) == -1
      || asynch_read_stream.read (*mb, mb->space (), act) == -1)
    {
      delete jecaha;
      JAWS_Event_Result io_result ( 0
                                  , JAWS_Event_Result::JE_ERROR
                                  , JAWS_Event_Result::JE_RECV_FAIL
                                  );

      if (completer)
        completer->output_complete (io_result, act);
    }
}


void
JAWS_Asynch_IO::transmit ( ACE_HANDLE handle
                         , ACE_HANDLE source
                         , JAWS_Event_Completer *completer
                         , void *act
                         , ACE_Message_Block *header
                         , ACE_Message_Block *trailer
                         )
{
  JAWS_EC_AH_Adapter *jecaha;
  jecaha = JAWS_EC_AH_Adapter::make (completer);

  ACE_Asynch_Transmit_File::Header_And_Trailer *header_and_trailer = 0;
  header_and_trailer =
    new ACE_Asynch_Transmit_File::Header_And_Trailer ( header
                                                     , header->length ()
                                                     , trailer
                                                     , trailer->length ()
                                                     );

  ACE_Asynch_Transmit_File asynch_transmit_file;

  if (source == ACE_INVALID_HANDLE
      || jecaha == 0
      || header_and_trailer == 0
      || asynch_transmit_file.open (*jecaha, handle) == -1
      || asynch_transmit_file.transmit_file ( source
                                            , header_and_trailer
                                            , 0
                                            , 0
                                            , 0
                                            , 0
                                            , 0
                                            , act
                                            ) == -1)
    {
      delete jecaha;
      delete header_and_trailer;
      JAWS_Event_Result io_result ( 0
                                  , JAWS_Event_Result::JE_ERROR
                                  , JAWS_Event_Result::JE_TRANSMIT_FAIL
                                  );

      if (completer)
        completer->output_complete (io_result, act);
    }
}



JAWS_EC_AH_Adapter *
JAWS_EC_AH_Adapter::make (JAWS_Event_Completer *completer)
{
  return new JAWS_EC_AH_Adapter (completer);
}

void
JAWS_EC_AH_Adapter
::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  JAWS_Event_Result io_result;

  io_result = this->make_io_result ( result
                                   , JAWS_Event_Result::JE_RECV_OK
                                   , JAWS_Event_Result::JE_RECV_FAIL
                                   );
  // More useful diagnostics not implemented yet.

  void *act = const_cast<void *> (result.act ());

  this->completer_->input_complete (io_result, act);
  delete this;
}

void
JAWS_EC_AH_Adapter
::handle_write_stream (const ACE_Asynch_Write_Stream::Result &result)
{
  JAWS_Event_Result io_result;

  io_result = this->make_io_result ( result
                                   , JAWS_Event_Result::JE_SEND_OK
                                   , JAWS_Event_Result::JE_SEND_FAIL
                                   );
  // More useful diagnostics not implemented yet.

  void *act = const_cast<void *> (result.act ());

  this->completer_->output_complete (io_result, act);
  delete this;
}

void
JAWS_EC_AH_Adapter
::handle_transmit_file (const ACE_Asynch_Transmit_File::Result &result)
{
  JAWS_Event_Result io_result;

  io_result = this->make_io_result ( result
                                   , JAWS_Event_Result::JE_TRANSMIT_OK
                                   , JAWS_Event_Result::JE_TRANSMIT_FAIL
                                   );
  // More useful diagnostics not implemented yet.
  // Watch out for files not opened in overlapped IO mode.

  void *act = const_cast<void *> (result.act ());

  this->completer_->output_complete (io_result, act);
  delete this;
}

JAWS_Event_Result
JAWS_EC_AH_Adapter
::make_io_result ( const ACE_Asynch_Result &result
                 , JAWS_Event_Result::JE_REASON reason_ok
                 , JAWS_Event_Result::JE_REASON reason_fail
                 )
{
  size_t bytes = result.bytes_transferred ();

  JAWS_Event_Result::JE_STATUS status;
  JAWS_Event_Result::JE_REASON reason;

  if (result.success ())
    {
      status = JAWS_Event_Result::JE_OK;
      reason = reason_ok;
    }
  else
    {
      status = JAWS_Event_Result::JE_ERROR;
      reason = reason_fail;
    }

  JAWS_Event_Result io_result (bytes, status, reason);

  return io_result;
}

#else /* EMULATE AIO WITH REACTOR */

#include "jaws3/Reactive_IO.h"

void
JAWS_Asynch_IO::send ( ACE_HANDLE handle
                     , ACE_Message_Block *mb
                     , JAWS_Event_Completer *completer
                     , void *act
                     )
{
  JAWS_Reactive_IO::instance ()->send (handle, mb, completer, act);
}


void
JAWS_Asynch_IO::recv ( ACE_HANDLE handle
                     , ACE_Message_Block *mb
                     , JAWS_Event_Completer *completer
                     , void *act
                     )
{
  JAWS_Reactive_IO::instance ()->recv (handle, mb, completer, act);
}


void
JAWS_Asynch_IO::transmit ( ACE_HANDLE handle
                         , ACE_HANDLE source
                         , JAWS_Event_Completer *completer
                         , void *act
                         , ACE_Message_Block *header
                         , ACE_Message_Block *trailer
                         )
{
  JAWS_Reactive_IO::instance ()->transmit ( handle
                                          , source
                                          , completer
                                          , act
                                          , header
                                          , trailer
                                          );
}

#endif /* defined (ACE_HAS_AIO_CALLS) || defined (ACE_WIN32) */

// For now, we will simulate timed Asynch IO with timed Reactive IO.
// In the future, we will implement the timed Asynch IO with timers
// and Asynch IO cancelation.

#include "jaws3/Reactive_IO.h"

void
JAWS_Asynch_IO::send ( ACE_HANDLE handle
                     , ACE_Message_Block *mb
                     , JAWS_Event_Completer *completer
                     , const ACE_Time_Value &tv
                     , void *act
                     )
{
  JAWS_Reactive_IO::instance ()->send (handle, mb, completer, tv, act);
}


void
JAWS_Asynch_IO::recv ( ACE_HANDLE handle
                     , ACE_Message_Block *mb
                     , JAWS_Event_Completer *completer
                     , const ACE_Time_Value &tv
                     , void *act
                     )
{
  JAWS_Reactive_IO::instance ()->recv (handle, mb, completer, tv, act);
}


void
JAWS_Asynch_IO::transmit ( ACE_HANDLE handle
                         , ACE_HANDLE source
                         , JAWS_Event_Completer *completer
                         , const ACE_Time_Value &tv
                         , void *act
                         , ACE_Message_Block *header
                         , ACE_Message_Block *trailer
                         )
{
  JAWS_Reactive_IO::instance ()->transmit ( handle
                                          , source
                                          , completer
                                          , tv
                                          , act
                                          , header
                                          , trailer
                                          );
}