summaryrefslogtreecommitdiff
path: root/ACE/ace/MEM_Acceptor.cpp
blob: 27bc04ab4d05238ab8f8791a0b12d9373488789b (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
#include "ace/MEM_Acceptor.h"
#include "ace/Lib_Find.h"

#if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)

#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/OS_NS_unistd.h"

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



ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_ALLOC_HOOK_DEFINE(ACE_MEM_Acceptor)

void
ACE_MEM_Acceptor::dump () const
{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_MEM_Acceptor::dump");
#endif /* ACE_HAS_DUMP */
}

// Do nothing routine for constructor.

ACE_MEM_Acceptor::ACE_MEM_Acceptor ()
  : mmap_prefix_ (0),
    malloc_options_ (ACE_DEFAULT_BASE_ADDR, 0),
    preferred_strategy_ (ACE_MEM_IO::Reactive)
{
  ACE_TRACE ("ACE_MEM_Acceptor::ACE_MEM_Acceptor");
}

ACE_MEM_Acceptor::~ACE_MEM_Acceptor ()
{
  ACE_TRACE ("ACE_MEM_Acceptor::~ACE_MEM_Acceptor");
#if defined (ACE_HAS_ALLOC_HOOKS)
  ACE_Allocator::instance()->free(this->mmap_prefix_);
#else
  delete[] this->mmap_prefix_;
#endif /* ACE_HAS_ALLOC_HOOKS */
}

// General purpose routine for performing server ACE_SOCK creation.

ACE_MEM_Acceptor::ACE_MEM_Acceptor (const ACE_MEM_Addr &remote_sap,
                                    int reuse_addr,
                                    int backlog,
                                    int protocol)
  : mmap_prefix_ (0),
    malloc_options_ (ACE_DEFAULT_BASE_ADDR, 0),
    preferred_strategy_ (ACE_MEM_IO::Reactive)
{
  ACE_TRACE ("ACE_MEM_Acceptor::ACE_MEM_Acceptor");
  if (this->open (remote_sap,
                  reuse_addr,
                  backlog,
                  protocol) == -1)
    ACELIB_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_MEM_Acceptor::ACE_MEM_Acceptor")));
}

int
ACE_MEM_Acceptor::open (const ACE_MEM_Addr &remote_sap,
                        int reuse_addr,
                        int back_log,
                        int protocol)
{
  ACE_TRACE ("ACE_MEM_Acceptor::open");
  return this->ACE_SOCK_Acceptor::open (remote_sap.get_local_addr (),
                                        reuse_addr,
                                        PF_INET,
                                        back_log,
                                        protocol);
}

// General purpose routine for accepting new connections.

int
ACE_MEM_Acceptor::accept (ACE_MEM_Stream &new_stream,
                          ACE_MEM_Addr *remote_sap,
                          ACE_Time_Value *timeout,
                          bool restart,
                          bool reset_new_handle)
{
  ACE_TRACE ("ACE_MEM_Acceptor::accept");

  int in_blocking_mode = 1;
  if (this->shared_accept_start (timeout,
                                 restart,
                                 in_blocking_mode) == -1)
    return -1;
  else
    {
      sockaddr *addr = 0;
      struct sockaddr_in inet_addr;
      int *len_ptr = 0;
      int len = 0;

      if (remote_sap != 0)
        {
          addr = reinterpret_cast<sockaddr *> (&inet_addr);
          len = sizeof (inet_addr);
          len_ptr = &len;
        }

      do
        // On Win32 the third parameter to <accept> must be a NULL
        // pointer if to ignore the client's address.
        new_stream.set_handle (ACE_OS::accept (this->get_handle (),
                                               addr,
                                               len_ptr));
      while (new_stream.get_handle () == ACE_INVALID_HANDLE
             && restart != 0
             && errno == EINTR
             && timeout == 0);

      if (remote_sap != 0)
        {
          ACE_INET_Addr temp (&inet_addr, len);
          remote_sap->set_port_number (temp.get_port_number ());
        }
    }

  if (this->shared_accept_finish (new_stream,
                                  in_blocking_mode,
                                  reset_new_handle) == -1)
    return -1;

  // Allocate 2 * MAXPATHLEN so we can accomodate the unique
  // name that gets appended later
  ACE_TCHAR buf [2 * MAXPATHLEN + 1];

  ACE_INET_Addr local_addr;
  if (new_stream.get_local_addr (local_addr) == -1)
    return -1;

  if (this->mmap_prefix_ != 0)
    {
      ACE_OS::snprintf (buf, sizeof buf / sizeof buf[0],
                        ACE_TEXT ("%s_%d_"),
                        this->mmap_prefix_,
                        local_addr.get_port_number ());
    }
  else
    {
      ACE_TCHAR name[25];
      // - 24 is so we can append name to the end.
      if (ACE::get_temp_dir (buf, MAXPATHLEN - 24) == -1)
        {
          ACELIB_ERROR ((LM_ERROR,
                      ACE_TEXT ("Temporary path too long, ")
                      ACE_TEXT ("defaulting to current directory\n")));
          buf[0] = 0;
        }

      ACE_OS::snprintf (name, 25,
                        ACE_TEXT ("MEM_Acceptor_%d_"),
                        local_addr.get_port_number ());
      ACE_OS::strcat (buf, name);
    }
  ACE_TCHAR unique [MAXPATHLEN];
  ACE_OS::unique_name (&new_stream, unique, MAXPATHLEN);

  ACE_OS::strcat (buf, unique);

  // Make sure we have a fresh start.
  ACE_OS::unlink (buf);

  new_stream.disable (ACE_NONBLOCK);
  ACE_HANDLE new_handle = new_stream.get_handle ();

  // Protocol negociation:
  //   Tell the client side what level of signaling strategy
  //   we support.
  ACE_MEM_IO::Signal_Strategy client_signaling =
#if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
    this->preferred_strategy_;
#else
    // We don't support MT.
    ACE_MEM_IO::Reactive;
#endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */
  if (ACE::send (new_handle, &client_signaling,
                 sizeof (ACE_INT16)) == -1)
    ACELIB_ERROR_RETURN ((LM_DEBUG,
                       ACE_TEXT ("ACE_MEM_Acceptor::accept error sending strategy\n")),
                      -1);

  //   Now we get the signaling strategy the client support.
  if (ACE::recv (new_handle, &client_signaling,
                 sizeof (ACE_INT16)) == -1)
    ACELIB_ERROR_RETURN ((LM_DEBUG,
                       ACE_TEXT ("ACE_MEM_Acceptor::%p error receiving strategy\n"),
                       ACE_TEXT ("accept")),
                      -1);

  // Ensure minimum buffer size
  if (this->malloc_options_.minimum_bytes_ < ACE_MEM_STREAM_MIN_BUFFER)
    this->malloc_options_.minimum_bytes_ = ACE_MEM_STREAM_MIN_BUFFER;

  // Client will decide what signaling strategy to use.

  // Now set up the shared memory malloc pool.
  if (new_stream.init (buf,
                       static_cast<ACE_MEM_IO::Signal_Strategy> (client_signaling),
                       &this->malloc_options_) == -1)
    return -1;

  // @@ Need to handle timeout here.
  ACE_UINT16 buf_len = static_cast<ACE_UINT16> ((ACE_OS::strlen (buf) + 1) *
                                                sizeof (ACE_TCHAR));

  // No need to worry about byte-order because both parties should always
  // be on the same machine.
  if (ACE::send (new_handle, &buf_len, sizeof (ACE_UINT16)) == -1)
    return -1;

  // Now send the pathname of the mmap file.
  if (ACE::send (new_handle, buf, buf_len) == -1)
    return -1;
  return 0;
}

int
ACE_MEM_Acceptor::shared_accept_finish (ACE_MEM_Stream new_stream,
                                        int in_blocking_mode,
                                        bool reset_new_handle) const
{
  ACE_TRACE ("ACE_MEM_Acceptor::shared_accept_finish ()");

  ACE_HANDLE new_handle = new_stream.get_handle ();

  // Check to see if we were originally in blocking mode, and if so,
  // set the <new_stream>'s handle and <this> handle to be in blocking
  // mode.
  if (in_blocking_mode)
    {
      // Save/restore errno.
      ACE_Errno_Guard error (errno);

      // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
      // originally.
      ACE::clr_flags (this->get_handle (),
                                 ACE_NONBLOCK);
      ACE::clr_flags (new_handle,
                                 ACE_NONBLOCK);
    }

#if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
  if (reset_new_handle)
    // Reset the event association inherited by the new handle.
    ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
#else
  ACE_UNUSED_ARG (reset_new_handle);
#endif /* ACE_WIN32 */
  if (new_handle == ACE_INVALID_HANDLE)
    return -1;

  return 0;
}

ACE_END_VERSIONED_NAMESPACE_DECL

#endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */