/****************************************************************************
**
** Copyright (C) 2024 Ivailo Monev
**
** This file is part of the QtNetwork module of the Katie Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QSOCKETDEVICE_H
#define QSOCKETDEVICE_H

#include <QtCore/qiodevice.h>
#include <QtNetwork/qhostaddress.h>

QT_BEGIN_NAMESPACE

class QSocketDevicePrivate;

class Q_NETWORK_EXPORT QSocketDevice : public QIODevice
{
    Q_OBJECT
public:
    explicit QSocketDevice(QObject *parent = nullptr);
    virtual ~QSocketDevice();

    bool init(const QHostAddress &address, const quint16 port);
    bool init(const QString &name);

    quint16 socketPort() const;
    QHostAddress socketAddress() const;
    QString socketName() const;
    int socketDescriptor() const;

    int socketOption(const int option);
    bool setSocketOption(const int option, const int optionvalue);

    virtual bool isSequential() const;
    virtual bool open(QIODevice::OpenMode mode);
    virtual void close();
    virtual qint64 bytesAvailable() const;
    virtual bool waitForReadyRead(int msecs);
    virtual bool waitForBytesWritten(int msecs);

protected:
    virtual qint64 readData(char *data, qint64 maxlen);
    virtual qint64 writeData(const char *data, qint64 len);

private:
    friend class QSocketServerPrivate;

    Q_DISABLE_COPY(QSocketDevice)
    Q_DECLARE_PRIVATE(QSocketDevice)

    void setupDescriptor(const int socket);
};

QT_END_NAMESPACE

#endif // QSOCKETDEVICE_H
