From 75a9bd2a4f637fb8e8e3dc4609a7045547119e80 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Wed, 24 Sep 2014 17:31:33 +0300 Subject: Introduce SCTP sockets support Add protocol-specific code and the QSctpServer, QSctpSocket classes. Change-Id: Ie9a1d87bd1fda866a2405043d1c15c12ded5a96e Reviewed-by: Thiago Macieira --- examples/network/multistreamserver/animation.gif | Bin 0 -> 42629 bytes .../network/multistreamserver/chatprovider.cpp | 67 +++++++++ examples/network/multistreamserver/chatprovider.h | 57 ++++++++ examples/network/multistreamserver/main.cpp | 51 +++++++ .../network/multistreamserver/movieprovider.cpp | 63 ++++++++ examples/network/multistreamserver/movieprovider.h | 63 ++++++++ .../multistreamserver/multistreamserver.pro | 24 ++++ examples/network/multistreamserver/provider.h | 66 +++++++++ examples/network/multistreamserver/server.cpp | 159 +++++++++++++++++++++ examples/network/multistreamserver/server.h | 89 ++++++++++++ .../network/multistreamserver/timeprovider.cpp | 67 +++++++++ examples/network/multistreamserver/timeprovider.h | 55 +++++++ 12 files changed, 761 insertions(+) create mode 100644 examples/network/multistreamserver/animation.gif create mode 100644 examples/network/multistreamserver/chatprovider.cpp create mode 100644 examples/network/multistreamserver/chatprovider.h create mode 100644 examples/network/multistreamserver/main.cpp create mode 100644 examples/network/multistreamserver/movieprovider.cpp create mode 100644 examples/network/multistreamserver/movieprovider.h create mode 100644 examples/network/multistreamserver/multistreamserver.pro create mode 100644 examples/network/multistreamserver/provider.h create mode 100644 examples/network/multistreamserver/server.cpp create mode 100644 examples/network/multistreamserver/server.h create mode 100644 examples/network/multistreamserver/timeprovider.cpp create mode 100644 examples/network/multistreamserver/timeprovider.h (limited to 'examples/network/multistreamserver') diff --git a/examples/network/multistreamserver/animation.gif b/examples/network/multistreamserver/animation.gif new file mode 100644 index 0000000000..f674369efc Binary files /dev/null and b/examples/network/multistreamserver/animation.gif differ diff --git a/examples/network/multistreamserver/chatprovider.cpp b/examples/network/multistreamserver/chatprovider.cpp new file mode 100644 index 0000000000..9a88d491ab --- /dev/null +++ b/examples/network/multistreamserver/chatprovider.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "chatprovider.h" +#include +#include +#include + +ChatProvider::ChatProvider(QObject *parent) + : Provider(parent) +{ +} + +void ChatProvider::readDatagram(QSctpSocket &from, const QByteArray &ba) +{ + emit writeDatagram(0, QString(QLatin1String("<%1:%2> %3")) + .arg(from.peerAddress().toString()) + .arg(QString::number(from.peerPort())) + .arg(QString::fromUtf8(ba)).toUtf8()); +} + +void ChatProvider::newConnection(QSctpSocket &client) +{ + readDatagram(client, QString(tr("has joined")).toUtf8()); +} + +void ChatProvider::clientDisconnected(QSctpSocket &client) +{ + readDatagram(client, QString(tr("has left")).toUtf8()); +} diff --git a/examples/network/multistreamserver/chatprovider.h b/examples/network/multistreamserver/chatprovider.h new file mode 100644 index 0000000000..912101ec34 --- /dev/null +++ b/examples/network/multistreamserver/chatprovider.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CHATPROVIDER_H +#define CHATPROVIDER_H + +#include "provider.h" + +class ChatProvider : public Provider +{ + Q_OBJECT +public: + explicit ChatProvider(QObject *parent = nullptr); + + void readDatagram(QSctpSocket &from, const QByteArray &ba) Q_DECL_OVERRIDE; + void newConnection(QSctpSocket &client) Q_DECL_OVERRIDE; + void clientDisconnected(QSctpSocket &client) Q_DECL_OVERRIDE; +}; + +#endif diff --git a/examples/network/multistreamserver/main.cpp b/examples/network/multistreamserver/main.cpp new file mode 100644 index 0000000000..04350cd13d --- /dev/null +++ b/examples/network/multistreamserver/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "server.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Server server; + + return (server.exec() == QDialog::Accepted) ? 0 : -1; +} diff --git a/examples/network/multistreamserver/movieprovider.cpp b/examples/network/multistreamserver/movieprovider.cpp new file mode 100644 index 0000000000..cc534e6f40 --- /dev/null +++ b/examples/network/multistreamserver/movieprovider.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "movieprovider.h" +#include +#include +#include + +MovieProvider::MovieProvider(QObject *parent) + : Provider(parent) +{ + movie = new QMovie(this); + movie->setCacheMode(QMovie::CacheAll); + movie->setFileName(QLatin1String("animation.gif")); + connect(movie, &QMovie::frameChanged, this, &MovieProvider::frameChanged); + movie->start(); +} + +void MovieProvider::frameChanged() +{ + QByteArray buf; + QDataStream ds(&buf, QIODevice::WriteOnly); + + ds << movie->currentImage(); + emit writeDatagram(0, buf); +} diff --git a/examples/network/multistreamserver/movieprovider.h b/examples/network/multistreamserver/movieprovider.h new file mode 100644 index 0000000000..d6d8fb7449 --- /dev/null +++ b/examples/network/multistreamserver/movieprovider.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MOVIEPROVIDER_H +#define MOVIEPROVIDER_H + +#include "provider.h" + +QT_BEGIN_NAMESPACE +class QMovie; +QT_END_NAMESPACE + +class MovieProvider : public Provider +{ + Q_OBJECT +public: + explicit MovieProvider(QObject *parent = nullptr); + +private slots: + void frameChanged(); + +private: + QMovie *movie; +}; + +#endif diff --git a/examples/network/multistreamserver/multistreamserver.pro b/examples/network/multistreamserver/multistreamserver.pro new file mode 100644 index 0000000000..75a7e6bbec --- /dev/null +++ b/examples/network/multistreamserver/multistreamserver.pro @@ -0,0 +1,24 @@ +QT += network widgets + +HEADERS = server.h \ + provider.h \ + movieprovider.h \ + timeprovider.h \ + chatprovider.h +SOURCES = server.cpp \ + movieprovider.cpp \ + timeprovider.cpp \ + chatprovider.cpp \ + main.cpp + +EXAMPLE_FILES = animation.gif + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/network/multistreamserver +INSTALLS += target + +wince*: { + addFiles.files += *.gif + addFiles.path = . + DEPLOYMENT += addFiles +} diff --git a/examples/network/multistreamserver/provider.h b/examples/network/multistreamserver/provider.h new file mode 100644 index 0000000000..c6ec612275 --- /dev/null +++ b/examples/network/multistreamserver/provider.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PROVIDER_H +#define PROVIDER_H + +#include + +QT_BEGIN_NAMESPACE +class QSctpSocket; +class QByteArray; +QT_END_NAMESPACE + +class Provider : public QObject +{ + Q_OBJECT + +public: + explicit inline Provider(QObject *parent = nullptr) : QObject(parent) { } + + virtual void readDatagram(QSctpSocket &, const QByteArray &) { } + virtual void newConnection(QSctpSocket &) { } + virtual void clientDisconnected(QSctpSocket &) { } + +signals: + void writeDatagram(QSctpSocket *to, const QByteArray &ba); +}; + +#endif diff --git a/examples/network/multistreamserver/server.cpp b/examples/network/multistreamserver/server.cpp new file mode 100644 index 0000000000..1fb18e80b9 --- /dev/null +++ b/examples/network/multistreamserver/server.cpp @@ -0,0 +1,159 @@ +/**************************************************************************** +** +** Copyright (C) 2015 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +#include "server.h" +#include "movieprovider.h" +#include "timeprovider.h" +#include "chatprovider.h" + +Server::Server(QWidget *parent) + : QDialog(parent) + , providers(NumberOfChannels) +{ + setWindowTitle(tr("Multi-stream Server")); + + sctpServer = new QSctpServer(this); + sctpServer->setMaxChannelCount(NumberOfChannels); + + statusLabel = new QLabel; + QPushButton *quitButton = new QPushButton(tr("Quit")); + + providers[Movie] = new MovieProvider(this); + providers[Time] = new TimeProvider(this); + providers[Chat] = new ChatProvider(this); + + connect(sctpServer, &QSctpServer::newConnection, this, &Server::newConnection); + connect(quitButton, &QPushButton::clicked, this, &Server::accept); + connect(providers[Movie], &Provider::writeDatagram, this, &Server::writeDatagram); + connect(providers[Time], &Provider::writeDatagram, this, &Server::writeDatagram); + connect(providers[Chat], &Provider::writeDatagram, this, &Server::writeDatagram); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(statusLabel); + mainLayout->addWidget(quitButton); + setLayout(mainLayout); +} + +Server::~Server() +{ + qDeleteAll(connections.begin(), connections.end()); +} + +int Server::exec() +{ + if (!sctpServer->listen()) { + QMessageBox::critical(this, windowTitle(), + tr("Unable to start the server: %1.") + .arg(sctpServer->errorString())); + return QDialog::Rejected; + } + + QString ipAddress; + QList ipAddressesList = QNetworkInterface::allAddresses(); + // use the first non-localhost IPv4 address + for (int i = 0; i < ipAddressesList.size(); ++i) { + if (ipAddressesList.at(i) != QHostAddress::LocalHost && + ipAddressesList.at(i).toIPv4Address()) { + ipAddress = ipAddressesList.at(i).toString(); + break; + } + } + // if we did not find one, use IPv4 localhost + if (ipAddress.isEmpty()) + ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); + statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n" + "Run the Multi-stream Client example now.") + .arg(ipAddress).arg(sctpServer->serverPort())); + + return QDialog::exec(); +} + +void Server::newConnection() +{ + QSctpSocket *connection = sctpServer->nextPendingDatagramConnection(); + + connections.append(connection); + connect(connection, &QSctpSocket::channelReadyRead, this, &Server::readDatagram); + connect(connection, &QSctpSocket::disconnected, this, &Server::clientDisconnected); + + for (Provider *provider : providers) + provider->newConnection(*connection); +} + +void Server::clientDisconnected() +{ + QSctpSocket *connection = static_cast(sender()); + + connections.removeOne(connection); + connection->disconnect(); + + for (Provider *provider : providers) + provider->clientDisconnected(*connection); + + connection->deleteLater(); +} + +void Server::readDatagram(int channel) +{ + QSctpSocket *connection = static_cast(sender()); + + connection->setCurrentReadChannel(channel); + providers[channel]->readDatagram(*connection, connection->readDatagram().data()); +} + +void Server::writeDatagram(QSctpSocket *to, const QByteArray &ba) +{ + int channel = providers.indexOf(static_cast(sender())); + + if (to) { + to->setCurrentWriteChannel(channel); + to->writeDatagram(ba); + return; + } + + for (QSctpSocket *connection : connections) { + connection->setCurrentWriteChannel(channel); + connection->writeDatagram(ba); + } +} diff --git a/examples/network/multistreamserver/server.h b/examples/network/multistreamserver/server.h new file mode 100644 index 0000000000..6d01f7a238 --- /dev/null +++ b/examples/network/multistreamserver/server.h @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SERVER_H +#define SERVER_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QSctpServer; +class QSctpSocket; +class QLabel; +class QByteArray; +QT_END_NAMESPACE + +class Provider; + +class Server : public QDialog +{ + Q_OBJECT +public: + explicit Server(QWidget *parent = nullptr); + virtual ~Server(); + +public slots: + int exec() Q_DECL_OVERRIDE; + +private slots: + void newConnection(); + void clientDisconnected(); + void readDatagram(int channel); + void writeDatagram(QSctpSocket *to, const QByteArray &ba); + +private: + enum ChannelNumber { + Movie = 0, + Time = 1, + Chat = 2, + + NumberOfChannels = 3 + }; + + QVector providers; + QSctpServer *sctpServer; + QList connections; + + QLabel *statusLabel; +}; + +#endif diff --git a/examples/network/multistreamserver/timeprovider.cpp b/examples/network/multistreamserver/timeprovider.cpp new file mode 100644 index 0000000000..043810cdf3 --- /dev/null +++ b/examples/network/multistreamserver/timeprovider.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "timeprovider.h" +#include +#include +#include + +TimeProvider::TimeProvider(QObject *parent) + : Provider(parent) +{ +} + +void TimeProvider::readDatagram(QSctpSocket &from, const QByteArray &ba) +{ + QDataStream in_ds(ba); + QTime curTime = QTime::currentTime(); + QTime clientTime; + + in_ds >> clientTime; + if (!clientTime.isValid() || curTime.second() != clientTime.second() + || curTime.minute() != clientTime.minute() + || curTime.hour() != clientTime.hour()) { + QByteArray buf; + QDataStream out_ds(&buf, QIODevice::WriteOnly); + + out_ds << curTime; + emit writeDatagram(&from, buf); + } +} diff --git a/examples/network/multistreamserver/timeprovider.h b/examples/network/multistreamserver/timeprovider.h new file mode 100644 index 0000000000..b57c066c24 --- /dev/null +++ b/examples/network/multistreamserver/timeprovider.h @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Alex Trotsenko +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TIMEPROVIDER_H +#define TIMEPROVIDER_H + +#include "provider.h" + +class TimeProvider : public Provider +{ + Q_OBJECT +public: + explicit TimeProvider(QObject *parent = nullptr); + + void readDatagram(QSctpSocket &from, const QByteArray &ba) Q_DECL_OVERRIDE; +}; + +#endif -- cgit v1.2.3