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

Unix socket client for 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 (int 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

int socket_fd_ = -1
 
int event_socket_fd_ = -1
 
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

Unix socket client for Mesen2-OoS fork.

Connects to Mesen2's socket API at /tmp/mesen2-<pid>.sock and provides type-safe wrapper methods for all commands.

Definition at line 136 of file mesen_socket_client.h.

Constructor & Destructor Documentation

◆ MesenSocketClient() [1/2]

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

◆ ~MesenSocketClient()

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

Definition at line 238 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.

Returns
Status indicating success or error

Definition at line 242 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_pathFull path to Unix socket (e.g., /tmp/mesen2-12345.sock)

Definition at line 251 of file mesen_socket_client.cc.

References connected_, Disconnect(), IsConnected(), 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 276 of file mesen_socket_client.cc.

References connected_, 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 288 of file mesen_socket_client.cc.

References connected_.

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

◆ GetSocketPath()

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

Get the current socket path.

Definition at line 174 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 340 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 439 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 461 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 466 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 471 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 476 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 481 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 491 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 531 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 539 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 547 of file mesen_socket_client.cc.

References SendCommand().

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 577 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 584 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 591 of file mesen_socket_client.cc.

References SendCommand().

Here is the call graph for this function:

◆ GetCpuState()

◆ Disassemble()

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

Disassemble instructions at address.

Definition at line 626 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 636 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 670 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 676 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 682 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 792 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 804 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 810 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 816 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 827 of file mesen_socket_client.cc.

References event_socket_fd_, event_thread_, event_thread_running_, EventLoop(), IsConnected(), ParseResponse(), pending_event_payload_, socket_path_, and Unsubscribe().

Here is the call graph for this function:

◆ Unsubscribe()

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

Unsubscribe from events.

Definition at line 919 of file mesen_socket_client.cc.

References event_socket_fd_, event_thread_, event_thread_running_, and pending_event_payload_.

Referenced by Disconnect(), and Subscribe().

◆ SetEventCallback()

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

Set callback for received events.

Definition at line 935 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 940 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 950 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 292 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 421 of file mesen_socket_client.cc.

Referenced by SendCommandOnSocket(), and Subscribe().

◆ SendCommandOnSocket()

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

Send a command using a specific socket descriptor.

Definition at line 344 of file mesen_socket_client.cc.

References connected_, and ParseResponse().

Referenced by SendCommand().

Here is the call graph for this function:

◆ EventLoop()

void yaze::emu::mesen::MesenSocketClient::EventLoop ( )
private

Member Data Documentation

◆ socket_fd_

int yaze::emu::mesen::MesenSocketClient::socket_fd_ = -1
private

Definition at line 411 of file mesen_socket_client.h.

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

◆ event_socket_fd_

int yaze::emu::mesen::MesenSocketClient::event_socket_fd_ = -1
private

Definition at line 412 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 413 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 414 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 415 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 419 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 420 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 421 of file mesen_socket_client.h.

Referenced by AddEventListener().

◆ event_thread_

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

Definition at line 422 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 423 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 424 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 427 of file mesen_socket_client.h.

Referenced by SetButton(), and SetButtons().


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