Files
ppsspp/Core/Debugger/WebSocket/WebSocketUtils.h
Henrik RydgårdandClaude Opus 5 a8933099b7 Make the deferred-request acknowledgement opt-in, and distinct
The acknowledgement added in "Reply to every debugger request" broke two cases
Nemoumbra pointed out, both of which come down to it reusing the request's own
event name.

A ticketless request is the bad one. {"event":"cpu.resume"} with no ticket drew
an immediate {"event":"cpu.resume"} - byte-identical to the broadcast that fires
when the game actually resumes. A client waiting for that broadcast concluded
the game was running while it was still stopped. Before, it correctly got
nothing until the resume really happened.

input.buttons.press is broken even with a ticket: it answers with the request's
own event name *and* ticket once the button has been held for the requested
frames, so the acknowledgement was indistinguishable from the real completion
and a client resolved on the first of the two. The claim in that commit that
the two are easy to tell apart was simply wrong for this handler.

So the acknowledgement is now off by default - the wire behaviour for every
existing client is exactly what it was - and a client that wants it asks, with
client.config.set {"acknowledgeDeferred": true}. It then arrives as its own
event rather than an echo:

  -> {"event":"cpu.resume","ticket":7}
  <- {"event":"deferred","for":"cpu.resume","ticket":7}
  <- {"event":"cpu.resume"}

which is unambiguous in both cases above. That still gets the original goal -
correlating any request to a reply without hardcoding which events answer
immediately, including ones added later - just without imposing it on clients
that never asked.

Also documents the ticket convention this rests on: send one when you care
about the answer, leave it off to say you aren't waiting. wsdbg followed that
convention badly, silently inserting a ticket into a raw JSON line that
deliberately omitted one; it now sends raw lines exactly as written and simply
doesn't wait on those. It opts into acknowledgements at connect, so --sync
keeps working.

pspautotests 314/314, UnitTest 55/55.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZq8ZtJmFY7bkX5FVkr3P9
2026-08-18 09:32:04 +02:00

158 lines
4.8 KiB
C++

// Copyright (c) 2018- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#pragma once
#include "ppsspp_config.h"
#include <string>
#include "Common/Log.h"
#include "Common/Data/Format/JSONReader.h"
#include "Common/Data/Format/JSONWriter.h"
#include "Common/Net/WebsocketServer.h"
#if PPSSPP_PLATFORM(UWP)
// Enum name overlapped with UWP macro, quick hack to disable it
#undef OPTIONAL
#endif
using namespace json;
struct WebSocketClientInfo {
WebSocketClientInfo() = default;
std::string name;
std::string version;
std::map<std::string, bool> disallowed;
// Whether to send a "deferred" event for requests that only finish later. Off by default,
// and it has to stay that way: an extra message would break a client that correlates purely
// by ticket, or one that waits for the bare event name a resume or a button press answers
// with. See client.config.set.
bool acknowledgeDeferred = false;
};
// Sent, if the client asked for it, when a handler accepted a request but arranged to finish it
// later - a resume, a step, a button held for some frames. Deliberately not named after the
// request: the event that reports the actual outcome uses that name, and for input.buttons.press
// it carries the same ticket too, so anything less distinct would be impossible to tell apart.
struct DebuggerDeferredEvent {
DebuggerDeferredEvent(const char *n, const JsonGet data) : name(n) {
const JsonNode *value = data ? data.get("ticket") : nullptr;
if (value)
ticketRaw = json_stringify(value);
}
const char *name;
std::string ticketRaw;
operator std::string() const {
JsonWriter j;
j.begin();
j.writeString("event", "deferred");
j.writeString("for", name);
if (!ticketRaw.empty()) {
j.writeRaw("ticket", ticketRaw);
}
j.end();
return j.str();
}
};
struct DebuggerErrorEvent {
DebuggerErrorEvent(const std::string m, LogLevel l, const JsonGet data = JsonValue(JSON_NULL))
: message(m), level(l) {
// Need to format right away, before it's out of scope.
if (data) {
const JsonNode *value = data.get("ticket");
if (value)
ticketRaw = json_stringify(value);
}
}
std::string message;
LogLevel level;
std::string ticketRaw;
operator std::string() const {
JsonWriter j;
j.begin();
j.writeString("event", "error");
j.writeString("message", message);
j.writeInt("level", (int)level);
if (!ticketRaw.empty()) {
j.writeRaw("ticket", ticketRaw);
}
j.end();
return j.str();
}
};
enum class DebuggerParamType {
REQUIRED,
OPTIONAL,
REQUIRED_LOOSE,
OPTIONAL_LOOSE,
};
struct DebuggerRequest {
DebuggerRequest(const char *n, net::WebSocketServer *w, const JsonGet &d, WebSocketClientInfo *client_info)
: name(n), ws(w), data(d), client(client_info) {
}
const char *name;
net::WebSocketServer *ws;
const JsonGet data;
WebSocketClientInfo *client;
void Fail(const std::string &message) {
ws->Send(DebuggerErrorEvent(message, LogLevel::LERROR, data));
responseSent_ = true;
}
bool HasParam(const char *name, bool ignoreNull = false);
bool ParamU32(const char *name, uint32_t *out, bool allowFloatBits = false, DebuggerParamType type = DebuggerParamType::REQUIRED);
// For quantities that don't fit in 32 bits, like emulated microseconds - JSON numbers are
// doubles anyway, which is exact well past any plausible session length.
bool ParamF64(const char *name, double *out, DebuggerParamType type = DebuggerParamType::REQUIRED);
bool ParamBool(const char *name, bool *out, DebuggerParamType type = DebuggerParamType::REQUIRED);
bool ParamString(const char *name, std::string *out, DebuggerParamType type = DebuggerParamType::REQUIRED);
JsonWriter &Respond();
void Flush();
bool Finish();
private:
JsonWriter writer_;
bool responseBegun_ = false;
bool responseSent_ = false;
bool responsePartial_ = false;
};
class DebuggerSubscriber {
public:
virtual ~DebuggerSubscriber() {}
// Subscribers can also broadcast if they have simple cases to.
virtual void Broadcast(net::WebSocketServer *ws) {}
};
typedef std::function<void(DebuggerRequest &req)> DebuggerEventHandler;
typedef std::unordered_map<std::string_view, DebuggerEventHandler> DebuggerEventHandlerMap;
uint32_t RoundMemAddressUp(uint32_t addr);