This commit is contained in:
curtis
2024-11-26 16:21:02 +08:00
parent a8eaaf6d0e
commit 2ea5d0b7eb
35 changed files with 1321 additions and 29 deletions

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file QIntPtr.cpp
* @brief QObject wrapper for qintptr
* @author Ayberk Özgür
* @date 2016-11-15
*/
#include"QIntPtr.h"
namespace QMLTcpSockets{
QIntPtr::QIntPtr(QObject* parent) : QObject(parent){
ptr = 0;
}
QIntPtr::~QIntPtr(){ }
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file QIntPtr.h
* @brief QObject wrapper for qintptr
* @author Ayberk Özgür
* @date 2016-11-15
*/
#ifndef QINTPTR_H
#define QINTPTR_H
#include <QObject>
namespace QMLTcpSockets{
/**
* @brief QObject wrapper for qintptr. Can be passed around in QML but doesn't have any API.
*/
class QIntPtr : public QObject {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
public:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Creates a new QIntPtr with the given Qt parent
*
* @param parent The Qt parent
*/
QIntPtr(QObject* parent = 0);
/**
* @brief Destroys this QIntPtr
*/
~QIntPtr();
qintptr ptr; ///< Wrapped low level pointer
/** @endcond */
};
}
Q_DECLARE_METATYPE(QMLTcpSockets::QIntPtr*)
#endif /* QINTPTR_H */

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file QMLTcpSocketsPlugin.h
* @brief Object that exposes the QMLTcpSockets plugin components as QML objects
* @author Ayberk Özgür
* @date 2016-11-10
*/
#include "QMLTcpSocketsPlugin.h"
#include "TcpServer.h"
#include "TcpSocket.h"
#include "QIntPtr.h"
#include "TcpSocketFactory.h"
#include "TcpSocketEnums.h"
namespace QMLTcpSockets{
void QMLTcpSocketsPlugin::registerTypes(const char* uri){
qmlRegisterType<TcpServer>(uri, 1, 0, "TcpServer");
qmlRegisterType<TcpSocket>(uri, 1, 0, "TcpSocket");
qmlRegisterType<QIntPtr>(uri, 1, 0, "QIntPtr");
qmlRegisterSingletonType<TcpSocketFactory>(uri, 1, 0, "TcpSocketFactory",
[] (QQmlEngine* qmlEngine, QJSEngine* jsEngine)->QObject* {
Q_UNUSED(qmlEngine)
Q_UNUSED(jsEngine)
return new TcpSocketFactory();
});
qmlRegisterType<TcpSocketEnums>(uri, 1, 0, "TcpSocketEnums");
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @mainpage qml-tcpsockets
*
* qml-tcpsockets contains QML wrappers for QTcpServer and QTcpSocket. More Qt Networking wrappers might come soon. See README.md for more information.
*/
/**
* @file QMLTcpSocketsPlugin.h
* @brief Object that exposes the QMLTcpSockets plugin components as QML objects
* @author Ayberk Özgür
* @date 2016-11-10
*/
#ifndef QMLTCPSOCKETSPLUGIN_H
#define QMLTCPSOCKETSPLUGIN_H
#include <QQmlExtensionPlugin>
#include <qqml.h>
namespace QMLTcpSockets{
/** @cond DO_NOT_DOCUMENT */
class QMLTcpSocketsPlugin : public QQmlExtensionPlugin {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char* uri);
};
/** @endcond */
}
#endif /* QMLTCPSOCKETSPLUGIN_H */

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file QTcpServerPub.cpp
* @brief A QTcpServer that emits incomingConnection() as signal (duh)
* @author Ayberk Özgür
* @date 2016-11-15
*/
#include"QTcpServerPub.h"
namespace QMLTcpSockets{
QTcpServerPub::QTcpServerPub(QObject* parent) : QTcpServer(parent){ }
void QTcpServerPub::incomingConnection(qintptr socketDescriptor){
emit incomingConnectionSignal(socketDescriptor);
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file QTcpServerPub.h
* @brief A QTcpServer that emits incomingConnection() as signal (duh)
* @author Ayberk Özgür
* @date 2016-11-15
*/
#ifndef QTCPSERVERPUB_H
#define QTCPSERVERPUB_H
#include <QTcpServer>
namespace QMLTcpSockets{
/** @cond DO_NOT_DOCUMENT */
/**
* @brief A QTcpServer that emits incomingConnection() as signal (duh)
*/
class QTcpServerPub : public QTcpServer {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
public:
/**
* @brief Creates a new QTcpServerPub with the given Qt parent
*
* @param parent The Qt parent
*/
QTcpServerPub(QObject* parent = 0);
signals:
/**
* @brief Emitted when there's a new incoming connection
*
* @param socketDescriptor Descriptor of the incoming connection that can be used to create a QTcpSocket
*/
void incomingConnectionSignal(qintptr socketDescriptor);
protected:
/**
* @brief Called by the super class when there's a new incoming connection
*
* @param socketDescriptor Descriptor of the incoming connection that can be used to create a QTcpSocket
*/
void incomingConnection(qintptr socketDescriptor) override;
};
/** @endcond */
}
#endif /* QTCPSERVERPUB_H */

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpServer.cpp
* @brief QML wrapper source for QTcpServer
* @author Ayberk Özgür
* @date 2016-11-10
*/
#include "TcpServer.h"
namespace QMLTcpSockets{
TcpServer::TcpServer(QQuickItem* parent):
QQuickItem(parent),
server(this)
{
host = "0.0.0.0";
port = 0;
connect(&server, SIGNAL(incomingConnectionSignal(qintptr)), this, SLOT(publishIncomingConnection(qintptr)));
}
TcpServer::~TcpServer(){}
bool TcpServer::isListening() const{
return server.isListening();
}
void TcpServer::setListening(bool enable){
bool wasListening = server.isListening();
if(enable){
if(!server.listen(QHostAddress(host), port))
qWarning() << "TcpServer::setListening(): Couldn't start listening: " << server.errorString();
}
else
server.close();
if(wasListening != server.isListening())
emit listeningChanged();
}
void TcpServer::setHost(QString host){
if(host != this->host){
bool wasListening = isListening();
setListening(false);
this->host = host;
if(wasListening)
setListening(true);
emit hostChanged();
}
}
void TcpServer::setPort(int port){
if(port < 0){
qWarning() << "TcpServer::setPort(): port given was negative, setting to 0.";
port = 0;
}
else if(port > 0xFFFF){
qWarning() << "TcpServer::setPort(): port given was larger than 65535, setting to 65535.";
port = 0xFFFF;
}
if(port != this->port){
bool wasListening = isListening();
setListening(false);
this->port = port;
if(wasListening)
setListening(true);
emit portChanged();
}
}
void TcpServer::publishIncomingConnection(qintptr socketDescriptor){
QIntPtr* wrappedSocketDesc = new QIntPtr();
wrappedSocketDesc->ptr = socketDescriptor;
emit newConnection(wrappedSocketDesc);
}
}

View File

@@ -0,0 +1,160 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpServer.h
* @brief QML wrapper header for QTcpServer
* @author Ayberk Özgür
* @date 2016-11-10
*/
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QQuickItem>
#include "QTcpServerPub.h"
#include "TcpSocket.h"
#include "QIntPtr.h"
namespace QMLTcpSockets{
/**
* @brief QML wrapper for QTcpServer
*
* Due to the sandboxing of WinRT, cannot listen on nor connect to localhost sockets on this platform.
*/
class TcpServer : public QQuickItem {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
/** @brief Whether to listen for incoming connections, default false */
Q_PROPERTY(bool listen READ isListening WRITE setListening NOTIFY listeningChanged)
/** @brief Address to listen on, default "0.0.0.0" i.e any address */
Q_PROPERTY(QString host READ getHost WRITE setHost NOTIFY hostChanged)
/** @brief Port to listen on, must be in [0,65535], default 0 */
Q_PROPERTY(int port READ getPort WRITE setPort NOTIFY portChanged)
public:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Creates a new TcpServer with the given QML parent
*
* @param parent The QML parent
*/
TcpServer(QQuickItem* parent = 0);
/**
* @brief Destroys this TcpServer
*/
~TcpServer();
/**
* @brief Gets whether the socket is listening
*
* @return Whether the socket is listening
*/
bool isListening() const;
/**
* @brief Enables/disables listening
*
* @param enable Whether to listen or close the socket and stop listening
*/
void setListening(bool enable);
/**
* @brief Gets the current host name
*
* @return Current host name, e.g "127.0.0.1"
*/
QString getHost() const { return host; }
/**
* @brief Sets the host name
*
* @param host The new host name, e.g "127.0.0.1"
*/
void setHost(QString host);
/**
* @brief Gets the current port
*
* @return Current port
*/
int getPort() const { return port; }
/**
* @brief Sets the port
*
* @param port The new port, must be in [0,65535]
*/
void setPort(int port);
/** @endcond */
signals:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Emitted when listening changes
*/
void listeningChanged();
/**
* @brief Emitted when the host name changes
*/
void hostChanged();
/**
* @brief Emitted whe the port changes
*/
void portChanged();
/** @endcond */
/**
* @brief Emitted when a new connection is made to the server, socketDescriptor must be passed to a TcpSocket or a TcpSocketFactory to create the socket
*
* @param socketDescriptor Native descriptor to the socket that has just connected
*/
void newConnection(QMLTcpSockets::QIntPtr* socketDescriptor);
private slots:
/**
* @brief Gets the incoming connection descriptor, wraps it in a QIntPtr and publishes it
*/
void publishIncomingConnection(qintptr socketDescriptor);
private:
QString host; ///< Host address, e.g "127.0.0.1"
quint16 port; ///< Port to listen to
QTcpServerPub server; ///< The QTcpServer object to wrap
};
}
#endif /* TCPSERVER_H */

View File

@@ -0,0 +1,160 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpSocket.cpp
* @brief QML wrapper source for QTcpSocket
* @author Ayberk Özgür
* @date 2016-11-10
*/
#include "TcpSocket.h"
#include <QHostAddress>
namespace QMLTcpSockets{
TcpSocket::TcpSocket(QQuickItem* parent):
QQuickItem(parent),
socket(this)
{
connect(&socket, SIGNAL(connected()), this, SIGNAL(connected()));
connect(&socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(emitErrorAsInt(QAbstractSocket::SocketError)));
connect(&socket, SIGNAL(readyRead()), this, SLOT(publish()));
peer = "";
port = 0;
}
TcpSocket::TcpSocket(QIntPtr* socketDescriptor, QQuickItem* parent):
TcpSocket(parent)
{
socket.setSocketDescriptor(socketDescriptor->ptr);
socketDescriptor->deleteLater();
peer = socket.peerAddress().toString();
port = socket.peerPort();
}
TcpSocket::~TcpSocket(){
socket.flush();
}
void TcpSocket::setSocketDescriptor(QIntPtr* socketDescriptor){
socket.flush();
socket.disconnectFromHost();
socket.setSocketDescriptor(socketDescriptor->ptr);
socketDescriptor->deleteLater();
QString peer = socket.peerAddress().toString();
if(this->peer != peer){
this->peer = peer;
emit peerChanged();
}
quint16 port = socket.peerPort();
if(this->port != port){
this->port = port;
emit portChanged();
}
emit connected();
}
void TcpSocket::setSocketOption(int option, QVariant value){
socket.setSocketOption((QAbstractSocket::SocketOption)option, value);
}
void TcpSocket::setPeer(QString peer){
if(peer != this->peer){
if(socket.state() != TcpSocketEnums::UnconnectedState)
qWarning() << "TcpSocket::setPeer(): Can only set peer while disconnected.";
else{
this->peer = peer;
emit peerChanged();
}
}
}
void TcpSocket::setPort(int port){
if(port < 0){
qWarning() << "TcpSocket::setPort(): port given was negative, setting to 0.";
port = 0;
}
else if(port > 0xFFFF){
qWarning() << "TcpSocket::setPort(): port given was larger than 65535, setting to 65535.";
port = 0xFFFF;
}
if(port != this->port){
if(socket.state() != TcpSocketEnums::UnconnectedState)
qWarning() << "TcpSocket::setPort(): Can only set port while disconnected.";
else{
this->port = port;
emit portChanged();
}
}
}
void TcpSocket::connectToHost(){
socket.connectToHost(peer, port);
}
void TcpSocket::disconnectFromHost(){
socket.disconnectFromHost();
}
void TcpSocket::emitErrorAsInt(QAbstractSocket::SocketError socketError){
emit error(socketError);
}
void TcpSocket::publish(){
QList<int> list;
QByteArray receivedBytes = socket.readAll();
for(char c : receivedBytes)
list << (int)(*((unsigned char*)(&c)));
emit bytesReceived(list);
}
bool TcpSocket::writeBytes(QList<int> bytes){
QByteArray container;
for(int b : bytes){
if(b < 0 || b > 0xFF){
qWarning() << "TcpSocket::writeBytes(): bytes contain integer not in [0x00,0xFF], not writing";
return false;
}
else
container.append((char)b);
}
int numBytesToWrite = container.size();
const char* bytesRaw = container.constData();
while(numBytesToWrite > 0){
int bytesWritten = socket.write(bytesRaw, numBytesToWrite);
if(bytesWritten < 0){
qWarning() << "TcpSocket::writeBytes(): Bytes were not written: " << socket.errorString();
return false;
}
numBytesToWrite -= bytesWritten;
bytesRaw += bytesWritten;
}
socket.flush();
return true;
}
}

View File

@@ -0,0 +1,209 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpSocket.h
* @brief QML wrapper header for QTcpSocket
* @author Ayberk Özgür
* @date 2016-11-10
*/
#ifndef TCPSOCKET_H
#define TCPSOCKET_H
#include <QQuickItem>
#include <QTcpSocket>
#include <QList>
#include "QIntPtr.h"
#include "TcpSocketEnums.h"
namespace QMLTcpSockets{
/**
* @brief QML wrapper for QTcpSocket
*
* Can be created from a native socket descriptor returned from a TcpServer incoming connection, or can connect to a peer on its own.
* Due to the sandboxing of WinRT, cannot listen on nor connect to localhost sockets on this platform.
*/
class TcpSocket : public QQuickItem {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
/** @brief Address to connect to, or address of the already open remote connection, default "" */
Q_PROPERTY(QString peer READ getPeer WRITE setPeer NOTIFY peerChanged)
/** @brief Port to connect to, or port of the already open remote connection , must be in [0,65535], default 0 */
Q_PROPERTY(int port READ getPort WRITE setPort NOTIFY portChanged)
public:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Creates a new TcpSocket with the given QML parent
*
* @param parent The QML parent
*/
TcpSocket(QQuickItem* parent = 0);
/**
* @brief Creates a new TcpSocket with the given QML parent, calls socketDescriptor->deleteLater()
*
* @param socketDescriptor Descriptor to the external native socket that was opened outside this wrapper, calls deleteLater() on it in the end
* @param parent The QML parent
*/
TcpSocket(QMLTcpSockets::QIntPtr* socketDescriptor, QQuickItem* parent = 0);
/**
* @brief Destroys this TcpSocket
*/
~TcpSocket();
/**
* @brief Gets the current peer name
*
* @return Current peer name, e.g "127.0.0.1"
*/
QString getPeer() const { return peer; }
/**
* @brief Sets the peer name
*
* @param peer The new peer name, e.g "127.0.0.1"
*/
void setPeer(QString peer);
/**
* @brief Gets the current port
*
* @return Current port
*/
int getPort() const { return port; }
/**
* @brief Sets the port
*
* @param port The new port, must be in [0,65535]
*/
void setPort(int port);
/** @endcond */
public slots:
/**
* @brief Initializes the socket with the given native descriptor (returned from TcpServer), calls socketDescriptor->deleteLater() in the end
*
* @param socketDescriptor Native socket descriptor, calls deleterLater() on it in the end
*/
void setSocketDescriptor(QMLTcpSockets::QIntPtr* socketDescriptor);
/**
* Sets the given low level option to the value described by value.
*
* @param option The option of TcpSocketEnums::SocketOption type (see http://doc.qt.io/qt-5/qabstractsocket.html#SocketOption-enum)
* @param value The value
*/
void setSocketOption(int option, QVariant value);
/**
* @brief Initiates a connection to the peer on port
*/
void connectToHost();
/**
* @brief Starts closing the socket
*/
void disconnectFromHost();
/**
* @brief Writes bytes over the socket
*
* @param bytes Bytes to write, all must be within [0x00,0xFF]
* @return Whether all bytes were successfully written
*/
bool writeBytes(QList<int> bytes);
signals:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Emitted when the peer name changes
*/
void peerChanged();
/**
* @brief Emitted whe the port changes
*/
void portChanged();
/** @endcond */
/**
* @brief Emitted when the socket is connected
*/
void connected();
/**
* @brief Emitted when the socket is disconected
*/
void disconnected();
/**
* @brief Emitted when there is an error
*
* @param socketError The error of TcpSocketEnums::SocketError type (see http://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum)
*/
void error(int socketError);
/**
* @brief Emitted when some bytes are received
*
* @param bytes Byte array that was received, all elements are guaranteed to be in [0x00, 0xFF]
*/
void bytesReceived(QList<int> bytes);
private slots:
/**
* @brief Publishes all available received bytes via bytesReceived(QVariant)
*/
void publish();
/**
* @brief Emits error(int socketError)
*
* @param socketError The socket error to emit
*/
void emitErrorAsInt(QAbstractSocket::SocketError socketError);
private:
QTcpSocket socket; ///< The low level socket
QString peer; ///< Peer address
quint16 port; ///< Connection port
};
}
Q_DECLARE_METATYPE(QMLTcpSockets::TcpSocket*)
#endif /* TCPSOCKET_H */

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpSocketEnums.cpp
* @brief Wrapper to expose QAbstractSocket enums
* @author Ayberk Özgür
* @date 2016-11-16
*/
#include "TcpSocketEnums.h"
namespace QMLTcpSockets{
TcpSocketEnums::TcpSocketEnums(QObject* parent) : QAbstractSocket(QAbstractSocket::TcpSocket, parent){ }
TcpSocketEnums::~TcpSocketEnums(){ }
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpSocketEnums.h
* @brief Wrapper to expose QAbstractSocket enums
* @author Ayberk Özgür
* @date 2016-11-16
*/
#ifndef TCPSOCKETENUMS_H
#define TCPSOCKETENUMS_H
#include <QAbstractSocket>
namespace QMLTcpSockets{
/**
* @brief Wrapper to expose QAbstractSocket enums, see http://doc.qt.io/qt-5/qabstractsocket.html
*/
class TcpSocketEnums : public QAbstractSocket {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
public:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Creates a new TcpSocketEnums with the given Qt parent
*
* @param parent The Qt parent
*/
TcpSocketEnums(QObject* parent = 0);
/**
* @brief Destroys this TcpSocketEnums
*/
~TcpSocketEnums();
/** @endcod */
};
}
#endif /* TCPSOCKETENUMS_H */

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpSocketFactory.cpp
* @brief Singleton TcpSocket factory for QML
* @author Ayberk Özgür
* @date 2016-11-15
*/
#include"TcpSocketFactory.h"
namespace QMLTcpSockets{
TcpSocketFactory::TcpSocketFactory(QQuickItem* parent) : QQuickItem(parent){ }
TcpSocketFactory::~TcpSocketFactory(){ }
TcpSocket* TcpSocketFactory::fromDescriptor(QIntPtr* socketDescriptor, QQuickItem* parent){
return new TcpSocket(socketDescriptor, parent);
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2016 EPFL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
/**
* @file TcpSocketFactory.h
* @brief Singleton TcpSocket factory for QML
* @author Ayberk Özgür
* @date 2016-11-15
*/
#ifndef TCPSOCKETFACTORY_H
#define TCPSOCKETFACTORY_H
#include <QQuickItem>
#include "TcpSocket.h"
#include "QIntPtr.h"
namespace QMLTcpSockets{
/**
* @brief Creates new TcpSocket from native socket descriptor (returned from a TcpServer incoming connection).
* @singleton
*/
class TcpSocketFactory : public QQuickItem {
/* *INDENT-OFF* */
Q_OBJECT
/* *INDENT-ON* */
public:
/** @cond DO_NOT_DOCUMENT */
/**
* @brief Creates a new TcpSocketFactory with the given QML parent
*
* @param parent The QML parent
*/
TcpSocketFactory(QQuickItem* parent = 0);
/**
* @brief Destroys this TcpSocketFactory
*/
~TcpSocketFactory();
/** @endcond */
public slots:
/**
* @brief Creates and returns a new open socket that wraps the native socket descriptor, with the given QML parent (optional)
*
* @param socketDescriptor Native socket descriptor
* @param parent QML parent of the newly created TcpSocket
* @return New socket, open if the descriptor is valid
*/
static QMLTcpSockets::TcpSocket* fromDescriptor(QMLTcpSockets::QIntPtr* socketDescriptor, QQuickItem* parent = 0);
};
}
#endif /* TCPSOCKETFACTORY_H */