-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket-handler.cpp
More file actions
99 lines (87 loc) · 2.95 KB
/
socket-handler.cpp
File metadata and controls
99 lines (87 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
/*
* Copyright (C) 2025
* Jan van Katwijk (J.vanKatwijk@gmail.com)
* Lazy Chair Computing
*
* This file is part of Qt-DAB
*
* Qt-DAB 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 2 of the License, or
* (at your option) any later version.
*
* Qt-DAB 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 Qt-DAB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QFile>
#include <QByteArray>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <cstring>
#include "socket-handler.h"
socketHandler::
socketHandler (const QString &hostAddress,
int portNumber,
RingBuffer<std::complex<int16_t>> *b) {
this -> hostAddress = hostAddress;
this -> portNumber = portNumber;
this -> _I_Buffer = b;
this -> socket = new QWebSocket;
connect (socket, &QWebSocket::connected,
this, &socketHandler::onConnected);
connect (socket, &QWebSocket::disconnected,
this, &socketHandler::onDisconnect);
connect (socket, &QWebSocket::errorOccurred,
this, &socketHandler::onSocketError);
connected = false;
QString urlString = "ws://%1:%2";
socket -> open (QUrl (urlString. arg (hostAddress). arg(QString::number (portNumber))));
}
socketHandler::~socketHandler () {
}
void socketHandler::onConnected () {
connected = true;
connect (socket, &QWebSocket::textFrameReceived,
this, &socketHandler::textMessageReceived);
connect (socket, &QWebSocket::textMessageReceived,
this, &socketHandler::textMessageReceived);
connect (socket, &QWebSocket::binaryFrameReceived,
this, &socketHandler::binaryMessageReceived);
emit reportConnect ();
}
void socketHandler::onDisconnect () {
if (socket != 0)
socket -> deleteLater ();
emit reportDisconnect ();
// socket = nullptr;
}
void socketHandler::onSocketError (QAbstractSocket::SocketError error) {
(void)error;
onDisconnect ();
emit reportDisconnect ();
}
void socketHandler::sendMessage (const QString &m) {
if (connected)
socket -> sendTextMessage (m);
}
void socketHandler::binaryMessageReceived (const QByteArray &m) {
int16_t *p = (int16_t *)(m. data ());
if (p [0] != 1)
return;
if (_I_Buffer -> GetRingBufferWriteAvailable () < m. size () / 4)
return;
_I_Buffer -> putDataIntoBuffer (&(p [1]), m. size () / 4 - 1);
emit binDataAvailable ();
}
void socketHandler::textMessageReceived (const QString &m) {
dispatchMessage (m);
}