# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# ksocketbase.cpp

    00001 /*  -*- C++ -*-
    00002  *  Copyright (C) 2003-2005 Thiago Macieira <thiago.macieira@kdemail.net>
    00003  *
    00004  *
    00005  *  Permission is hereby granted, free of charge, to any person obtaining
    00006  *  a copy of this software and associated documentation files (the
    00007  *  "Software"), to deal in the Software without restriction, including
    00008  *  without limitation the rights to use, copy, modify, merge, publish,
    00009  *  distribute, sublicense, and/or sell copies of the Software, and to
    00010  *  permit persons to whom the Software is furnished to do so, subject to
    00011  *  the following conditions:
    00012  *
    00013  *  The above copyright notice and this permission notice shall be included
    00014  *  in all copies or substantial portions of the Software.
    00015  *
    00016  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    00017  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    00018  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    00019  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    00020  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    00021  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    00022  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    00023  */
    00024
    00025 #include <config.h>
    00026 #include <qmutex.h>
    00027 #include "klocale.h"
    00028
    00029 #include "ksocketbase.h"
    00030 #include "ksocketdevice.h"
    00031
    00032 using namespace KNetwork;
    00033
    00034 class KNetwork::KSocketBasePrivate
    00035 {
    00036 public:
    00037   int socketOptions;
    00038   int socketError;
    00039   int capabilities;
    00040
    00041   mutable KSocketDevice* device;
    00042
    00043   QMutex mutex;
    00044
    00045   KSocketBasePrivate()
    00046     : mutex(true)       // create recursive
    00047   { }
    00048 };
    00049
    00050 KSocketBase::KSocketBase()
    00051   : d(new KSocketBasePrivate)
    00052 {
    00053   d->socketOptions = Blocking;
    00054   d->socketError = 0;
    00055   d->device = 0L;
    00056   d->capabilities = 0;
    00057 }
    00058
    00059 KSocketBase::~KSocketBase()
    00060 {
    00061   delete d->device;
    00062   delete d;
    00063 }
    00064
    00065 bool KSocketBase::setSocketOptions(int opts)
    00066 {
    00067   d->socketOptions = opts;
    00068   return true;
    00069 }
    00070
    00071 int KSocketBase::socketOptions() const
    00072 {
    00073   return d->socketOptions;
    00074 }
    00075
    00076 bool KSocketBase::setBlocking(bool enable)
    00077 {
    00078   return setSocketOptions((socketOptions() & ~Blocking) | (enable ? Blocking : 0));
    00079 }
    00080
    00081 bool KSocketBase::blocking() const
    00082 {
    00083   return socketOptions() & Blocking;
    00084 }
    00085
    00086 bool KSocketBase::setAddressReuseable(bool enable)
    00087 {
    00088   return setSocketOptions((socketOptions() & ~AddressReuseable) | (enable ? AddressReuseable : 0));
    00089 }
    00090
    00091 bool KSocketBase::addressReuseable() const
    00092 {
    00093   return socketOptions() & AddressReuseable;
    00094 }
    00095
    00096 bool KSocketBase::setIPv6Only(bool enable)
    00097 {
    00098   return setSocketOptions((socketOptions() & ~IPv6Only) | (enable ? IPv6Only : 0));
    00099 }
    00100
    00101 bool KSocketBase::isIPv6Only() const
    00102 {
    00103   return socketOptions() & IPv6Only;
    00104 }
    00105
    00106 bool KSocketBase::setBroadcast(bool enable)
    00107 {
    00108   return setSocketOptions((socketOptions() & ~Broadcast) | (enable ? Broadcast : 0));
    00109 }
    00110
    00111 bool KSocketBase::broadcast() const
    00112 {
    00113   return socketOptions() & Broadcast;
    00114 }
    00115
    00116 KSocketDevice* KSocketBase::socketDevice() const
    00117 {
    00118   if (d->device)
    00119     return d->device;
    00120
    00121   // it doesn't exist, so create it
    00122   QMutexLocker locker(mutex());
    00123   if (d->device)
    00124     return d->device;
    00125
    00126   KSocketBase* that = const_cast<KSocketBase*>(this);
    00127   KSocketDevice* dev = 0;
    00128   if (d->capabilities)
    00129     dev = KSocketDevice::createDefault(that, d->capabilities);
    00130   if (!dev)
    00131     dev = KSocketDevice::createDefault(that);
    00132   that->setSocketDevice(dev);
    00133   return d->device;
    00134 }
    00135
    00136 void KSocketBase::setSocketDevice(KSocketDevice* device)
    00137 {
    00138   QMutexLocker locker(mutex());
    00139   if (d->device == 0L)
    00140     d->device = device;
    00141 }
    00142
    00143 int KSocketBase::setRequestedCapabilities(int add, int remove)
    00144 {
    00145   d->capabilities |= add;
    00146   d->capabilities &= ~remove;
    00147   return d->capabilities;
    00148 }
    00149
    00150 bool KSocketBase::hasDevice() const
    00151 {
    00152   return d->device != 0L;
    00153 }
    00154
    00155 void KSocketBase::setError(SocketError error)
    00156 {
    00157   d->socketError = error;
    00158 }
    00159
    00160 KSocketBase::SocketError KSocketBase::error() const
    00161 {
    00162   return static_cast<KSocketBase::SocketError>(d->socketError);
    00163 }
    00164
    00165 // static
    00166 QString KSocketBase::errorString(KSocketBase::SocketError code)
    00167 {
    00168   QString reason;
    00169   switch (code)
    00170     {
    00171     case NoError:
    00172       reason = i18n("Socket error code NoError", "no error");
    00173       break;
    00174
    00175     case LookupFailure:
    00176       reason = i18n("Socket error code LookupFailure",
    00177             "name lookup has failed");
    00178       break;
    00179
    00180     case AddressInUse:
    00181       reason = i18n("Socket error code AddressInUse",
    00182             "address already in use");
    00183       break;
    00184
    00185     case AlreadyBound:
    00186       reason = i18n("Socket error code AlreadyBound",
    00187             "socket is already bound");
    00188       break;
    00189
    00190     case AlreadyCreated:
    00191       reason = i18n("Socket error code AlreadyCreated",
    00192             "socket is already created");
    00193       break;
    00194
    00195     case NotBound:
    00196       reason = i18n("Socket error code NotBound",
    00197             "socket is not bound");
    00198       break;
    00199
    00200     case NotCreated:
    00201       reason = i18n("Socket error code NotCreated",
    00202             "socket has not been created");
    00203       break;
    00204
    00205     case WouldBlock:
    00206       reason = i18n("Socket error code WouldBlock",
    00207             "operation would block");
    00208       break;
    00209
    00210     case ConnectionRefused:
    00211       reason = i18n("Socket error code ConnectionRefused",
    00212             "connection actively refused");
    00213       break;
    00214
    00215     case ConnectionTimedOut:
    00216       reason = i18n("Socket error code ConnectionTimedOut",
    00217             "connection timed out");
    00218       break;
    00219
    00220     case InProgress:
    00221       reason = i18n("Socket error code InProgress",
    00222             "operation is already in progress");
    00223       break;
    00224
    00225     case NetFailure:
    00226       reason = i18n("Socket error code NetFailure",
    00227             "network failure occurred");
    00228       break;
    00229
    00230     case NotSupported:
    00231       reason = i18n("Socket error code NotSupported",
    00232             "operation is not supported");
    00233       break;
    00234
    00235     case Timeout:
    00236       reason = i18n("Socket error code Timeout",
    00237             "timed operation timed out");
    00238       break;
    00239
    00240     case UnknownError:
    00241       reason = i18n("Socket error code UnknownError",
    00242             "an unknown/unexpected error has happened");
    00243       break;
    00244
    00245     case RemotelyDisconnected:
    00246       reason = i18n("Socket error code RemotelyDisconnected",
    00247             "remote host closed connection");
    00248       break;
    00249
    00250     default:
    00251       reason = QString::null;
    00252       break;
    00253     }
    00254
    00255   return reason;
    00256 }
    00257
    00258 // static
    00259 bool KSocketBase::isFatalError(int code)
    00260 {
    00261   switch (code)
    00262     {
    00263     case WouldBlock:
    00264     case InProgress:
    00265     case NoError:
    00266     case RemotelyDisconnected:
    00267       return false;
    00268     }
    00269
    00270   return true;
    00271 }
    00272
    00273 void KSocketBase::unsetSocketDevice()
    00274 {
    00275   d->device = 0L;
    00276 }
    00277
    00278 QMutex* KSocketBase::mutex() const
    00279 {
    00280   return &d->mutex;
    00281 }
    00282
    00283 KActiveSocketBase::KActiveSocketBase()
    00284 {
    00285 }
    00286
    00287 KActiveSocketBase::~KActiveSocketBase()
    00288 {
    00289 }
    00290
    00291 int KActiveSocketBase::getch()
    00292 {
    00293   unsigned char c;
    00294   if (readBlock((char*)&c, 1) != 1)
    00295     return -1;
    00296
    00297   return c;
    00298 }
    00299
    00300 int KActiveSocketBase::putch(int ch)
    00301 {
    00302   unsigned char c = (unsigned char)ch;
    00303   if (writeBlock((char*)&c, 1) != 1)
    00304     return -1;
    00305
    00306   return c;
    00307 }
    00308
    00309 void KActiveSocketBase::setError(int status, SocketError error)
    00310 {
    00311   KSocketBase::setError(error);
    00312   setStatus(status);
    00313 }
    00314
    00315 void KActiveSocketBase::resetError()
    00316 {
    00317   KSocketBase::setError(NoError);
    00318   resetStatus();
    00319 }
    00320
    00321 KPassiveSocketBase::KPassiveSocketBase()
    00322 {
    00323 }
    00324
    00325 KPassiveSocketBase::~KPassiveSocketBase()
    00326 {
    00327 }

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:10 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003