summaryrefslogtreecommitdiff
path: root/ace/TLI_Connector.cpp
blob: 68a9b3c9fac5bcc885cca7f72078a6c756f36aa5 (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
// TLI_Connector.cpp
// $Id$

#define ACE_BUILD_DLL
#include "ace/Handle_Set.h"
#include "ace/TLI_Connector.h"

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

#if defined (ACE_HAS_TLI)

ACE_ALLOC_HOOK_DEFINE(ACE_TLI_Connector)

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

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

// Connect the <new_stream> to the <remote_sap>, waiting up to
// <timeout> amount of time if necessary.  It's amazing how
// complicated this is to do in TLI...

int
ACE_TLI_Connector::connect (ACE_TLI_Stream &new_stream, 
			    const ACE_Addr &remote_sap, 
			    ACE_Time_Value *timeout,
			    const ACE_Addr &local_sap, 
			    int reuse_addr,
			    int flags,
			    int /* perms */,
			    const char device[], 
			    struct t_info *info, 
			    int rwf,
			    netbuf *udata,
                            netbuf *opt)
{
  ACE_TRACE ("ACE_TLI_Connector::connect");
  int result = 0;

  // Only open a new endpoint if we don't already have a valid handle.

  if (new_stream.get_handle () == ACE_INVALID_HANDLE)
    {
      if (ACE_TLI::open (device, flags, info) == -1)
	return -1;
    }
  else // Borrow the handle from the NEW_STREAM. 
    this->set_handle (new_stream.get_handle ());

  if (local_sap != ACE_Addr::sap_any)
    {
      // Bind the local endpoint to a specific addr.

      struct t_bind *localaddr;

      localaddr = (struct t_bind *) 
	::t_alloc (this->get_handle (), T_BIND, T_ADDR);

      if (localaddr == 0)
	result = -1;
      else
	{
	  int one = 1;
	  if (reuse_addr && this->set_option (SOL_SOCKET, SO_REUSEADDR,
					      &one, sizeof one) == -1)
	    result = -1;
	  else
	    {
	      // localaddr->glen = 0;
	      localaddr->addr.maxlen = local_sap.get_size ();
	      localaddr->addr.len = local_sap.get_size ();
	      localaddr->addr.buf = (char *) local_sap.get_addr ();

	      if (ACE_OS::t_bind (this->get_handle (), localaddr, localaddr) == -1)
		result = -1;

	      ACE_OS::t_free ((char *) localaddr, T_BIND);
	    }
	}

      if (result == -1)
	{
	  this->close ();
	  return -1;
	}
    }
  // Let TLI select the local endpoint addr.
  else if (ACE_OS::t_bind (this->get_handle (), 0, 0) == -1)
    return -1;

  struct t_call *callptr = 0;

  callptr = (struct t_call *) 
    ACE_OS::t_alloc (this->get_handle (), T_CALL, T_ADDR);

  if (callptr == 0)
    {
      this->close ();
      return -1;
    }
  callptr->addr.maxlen = remote_sap.get_size ();
  callptr->addr.len = remote_sap.get_size ();
  callptr->addr.buf = (char *) remote_sap.get_addr ();

  if (udata != 0)
    ACE_OS::memcpy ((void *) &callptr->udata, (void *) udata, sizeof *udata);
  if (opt != 0)
    ACE_OS::memcpy ((void *) &callptr->opt, (void *) opt, sizeof *opt);

  // Connect to remote endpoint.

  if (timeout != 0)   // Enable non-blocking, if required.
    {
      if (this->enable (ACE_NONBLOCK) == -1)
	result = -1;
      
      // Do a non-blocking connect.
      if (ACE_OS::t_connect (this->get_handle (), callptr, 0) == -1)
	{
	  result = -1;
	  
	  // Check to see if we simply haven't connected yet on a
	  // non-blocking handle or whether there's really an error.
	  if (t_errno == TNODATA) 
	    {
	      if (timeout->sec () == 0 && timeout->usec () == 0)
		errno = EWOULDBLOCK;
	      else 
		result = this->complete (new_stream, 0, timeout);
	    }
	  else if (t_errno == TLOOK && this->look () == T_DISCONNECT)
	    this->rcvdis ();
	}
    }
  // Do a blocking connect to the server.
  else if (ACE_OS::t_connect (this->get_handle (), callptr, 0) == -1)
    result = -1;

  if (result != -1) 
    {
      // If everything succeeded transfer ownership to <new_stream>.
      new_stream.set_handle (this->get_handle ());
      this->set_handle (ACE_INVALID_HANDLE);
      new_stream.set_rwflag (rwf);
#if defined (I_PUSH)
      if (new_stream.get_rwflag ())
	result = ACE_OS::ioctl (new_stream.get_handle (), I_PUSH, "tirdwr");
#endif /* I_PUSH */
    }
  else if (!(errno == EWOULDBLOCK || errno == ETIME))
    {
      // If things have gone wrong, close down and return an error.
      this->close ();
      new_stream.set_handle (ACE_INVALID_HANDLE);
    }

  if (ACE_OS::t_free ((char *) callptr, T_CALL) == -1)
    return -1;
  return result;
}

// Try to complete a non-blocking connection.

int
ACE_TLI_Connector::complete (ACE_TLI_Stream &new_stream, 
			     ACE_Addr *remote_sap,
			     ACE_Time_Value *tv)
{
  ACE_TRACE ("ACE_TLI_Connector::complete");
  ACE_HANDLE h = ACE::handle_timed_complete (this->get_handle (), tv, 1);

  if (h == ACE_INVALID_HANDLE)
    {
      this->close ();
      return -1;
    }
  else 	  // We've successfully connected!
    {
      if (remote_sap != 0)
	{
#if defined (ACE_HAS_SVR4_TLI)
	  struct netbuf name;

	  name.maxlen = remote_sap->get_size ();
	  name.buf    = (char *) remote_sap->get_addr ();

	  if (ACE_OS::ioctl (this->get_handle (), TI_GETPEERNAME, &name) == -1) 
#else /* SunOS4 sucks... */
	  if (0)
#endif /* ACE_HAS_SVR4_TLI */  
	    {
	      this->close ();
	      return -1;
	    }
	}
      new_stream.set_handle (this->get_handle ());

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

      this->set_handle (ACE_INVALID_HANDLE);
      return 0;
    }
}

#endif /* ACE_HAS_TLI */