ksocketbase.h
/* -*- C++ -*-
* Copyright (C) 2003,2005 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.
*/
/*
* Even before our #ifdef, clean up the namespace
*/
#ifdef socket
#undef socket
#endif
#ifdef bind
#undef bind
#endif
#ifdef listen
#undef listen
#endif
#ifdef connect
#undef connect
#endif
#ifdef accept
#undef accept
#endif
#ifdef getpeername
#undef getpeername
#endif
#ifdef getsockname
#undef getsockname
#endif
#ifndef KSOCKETBASE_H
#define KSOCKETBASE_H
#include <qiodevice.h>
#include <qstring.h>
#include "ksocketaddress.h"
#include <kdelibs_export.h>
/*
* This is extending QIODevice's error codes
*
* According to qiodevice.h, the last error is IO_UnspecifiedError
* These errors will never occur in functions declared in QIODevice
* (except open, but you shouldn't call open)
*/
#define IO_ListenError (IO_UnspecifiedError+1)
#define IO_AcceptError (IO_UnspecifiedError+2)
#define IO_LookupError (IO_UnspecifiedError+3)
#define IO_SocketCreateError (IO_UnspecifiedError+4)
#define IO_BindError (IO_UnspecifiedError+5)
class QMutex;
namespace KNetwork {
class KResolverEntry;
class KSocketDevice;
class KSocketBasePrivate;
class KDECORE_EXPORT KSocketBase
{
public:
enum SocketOptions
{
Blocking = 0x01,
AddressReuseable = 0x02,
IPv6Only = 0x04,
Keepalive = 0x08,
Broadcast = 0x10
};
enum SocketError
{
NoError = 0,
LookupFailure,
AddressInUse,
AlreadyCreated,
AlreadyBound,
AlreadyConnected,
NotConnected,
NotBound,
NotCreated,
WouldBlock,
ConnectionRefused,
ConnectionTimedOut,
InProgress,
NetFailure,
NotSupported,
Timeout,
UnknownError,
RemotelyDisconnected
};
public:
KSocketBase();
virtual ~KSocketBase();
/*
* The following functions are shared by all descended classes and will have
* to be reimplemented.
*/
protected:
virtual bool setSocketOptions(int opts);
virtual int socketOptions() const;
public:
virtual bool setBlocking(bool enable);
bool blocking() const;
virtual bool setAddressReuseable(bool enable);
bool addressReuseable() const;
virtual bool setIPv6Only(bool enable);
bool isIPv6Only() const;
virtual bool setBroadcast(bool enable);
bool broadcast() const;
KSocketDevice* socketDevice() const;
virtual void setSocketDevice(KSocketDevice* device);
int setRequestedCapabilities(int add, int remove = 0);
protected:
bool hasDevice() const;
void setError(SocketError error);
public:
SocketError error() const;
inline QString errorString() const
{ return errorString(error()); }
QMutex* mutex() const;
public:
static QString errorString(SocketError code);
static bool isFatalError(int code);
private:
void unsetSocketDevice();
KSocketBase(const KSocketBase&);
KSocketBase& operator =(const KSocketBase&);
KSocketBasePrivate *d;
friend class KSocketDevice;
};
class KDECORE_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase
{
public:
KActiveSocketBase();
virtual ~KActiveSocketBase();
virtual bool bind(const KResolverEntry& address) = 0;
virtual bool connect(const KResolverEntry& address) = 0;
virtual bool disconnect() = 0;
virtual Offset size() const
{ return 0; }
virtual Offset at() const
{ return 0; }
virtual bool at(Offset)
{ return false; }
virtual bool atEnd() const
{ return true; }
virtual Q_LONG bytesAvailable() const = 0;
virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L) = 0;
virtual Q_LONG readBlock(char *data, Q_ULONG len) = 0;
virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen) = 0;
virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
virtual Q_LONG writeBlock(const char *data, Q_ULONG len) = 0;
virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to) = 0;
virtual int getch();
virtual int putch(int ch);
virtual int ungetch(int)
{ return -1; }
virtual KSocketAddress localAddress() const = 0;
virtual KSocketAddress peerAddress() const = 0;
// FIXME KDE 4.0:
// enable this function
#if 0
virtual KSocketAddress externalAddress() const = 0;
#endif
protected:
void setError(int status, SocketError error);
void resetError();
};
class KDECORE_EXPORT KPassiveSocketBase: virtual public KSocketBase
{
public:
KPassiveSocketBase();
virtual ~KPassiveSocketBase();
virtual bool bind(const KResolverEntry& address) = 0;
virtual bool listen(int backlog) = 0;
virtual void close() = 0;
virtual KActiveSocketBase* accept() = 0;
virtual KSocketAddress localAddress() const = 0;
virtual KSocketAddress externalAddress() const = 0;
private:
KPassiveSocketBase(const KPassiveSocketBase&);
KPassiveSocketBase& operator = (const KPassiveSocketBase&);
};
} // namespace KNetwork
#endif
KNetwork::KActiveSocketBase::atEnd
virtual bool atEnd() const
This call is not supported on sockets.
Definition: ksocketbase.h:528
KNetwork::KActiveSocketBase
Abstract class for active sockets.
Definition: ksocketbase.h:443
KNetwork::KResolverEntry
One resolution entry.
Definition: kresolver.h:66
KNetwork::KSocketAddress
A generic socket address.
Definition: ksocketaddress.h:423
KNetwork::KActiveSocketBase::at
virtual Offset at() const
This call is not supported on sockets.
Definition: ksocketbase.h:514
KNetwork::KSocketBase::SocketError
SocketError
Possible socket error codes.
Definition: ksocketbase.h:152
KNetwork::KActiveSocketBase::size
virtual Offset size() const
This call is not supported on sockets.
Definition: ksocketbase.h:507
KNetwork::KActiveSocketBase::ungetch
virtual int ungetch(int)
This call is not supported on sockets.
Definition: ksocketbase.h:635
KNetwork::KSocketBase::SocketOptions
SocketOptions
Possible socket options.
Definition: ksocketbase.h:118
KNetwork::KActiveSocketBase::at
virtual bool at(Offset)
This call is not supported on sockets.
Definition: ksocketbase.h:521
KNetwork::KSocketBase::errorString
QString errorString() const
Returns the error string corresponding to this error condition.
Definition: ksocketbase.h:383
KNetwork::KSocketDevice
Low-level socket functionality.
Definition: ksocketdevice.h:50
KNetwork::KPassiveSocketBase
Abstract base class for passive sockets.
Definition: ksocketbase.h:683
KNetwork::KSocketBase
Basic socket functionality.
Definition: ksocketbase.h:97

KDE Home | KDE Accessibility Home | Description of Access Keys