mirror of
https://github.com/libretro/MesenCE.git
synced 2026-11-10 03:02:05 -10:00
* GB: Link cable support with two local consoles * GB: Fix crash when color blending is enabled * GB: Fix formatting * GB: Link UI fixes, control fixes, audio fixes Copied from Sour's branch. Also a separate change to use separate savefiles. * GB: Try to avoid crashes from buffer overflows * GB: Separate savefiles with linked multiplayer * GB: Add support for faster link speed * GB: Move enum to correct file * GB: Refactor, move serial transfer to function * -Fixed secondary console sometimes running far ahead of the main console when frame counters did not match (e.g because one console had their LCD disabled longer, etc.) This would cause buffer overflows in the audio buffer. -Fixed screen tearing when both consoles are not perfectly in sync (because of the LCD being disabled for a different amount of time, etc.) -Fixed main console not calling PlayQueuedAudio properly mid-frame -Reuse VS Dualsystem code to merge audio/video when possible/simple and moved that to AvMergeUtilities Some visual glitches can appear on the secondary screen (for GB) while rewinding, but this can't really be fixed. * Fixed warning (unused local variable) * Prevent loading single-console save states when in dual-console modes * Disable debugger for VS DualSystem's subconsole. Fixes issues when debugging VS DualSystem games * Code cleanup * Rework solution to disable debugger on second console to improve performance Added InternalRunFrame for nes & gb to avoid checking for the presence of the second console after each cpu instruction * Don't enable the linked GB option by default * Removed unused function * Netplay: Force game to reload after updating emulation settings when connecting to the server (or when the host changes game) This ensures that e.g the GB link cable setting is applied and that the game loads correctly, regardless of the client's link cable settings (otherwise the game could load without the link mode enabled, etc.) --------- Co-authored-by: Sour <[email protected]>
107 lines
3.2 KiB
C++
107 lines
3.2 KiB
C++
#include "pch.h"
|
|
#include "Netplay/GameConnection.h"
|
|
#include "Netplay/HandShakeMessage.h"
|
|
#include "Netplay/InputDataMessage.h"
|
|
#include "Netplay/MovieDataMessage.h"
|
|
#include "Netplay/GameInformationMessage.h"
|
|
#include "Netplay/SaveStateMessage.h"
|
|
#include "Netplay/PlayerListMessage.h"
|
|
#include "Netplay/SelectControllerMessage.h"
|
|
#include "Netplay/ClientConnectionData.h"
|
|
#include "Netplay/ForceDisconnectMessage.h"
|
|
#include "Netplay/ServerInformationMessage.h"
|
|
|
|
GameConnection::GameConnection(Emulator* emu, unique_ptr<Socket> socket)
|
|
{
|
|
_emu = emu;
|
|
_socket.swap(socket);
|
|
}
|
|
|
|
GameConnection::~GameConnection()
|
|
{
|
|
Disconnect();
|
|
}
|
|
|
|
void GameConnection::ReadSocket()
|
|
{
|
|
auto lock = _socketLock.AcquireSafe();
|
|
int bytesReceived = _socket->Recv((char*)_readBuffer + _readPosition, GameConnection::MaxMsgLength - _readPosition, 0);
|
|
if(bytesReceived > 0) {
|
|
_readPosition += bytesReceived;
|
|
}
|
|
}
|
|
|
|
bool GameConnection::ExtractMessage(void* buffer, uint32_t& messageLength)
|
|
{
|
|
messageLength = _readBuffer[0] | (_readBuffer[1] << 8) | (_readBuffer[2] << 16) | (_readBuffer[3] << 24);
|
|
|
|
if(messageLength > GameConnection::MaxMsgLength) {
|
|
MessageManager::Log("[Netplay] Invalid data received, closing connection.");
|
|
Disconnect();
|
|
return false;
|
|
}
|
|
|
|
int packetLength = messageLength + sizeof(messageLength);
|
|
|
|
if(_readPosition >= packetLength) {
|
|
memcpy(buffer, _readBuffer + sizeof(messageLength), messageLength);
|
|
memmove(_readBuffer, _readBuffer + packetLength, _readPosition - packetLength);
|
|
_readPosition -= packetLength;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
NetMessage* GameConnection::ReadMessage()
|
|
{
|
|
ReadSocket();
|
|
|
|
if(_readPosition > 4) {
|
|
uint32_t messageLength;
|
|
if(ExtractMessage(_messageBuffer, messageLength)) {
|
|
switch((MessageType)_messageBuffer[0]) {
|
|
case MessageType::HandShake: return new HandShakeMessage(_messageBuffer, messageLength);
|
|
case MessageType::SaveState: return new SaveStateMessage(_messageBuffer, messageLength);
|
|
case MessageType::InputData: return new InputDataMessage(_messageBuffer, messageLength);
|
|
case MessageType::MovieData: return new MovieDataMessage(_messageBuffer, messageLength);
|
|
case MessageType::GameInformation: return new GameInformationMessage(_emu->GetSettings(), _messageBuffer, messageLength);
|
|
case MessageType::PlayerList: return new PlayerListMessage(_messageBuffer, messageLength);
|
|
case MessageType::SelectController: return new SelectControllerMessage(_messageBuffer, messageLength);
|
|
case MessageType::ForceDisconnect: return new ForceDisconnectMessage(_messageBuffer, messageLength);
|
|
case MessageType::ServerInformation: return new ServerInformationMessage(_messageBuffer, messageLength);
|
|
}
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void GameConnection::SendNetMessage(NetMessage& message)
|
|
{
|
|
auto lock = _socketLock.AcquireSafe();
|
|
message.Send(*_socket.get());
|
|
}
|
|
|
|
void GameConnection::Disconnect()
|
|
{
|
|
auto lock = _socketLock.AcquireSafe();
|
|
_socket->Close();
|
|
}
|
|
|
|
bool GameConnection::ConnectionError()
|
|
{
|
|
return _socket->ConnectionError();
|
|
}
|
|
|
|
void GameConnection::ProcessMessages()
|
|
{
|
|
NetMessage* message;
|
|
|
|
ProcessPendingEvents();
|
|
|
|
while((message = ReadMessage()) != nullptr) {
|
|
//Loop until all messages have been processed
|
|
message->Initialize();
|
|
ProcessMessage(message);
|
|
delete message;
|
|
}
|
|
} |