summaryrefslogtreecommitdiff
path: root/ace/SOCK_Connector.cpp
blob: 6a1edc9028d0f9b3a07c9c02de3a1bf5299274f8 (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
// SOCK_Connector.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/SOCK_Connector.h"
#include "ace/Handle_Set.h"
#include "ace/INET_Addr.h"

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

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

ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_Connector)

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

// Actively connect and produce a new ACE_SOCK_Stream if things go well...

int
ACE_SOCK_Connector::connect (ACE_SOCK_Stream &new_stream, 
			     const ACE_Addr &remote_sap, 
			     ACE_Time_Value *timeout,
			     const ACE_Addr &local_sap,
			     int reuse_addr,
			     int /* flags */,
			     int /* perms */,
			     int protocol_family, 
			     int protocol)
{
  ACE_TRACE ("ACE_SOCK_Connector::connect");
  int result = 0;

  // Only open a new socket if we don't already have a valid handle.
  if (new_stream.get_handle () == ACE_INVALID_HANDLE
      && new_stream.open (SOCK_STREAM,
			  protocol_family, 
			  protocol,
			  reuse_addr) == -1)
    return -1;

  sockaddr *raddr = (sockaddr *) remote_sap.get_addr ();
  size_t rsize = remote_sap.get_size ();

  if (local_sap != ACE_Addr::sap_any)
    {
      sockaddr *laddr = (sockaddr *) local_sap.get_addr ();
      size_t size = local_sap.get_size ();
      result = ACE_OS::bind (new_stream.get_handle (),
                             laddr,
                             size);

      if (result == -1)
	{
	  new_stream.close ();
	  return -1;
	}
    }

  // Enable non-blocking, if required.
  if (timeout != 0)
    {
      if (new_stream.enable (ACE_NONBLOCK) == -1)
	result = -1;
      
      if (ACE_OS::connect (new_stream.get_handle (), raddr, rsize) == -1)
	{
	  result = -1;

	  // Check whether the connection is in progress.
	  if (errno == EINPROGRESS || errno == EWOULDBLOCK)
	    {
	      // This expression checks if we were polling.
	      if (timeout->sec () == 0 && timeout->usec () == 0)
		errno = EWOULDBLOCK;
	      // Wait synchronously
	      else if (this->complete (new_stream, 0, timeout) != -1)
		return 0;
	    }
	}
    }
  // Do a blocking connect.
  else if (ACE_OS::connect (new_stream.get_handle (), raddr, rsize) == -1)
    result = -1;

  // EISCONN is treated specially since this routine may be used to
  // check if we are already connected.
  if (result != -1 || errno == EISCONN)
    // Start out with non-blocking disabled on the <new_stream>.
    new_stream.disable (ACE_NONBLOCK);
  else if (!(errno == EWOULDBLOCK || errno == ETIMEDOUT))
    {
      // Save/restore errno.
      ACE_Errno_Guard error (errno);
      new_stream.close ();
    }
    
  return result;
}

// Try to complete a non-blocking connection.

int
ACE_SOCK_Connector::complete (ACE_SOCK_Stream &new_stream,
			      ACE_Addr *remote_sap,
			      ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_SOCK_Connector::complete");
#if defined (ACE_HAS_BROKEN_NON_BLOCKING_CONNECTS)
  // Win32 has a timing problem - if you check to see if the
  // connection has completed too fast, it will fail - so wait 1
  // millisecond to let it catch up.
  ACE_Time_Value time (0, ACE_NON_BLOCKING_BUG_DELAY);
  ACE_OS::sleep (time);
#endif /* ACE_HAS_BROKEN_NON_BLOCKING_CONNECTS */
  ACE_HANDLE h = ACE::handle_timed_complete (new_stream.get_handle (), tv);

  if (h == ACE_INVALID_HANDLE)
    {
      // Save/restore errno.
      ACE_Errno_Guard error (errno);
      new_stream.close ();
      return -1;
    }
  else 	  // We've successfully connected!
    {
      if (remote_sap != 0)
	{
	  int len;

	  len = remote_sap->get_size ();
	  sockaddr *addr = (sockaddr *) remote_sap->get_addr ();

	  if (ACE_OS::getpeername (h, addr, &len) == -1)
	    {
              // Save/restore errno.
              ACE_Errno_Guard error (errno);
	      new_stream.close ();
	      return -1;
	    }
	}

      // Start out with non-blocking disabled on the <new_stream>.
      new_stream.disable (ACE_NONBLOCK);
      return 0;
    }
}