Examples: use CBOR in the network-chat broadcast message
Instead of sending one @-separated message, send one CBOR message. The message structure is, using the CBOR Data Definition Language: broadcast = [ username: tstr, port: 0..65535 ] Change-Id: Ic38ec929fc3f4bb795dafffd150ac2614d18c6bf Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
bfcb8c6dca
commit
9998654eac
@ -78,7 +78,7 @@ void Client::sendMessage(const QString &message)
|
|||||||
|
|
||||||
QString Client::nickName() const
|
QString Client::nickName() const
|
||||||
{
|
{
|
||||||
return QString(peerManager->userName()) + '@' + QHostInfo::localHostName()
|
return peerManager->userName() + '@' + QHostInfo::localHostName()
|
||||||
+ ':' + QString::number(server.serverPort());
|
+ ':' + QString::number(server.serverPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Copyright (C) 2018 Intel Corporation.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -62,16 +63,14 @@ PeerManager::PeerManager(Client *client)
|
|||||||
{
|
{
|
||||||
this->client = client;
|
this->client = client;
|
||||||
|
|
||||||
QStringList envVariables;
|
static const char *envVariables[] = {
|
||||||
envVariables << "USERNAME" << "USER" << "USERDOMAIN"
|
"USERNAME", "USER", "USERDOMAIN", "HOSTNAME", "DOMAINNAME"
|
||||||
<< "HOSTNAME" << "DOMAINNAME";
|
};
|
||||||
|
|
||||||
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
|
for (const char *varname : envVariables) {
|
||||||
foreach (QString string, envVariables) {
|
username = qEnvironmentVariable(varname);
|
||||||
if (environment.contains(string)) {
|
if (!username.isNull())
|
||||||
username = environment.value(string).toUtf8();
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (username.isEmpty())
|
if (username.isEmpty())
|
||||||
@ -95,7 +94,7 @@ void PeerManager::setServerPort(int port)
|
|||||||
serverPort = port;
|
serverPort = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray PeerManager::userName() const
|
QString PeerManager::userName() const
|
||||||
{
|
{
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
@ -116,9 +115,14 @@ bool PeerManager::isLocalHostAddress(const QHostAddress &address)
|
|||||||
|
|
||||||
void PeerManager::sendBroadcastDatagram()
|
void PeerManager::sendBroadcastDatagram()
|
||||||
{
|
{
|
||||||
QByteArray datagram(username);
|
QByteArray datagram;
|
||||||
datagram.append('@');
|
{
|
||||||
datagram.append(QByteArray::number(serverPort));
|
QCborStreamWriter writer(&datagram);
|
||||||
|
writer.startArray(2);
|
||||||
|
writer.append(username);
|
||||||
|
writer.append(serverPort);
|
||||||
|
writer.endArray();
|
||||||
|
}
|
||||||
|
|
||||||
bool validBroadcastAddresses = true;
|
bool validBroadcastAddresses = true;
|
||||||
foreach (QHostAddress address, broadcastAddresses) {
|
foreach (QHostAddress address, broadcastAddresses) {
|
||||||
@ -142,11 +146,27 @@ void PeerManager::readBroadcastDatagram()
|
|||||||
&senderIp, &senderPort) == -1)
|
&senderIp, &senderPort) == -1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QList<QByteArray> list = datagram.split('@');
|
int senderServerPort;
|
||||||
if (list.size() != 2)
|
{
|
||||||
continue;
|
// decode the datagram
|
||||||
|
QCborStreamReader reader(datagram);
|
||||||
|
if (reader.lastError() != QCborError::NoError || !reader.isArray())
|
||||||
|
continue;
|
||||||
|
if (!reader.isLengthKnown() || reader.length() != 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
reader.enterContainer();
|
||||||
|
if (reader.lastError() != QCborError::NoError || !reader.isString())
|
||||||
|
continue;
|
||||||
|
while (reader.readString().status == QCborStreamReader::Ok) {
|
||||||
|
// we don't actually need the username right now
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reader.lastError() != QCborError::NoError || !reader.isUnsignedInteger())
|
||||||
|
continue;
|
||||||
|
senderServerPort = reader.toInteger();
|
||||||
|
}
|
||||||
|
|
||||||
int senderServerPort = list.at(1).toInt();
|
|
||||||
if (isLocalHostAddress(senderIp) && senderServerPort == serverPort)
|
if (isLocalHostAddress(senderIp) && senderServerPort == serverPort)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ public:
|
|||||||
PeerManager(Client *client);
|
PeerManager(Client *client);
|
||||||
|
|
||||||
void setServerPort(int port);
|
void setServerPort(int port);
|
||||||
QByteArray userName() const;
|
QString userName() const;
|
||||||
void startBroadcasting();
|
void startBroadcasting();
|
||||||
bool isLocalHostAddress(const QHostAddress &address);
|
bool isLocalHostAddress(const QHostAddress &address);
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ private:
|
|||||||
QList<QHostAddress> ipAddresses;
|
QList<QHostAddress> ipAddresses;
|
||||||
QUdpSocket broadcastSocket;
|
QUdpSocket broadcastSocket;
|
||||||
QTimer broadcastTimer;
|
QTimer broadcastTimer;
|
||||||
QByteArray username;
|
QString username;
|
||||||
int serverPort;
|
int serverPort;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user