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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "msocketdevice.h"
00045
00046 #include "qwindowdefs.h"
00047 #include <string.h>
00048
00049
00050
00051
00052 class MSocketDevicePrivate
00053 {
00054
00055 public:
00056 MSocketDevicePrivate(MSocketDevice::Protocol p)
00057 : protocol(p)
00058 { }
00059
00060 MSocketDevice::Protocol protocol;
00061 };
00062
00063
00159 MSocketDevice::MSocketDevice(int socket, Type type)
00160 : fd(socket), t(type), p(0), pp(0), e(NoError),
00161 d(new MSocketDevicePrivate(Unknown))
00162 {
00163 #if defined(MSOCKETDEVICE_DEBUG)
00164 qDebug("MSocketDevice: Created MSocketDevice %p (socket %x, type %d)",
00165 this, socket, type);
00166 #endif
00167 init();
00168 setSocket(socket, type);
00169 }
00170
00182 MSocketDevice::MSocketDevice(Type type)
00183 : fd(-1), t(type), p(0), pp(0), e(NoError),
00184 d(new MSocketDevicePrivate(Unknown))
00185 {
00186 #if defined(MSOCKETDEVICE_DEBUG)
00187 qDebug("MSocketDevice: Created MSocketDevice object %p, type %d",
00188 this, type);
00189 #endif
00190 init();
00191 setSocket(createNewSocket(), type);
00192 }
00193
00211 MSocketDevice::MSocketDevice(Type type, Protocol protocol, int)
00212 : fd(-1), t(type), p(0), pp(0), e(NoError),
00213 d(new MSocketDevicePrivate(protocol))
00214 {
00215 #if defined(MSOCKETDEVICE_DEBUG)
00216 qDebug("MSocketDevice: Created MSocketDevice object %p, type %d",
00217 this, type);
00218 #endif
00219 init();
00220 setSocket(createNewSocket(), type);
00221 }
00222
00226 MSocketDevice::~MSocketDevice()
00227 {
00228 close();
00229 delete d;
00230 d = 0;
00231 #if defined(MSOCKETDEVICE_DEBUG)
00232 qDebug("MSocketDevice: Destroyed MSocketDevice %p", this);
00233 #endif
00234 }
00235
00236
00242 bool MSocketDevice::isValid() const
00243 {
00244 return fd != -1;
00245 }
00246
00247
00256 MSocketDevice::Type MSocketDevice::type() const
00257 {
00258 return t;
00259 }
00260
00273 MSocketDevice::Protocol MSocketDevice::protocol() const
00274 {
00275 if (d->protocol == Unknown)
00276 d->protocol = getProtocol();
00277
00278 return d->protocol;
00279 }
00280
00281 void MSocketDevice::setProtocol(Protocol protocol)
00282 {
00283 d->protocol = protocol;
00284 }
00285
00291 int MSocketDevice::socket() const
00292 {
00293 return fd;
00294 }
00295
00296
00310 void MSocketDevice::setSocket(int socket, Type type)
00311 {
00312 if (fd != -1)
00313 close();
00314
00315 #if defined(MSOCKETDEVICE_DEBUG)
00316 qDebug("MSocketDevice::setSocket: socket %x, type %d", socket, type);
00317
00318 #endif
00319 t = type;
00320
00321 fd = socket;
00322
00323 d->protocol = Unknown;
00324
00325 e = NoError;
00326
00327
00328 open(ReadWrite);
00329
00330 fetchConnectionParameters();
00331 }
00332
00333
00341 bool MSocketDevice::open(OpenMode mode)
00342 {
00343 if (isOpen() || !isValid())
00344 return false;
00345
00346 #if defined(MSOCKETDEVICE_DEBUG)
00347 qDebug("MSocketDevice::open: mode %x", mode);
00348
00349 #endif
00350 setOpenMode((mode & ReadWrite) | Unbuffered);
00351
00352 return true;
00353 }
00354
00363 bool MSocketDevice::flush()
00364 {
00365 return true;
00366 }
00367
00368
00374 qint64 MSocketDevice::size() const
00375 {
00376 return 0;
00377 }
00378
00379
00384 qint64 MSocketDevice::pos() const
00385 {
00386 return 0;
00387 }
00388
00389
00396 bool MSocketDevice::seek(qint64 )
00397 {
00398 return true;
00399 }
00400
00401
00408 bool MSocketDevice::atEnd() const
00409 {
00410 return bytesAvailable() <= 0;
00411 }
00412
00418 bool MSocketDevice::broadcast() const
00419 {
00420 return option(Broadcast);
00421 }
00422
00428 void MSocketDevice::setBroadcast(bool enable)
00429 {
00430 setOption(Broadcast, enable);
00431 }
00432
00440 bool MSocketDevice::addressReusable() const
00441 {
00442 return option(ReuseAddress);
00443 }
00444
00445
00459 void MSocketDevice::setAddressReusable(bool enable)
00460 {
00461 setOption(ReuseAddress, enable);
00462 }
00463
00464
00470 bool MSocketDevice::keepalive() const
00471 {
00472 return option(Keepalive);
00473 }
00474
00480 void MSocketDevice::setKeepalive(bool enable)
00481 {
00482 setOption(Keepalive, enable);
00483 }
00484
00485
00491 int MSocketDevice::receiveBufferSize() const
00492 {
00493 return option(ReceiveBuffer);
00494 }
00495
00496
00508 void MSocketDevice::setReceiveBufferSize(uint size)
00509 {
00510 setOption(ReceiveBuffer, size);
00511 }
00512
00513
00519 int MSocketDevice::sendBufferSize() const
00520 {
00521 return option(SendBuffer);
00522 }
00523
00524
00535 void MSocketDevice::setSendBufferSize(uint size)
00536 {
00537 setOption(SendBuffer, size);
00538 }
00539
00540
00549 quint16 MSocketDevice::port() const
00550 {
00551 return p;
00552 }
00553
00554
00560 QHostAddress MSocketDevice::address() const
00561 {
00562
00563 QString ipaddress;
00564
00565 if (a.toString().startsWith("0:0:0:0:0:FFFF:"))
00566 {
00567 Q_IPV6ADDR addr = a.toIPv6Address();
00568
00569
00570 ipaddress = QString("%1.%2.%3.%4").arg(addr[12]).arg(addr[13]).arg(addr[14]).arg(addr[15]);
00571 }
00572 else
00573 ipaddress = a.toString();
00574
00575 return QHostAddress(ipaddress);
00576 }
00577
00578
00582 MSocketDevice::Error MSocketDevice::error() const
00583 {
00584 return e;
00585 }
00586
00587
00591 void MSocketDevice::setError(Error err)
00592 {
00593 e = err;
00594 }
00595