summaryrefslogtreecommitdiff
path: root/ace/UPIPE_Connector.cpp
blob: 51db226f879ebcc9097ba9738b8b7f824bc7966f (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
// UPIPE_Connector.cpp
// $Id$

#include "ace/UPIPE_Connector.h"

ACE_RCSID(ace, UPIPE_Connector, "$Id$")

#if defined (ACE_HAS_THREADS)

#if defined (ACE_LACKS_INLINE_FUNCTIONS)
#include "ace/UPIPE_Connector.i"
#endif

ACE_ALLOC_HOOK_DEFINE(ACE_UPIPE_Connector)

void
ACE_UPIPE_Connector::dump (void) const
{
  ACE_TRACE ("ACE_UPIPE_Connector::dump");
}

ACE_UPIPE_Connector::ACE_UPIPE_Connector (void)
{
  ACE_TRACE ("ACE_UPIPE_Connector::ACE_UPIPE_Connector");
}

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)
	ACE_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)
	  ACE_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 result;
    }
}
#endif /* ACE_HAS_THREADS */