notify when data is read and written to QSocketDevice via signal

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-29 05:41:47 +03:00
parent 183646de08
commit ad07f18f70
2 changed files with 28 additions and 7 deletions

View file

@ -20,6 +20,7 @@
#include "qsocketdevice.h"
#include "qiodevice_p.h"
#include "qsocketnotifier.h"
#include "qfile.h"
#include "qdir.h"
#include "qnet_unix_p.h"
@ -55,6 +56,8 @@ public:
#endif
struct sockaddr* socketaddrptr;
QT_SOCKLEN_T socketaddrsize;
QSocketNotifier* socketnotifier;
};
QSocketDevicePrivate::QSocketDevicePrivate()
@ -62,7 +65,8 @@ QSocketDevicePrivate::QSocketDevicePrivate()
socketport(0),
socketaddrptr(nullptr),
socketaddrsize(0),
socketdomain(PF_UNSPEC)
socketdomain(PF_UNSPEC),
socketnotifier(nullptr)
{
::memset(&socketaddrun, 0, sizeof(struct sockaddr_un));
::memset(&socketaddripv4, 0, sizeof(struct sockaddr_in));
@ -207,6 +211,10 @@ void QSocketDevice::setSocketDescriptor(const int socket)
{
Q_D(QSocketDevice);
d->handle = socket;
if (d->socketnotifier) {
delete d->socketnotifier;
d->socketnotifier = nullptr;
}
int result = ::fcntl(d->handle, F_GETFL, 0);
if (result == -1) {
const int savederrno = errno;
@ -218,6 +226,11 @@ void QSocketDevice::setSocketDescriptor(const int socket)
const int savederrno = errno;
setErrorString(qt_error_string(savederrno));
}
d->socketnotifier = new QSocketNotifier(d->handle, QSocketNotifier::Read, this);
connect(
d->socketnotifier, SIGNAL(activated(int)),
this, SIGNAL(readyRead())
);
}
bool QSocketDevice::isSequential() const
@ -228,6 +241,9 @@ bool QSocketDevice::isSequential() const
bool QSocketDevice::open(QIODevice::OpenMode mode)
{
Q_D(QSocketDevice);
if (isOpen()) {
return true;
}
if (d->handle <= 0) {
#ifdef SOCK_NONBLOCK
d->handle = qt_safe_socket(d->socketdomain, SOCK_NONBLOCK | SOCK_STREAM, 0);
@ -240,6 +256,9 @@ bool QSocketDevice::open(QIODevice::OpenMode mode)
return false;
}
// setup
setSocketDescriptor(d->handle);
const int result = qt_safe_connect(d->handle, d->socketaddrptr, d->socketaddrsize);
if (result == -1) {
if (errno != EINPROGRESS) {
@ -271,7 +290,7 @@ qint64 QSocketDevice::bytesAvailable() const
if (result == -1) {
return 0;
}
return available;
return (QIODevice::bytesAvailable() + available);
}
bool QSocketDevice::waitForReadyRead(int msecs)
@ -285,9 +304,9 @@ bool QSocketDevice::waitForReadyRead(int msecs)
if (result == -1) {
const int savederrno = errno;
setErrorString(qt_error_string(savederrno));
return false;
close();
}
return true;
return (result > 0);
}
bool QSocketDevice::waitForBytesWritten(int msecs)
@ -301,9 +320,9 @@ bool QSocketDevice::waitForBytesWritten(int msecs)
if (result == -1) {
const int savederrno = errno;
setErrorString(qt_error_string(savederrno));
return false;
close();
}
return true;
return (result > 0);
}
qint64 QSocketDevice::readData(char *data, qint64 maxlen)
@ -330,6 +349,8 @@ qint64 QSocketDevice::writeData(const char *data, qint64 len)
if (result == -1) {
const int savederrno = errno;
setErrorString(qt_error_string(savederrno));
} else {
emit bytesWritten(result);
}
return result;
}

View file

@ -248,7 +248,7 @@ QSocketDevice* QSocketServer::acceptConnection()
} else {
device->init(d->socketaddress, d->socketport);
}
const int result = qt_safe_accept(d->handle, 0, 0);
const int result = qt_safe_accept(d->handle, d->socketaddrptr, &d->socketaddrsize);
if (result == -1) {
const int savederrno = errno;
d->errorstring = qt_error_string(savederrno);