00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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)
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
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
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
default:
00246 reason = QString::null;
00247
break;
00248 }
00249
00250
return reason;
00251 }
00252
00253
00254 bool KSocketBase::isFatalError(
int code)
00255 {
00256
switch (code)
00257 {
00258
case WouldBlock:
00259
case InProgress:
00260
return false;
00261 }
00262
00263
return true;
00264 }
00265
00266
void KSocketBase::unsetSocketDevice()
00267 {
00268 d->device = 0L;
00269 }
00270
00271 QMutex*
KSocketBase::mutex()
const
00272
{
00273
return &d->mutex;
00274 }
00275
00276 KActiveSocketBase::KActiveSocketBase()
00277 {
00278 }
00279
00280 KActiveSocketBase::~KActiveSocketBase()
00281 {
00282 }
00283
00284 int KActiveSocketBase::getch()
00285 {
00286
unsigned char c;
00287
if (
readBlock((
char*)&c, 1) != 1)
00288
return -1;
00289
00290
return c;
00291 }
00292
00293 int KActiveSocketBase::putch(
int ch)
00294 {
00295
unsigned char c = (
unsigned char)ch;
00296
if (
writeBlock((
char*)&c, 1) != 1)
00297
return -1;
00298
00299
return c;
00300 }
00301
00302 void KActiveSocketBase::setError(
int status, SocketError error)
00303 {
00304
KSocketBase::setError(error);
00305 setStatus(status);
00306 }
00307
00308 void KActiveSocketBase::resetError()
00309 {
00310
KSocketBase::setError(NoError);
00311 resetStatus();
00312 }
00313
00314 KPassiveSocketBase::KPassiveSocketBase()
00315 {
00316 }
00317
00318 KPassiveSocketBase::~KPassiveSocketBase()
00319 {
00320 }