From c90c389a8a8d9d8661e9772ec4144c5cf2039f23 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdegames@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- libksirtet/lib/socket.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 libksirtet/lib/socket.cpp (limited to 'libksirtet/lib/socket.cpp') diff --git a/libksirtet/lib/socket.cpp b/libksirtet/lib/socket.cpp new file mode 100644 index 00000000..9ef3ba3c --- /dev/null +++ b/libksirtet/lib/socket.cpp @@ -0,0 +1,80 @@ +#include "socket.h" + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#ifdef HAVE_SYS_SELECT_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_FILIO_H +#include // for FIONREAD +#endif + +Socket::Socket(KExtendedSocket *s, bool createNotifier, + QObject *parent, const char *name) +: _socket(s), _notifier(0) +{ + Q_ASSERT(s); + if (createNotifier) { + _notifier = new QSocketNotifier(s->fd(), QSocketNotifier::Read, + parent, name); + _notifier->setEnabled(FALSE); + } +} + +Socket::~Socket() +{ + delete _notifier; + delete _socket; +} + +bool Socket::write(const QByteArray &a) +{ + return ( _socket->writeBlock(a.data(), a.size())==(int)a.size() ); +} + +bool Socket::write() +{ + bool res = write(writing.buffer()); + writing.clear(); + return res; +} + +int Socket::pendingData() const +{ + int size = 0; + if ( ioctl(_socket->fd(), FIONREAD, (char *)&size)<0 ) return -1; + return size; +} + +int Socket::read() +{ + reading.clearRead(); + + int size = pendingData(); + if ( size==-1 ) return -1; + + reading.device()->close(); + int dec = reading.size(); + reading.buffer().resize(dec + size); + size = _socket->readBlock(reading.buffer().data() + dec, size); + if ( size==-1 ) reading.buffer().resize(dec); + reading.device()->open(IO_ReadOnly); + + return size; +} + +int Socket::accept(KExtendedSocket *&s) +{ + return _socket->accept(s); +} -- cgit v1.2.1