yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::emu::mesen::MesenSocketClient Class Reference

Local or TCP socket client for the Mesen2-OoS fork. More...

#include <mesen_socket_client.h>

Collaboration diagram for yaze::emu::mesen::MesenSocketClient:

Public Member Functions

 MesenSocketClient ()
 
 ~MesenSocketClient ()
 
 MesenSocketClient (const MesenSocketClient &)=delete
 
MesenSocketClientoperator= (const MesenSocketClient &)=delete
 
absl::Status Connect ()
 Auto-discover and connect to first available Mesen2 socket.
 
absl::Status Connect (const std::string &socket_path)
 Connect to a specific socket path.
 
void Disconnect ()
 Disconnect from Mesen2.
 
bool IsConnected () const
 Check if connected to Mesen2.
 
const std::string & GetSocketPath () const
 Get the current socket path.
 
absl::Status Ping ()
 Ping Mesen2 to check connectivity.
 
absl::StatusOr< MesenStateGetState ()
 Get current emulation state.
 
absl::Status Pause ()
 Pause emulation.
 
absl::Status Resume ()
 Resume emulation.
 
absl::Status Reset ()
 Reset the console.
 
absl::Status Frame ()
 Run exactly one frame.
 
absl::Status Step (int count=1, const std::string &mode="into")
 Step N CPU instructions (default 1)
 
absl::Status SetButton (emu::input::SnesButton button, bool pressed)
 Set controller button state.
 
absl::Status SetButtons (const emu::input::ControllerState &state)
 Set all buttons at once.
 
absl::StatusOr< uint8_t > ReadByte (uint32_t addr)
 Read a single byte from memory.
 
absl::StatusOr< uint16_t > ReadWord (uint32_t addr)
 Read a 16-bit word from memory.
 
absl::StatusOr< std::vector< uint8_t > > ReadBlock (uint32_t addr, size_t len)
 Read a block of bytes from memory.
 
absl::Status WriteByte (uint32_t addr, uint8_t value)
 Write a single byte to memory.
 
absl::Status WriteWord (uint32_t addr, uint16_t value)
 Write a 16-bit word to memory.
 
absl::Status WriteBlock (uint32_t addr, const std::vector< uint8_t > &data)
 Write a block of bytes to memory.
 
absl::StatusOr< CpuStateGetCpuState ()
 Get CPU register state.
 
absl::StatusOr< std::string > Disassemble (uint32_t addr, int count=10)
 Disassemble instructions at address.
 
absl::StatusOr< int > AddBreakpoint (uint32_t addr, BreakpointType type, const std::string &condition="")
 Add a breakpoint.
 
absl::Status RemoveBreakpoint (int id)
 Remove a breakpoint by ID.
 
absl::Status ClearBreakpoints ()
 Clear all breakpoints.
 
absl::StatusOr< std::string > GetTrace (int count=20)
 Get execution trace log.
 
absl::StatusOr< GameStateGetGameState ()
 Get comprehensive ALTTP game state.
 
absl::StatusOr< std::vector< SpriteInfo > > GetSprites (bool all=false)
 Get active sprites.
 
absl::Status SetCollisionOverlay (bool enable, const std::string &colmap="A")
 Enable/disable collision overlay.
 
absl::Status SaveState (int slot)
 Save state to slot.
 
absl::Status LoadState (int slot)
 Load state from slot.
 
absl::StatusOr< std::string > Screenshot ()
 Take a screenshot.
 
absl::Status Subscribe (const std::vector< std::string > &events)
 Subscribe to events.
 
absl::Status Unsubscribe ()
 Unsubscribe from events.
 
void SetEventCallback (EventCallback callback)
 Set callback for received events.
 
EventListenerId AddEventListener (EventCallback callback)
 Add an event listener without replacing existing listeners.
 
void RemoveEventListener (EventListenerId id)
 Remove a previously added event listener.
 
absl::StatusOr< std::string > SendCommand (const std::string &json)
 Send a raw JSON command and get raw response.
 

Static Public Member Functions

static std::vector< std::string > ListAvailableSockets ()
 List available Mesen2 sockets on the system.
 

Private Member Functions

absl::StatusOr< std::string > ParseResponse (const std::string &response)
 Parse JSON response for success/error.
 
absl::StatusOr< std::string > SendCommandOnSocket (SocketHandle fd, const std::string &json, bool update_connection_state)
 Send a command using a specific socket descriptor.
 
void EventLoop ()
 Event listening thread function.
 

Static Private Member Functions

static std::vector< std::string > FindSocketPaths ()
 Find available Mesen2 socket paths.
 

Private Attributes

SocketHandle socket_fd_ = kInvalidSocketHandle
 
SocketHandle event_socket_fd_ = kInvalidSocketHandle
 
std::string socket_path_
 
std::atomic< bool > connected_ {false}
 
std::mutex command_mutex_
 
std::mutex event_callback_mutex_
 
EventCallback event_callback_
 
std::unordered_map< EventListenerId, EventCallbackevent_listeners_
 
EventListenerId next_event_listener_id_ = 1
 
std::thread event_thread_
 
std::atomic< bool > event_thread_running_ {false}
 
std::string pending_event_payload_
 
emu::input::ControllerState current_input_
 

Detailed Description

Local or TCP socket client for the Mesen2-OoS fork.

Connects to Mesen2's socket API at /tmp/mesen2-<pid>.sock or a TCP endpoint (tcp://127.0.0.1:27015) used by the Android handheld host.

Definition at line 140 of file mesen_socket_client.h.

Constructor & Destructor Documentation

◆ MesenSocketClient() [1/2]

yaze::emu::mesen::MesenSocketClient::MesenSocketClient ( )

Definition at line 635 of file mesen_socket_client.cc.

◆ ~MesenSocketClient()

yaze::emu::mesen::MesenSocketClient::~MesenSocketClient ( )

Definition at line 643 of file mesen_socket_client.cc.

References Disconnect().

Here is the call graph for this function:

◆ MesenSocketClient() [2/2]

yaze::emu::mesen::MesenSocketClient::MesenSocketClient ( const MesenSocketClient & )
delete

Member Function Documentation

◆ operator=()

MesenSocketClient & yaze::emu::mesen::MesenSocketClient::operator= ( const MesenSocketClient & )
delete

◆ Connect() [1/2]

absl::Status yaze::emu::mesen::MesenSocketClient::Connect ( )

Auto-discover and connect to first available Mesen2 socket.

If MESEN2_SOCKET_PATH names a TCP target, that target is authoritative. A malformed TCP target returns its parse error instead of falling back to a discovered local emulator.

Definition at line 652 of file mesen_socket_client.cc.

References Connect(), and FindSocketPaths().

Referenced by Connect().

Here is the call graph for this function:

◆ Connect() [2/2]

absl::Status yaze::emu::mesen::MesenSocketClient::Connect ( const std::string & socket_path)

Connect to a specific socket path.

Parameters
socket_pathUnix socket path or tcp://host:port

TCP connection establishment is bounded to two seconds. DNS resolution for hostnames still uses the platform's synchronous resolver.

Definition at line 661 of file mesen_socket_client.cc.

References connected_, Disconnect(), yaze::emu::mesen::kInvalidSocketHandle, Ping(), socket_fd_, and socket_path_.

Here is the call graph for this function:

◆ Disconnect()

void yaze::emu::mesen::MesenSocketClient::Disconnect ( )

Disconnect from Mesen2.

Definition at line 694 of file mesen_socket_client.cc.

References connected_, yaze::emu::mesen::kInvalidSocketHandle, socket_fd_, socket_path_, and Unsubscribe().

Referenced by Connect(), and ~MesenSocketClient().

Here is the call graph for this function:

◆ IsConnected()

bool yaze::emu::mesen::MesenSocketClient::IsConnected ( ) const

Check if connected to Mesen2.

Definition at line 706 of file mesen_socket_client.cc.

References connected_.

Referenced by SendCommand(), and Subscribe().

◆ GetSocketPath()

const std::string & yaze::emu::mesen::MesenSocketClient::GetSocketPath ( ) const
inline

Get the current socket path.

Definition at line 184 of file mesen_socket_client.h.

References socket_path_.

◆ ListAvailableSockets()

std::vector< std::string > yaze::emu::mesen::MesenSocketClient::ListAvailableSockets ( )
static

List available Mesen2 sockets on the system.

Definition at line 765 of file mesen_socket_client.cc.

References FindSocketPaths().

Referenced by yaze::editor::MesenDebugPanel::RefreshSocketList(), yaze::editor::MesenScreenshotPanel::RefreshSocketList(), and yaze::editor::SramViewerPanel::RefreshSocketList().

Here is the call graph for this function:

◆ Ping()

absl::Status yaze::emu::mesen::MesenSocketClient::Ping ( )

Ping Mesen2 to check connectivity.

Definition at line 867 of file mesen_socket_client.cc.

References SendCommand().

Referenced by Connect().

Here is the call graph for this function:

◆ GetState()

absl::StatusOr< MesenState > yaze::emu::mesen::MesenSocketClient::GetState ( )

◆ Pause()

absl::Status yaze::emu::mesen::MesenSocketClient::Pause ( )

Pause emulation.

Definition at line 889 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ Resume()

absl::Status yaze::emu::mesen::MesenSocketClient::Resume ( )

Resume emulation.

Definition at line 894 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ Reset()

absl::Status yaze::emu::mesen::MesenSocketClient::Reset ( )

Reset the console.

Definition at line 899 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ Frame()

absl::Status yaze::emu::mesen::MesenSocketClient::Frame ( )

Run exactly one frame.

Definition at line 904 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ Step()

absl::Status yaze::emu::mesen::MesenSocketClient::Step ( int count = 1,
const std::string & mode = "into" )

Step N CPU instructions (default 1)

Parameters
countNumber of steps
modeStep mode: "into", "over", "out"

Definition at line 909 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ SetButton()

absl::Status yaze::emu::mesen::MesenSocketClient::SetButton ( emu::input::SnesButton button,
bool pressed )

Set controller button state.

Parameters
buttonSNES button
pressedTrue to press, false to release

Definition at line 919 of file mesen_socket_client.cc.

References current_input_, yaze::emu::input::ControllerState::SetButton(), and SetButtons().

Here is the call graph for this function:

◆ SetButtons()

◆ ReadByte()

absl::StatusOr< uint8_t > yaze::emu::mesen::MesenSocketClient::ReadByte ( uint32_t addr)

Read a single byte from memory.

Definition at line 971 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ ReadWord()

absl::StatusOr< uint16_t > yaze::emu::mesen::MesenSocketClient::ReadWord ( uint32_t addr)

Read a 16-bit word from memory.

Definition at line 979 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ ReadBlock()

absl::StatusOr< std::vector< uint8_t > > yaze::emu::mesen::MesenSocketClient::ReadBlock ( uint32_t addr,
size_t len )

Read a block of bytes from memory.

Definition at line 987 of file mesen_socket_client.cc.

References SendCommand().

Referenced by yaze::editor::anonymous_namespace{oracle_hack_workflow_backend.cc}::ReadOracleProgressionState().

Here is the call graph for this function:

◆ WriteByte()

absl::Status yaze::emu::mesen::MesenSocketClient::WriteByte ( uint32_t addr,
uint8_t value )

Write a single byte to memory.

Definition at line 1017 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ WriteWord()

absl::Status yaze::emu::mesen::MesenSocketClient::WriteWord ( uint32_t addr,
uint16_t value )

Write a 16-bit word to memory.

Definition at line 1024 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ WriteBlock()

absl::Status yaze::emu::mesen::MesenSocketClient::WriteBlock ( uint32_t addr,
const std::vector< uint8_t > & data )

Write a block of bytes to memory.

Definition at line 1031 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ GetCpuState()

absl::StatusOr< CpuState > yaze::emu::mesen::MesenSocketClient::GetCpuState ( )

Get CPU register state.

Definition at line 1047 of file mesen_socket_client.cc.

References yaze::emu::mesen::CpuState::A, and SendCommand().

Here is the call graph for this function:

◆ Disassemble()

absl::StatusOr< std::string > yaze::emu::mesen::MesenSocketClient::Disassemble ( uint32_t addr,
int count = 10 )

Disassemble instructions at address.

Definition at line 1066 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ AddBreakpoint()

absl::StatusOr< int > yaze::emu::mesen::MesenSocketClient::AddBreakpoint ( uint32_t addr,
BreakpointType type,
const std::string & condition = "" )

Add a breakpoint.

Returns
Breakpoint ID

Definition at line 1076 of file mesen_socket_client.cc.

References yaze::emu::mesen::kExecute, yaze::emu::mesen::kRead, yaze::emu::mesen::kReadWrite, yaze::emu::mesen::kWrite, and SendCommand().

Here is the call graph for this function:

◆ RemoveBreakpoint()

absl::Status yaze::emu::mesen::MesenSocketClient::RemoveBreakpoint ( int id)

Remove a breakpoint by ID.

Definition at line 1110 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ ClearBreakpoints()

absl::Status yaze::emu::mesen::MesenSocketClient::ClearBreakpoints ( )

Clear all breakpoints.

Definition at line 1116 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ GetTrace()

absl::StatusOr< std::string > yaze::emu::mesen::MesenSocketClient::GetTrace ( int count = 20)

Get execution trace log.

Definition at line 1122 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ GetGameState()

◆ GetSprites()

absl::StatusOr< std::vector< SpriteInfo > > yaze::emu::mesen::MesenSocketClient::GetSprites ( bool all = false)

◆ SetCollisionOverlay()

absl::Status yaze::emu::mesen::MesenSocketClient::SetCollisionOverlay ( bool enable,
const std::string & colmap = "A" )

Enable/disable collision overlay.

Definition at line 1232 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ SaveState()

absl::Status yaze::emu::mesen::MesenSocketClient::SaveState ( int slot)

Save state to slot.

Definition at line 1244 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ LoadState()

absl::Status yaze::emu::mesen::MesenSocketClient::LoadState ( int slot)

Load state from slot.

Definition at line 1250 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ Screenshot()

absl::StatusOr< std::string > yaze::emu::mesen::MesenSocketClient::Screenshot ( )

Take a screenshot.

Returns
Base64-encoded PNG data

Definition at line 1256 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ Subscribe()

absl::Status yaze::emu::mesen::MesenSocketClient::Subscribe ( const std::vector< std::string > & events)

Subscribe to events.

Parameters
eventsEvent types: "breakpoint_hit", "frame_complete", "all", etc.

Definition at line 1267 of file mesen_socket_client.cc.

References event_socket_fd_, event_thread_, event_thread_running_, EventLoop(), IsConnected(), yaze::emu::mesen::kInvalidSocketHandle, ParseResponse(), pending_event_payload_, socket_path_, and Unsubscribe().

Here is the call graph for this function:

◆ Unsubscribe()

absl::Status yaze::emu::mesen::MesenSocketClient::Unsubscribe ( )

◆ SetEventCallback()

void yaze::emu::mesen::MesenSocketClient::SetEventCallback ( EventCallback callback)

Set callback for received events.

Definition at line 1382 of file mesen_socket_client.cc.

References event_callback_, and event_callback_mutex_.

◆ AddEventListener()

EventListenerId yaze::emu::mesen::MesenSocketClient::AddEventListener ( EventCallback callback)

Add an event listener without replacing existing listeners.

Returns
Listener ID used for removal.

Definition at line 1387 of file mesen_socket_client.cc.

References event_callback_mutex_, event_listeners_, and next_event_listener_id_.

◆ RemoveEventListener()

void yaze::emu::mesen::MesenSocketClient::RemoveEventListener ( EventListenerId id)

Remove a previously added event listener.

Definition at line 1397 of file mesen_socket_client.cc.

References event_callback_mutex_, and event_listeners_.

◆ SendCommand()

absl::StatusOr< std::string > yaze::emu::mesen::MesenSocketClient::SendCommand ( const std::string & json)

◆ FindSocketPaths()

std::vector< std::string > yaze::emu::mesen::MesenSocketClient::FindSocketPaths ( )
staticprivate

Find available Mesen2 socket paths.

Definition at line 710 of file mesen_socket_client.cc.

Referenced by Connect(), and ListAvailableSockets().

◆ ParseResponse()

absl::StatusOr< std::string > yaze::emu::mesen::MesenSocketClient::ParseResponse ( const std::string & response)
private

Parse JSON response for success/error.

Definition at line 849 of file mesen_socket_client.cc.

Referenced by SendCommandOnSocket(), and Subscribe().

◆ SendCommandOnSocket()

absl::StatusOr< std::string > yaze::emu::mesen::MesenSocketClient::SendCommandOnSocket ( SocketHandle fd,
const std::string & json,
bool update_connection_state )
private

Send a command using a specific socket descriptor.

Definition at line 769 of file mesen_socket_client.cc.

References connected_, yaze::emu::mesen::kInvalidSocketHandle, and ParseResponse().

Referenced by SendCommand().

Here is the call graph for this function:

◆ EventLoop()

Member Data Documentation

◆ socket_fd_

SocketHandle yaze::emu::mesen::MesenSocketClient::socket_fd_ = kInvalidSocketHandle
private

Definition at line 422 of file mesen_socket_client.h.

Referenced by Connect(), Disconnect(), and SendCommand().

◆ event_socket_fd_

SocketHandle yaze::emu::mesen::MesenSocketClient::event_socket_fd_ = kInvalidSocketHandle
private

Definition at line 423 of file mesen_socket_client.h.

Referenced by EventLoop(), Subscribe(), and Unsubscribe().

◆ socket_path_

std::string yaze::emu::mesen::MesenSocketClient::socket_path_
private

Definition at line 428 of file mesen_socket_client.h.

Referenced by Connect(), Disconnect(), GetSocketPath(), and Subscribe().

◆ connected_

std::atomic<bool> yaze::emu::mesen::MesenSocketClient::connected_ {false}
private

Definition at line 429 of file mesen_socket_client.h.

Referenced by Connect(), Disconnect(), IsConnected(), and SendCommandOnSocket().

◆ command_mutex_

std::mutex yaze::emu::mesen::MesenSocketClient::command_mutex_
private

Definition at line 430 of file mesen_socket_client.h.

Referenced by SendCommand().

◆ event_callback_mutex_

std::mutex yaze::emu::mesen::MesenSocketClient::event_callback_mutex_
private

◆ event_callback_

EventCallback yaze::emu::mesen::MesenSocketClient::event_callback_
private

Definition at line 434 of file mesen_socket_client.h.

Referenced by EventLoop(), and SetEventCallback().

◆ event_listeners_

std::unordered_map<EventListenerId, EventCallback> yaze::emu::mesen::MesenSocketClient::event_listeners_
private

Definition at line 435 of file mesen_socket_client.h.

Referenced by AddEventListener(), EventLoop(), and RemoveEventListener().

◆ next_event_listener_id_

EventListenerId yaze::emu::mesen::MesenSocketClient::next_event_listener_id_ = 1
private

Definition at line 436 of file mesen_socket_client.h.

Referenced by AddEventListener().

◆ event_thread_

std::thread yaze::emu::mesen::MesenSocketClient::event_thread_
private

Definition at line 437 of file mesen_socket_client.h.

Referenced by Subscribe(), and Unsubscribe().

◆ event_thread_running_

std::atomic<bool> yaze::emu::mesen::MesenSocketClient::event_thread_running_ {false}
private

Definition at line 438 of file mesen_socket_client.h.

Referenced by EventLoop(), Subscribe(), and Unsubscribe().

◆ pending_event_payload_

std::string yaze::emu::mesen::MesenSocketClient::pending_event_payload_
private

Definition at line 439 of file mesen_socket_client.h.

Referenced by EventLoop(), Subscribe(), and Unsubscribe().

◆ current_input_

emu::input::ControllerState yaze::emu::mesen::MesenSocketClient::current_input_
private

Definition at line 442 of file mesen_socket_client.h.

Referenced by SetButton(), and SetButtons().


The documentation for this class was generated from the following files: