Skip to content

Commit beb467f

Browse files
committed
Merge the work from experimental into master. .pro are not updated so expect fail compile
1 parent 8eef3d1 commit beb467f

26 files changed

+566
-531
lines changed

QUsb2snes.qbs

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,47 @@ Project {
1212
"TODO",
1313
"client/usb2snesclient.cpp",
1414
"client/usb2snesclient.h",
15-
"devices/EmuNWAccess-qt/emunwaccessclient.cpp",
16-
"devices/EmuNWAccess-qt/emunwaccessclient.h",
15+
"core/types.h",
1716
"core/adevice.cpp",
1817
"core/adevice.h",
19-
"devices/remoteusb2sneswdevice.cpp",
20-
"devices/remoteusb2sneswdevice.h",
21-
"devices/remoteusb2sneswfactory.cpp",
22-
"devices/remoteusb2sneswfactory.h",
23-
"settings.hpp",
24-
"sqpath.h",
25-
"ui/appui.cpp",
26-
"ui/appuimenu.cpp",
27-
"ui/appui.h",
28-
"ui/appuipoptracker.cpp",
29-
"ui/appuiupdate.cpp",
30-
"ui/systraywidget.cpp",
31-
"ui/systraywidget.h",
32-
"ui/systraywidget.ui",
33-
"ui/diagnosticdialog.cpp",
34-
"ui/diagnosticdialog.h",
35-
"ui/diagnosticdialog.ui",
36-
"backward.hpp",
18+
"core/usb2snes.h",
19+
"core/wsserver.cpp",
20+
"core/wsserver.h",
3721
"core/devicefactory.cpp",
3822
"core/devicefactory.h",
23+
"core/wsservercommands.cpp",
24+
"core/localstorage.cpp",
25+
"core/localstorage.h",
26+
"core/aclient.h",
27+
"core/aclientprovider.h",
28+
"core/websocketclient.cpp",
29+
"core/websocketclient.h",
30+
"core/websocketprovider.cpp",
31+
"core/websocketprovider.h",
32+
"sqpath.h",
33+
"backward.hpp",
34+
35+
"settings.hpp",
36+
"utils/ipsparse.cpp",
37+
"utils/ipsparse.h",
38+
"main.cpp",
39+
"qskarsnikringlist.hpp",
40+
"qusb2snes.rc",
41+
"ressources.qrc",
42+
43+
"utils/rommapping/mapping_hirom.c",
44+
"utils/rommapping/mapping_lorom.c",
45+
"utils/rommapping/rommapping.c",
46+
"utils/rommapping/rominfo.c",
47+
48+
"devices/sd2snesdevice.cpp",
49+
"devices/sd2snesdevice.h",
50+
"devices/luabridge.cpp",
51+
"devices/luabridge.h",
52+
"devices/luabridgedevice.cpp",
53+
"devices/luabridgedevice.h",
54+
"devices/snesclassic.cpp",
55+
"devices/snesclassic.h",
3956
"devices/deviceerror.cpp",
4057
"devices/deviceerror.h",
4158
"devices/emunetworkaccessdevice.cpp",
@@ -46,32 +63,32 @@ Project {
4663
"devices/retroarchfactory.h",
4764
"devices/retroarchhost.cpp",
4865
"devices/retroarchhost.h",
66+
"devices/retroarchdevice.cpp",
67+
"devices/retroarchdevice.h",
4968
"devices/sd2snesfactory.cpp",
5069
"devices/sd2snesfactory.h",
5170
"devices/snesclassicfactory.cpp",
5271
"devices/snesclassicfactory.h",
53-
"ipsparse.cpp",
54-
"ipsparse.h",
55-
"devices/luabridge.cpp",
56-
"devices/luabridge.h",
57-
"devices/luabridgedevice.cpp",
58-
"devices/luabridgedevice.h",
59-
"core/localstorage.cpp",
60-
"core/localstorage.h",
61-
"main.cpp",
62-
"qskarsnikringlist.hpp",
63-
"qusb2snes.rc",
64-
"ressources.qrc",
65-
"devices/retroarchdevice.cpp",
66-
"devices/retroarchdevice.h",
67-
"rommapping/mapping_hirom.c",
68-
"rommapping/mapping_lorom.c",
69-
"rommapping/rommapping.c",
70-
"rommapping/rominfo.c",
71-
"devices/sd2snesdevice.cpp",
72-
"devices/sd2snesdevice.h",
73-
"devices/snesclassic.cpp",
74-
"devices/snesclassic.h",
72+
"devices/EmuNWAccess-qt/emunwaccessclient.cpp",
73+
"devices/EmuNWAccess-qt/emunwaccessclient.h",
74+
"devices/remoteusb2sneswdevice.cpp",
75+
"devices/remoteusb2sneswdevice.h",
76+
"devices/remoteusb2sneswfactory.cpp",
77+
"devices/remoteusb2sneswfactory.h",
78+
79+
80+
"ui/appui.cpp",
81+
"ui/appuimenu.cpp",
82+
"ui/appui.h",
83+
"ui/appuipoptracker.cpp",
84+
"ui/appuiupdate.cpp",
85+
"ui/systraywidget.cpp",
86+
"ui/systraywidget.h",
87+
"ui/systraywidget.ui",
88+
"ui/diagnosticdialog.cpp",
89+
"ui/diagnosticdialog.h",
90+
"ui/diagnosticdialog.ui",
91+
7592
"ui/wizard/deviceselectorpage.cpp",
7693
"ui/wizard/deviceselectorpage.h",
7794
"ui/wizard/deviceselectorpage.ui",
@@ -93,10 +110,6 @@ Project {
93110
"ui/wizard/snesclassicpage.cpp",
94111
"ui/wizard/snesclassicpage.h",
95112
"ui/wizard/snesclassicpage.ui",
96-
"core/usb2snes.h",
97-
"core/wsserver.cpp",
98-
"core/wsserver.h",
99-
"core/wsservercommands.cpp",
100113
]
101114

102115
Group {

core/aclientprovider.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <QObject>
4+
5+
class AClient;
6+
7+
class AClientProvider : public QObject {
8+
Q_OBJECT
9+
public:
10+
AClientProvider(QObject* parent = nullptr) : QObject(parent) {}
11+
QString name;
12+
virtual void deleteClient(AClient* client) = 0;
13+
14+
signals:
15+
void newClient(AClient* client);
16+
};

core/types.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <QObject>
4+
#include <QTime>
5+
#include "usb2snes.h"
6+
class AClient;
7+
//#include "aclient.h"
8+
9+
namespace Core {
10+
Q_NAMESPACE;
11+
12+
enum class RequestState {
13+
NEW,
14+
SENT,
15+
WAITINGREPLY,
16+
WAITINGBDATAREPLY,
17+
DONE,
18+
CANCELLED
19+
};
20+
Q_ENUM_NS(RequestState)
21+
22+
enum class ErrorType {
23+
CommandError,
24+
ProtocolError,
25+
DeviceError,
26+
};
27+
Q_ENUM_NS(ErrorType)
28+
29+
struct MRequest {
30+
MRequest() {
31+
id = gId++;
32+
wasPending = false;
33+
}
34+
quint64 id;
35+
AClient* owner;
36+
QTime timeCreated;
37+
USB2SnesWS::opcode opcode;
38+
SD2Snes::space space;
39+
QStringList arguments;
40+
QStringList flags;
41+
RequestState state;
42+
bool wasPending;
43+
friend QDebug operator<<(QDebug debug, const MRequest& req);
44+
private:
45+
static quint64 gId;
46+
};
47+
48+
49+
}

core/websocketclient.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include "types.h"
2+
#include "websocketclient.h"
3+
4+
#include <QLoggingCategory>
5+
#include <QJsonArray>
6+
#include <QJsonDocument>
7+
#include <QJsonObject>
8+
#include <QMetaEnum>
9+
10+
Q_LOGGING_CATEGORY(log_wsclient, "WSClient")
11+
#define sDebug() qCDebug(log_wsclient) << name
12+
#define sInfo() qCInfo(log_wsclient) << name
13+
14+
using namespace Core;
15+
16+
WebSocketClient::WebSocketClient(QWebSocket *socket, AClientProvider* parent = nullptr) : AClient(parent)
17+
{
18+
m_socket = socket;
19+
name = "Websocket " + QString::number((size_t) socket, 16);
20+
connect(m_socket, &QWebSocket::textMessageReceived, this, &WebSocketClient::onTextMessageReceived);
21+
connect(m_socket, &QWebSocket::binaryMessageReceived, this, &WebSocketClient::onBinaryMessageReceived);
22+
connect(m_socket, &QWebSocket::disconnected, this, &WebSocketClient::onClientDisconnected);
23+
connect(m_socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebSocketClient::onWSError);
24+
}
25+
26+
void WebSocketClient::close()
27+
{
28+
m_socket->close();
29+
}
30+
31+
QString WebSocketClient::origin() const
32+
{
33+
return m_socket->origin();
34+
}
35+
36+
void WebSocketClient::close(QWebSocketProtocol::CloseCode code, QString str)
37+
{
38+
m_socket->close(code, str);
39+
}
40+
41+
void WebSocketClient::onWSError(QAbstractSocket::SocketError error)
42+
{
43+
44+
}
45+
46+
void WebSocketClient::onTextMessageReceived(QString message)
47+
{
48+
static const QMetaObject &mo = USB2SnesWS::staticMetaObject;
49+
static const QMetaObject &mo2 = SD2Snes::staticMetaObject;
50+
51+
sDebug() << "received " << message;
52+
MRequest *req = new MRequest();
53+
int i = mo.indexOfEnumerator("opcode");
54+
QMetaEnum cmdMetaEnum = mo.enumerator(i);
55+
i = mo2.indexOfEnumerator("space");
56+
QMetaEnum spaceMetaEnum = mo2.enumerator(i);
57+
i = mo2.indexOfEnumerator("server_flags");
58+
QMetaEnum flagsMetaEnum = mo2.enumerator(i);
59+
req->state = RequestState::NEW;
60+
req->owner = this;
61+
QJsonDocument jdoc = QJsonDocument::fromJson(message.toLatin1());
62+
QJsonObject job = jdoc.object();
63+
QString opcode = job["Opcode"].toString();
64+
if (cmdMetaEnum.keyToValue(qPrintable(opcode)) == -1)
65+
{
66+
delete req;
67+
emit errorOccured(ErrorType::ProtocolError, "Invalid OPcode send " + opcode);
68+
return ;
69+
}
70+
if (job.contains("Space"))
71+
{
72+
QString space = job["Space"].toString();
73+
74+
if (spaceMetaEnum.keyToValue(qPrintable(space)) == -1)
75+
{
76+
delete req;
77+
emit errorOccured(ErrorType::ProtocolError, "Invalid Space send" + space);
78+
return ;
79+
}
80+
req->space = (SD2Snes::space) spaceMetaEnum.keyToValue(qPrintable(space));
81+
}
82+
req->opcode = (USB2SnesWS::opcode) cmdMetaEnum.keyToValue(qPrintable(opcode));
83+
if (job.contains("Operands"))
84+
{
85+
QJsonArray jarray = job["Operands"].toArray();
86+
foreach(QVariant entry, jarray.toVariantList())
87+
{
88+
req->arguments << entry.toString();
89+
}
90+
}
91+
if (job.contains("Flags"))
92+
{
93+
QJsonArray jarray = job["Flags"].toArray();
94+
foreach(QVariant entry, jarray.toVariantList())
95+
{
96+
req->flags << entry.toString();
97+
}
98+
}
99+
req->timeCreated = QTime::currentTime();
100+
emit newRequest(req);
101+
}
102+
103+
void WebSocketClient::onBinaryMessageReceived(QByteArray data)
104+
{
105+
emit binaryData(data);
106+
}
107+
108+
void WebSocketClient::onClientDisconnected()
109+
{
110+
111+
}
112+
113+
114+
void WebSocketClient::sendData(QByteArray data)
115+
{
116+
m_socket->sendBinaryMessage(data);
117+
}
118+
119+
void WebSocketClient::sendReply(QStringList list)
120+
{
121+
// We don't reply on these commands
122+
switch (currentOpcode) {
123+
case USB2SnesWS::Rename :
124+
case USB2SnesWS::Remove :
125+
case USB2SnesWS::MakeDir :
126+
case USB2SnesWS::PutFile :
127+
case USB2SnesWS::PutAddress :
128+
case USB2SnesWS::Menu :
129+
case USB2SnesWS::Reset :
130+
case USB2SnesWS::Boot :
131+
case USB2SnesWS::Attach:
132+
return ;
133+
default:
134+
break;
135+
}
136+
QJsonObject jObj;
137+
QJsonArray ja;
138+
139+
foreach(QString s, list)
140+
{
141+
ja.append(QJsonValue(s));
142+
}
143+
jObj["Results"] = ja;
144+
sDebug() << ">>" << QJsonDocument(jObj).toJson();
145+
m_socket->sendTextMessage(QJsonDocument(jObj).toJson());
146+
}
147+
148+
void WebSocketClient::sendError(ErrorType err, QString errString)
149+
{
150+
sDebug() << "Error : " << err << errString;
151+
m_socket->close(QWebSocketProtocol::CloseCodeNormal, errString);
152+
}

core/websocketclient.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef WEBSOCKETCLIENT_H
2+
#define WEBSOCKETCLIENT_H
3+
4+
#include "aclient.h"
5+
#include <QWebSocket>
6+
7+
class WebSocketClient : public AClient
8+
{
9+
Q_OBJECT
10+
public:
11+
WebSocketClient(QWebSocket* socket, AClientProvider* parent);
12+
13+
public:
14+
void close();
15+
QString origin() const;
16+
void close(QWebSocketProtocol::CloseCode code, QString str);
17+
18+
private:
19+
QWebSocket *m_socket;
20+
21+
private slots:
22+
void onWSError(QAbstractSocket::SocketError error);
23+
void onTextMessageReceived(QString message);
24+
void onBinaryMessageReceived(QByteArray data);
25+
void onClientDisconnected();
26+
27+
// AClient interface
28+
public:
29+
void sendData(QByteArray data);
30+
void sendReply(QStringList list);
31+
void sendError(Core::ErrorType err, QString errString);
32+
};
33+
34+
#endif // WEBSOCKETCLIENT_H

0 commit comments

Comments
 (0)