summaryrefslogtreecommitdiff
path: root/ace/Proactor.cpp
blob: 7a76cfa55f8c269108a1355a49cccc29bd30a4b3 (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
// Proactor.cpp
// $Id: Proactor.cpp,v

#define ACE_BUILD_DLL
#include "ace/Proactor.h"

#if defined (ACE_WIN32)
// This only works on Win32 platforms

#include "ace/Timer_Queue.h"
#include "ace/Log_Msg.h"
#include "ace/Asynch_IO.h"

#if !defined (__ACE_INLINE__)
#include "ace/Proactor.i"
#endif /* __ACE_INLINE__ */

ACE_Proactor::ACE_Proactor (size_t number_of_threads, 
			    ACE_Timer_Queue *tq)
  : completion_port_ (0), // This *MUST* be 0, *NOT* ACE_INVALID_HANDLE!!!!
    number_of_threads_ (number_of_threads)
{
  this->completion_port_ = ::CreateIoCompletionPort (INVALID_HANDLE_VALUE,						     
						     this->completion_port_,
						     0,
						     this->number_of_threads_);
  if (this->completion_port_ == 0)
    ACE_ERROR ((LM_ERROR, "%p\n", "CreateIoCompletionPort"));
}

ACE_Proactor::~ACE_Proactor (void)
{
  this->close ();
}

int 
ACE_Proactor::close (void)
{
  // Close the completion port
  if (this->completion_port_ != 0)
    {
      int result = ACE_OS::close (this->completion_port_);
      this->completion_port_ = 0;
      return result;
    }
  else
    return 0;
}

int 
ACE_Proactor::register_handle (ACE_HANDLE handle, 
			       const void *completion_key)
{
  // No locking is needed here as no state changes
  ACE_HANDLE cp = ::CreateIoCompletionPort (handle,
					    this->completion_port_,
					    (u_long) completion_key,
					    this->number_of_threads_);
  if (cp == 0)
    {
      errno = ::GetLastError ();
      // If errno == ERROR_INVALID_PARAMETER, then this handle was
      // already registered.
      if (errno != ERROR_INVALID_PARAMETER)
	ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "CreateIoCompletionPort"), -1);
    }
  return 0;
}

int 
ACE_Proactor::handle_events (ACE_Time_Value &wait_time)
{
  return 0;
}

int 
ACE_Proactor::handle_events (void)
{
  OVERLAPPED *overlapped = 0;
  u_long bytes_transferred = 0;
  u_long completion_key = 0;

  // Get the next asynchronous operation that completes
  BOOL result = ::GetQueuedCompletionStatus (this->completion_port_,
					     &bytes_transferred,
					     &completion_key,
					     &overlapped,
					     INFINITE);


  if (result == FALSE && overlapped == 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "GetQueuedCompletionStatus"), -1);
  else
    {
      // Narrow result
      ACE_Asynch_Result *asynch_result = (ACE_Asynch_Result *) overlapped;
      // If errors happen, grab the error
      if (result == FALSE)
	errno = ::GetLastError ();
      
      this->application_specific_code (asynch_result,
				       bytes_transferred,
				       result,
				       (void *) completion_key,
				       errno);
    }
  return 0;
}

void
ACE_Proactor::application_specific_code (ACE_Asynch_Result *asynch_result,
					 u_long bytes_transferred,
					 int success,
					 const void *completion_key,
					 u_long error)
{
  ACE_SEH_TRY
    {
      // Call completion hook
      asynch_result->complete (bytes_transferred,
			       success,
			       (void *) completion_key,
			       errno);
    }
  ACE_SEH_FINALLY
    {
      // This is crucial to prevent memory leaks
      delete asynch_result;
    }
}

int 
ACE_Proactor::run_proactor_event_loop (void)
{
  return 0;
}

int 
ACE_Proactor::run_event_loop (ACE_Time_Value &)
{
  return 0;
}

int 
ACE_Proactor::end_event_loop (void)
{
  return 0;
}

sig_atomic_t 
ACE_Proactor::event_loop_done (void)
{
  return 0;
}

int 
ACE_Proactor::wake_up_dispatch_threads (void)
{
  return 0;
}

int 
ACE_Proactor::close_dispatch_threads (int)
{
  return 0;
}

size_t 
ACE_Proactor::number_of_threads (void) const
{
  return this->number_of_threads_;
}

void 
ACE_Proactor::number_of_threads (size_t threads)
{
  this->number_of_threads_ = threads;
}

#endif /* ACE_WIN32 */