• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.3 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • network
k3datagramsocket.cpp
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * Copyright (C) 2003,2004 Thiago Macieira <thiago@kde.org>
3  *
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "k3datagramsocket.h"
26 
27 #include <config.h>
28 #include <config-network.h>
29 
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 
33 #include "k3socketaddress.h"
34 #include "k3resolver.h"
35 #include "k3socketdevice.h"
36 
37 using namespace KNetwork;
38 
39 /*
40  * TODO:
41  *
42  * don't use signals and slots to track state changes: use stateChanging
43  *
44  */
45 
46 KDatagramSocket::KDatagramSocket(QObject* parent)
47  : KClientSocketBase(parent), d(0L)
48 {
49  peerResolver().setFamily(KResolver::KnownFamily);
50  localResolver().setFamily(KResolver::KnownFamily);
51 
52  peerResolver().setSocketType(SOCK_DGRAM);
53  localResolver().setSocketType(SOCK_DGRAM);
54 
55  localResolver().setFlags(KResolver::Passive);
56 
57  // QObject::connect(localResolver(), SIGNAL(finished(KNetwork::KResolverResults))
58  // this, SLOT(lookupFinishedLocal()));
59  QObject::connect(&peerResolver(),
60  SIGNAL(finished(KNetwork::KResolverResults)),
61  this, SLOT(lookupFinishedPeer()));
62  QObject::connect(this, SIGNAL(hostFound()), this, SLOT(lookupFinishedLocal()));
63 }
64 
65 KDatagramSocket::~KDatagramSocket()
66 {
67  // KClientSocketBase's destructor closes the socket
68 
69  //delete d;
70 }
71 
72 bool KDatagramSocket::bind(const QString& node, const QString& service)
73 {
74  if (state() >= Bound)
75  return false;
76 
77  if (localResolver().isRunning())
78  localResolver().cancel(false);
79 
80  // no, we must do a host lookup
81  localResolver().setAddress(node, service);
82 
83  if (!lookup())
84  return false;
85 
86  // see if lookup has finished already
87  // this also catches blocking mode, since lookup has to finish
88  // its processing if we're in blocking mode
89  if (state() > HostLookup)
90  return doBind();
91 
92  return true;
93 }
94 
95 bool KDatagramSocket::bind(const KResolverEntry& entry)
96 {
97  return KClientSocketBase::bind(entry);
98 }
99 
100 bool KDatagramSocket::connect(const QString& node, const QString& service,
101  OpenMode mode)
102 {
103  if (state() >= Connected)
104  return true; // already connected
105 
106  if (peerResolver().nodeName() != node ||
107  peerResolver().serviceName() != service)
108  peerResolver().setAddress(node, service); // this resets the resolver's state
109 
110  // KClientSocketBase::lookup only works if the state is Idle or HostLookup
111  // therefore, we store the old state, call the lookup routine and then set
112  // it back.
113  SocketState s = state();
114  setState(s == Connecting ? HostLookup : Idle);
115  bool ok = lookup();
116  if (!ok)
117  {
118  setState(s); // go back
119  return false;
120  }
121 
122  // check if lookup is finished
123  // if we're in blocking mode, then the lookup has to be finished
124  if (state() == HostLookup)
125  {
126  // it hasn't finished
127  setState(Connecting);
128  emit stateChanged(Connecting);
129  return true;
130  }
131 
132  // it has to be finished here
133  if (state() != Connected)
134  {
135  setState(Connecting);
136  emit stateChanged(Connecting);
137  lookupFinishedPeer();
138  }
139 
140  KActiveSocketBase::open(mode | Unbuffered);
141  return state() == Connected;
142 }
143 
144 bool KDatagramSocket::connect(const KResolverEntry& entry, OpenMode mode)
145 {
146  return KClientSocketBase::connect(entry, mode);
147 }
148 
149 KDatagramPacket KDatagramSocket::receive()
150 {
151  qint64 size = bytesAvailable();
152  if (size == 0)
153  {
154  // nothing available yet to read
155  // wait for data if we're not blocking
156  if (blocking())
157  socketDevice()->waitForMore(-1); // wait forever
158  else
159  {
160  // mimic error
161  setError(WouldBlock);
162  emit gotError(WouldBlock);
163  return KDatagramPacket();
164  }
165 
166  // try again
167  size = bytesAvailable();
168  }
169 
170  QByteArray data;
171  data.resize(size);
172  KSocketAddress address;
173 
174  // now do the reading
175  size = read(data.data(), size, address);
176  if (size < 0)
177  // error has been set
178  return KDatagramPacket();
179 
180  data.resize(size); // just to be sure
181  return KDatagramPacket(data, address);
182 }
183 
184 qint64 KDatagramSocket::send(const KDatagramPacket& packet)
185 {
186  return write(packet.data(), packet.size(), packet.address());
187 }
188 
189 qint64 KDatagramSocket::writeData(const char *data, qint64 len,
190  const KSocketAddress* to)
191 {
192  if (to->family() != AF_UNSPEC)
193  {
194  // make sure the socket is open at this point
195  if (!socketDevice()->isOpen())
196  // error handling will happen below
197  socketDevice()->create(to->family(), SOCK_DGRAM, 0);
198  }
199  return KClientSocketBase::writeData(data, len, to);
200 }
201 
202 void KDatagramSocket::lookupFinishedLocal()
203 {
204  // bind lookup has finished and succeeded
205  // state() == HostFound
206 
207  if (!doBind())
208  return; // failed binding
209 
210  if (peerResults().count() > 0)
211  {
212  setState(Connecting);
213  emit stateChanged(Connecting);
214 
215  lookupFinishedPeer();
216  }
217 }
218 
219 void KDatagramSocket::lookupFinishedPeer()
220 {
221  // this function is called by lookupFinishedLocal above
222  // and is also connected to a signal
223  // so it might be called twice.
224 
225  if (state() != Connecting)
226  return;
227 
228  if (peerResults().count() == 0)
229  {
230  setState(Unconnected);
231  emit stateChanged(Unconnected);
232  return;
233  }
234 
235  KResolverResults::ConstIterator it = peerResults().begin();
236  for ( ; it != peerResults().end(); ++it)
237  if (connect(*it))
238  {
239  // weee, we connected
240 
241  setState(Connected); // this sets up signals
242  //setupSignals(); // setState sets up the signals
243 
244  emit stateChanged(Connected);
245  emit connected(*it);
246  return;
247  }
248 
249  // no connection
250  copyError();
251  setState(Unconnected);
252  emit stateChanged(Unconnected);
253  emit gotError(error());
254 }
255 
256 bool KDatagramSocket::doBind()
257 {
258  if (localResults().count() == 0)
259  return true;
260  if (state() >= Bound)
261  return true; // already bound
262 
263  KResolverResults::ConstIterator it = localResults().begin();
264  for ( ; it != localResults().end(); ++it)
265  if (bind(*it))
266  {
267  // bound
268  setupSignals();
269  KActiveSocketBase::open(ReadWrite | Unbuffered);
270  return true;
271  }
272 
273  // not bound
274  // no need to set state since it can only be HostFound already
275  copyError();
276  emit gotError(error());
277  return false;
278 }
279 
280 void KDatagramSocket::setupSignals()
281 {
282  QSocketNotifier *n = socketDevice()->readNotifier();
283  if (n)
284  {
285  n->setEnabled(emitsReadyRead());
286  QObject::connect(n, SIGNAL(activated(int)), this, SLOT(slotReadActivity()));
287  }
288  else
289  return;
290 
291  n = socketDevice()->writeNotifier();
292  if (n)
293  {
294  n->setEnabled(emitsReadyWrite());
295  QObject::connect(n, SIGNAL(activated(int)), this, SLOT(slotWriteActivity()));
296  }
297  else
298  return;
299 }
300 
301 #include "k3datagramsocket.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat May 18 2013 11:36:06 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.10.3 API Reference

Skip menu "kdelibs-4.10.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal