Skip to content

Commit

Permalink
NWA: Added name for the control connection and fixed some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarsnik committed Jul 16, 2022
1 parent 6293701 commit e108bbd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
34 changes: 28 additions & 6 deletions devices/emunetworkaccessdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ Q_LOGGING_CATEGORY(log_emunwaccessdevice, "Emu NWA Device")
#define sDebug() qCDebug(log_emunwaccessdevice)
#define sInfo() qCInfo(log_emunwaccessdevice)

EmuNetworkAccessDevice::EmuNetworkAccessDevice(QString _name)
EmuNetworkAccessDevice::EmuNetworkAccessDevice(QString _name, uint port)
{
myName = _name;
emu = new EmuNWAccessClient(this);
emu->connectToHost("127.0.0.1", 65400);
emu->connectToHost("127.0.0.1", port);
connect(emu, &EmuNWAccessClient::connected, this, [=]() {
sDebug() << "Connected";
emu->cmdMyNameIs("QUsb2Snes Device");
currentCmd = USB2SnesWS::Name;
m_state = READY;
});
connect(emu, &EmuNWAccessClient::readyRead, this, &EmuNetworkAccessDevice::onEmuReadyRead, Qt::UniqueConnection);
Expand All @@ -58,8 +60,16 @@ void EmuNetworkAccessDevice::onEmuReadyRead()
auto rep = emu->readReply();
sDebug() << "Reply ready";
sDebug() << rep;
if (rep.cmd == "MY_NAME_IS")
{
return ;
}
switch(currentCmd)
{
case USB2SnesWS::Name:
{
break;
}
// This is actually memory access request
case USB2SnesWS::Attach:
{
Expand All @@ -85,7 +95,11 @@ void EmuNetworkAccessDevice::onEmuReadyRead()
emu->cmdGameInfo();
}
} else {
whatRunning = rep["file"];
if (rep.contains("file"))
whatRunning = rep["file"];
else {
whatRunning = rep["name"];
}
step = 0;
emit commandFinished();
return;
Expand All @@ -94,6 +108,8 @@ void EmuNetworkAccessDevice::onEmuReadyRead()
sDebug() << "No emu info, checking them";
if (step == 0)
{
if (rep["state"] == "running")
whatRunning = rep["game"];
emu->cmdEmulatorInfo();
step = 1;
return ;
Expand All @@ -102,7 +118,7 @@ void EmuNetworkAccessDevice::onEmuReadyRead()
{
emuVersion = rep["version"];
emuName = rep["name"];
emu->cmdEmulatorInfo();
emu->cmdEmulationStatus();
step = 0;
}
}
Expand Down Expand Up @@ -326,6 +342,7 @@ void EmuNetworkAccessDevice::putAddrCommand(SD2Snes::space space, unsigned int a
NWAMemoriesToWrite.last().totalSize += newAddr.size;
NWAMemoriesToWrite.last().domain = newAddr.domain;
putAddressTotalSent = 0;
putAddressTotalSize = size;
prepareWriteMemory(currentMemorieToWrite->mems);
});
if (!memoryAccess.contains("WRAM"))
Expand Down Expand Up @@ -425,8 +442,12 @@ void EmuNetworkAccessDevice::writeData(QByteArray data)
return ;
}
putAddressTotalSent += data.size();
sDebug() << "Total Sent : " << putAddressTotalSent << "Total Size" << putAddressTotalSize;
if (putAddressTotalSent > putAddressTotalSize)
{
emit protocolError();
return;
}
// If we get data but the emu still has not confirm the previous write
if (currentMemorieToWrite->totalSize == currentMemorieToWrite->sizeWritten && NWAMemoriesToWrite.size() > 1)
{
Expand Down Expand Up @@ -486,8 +507,9 @@ USB2SnesInfo EmuNetworkAccessDevice::parseInfo(const QByteArray &data)

bool EmuNetworkAccessDevice::open()
{
m_state = READY;
return true;
//m_state = READY;
return emu->waitForConnected(20);
//return true;
}

void EmuNetworkAccessDevice::close()
Expand Down
2 changes: 1 addition & 1 deletion devices/emunetworkaccessdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EmuNetworkAccessDevice : public ADevice
Q_OBJECT

public:
EmuNetworkAccessDevice(QString name);
EmuNetworkAccessDevice(QString name, uint port);

// ADevice interface
public:
Expand Down
18 changes: 15 additions & 3 deletions devices/emunetworkaccessfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ADevice *EmuNetworkAccessFactory::attach(QString deviceName)
{
if (ci.device == nullptr)
{
ci.device = new EmuNetworkAccessDevice(deviceName);
ci.device = new EmuNetworkAccessDevice(deviceName, ci.port);
}
if (ci.device->state() == ADevice::BUSY)
return ci.device;
Expand Down Expand Up @@ -150,7 +150,18 @@ void EmuNetworkAccessFactory::onClientReadyRead()
if (info.doingAttach)
return ;
auto rep = client->readReply();
sDebug() << rep;
switch (info.checkState) {
case DetectState::DOING_NAME:
{
if (rep.isError) {
checkFailed(client, Error::DeviceError::DE_EMUNWA_INCOMPATIBLE_CLIENT);
break;
}
clientInfos[client].checkState = DetectState::CHECK_EMU_INFO;
client->cmdEmulatorInfo();
break;
}
case DetectState::CHECK_EMU_INFO:
{
if (rep.isError) {
Expand Down Expand Up @@ -321,8 +332,9 @@ void EmuNetworkAccessFactory::onClientConnected()
EmuNWAccessClient* client = qobject_cast<EmuNWAccessClient*>(sender());
if (clientInfos[client].checkState == DetectState::CHECK_CONNECTION)
{
clientInfos[client].checkState = DetectState::CHECK_EMU_INFO;
client->cmdEmulatorInfo();

clientInfos[client].checkState = DetectState::DOING_NAME;
client->cmdMyNameIs("QUsb2Snes control connection");
}
}

Expand Down
1 change: 1 addition & 0 deletions devices/emunetworkaccessfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class EmuNetworkAccessFactory : public DeviceFactory
enum DetectState {
NO_CHECK,
CHECK_CONNECTION,
DOING_NAME,
CHECK_EMU_INFO,
CHECK_CORE_LIST,
};
Expand Down

0 comments on commit e108bbd

Please sign in to comment.