ksocketbase.cpp
/* -*- 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.
*/
#include <config.h>
#include <qmutex.h>
#include "klocale.h"
#include "ksocketbase.h"
#include "ksocketdevice.h"
using namespace KNetwork;
class KNetwork::KSocketBasePrivate
{
public:
int socketOptions;
int socketError;
int capabilities;
mutable KSocketDevice* device;
QMutex mutex;
KSocketBasePrivate()
: mutex(true) // create recursive
{ }
};
KSocketBase::KSocketBase()
: d(new KSocketBasePrivate)
{
d->socketOptions = Blocking;
d->socketError = 0;
d->device = 0L;
d->capabilities = 0;
}
KSocketBase::~KSocketBase()
{
delete d->device;
delete d;
}
bool KSocketBase::setSocketOptions(int opts)
{
d->socketOptions = opts;
return true;
}
int KSocketBase::socketOptions() const
{
return d->socketOptions;
}
bool KSocketBase::setBlocking(bool enable)
{
return setSocketOptions((socketOptions() & ~Blocking) | (enable ? Blocking : 0));
}
bool KSocketBase::blocking() const
{
return socketOptions() & Blocking;
}
bool KSocketBase::setAddressReuseable(bool enable)
{
return setSocketOptions((socketOptions() & ~AddressReuseable) | (enable ? AddressReuseable : 0));
}
bool KSocketBase::addressReuseable() const
{
return socketOptions() & AddressReuseable;
}
bool KSocketBase::setIPv6Only(bool enable)
{
return setSocketOptions((socketOptions() & ~IPv6Only) | (enable ? IPv6Only : 0));
}
bool KSocketBase::isIPv6Only() const
{
return socketOptions() & IPv6Only;
}
bool KSocketBase::setBroadcast(bool enable)
{
return setSocketOptions((socketOptions() & ~Broadcast) | (enable ? Broadcast : 0));
}
bool KSocketBase::broadcast() const
{
return socketOptions() & Broadcast;
}
KSocketDevice* KSocketBase::socketDevice() const
{
if (d->device)
return d->device;
// it doesn't exist, so create it
QMutexLocker locker(mutex());
if (d->device)
return d->device;
KSocketBase* that = const_cast<KSocketBase*>(this);
KSocketDevice* dev = 0;
if (d->capabilities)
dev = KSocketDevice::createDefault(that, d->capabilities);
if (!dev)
dev = KSocketDevice::createDefault(that);
that->setSocketDevice(dev);
return d->device;
}
void KSocketBase::setSocketDevice(KSocketDevice* device)
{
QMutexLocker locker(mutex());
if (d->device == 0L)
d->device = device;
}
int KSocketBase::setRequestedCapabilities(int add, int remove)
{
d->capabilities |= add;
d->capabilities &= ~remove;
return d->capabilities;
}
bool KSocketBase::hasDevice() const
{
return d->device != 0L;
}
void KSocketBase::setError(SocketError error)
{
d->socketError = error;
}
KSocketBase::SocketError KSocketBase::error() const
{
return static_cast<KSocketBase::SocketError>(d->socketError);
}
// static
QString KSocketBase::errorString(KSocketBase::SocketError code)
{
QString reason;
switch (code)
{
case NoError:
reason = i18n("Socket error code NoError", "no error");
break;
case LookupFailure:
reason = i18n("Socket error code LookupFailure",
"name lookup has failed");
break;
case AddressInUse:
reason = i18n("Socket error code AddressInUse",
"address already in use");
break;
case AlreadyBound:
reason = i18n("Socket error code AlreadyBound",
"socket is already bound");
break;
case AlreadyCreated:
reason = i18n("Socket error code AlreadyCreated",
"socket is already created");
break;
case NotBound:
reason = i18n("Socket error code NotBound",
"socket is not bound");
break;
case NotCreated:
reason = i18n("Socket error code NotCreated",
"socket has not been created");
break;
case WouldBlock:
reason = i18n("Socket error code WouldBlock",
"operation would block");
break;
case ConnectionRefused:
reason = i18n("Socket error code ConnectionRefused",
"connection actively refused");
break;
case ConnectionTimedOut:
reason = i18n("Socket error code ConnectionTimedOut",
"connection timed out");
break;
case InProgress:
reason = i18n("Socket error code InProgress",
"operation is already in progress");
break;
case NetFailure:
reason = i18n("Socket error code NetFailure",
"network failure occurred");
break;
case NotSupported:
reason = i18n("Socket error code NotSupported",
"operation is not supported");
break;
case Timeout:
reason = i18n("Socket error code Timeout",
"timed operation timed out");
break;
case UnknownError:
reason = i18n("Socket error code UnknownError",
"an unknown/unexpected error has happened");
break;
case RemotelyDisconnected:
reason = i18n("Socket error code RemotelyDisconnected",
"remote host closed connection");
break;
default:
reason = QString::null;
break;
}
return reason;
}
// static
bool KSocketBase::isFatalError(int code)
{
switch (code)
{
case WouldBlock:
case InProgress:
case NoError:
case RemotelyDisconnected:
return false;
}
return true;
}
void KSocketBase::unsetSocketDevice()
{
d->device = 0L;
}
QMutex* KSocketBase::mutex() const
{
return &d->mutex;
}
KActiveSocketBase::KActiveSocketBase()
{
}
KActiveSocketBase::~KActiveSocketBase()
{
}
int KActiveSocketBase::getch()
{
unsigned char c;
if (readBlock((char*)&c, 1) != 1)
return -1;
return c;
}
int KActiveSocketBase::putch(int ch)
{
unsigned char c = (unsigned char)ch;
if (writeBlock((char*)&c, 1) != 1)
return -1;
return c;
}
void KActiveSocketBase::setError(int status, SocketError error)
{
KSocketBase::setError(error);
setStatus(status);
}
void KActiveSocketBase::resetError()
{
KSocketBase::setError(NoError);
resetStatus();
}
KPassiveSocketBase::KPassiveSocketBase()
{
}
KPassiveSocketBase::~KPassiveSocketBase()
{
}
KNetwork::KActiveSocketBase::readBlock
virtual Q_LONG readBlock(char *data, Q_ULONG len)=0
Reads data from the socket.
KNetwork::KSocketBase::blocking
bool blocking() const
Retrieves this socket's blocking mode.
Definition: ksocketbase.cpp:81
KNetwork::KSocketBase::isFatalError
static bool isFatalError(int code)
Returns true if the given error code is a fatal one, false otherwise.
Definition: ksocketbase.cpp:259
KNetwork::KSocketBase::socketDevice
KSocketDevice * socketDevice() const
Retrieves the socket implementation used on this socket.
Definition: ksocketbase.cpp:116
KNetwork::KSocketBase::addressReuseable
bool addressReuseable() const
Retrieves this socket's address reuseability flag.
Definition: ksocketbase.cpp:91
KNetwork::KSocketBase::setAddressReuseable
virtual bool setAddressReuseable(bool enable)
Sets this socket's address reuseable flag.
Definition: ksocketbase.cpp:86
KNetwork::KActiveSocketBase::getch
virtual int getch()
Reads one character from the socket.
Definition: ksocketbase.cpp:291
KNetwork::KSocketBase::broadcast
bool broadcast() const
Retrieves this socket's Broadcast flag.
Definition: ksocketbase.cpp:111
KNetwork::KActiveSocketBase::resetError
void resetError()
Resets the socket error code and the I/O Device's status.
Definition: ksocketbase.cpp:315
KNetwork::KActiveSocketBase::putch
virtual int putch(int ch)
Writes one character to the socket.
Definition: ksocketbase.cpp:300
KNetwork::KActiveSocketBase::KActiveSocketBase
KActiveSocketBase()
Constructor.
Definition: ksocketbase.cpp:283
KNetwork::KSocketBase::error
SocketError error() const
Retrieves the socket error code.
Definition: ksocketbase.cpp:160
KNetwork::KActiveSocketBase::writeBlock
virtual Q_LONG writeBlock(const char *data, Q_ULONG len)=0
Writes the given data to the socket.
KNetwork::KSocketBase::~KSocketBase
virtual ~KSocketBase()
Destructor.
Definition: ksocketbase.cpp:59
klocale.h
KNetwork::KPassiveSocketBase::~KPassiveSocketBase
virtual ~KPassiveSocketBase()
Destructor.
Definition: ksocketbase.cpp:325
KNetwork::KSocketBase::setRequestedCapabilities
int setRequestedCapabilities(int add, int remove=0)
Sets the internally requested capabilities for a socket device.
Definition: ksocketbase.cpp:143
KNetwork::KSocketBase::isIPv6Only
bool isIPv6Only() const
Retrieves this socket's IPv6 Only flag.
Definition: ksocketbase.cpp:101
KNetwork::KSocketBase::KSocketBase
KSocketBase()
Default constructor.
Definition: ksocketbase.cpp:50
KNetwork::KSocketBase::setBroadcast
virtual bool setBroadcast(bool enable)
Sets this socket Broadcast flag.
Definition: ksocketbase.cpp:106
KNetwork::KSocketBase::setIPv6Only
virtual bool setIPv6Only(bool enable)
Sets this socket's IPv6 Only flag.
Definition: ksocketbase.cpp:96
KNetwork::KSocketBase::SocketError
SocketError
Possible socket error codes.
Definition: ksocketbase.h:152
KNetwork::KSocketBase::mutex
QMutex * mutex() const
Returns the internal mutex for this class.
Definition: ksocketbase.cpp:278
KNetwork::KSocketBase::setError
void setError(SocketError error)
Sets the socket's error code.
Definition: ksocketbase.cpp:155
KNetwork::KActiveSocketBase::setError
void setError(int status, SocketError error)
Sets the socket's error code and the I/O Device's status.
Definition: ksocketbase.cpp:309
KNetwork::KSocketBase::setSocketOptions
virtual bool setSocketOptions(int opts)
Set the given socket options.
Definition: ksocketbase.cpp:65
KNetwork::KActiveSocketBase::~KActiveSocketBase
virtual ~KActiveSocketBase()
Destructor.
Definition: ksocketbase.cpp:287
KNetwork::KSocketBase::setSocketDevice
virtual void setSocketDevice(KSocketDevice *device)
Sets the socket implementation to be used on this socket.
Definition: ksocketbase.cpp:136
KNetwork::KSocketBase::errorString
QString errorString() const
Returns the error string corresponding to this error condition.
Definition: ksocketbase.h:383
KNetwork::KPassiveSocketBase::KPassiveSocketBase
KPassiveSocketBase()
Constructor.
Definition: ksocketbase.cpp:321
KNetwork::KSocketBase::hasDevice
bool hasDevice() const
Returns true if the socket device has been initialised in this object, either by calling socketDevice...
Definition: ksocketbase.cpp:150
KNetwork::KSocketBase::setBlocking
virtual bool setBlocking(bool enable)
Sets this socket's blocking mode.
Definition: ksocketbase.cpp:76
KNetwork::KSocketDevice
Low-level socket functionality.
Definition: ksocketdevice.h:50
KNetwork::KSocketBase::socketOptions
virtual int socketOptions() const
Retrieves the socket options that have been set.
Definition: ksocketbase.cpp:71
KNetwork::KSocketBase
Basic socket functionality.
Definition: ksocketbase.h:97
KNetwork::KSocketDevice::createDefault
static KSocketDevice * createDefault(KSocketBase *parent)
Creates a new default KSocketDevice object given the parent object.
Definition: ksocketdevice.cpp:839

KDE Home | KDE Accessibility Home | Description of Access Keys