summaryrefslogtreecommitdiffstats
path: root/tdecore/network/kstreamsocket.cpp
blob: 6ba8dfe8bba547010763d01ad6b57726bde1c964 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 *  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
 *
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included 
 *  in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <config.h>

#include <tqsocketnotifier.h>
#include <tqdatetime.h>
#include <tqtimer.h>
#include <tqguardedptr.h>

#include "tdesocketaddress.h"
#include "kresolver.h"
#include "tdesocketdevice.h"
#include "kstreamsocket.h"

using namespace KNetwork;

class KNetwork::KStreamSocketPrivate
{
public:
  KResolverResults::ConstIterator local, peer;
  TQTime startTime;
  TQTimer timer;

  int timeout;

  inline KStreamSocketPrivate()
    : timeout(0)
  { }
};

KStreamSocket::KStreamSocket(const TQString& node, const TQString& service,
			     TQObject* parent, const char *name)
  : KClientSocketBase(parent, name), d(new KStreamSocketPrivate)
{
  peerResolver().setNodeName(node);
  peerResolver().setServiceName(service);
  peerResolver().setFamily(KResolver::KnownFamily);
  localResolver().setFamily(KResolver::KnownFamily);

  setSocketOptions(socketOptions() & ~Blocking);

  TQObject::connect(&d->timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(timeoutSlot()));
}

KStreamSocket::~KStreamSocket()
{
  delete d;
  // KClientSocketBase's destructor closes the socket
}

int KStreamSocket::timeout() const
{
  return d->timeout;
}

int KStreamSocket::remainingTimeout() const
{
  if (state() != Connecting)
    return timeout();
  if (timeout() <= 0)
    return 0;

  return timeout() - d->startTime.elapsed();
}

void KStreamSocket::setTimeout(int msecs)
{
  d->timeout = msecs;

  if (state() == Connecting)
    d->timer.changeInterval(msecs);
}

bool KStreamSocket::bind(const TQString& node, const TQString& service)
{
  if (state() != Idle)
    return false;

  if (!node.isNull())
    localResolver().setNodeName(node);
  if (!service.isNull())
    localResolver().setServiceName(service);
  return true;
}

bool KStreamSocket::connect(const TQString& node, const TQString& service)
{
  if (state() == Connected)
    return true;		// already connected

  if (state() > Connected)
    return false;		// can't do much here

  if (!node.isNull())
    peerResolver().setNodeName(node);
  if (!service.isNull())
    peerResolver().setServiceName(service);

  if (state() == Connecting && !blocking())
    {
      setError(IO_ConnectError, InProgress);
      emit gotError(InProgress);
      return true;		// we're already connecting
    }

  if (state() < HostFound)
    {
      // connection hasn't started yet
      if (!blocking())
	{
	  TQObject::connect(this, TQT_SIGNAL(hostFound()), TQT_SLOT(hostFoundSlot()));
	  return lookup();
	}

      // blocking mode
      if (!lookup())
	return false;		// lookup failure
    }

  /*
   * lookup results are available here
   */

  if (timeout() > 0)
    {
      if (!blocking() && !d->timer.isActive())
	d->timer.start(timeout(), true);
      else
	{
	  // blocking connection with timeout
	  // this must be handled as a special case because it requires a
	  // non-blocking socket

	  d->timer.stop();	// no need for a timer here

	  socketDevice()->setBlocking(false);
	  while (true)
	    {
	      connectionEvent();
	      if (state() < Connecting)
		return false;	// error connecting
	      if (state() == Connected)
		return true;	// connected!

	      if (remainingTimeout() <= 0)
		{
		  // we've timed out
		  timeoutSlot();
		  return false;
		}

	      if (socketDevice()->error() == InProgress)
		{
		  bool timedout;
		  socketDevice()->poll(remainingTimeout(), &timedout);
		  if (timedout)
		    {
		      timeoutSlot();
		      return false;
		    }
		}
	    }
	}
    }

  connectionEvent();
  return error() == NoError;
}

bool KStreamSocket::connect(const KResolverEntry& entry)
{
  return KClientSocketBase::connect(entry);
}

