예제 #1
0
int
ACE_UPIPE_Acceptor::accept (ACE_UPIPE_Stream &new_stream,
                            ACE_UPIPE_Addr *remote_addr,
                            ACE_Time_Value *timeout,
                            bool restart,
                            bool reset_new_handle)
{
  ACE_TRACE ("ACE_UPIPE_Acceptor::accept");
  ACE_UNUSED_ARG (reset_new_handle);

  ACE_SPIPE_Stream new_io;

  if (this->ACE_SPIPE_Acceptor::accept (new_io, remote_addr,
                                        timeout, restart) == -1)
    return -1;
  else
    {
      ACE_UPIPE_Stream *remote_stream = 0;

      ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, new_stream.lock_, -1));

      new_stream.set_handle (new_io.get_handle ());
      new_stream.reference_count_++;

      // Transfer address ownership.
      new_io.get_local_addr (new_stream.local_addr_);
      new_io.get_remote_addr (new_stream.remote_addr_);

      // Now that we got the handle, we'll read the address of the
      // connector-side ACE_UPIPE_Stream out of the pipe and link that
      // ACE_UPIPE_Stream to our ACE_UPIPE_Stream.

      if (ACE_OS::read (new_stream.get_handle (),
                        (char *) &remote_stream,
                        sizeof remote_stream) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("ACE_UPIPE_Acceptor: %p\n"),
                    ACE_TEXT ("read stream address failed")));
      else if (new_stream.stream_.link (remote_stream->stream_) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("ACE_UPIPE_Acceptor: %p\n"),
                    ACE_TEXT ("link streams failed")));
      // Send a message over the new streampipe to confirm acceptance.
      else if (new_stream.send (&mb_, 0) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("ACE_UPIPE_Acceptor: %p\n"),
                    ACE_TEXT ("linked stream.put failed")));

      // Close down the new_stream at this point in order to conserve
      // handles.  Note that we don't need the SPIPE connection
      // anymore since we're now linked via the <Message_Queue>.
      new_stream.ACE_SPIPE::close ();
      return 0;
    }
}
예제 #2
0
int
ACE_UPIPE_Connector::connect (ACE_UPIPE_Stream &new_stream,
                              const ACE_UPIPE_Addr &addr,
                              ACE_Time_Value *timeout,
                              const ACE_Addr & /* local_sap */,
                              int /* reuse_addr */,
                              int flags,
                              int perms)
{
  ACE_TRACE ("ACE_UPIPE_Connector::connect");
  ACE_ASSERT (new_stream.get_handle () == ACE_INVALID_HANDLE);

  ACE_HANDLE handle = ACE::handle_timed_open (timeout,
                                              addr.get_path_name (),
                                              flags, perms);

  if (handle == ACE_INVALID_HANDLE)
    return -1;
#if !defined (ACE_WIN32)
  else if (ACE_OS::isastream (handle) != 1)
    return -1;
#endif
  else // We're connected!
    {
      ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, new_stream.lock_, -1));

      ACE_UPIPE_Stream *ustream = &new_stream;

      new_stream.set_handle (handle);
      new_stream.remote_addr_ = addr; // class copy.
      new_stream.reference_count_++;

      // Now send the address of our ACE_UPIPE_Stream over this pipe
      // to our corresponding ACE_UPIPE_Acceptor, so he may link the
      // two streams.
      ssize_t result = ACE_OS::write (handle,
                                      (const char *) &ustream,
                                      sizeof ustream);
      if (result == -1)
        ACELIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("ACE_UPIPE_Connector %p\n"),
                    ACE_TEXT ("write to pipe failed")));

      // Wait for confirmation of stream linking.
      ACE_Message_Block *mb_p = 0;

      // Our part is done, wait for server to confirm connection.
      result = new_stream.recv (mb_p, 0);

      // Do *not* coalesce the following two checks for result == -1.
      // They perform different checks and cannot be merged.
      if (result == -1)
          ACELIB_ERROR ((LM_ERROR,
                      ACE_TEXT ("ACE_UPIPE_Connector %p\n"),
                      ACE_TEXT ("no confirmation from server")));
      else
        // Close down the new_stream at this point in order to
        // conserve handles.  Note that we don't need the SPIPE
        // connection anymore since we're linked via the Message_Queue
        // now.
        new_stream.ACE_SPIPE::close ();
      return static_cast<int> (result);
    }
}