Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #9550 from endrift/gba-flush
SI/DeviceGBA: Ensure data socket isn't backed up
  • Loading branch information
leoetlino committed Mar 5, 2021
2 parents de30559 + f6e9003 commit a4de250
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
38 changes: 34 additions & 4 deletions Source/Core/Core/HW/SI/SI_DeviceGBA.cpp
Expand Up @@ -253,7 +253,7 @@ void GBASockServer::Send(const u8* si_buffer)
Disconnect();
}

int GBASockServer::Receive(u8* si_buffer)
int GBASockServer::Receive(u8* si_buffer, u8 bytes)
{
if (!m_client)
return 0;
Expand All @@ -267,8 +267,7 @@ int GBASockServer::Receive(u8* si_buffer)

size_t num_received = 0;
std::array<u8, RECV_MAX_SIZE> recv_data;
sf::Socket::Status recv_stat =
m_client->receive(recv_data.data(), recv_data.size(), num_received);
sf::Socket::Status recv_stat = m_client->receive(recv_data.data(), bytes, num_received);
if (recv_stat == sf::Socket::Disconnected)
{
Disconnect();
Expand All @@ -286,6 +285,21 @@ int GBASockServer::Receive(u8* si_buffer)
return static_cast<int>(std::min(num_received, recv_data.size()));
}

void GBASockServer::Flush()
{
if (!m_client || !m_booted)
return;

size_t num_received = 1;
u8 byte;
while (num_received)
{
sf::Socket::Status recv_stat = m_client->receive(&byte, 1, num_received);
if (recv_stat == sf::Socket::Disconnected)
break;
}
}

CSIDevice_GBA::CSIDevice_GBA(SIDevices device, int device_number) : ISIDevice(device, device_number)
{
}
Expand Down Expand Up @@ -329,7 +343,23 @@ int CSIDevice_GBA::RunBuffer(u8* buffer, int request_length)

case NextAction::ReceiveResponse:
{
int num_data_received = m_sock_server.Receive(buffer);
u8 bytes = 1;
switch (m_last_cmd)
{
case CMD_RESET:
case CMD_STATUS:
bytes = 3;
break;
case CMD_READ:
bytes = 5;
break;
default:
break;
}
int num_data_received = m_sock_server.Receive(buffer, bytes);
if (m_last_cmd == CMD_STATUS && num_data_received == 3)
m_sock_server.Flush();

m_next_action = NextAction::SendCommand;
if (num_data_received == 0)
{
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/HW/SI/SI_DeviceGBA.h
Expand Up @@ -28,7 +28,8 @@ class GBASockServer
bool IsConnected();
void ClockSync();
void Send(const u8* si_buffer);
int Receive(u8* si_buffer);
int Receive(u8* si_buffer, u8 bytes);
void Flush();

private:
void Disconnect();
Expand Down

0 comments on commit a4de250

Please sign in to comment.