void KStreamSocket::hostFoundSlot()
{
  TQObject::disconnect(this, TQT_SLOT(hostFoundSlot()));
  if (timeout() > 0)
    d->timer.start(timeout(), true);
  TQTimer::singleShot(0, this, TQT_SLOT(connectionEvent()));
}

void KStreamSocket::connectionEvent()
{
  if (state() != HostFound && state() != Connecting)
    return;			// nothing to do

  const KResolverResults& peer = peerResults();
  if (state() == HostFound)
    {
      d->startTime.start();

      setState(Connecting);
      emit stateChanged(Connecting);
      d->peer = peer.begin();
      d->local = localResults().begin(); // just to be on the safe side
    }

  while (d->peer != peer.end())
    {
      const KResolverEntry &r = *d->peer;

      if (socketDevice()->socket() != -1)
	{
	  // we have an existing file descriptor
	  // this means that we've got activity in it (connection result)
	  if (socketDevice()->connect(r) && socketDevice()->error() == NoError)
	    {
	      // yes, it did connect!
	      connectionSucceeded(r);
	      return;
	    }
	  else if (socketDevice()->error() == InProgress)
	    // nope, still in progress
	    return;

	  // no, the socket failed to connect
	  copyError();
	  socketDevice()->close();
	  ++d->peer;
	  continue;
	}

      // try to bind
      if (!bindLocallyFor(r))
	{
	  // could not find a matching family
	  ++d->peer;
	  continue;
	}

      {
	bool skip = false;
	emit aboutToConnect(r, skip);
	if (skip)
	  {
	    ++d->peer;
	    continue;
	  }
      }

      if (socketDevice()->connect(r) || socketDevice()->error() == InProgress)
	{
	  // socket is attempting to connect
	  if (socketDevice()->error() == InProgress)
	    {
	      TQSocketNotifier *n = socketDevice()->readNotifier();
	      TQObject::connect(n, TQT_SIGNAL(activated(int)),
			       this, TQT_SLOT(connectionEvent()));
	      n->setEnabled(true);

	      n = socketDevice()->writeNotifier();
	      TQObject::connect(n, TQT_SIGNAL(activated(int)),
			       this, TQT_SLOT(connectionEvent()));
	      n->setEnabled(true);

	      return;		// wait for activity
	    }

	  // socket has connected
	  connectionSucceeded(r);
	  return;
	}

      // connection failed
      // try next
      copyError();
      socketDevice()->close();
      ++d->peer;
    }

  // that was the last item
  socketDevice()->setSocketOptions(socketOptions());
  setState(Idle);
  emit stateChanged(Idle);
  emit gotError(error());
  return;
}

void KStreamSocket::timeoutSlot()
{
  if (state() != Connecting)
    return;

  // halt the connections
  socketDevice()->close();	// this also kills the notifiers

  setError(IO_TimeOutError, Timeout);
  setState(HostFound);
  emit stateChanged(HostFound);

  TQGuardedPtr<KStreamSocket> that = this;
  emit gotError(Timeout);
  if (!that.isNull())
    emit timedOut();
}

bool KStreamSocket::bindLocallyFor(const KResolverEntry& peer)
{
  const KResolverResults& local = localResults();

  if (local.isEmpty())
    // user doesn't want to bind to any specific local address
    return true;

  bool foundone = false;
  // scan the local resolution for a matching family
  for (d->local = local.begin(); d->local != local.end(); ++d->local)
    if ((*d->local).family() == peer.family())
      {
	// found a suitable address!
	foundone = true;

	if (socketDevice()->bind(*d->local))
	  return true;
      }
  
  if (!foundone)
    {
      // found nothing
      setError(IO_BindError, NotSupported);
      emit gotError(NotSupported);
    }
  else
    copyError();
  return false;
}

void KStreamSocket::connectionSucceeded(const KResolverEntry& peer)
{
  TQObject::disconnect(socketDevice()->readNotifier(), 0, this, TQT_SLOT(connectionEvent()));
  TQObject::disconnect(socketDevice()->writeNotifier(), 0, this, TQT_SLOT(connectionEvent()));

  resetError();
  setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
  setState(Connected);
  socketDevice()->setSocketOptions(socketOptions());
  d->timer.stop();
  emit stateChanged(Connected);
  
  if (!localResults().isEmpty())
    emit bound(*d->local);
  emit connected(peer);
}

#include "kstreamsocket.moc"