KDELibs4Support

k3socketbase.h
1 /* -*- C++ -*-
2  * Copyright (C) 2003,2005 Thiago Macieira <[email protected]>
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 /*
26  * Even before our #ifdef, clean up the namespace
27  */
28 #ifdef socket
29 #undef socket
30 #endif
31 
32 #ifdef bind
33 #undef bind
34 #endif
35 
36 #ifdef listen
37 #undef listen
38 #endif
39 
40 #ifdef connect
41 #undef connect
42 #endif
43 
44 #ifdef accept
45 #undef accept
46 #endif
47 
48 #ifdef getpeername
49 #undef getpeername
50 #endif
51 
52 #ifdef getsockname
53 #undef getsockname
54 #endif
55 
56 #ifndef KSOCKETBASE_H
57 #define KSOCKETBASE_H
58 
59 #include <QIODevice>
60 #include <QString>
61 
62 #include <kdelibs4support_export.h>
63 #include "k3socketaddress.h"
64 
65 class QMutex;
66 
67 namespace KNetwork
68 {
69 
70 class KResolverEntry;
71 class KSocketDevice;
72 
73 class KSocketBasePrivate;
86 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KSocketBase
87 {
88 public:
110  Blocking = 0x01,
111  AddressReuseable = 0x02,
112  IPv6Only = 0x04,
113  Keepalive = 0x08,
114  Broadcast = 0x10,
115  NoDelay = 0x20
116  };
117 
143  enum SocketError {
144  NoError = 0,
145  LookupFailure,
146  AddressInUse,
147  AlreadyCreated,
148  AlreadyBound,
149  AlreadyConnected,
150  NotConnected,
151  NotBound,
152  NotCreated,
153  WouldBlock,
154  ConnectionRefused,
155  ConnectionTimedOut,
156  InProgress,
157  NetFailure,
158  NotSupported,
159  Timeout,
160  UnknownError,
161  RemotelyDisconnected
162  };
163 
164 public:
168  KSocketBase();
169 
173  virtual ~KSocketBase();
174 
175  /*
176  * The following functions are shared by all descended classes and will have
177  * to be reimplemented.
178  */
179 
180 protected:
194  virtual bool setSocketOptions(int opts);
195 
205  virtual int socketOptions() const;
206 
207 public:
223  virtual bool setBlocking(bool enable);
224 
231  bool blocking() const;
232 
247  virtual bool setAddressReuseable(bool enable);
248 
255  bool addressReuseable() const;
256 
272  virtual bool setIPv6Only(bool enable);
273 
280  bool isIPv6Only() const;
281 
293  virtual bool setBroadcast(bool enable);
294 
301  bool broadcast() const;
302 
316  virtual bool setNoDelay(bool enable);
317 
323  bool noDelay() const;
324 
331  KSocketDevice *socketDevice() const;
332 
346  virtual void setSocketDevice(KSocketDevice *device);
347 
369  int setRequestedCapabilities(int add, int remove = 0);
370 
371 protected:
376  bool hasDevice() const;
377 
383  void setError(SocketError error);
384 
388  void resetError();
389 
390 public:
395  SocketError error() const;
396 
400  QString errorString() const;
401 
417  QMutex *mutex() const;
418 
419 public:
425  static QString errorString(SocketError code);
426 
435  static bool isFatalError(int code);
436 
437 private:
440  void unsetSocketDevice();
441 
442  KSocketBase(const KSocketBase &);
443  KSocketBase &operator =(const KSocketBase &);
444 
445  KSocketBasePrivate *const d;
446 
447  friend class KSocketDevice;
448 };
449 
460 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase
461 {
462  Q_OBJECT
463 public:
467  KActiveSocketBase(QObject *parent);
468 
472  virtual ~KActiveSocketBase();
473 
477  QString errorString() const;
478 
482  void setSocketDevice(KSocketDevice *device) override;
483 
487  bool open(OpenMode mode) override;
488 
499  virtual bool bind(const KResolverEntry &address) = 0;
500 
519  virtual bool connect(const KResolverEntry &address,
520  OpenMode mode = ReadWrite) = 0;
521 
537  virtual bool disconnect() = 0;
538 
542  bool isSequential() const override;
543 
548  qint64 size() const override;
549 
554  qint64 pos() const override;
555 
560  bool seek(qint64) override;
561 
566  bool atEnd() const override;
567 
574  qint64 read(char *data, qint64 maxlen);
575 
582  QByteArray read(qint64 len);
583 
595  qint64 read(char *data, qint64 maxlen, KSocketAddress &from);
596 
608  qint64 peek(char *data, qint64 maxlen);
609 
623  qint64 peek(char *data, qint64 maxlen, KSocketAddress &from);
624 
631  qint64 write(const char *data, qint64 len);
632 
639  qint64 write(const QByteArray &data);
640 
652  qint64 write(const char *data, qint64 len, const KSocketAddress &to);
653 
665  virtual qint64 waitForMore(int msecs, bool *timeout = nullptr) = 0;
666 
670  void ungetChar(char);
671 
675  virtual KSocketAddress localAddress() const = 0;
676 
682  virtual KSocketAddress peerAddress() const = 0;
683 
684  // FIXME KDE 4.0:
685  // enable this function
686 #if 0
687 
690  virtual KSocketAddress externalAddress() const = 0;
691 #endif
692 
693 protected:
700  qint64 readData(char *data, qint64 len) override;
701 
713  virtual qint64 readData(char *data, qint64 maxlen, KSocketAddress *from) = 0;
714 
727  virtual qint64 peekData(char *data, qint64 maxlen, KSocketAddress *from) = 0;
728 
735  qint64 writeData(const char *data, qint64 len) override;
736 
748  virtual qint64 writeData(const char *data, qint64 len, const KSocketAddress *to) = 0;
749 
755  void setError(SocketError error);
756 
760  void resetError();
761 };
762 
773 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KPassiveSocketBase: virtual public KSocketBase
774 {
775 public:
780 
784  virtual ~KPassiveSocketBase();
785 
796  virtual bool bind(const KResolverEntry &address) = 0;
797 
812  virtual bool listen(int backlog) = 0;
813 
818  virtual void close() = 0;
819 
833  virtual KActiveSocketBase *accept() = 0;
834 
838  virtual KSocketAddress localAddress() const = 0;
839 
843  virtual KSocketAddress externalAddress() const = 0;
844 
845 private:
847  KPassiveSocketBase &operator = (const KPassiveSocketBase &);
848 };
849 
850 } // namespace KNetwork
851 
852 #endif
Abstract class for active sockets.
Definition: k3socketbase.h:460
One resolution entry.
Definition: k3resolver.h:72
A generic socket address.
QString from() const
A namespace to store all networking-related (socket) classes.
SocketError
Possible socket error codes.
Definition: k3socketbase.h:143
QString to() const
SocketOptions
Possible socket options.
Definition: k3socketbase.h:109
int open(const QString &pathname, int flags, mode_t mode=0)
replacement for open()/::open64() to handle filenames in a platform independent way ...
Definition: kde_file.h:199
Low-level socket functionality.
Abstract base class for passive sockets.
Definition: k3socketbase.h:773
Basic socket functionality.
Definition: k3socketbase.h:86
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 22:41:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.