#ifndef QGREXRTGSOCKET_H
#define QGREXRTGSOCKET_H

#include "qglogger.h"
#include "rexrtgsocket.h"

#include <QUdpSocket>

class QNetworkDatagram;

class QgRexRtgSocket: public QObject, public RexRtgSocket
{
    Q_OBJECT

    Q_PROPERTY(bool connected READ connected)

public:

    QgRexRtgSocket(const QString& name, QObject* parent=0);
    virtual ~QgRexRtgSocket();

    bool localBind(unsigned int port=0);
    bool remoteBind(const QHostAddress& ha, unsigned int port=51000);

    bool connected() const { return m_connected;}

    bool send(unsigned int dst, const void* data, unsigned int len) override;

    //static void setNetworkInterface(const QHostAddress& adr);

    QUdpSocket& qtSocket() { return s;}

    bool sendMessage(unsigned int dst, unsigned int mailbox, const void* data, unsigned len);
    bool writeMemory(unsigned int dst, unsigned int address, const void* data, unsigned len, unsigned int doorbell);

public slots:
    void onError(QAbstractSocket::SocketError socketError);
    void onReceiveData();

signals:
    void connectedToHost(bool ok, QNetworkDatagram* d);

private:
    QgLogger logger;

    QUdpSocket s;
    QHostAddress remoteAddress;
    int remotePort;
    bool m_connected;

    class Implementation;
    Implementation& p_;

    Q_DISABLE_COPY(QgRexRtgSocket)
};

#if 0

class QgRexNetwork: public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString networkIP READ networkIP WRITE setNetworkIP)
    Q_PROPERTY(unsigned int networkMTU READ networkMTU WRITE setNetworkMTU)

public:
    QgRexNetwork(QObject* parent=0);
    virtual ~QgRexNetwork();

    void setNetworkInterface(const QHostAddress& adr);
    QHostAddress networkInterface();

    QHostAddress networkBroadcast();

    QString networkIP() const;
    void setNetworkIP(const QString& str);

    unsigned int networkMTU() const;
    void setNetworkMTU(unsigned int);
};
#endif


#endif // QGREXRTGSOCKET_H
