/* spms3c.h — scorpiox-scorpioplayer-spms-v3-tcp-media-client * * SCORPIO PLAYER 2027 row 35. The SPMS v3 binary TCP protocol, CLIENT side, in pure C99. * * Speaks every server dialect the legacy fleet deployed (see SPEC.md §2 for the measured * divergence table): * CORE — Core Server.cs / iOS host: handshake JSON, OpenFile silent, raw count bytes * (zero-padded past EOF), SeekRead(count, position). Canonical. * CLI — wpfcoretest.cli (the live .90 build): handshake "OK", OpenFile replies int64, * Read/SeekRead reply int32 len + bytes, SeekRead(position, count). * STORE — WPF/Store TCPIP.cs: handshake JSON (Version 3, Windows_Desktop) with the same * framing as CORE (measured: the two copies agree except the handshake platform). * UNKNOWN — not connected / probe failed. * * Dialect detection is explicit (SPEC D2): the handshake reply is parsed; a JSON body with a * numeric Version is CORE or STORE; the literal "OK" (or any non-JSON body) is CLI. The legacy * client's exception fallthrough (JsonConvert throws -> Version = 0) is NEVER relied on. * * EOF rule (SPEC D4): the protocol's Read/SeekRead reply has no length prefix on CORE/STORE and * the server zero-pads past EOF. Every read is therefore capped at size - position HERE, and * zero-pad bytes are stripped before anything reaches the caller. spms3c_read_at returns the * REAL number of file bytes read (0 at/after EOF), never a padded count. * * Threading/socket model (SPEC D6): one connection owns ONE current file (the server is * stateful per socket). The engine serialises all wire traffic behind one mutex; concurrent * spms3c_* calls from multiple threads are safe but interleaved (no two sessions at once). * A caller wanting parallel streams opens more connections with spms3c_connect() — but note * spms3c_open binds the file to the MOST RECENT connection... no: spms3c_open works on the * current connection; use spms3c_conn_count()/spms3c_conn_select(i) for the rest. * * Strings returned by spms3c_* are owned by the library and valid until the next call that * changes the same state. Copy them. */ #ifndef SPMS3C_H #define SPMS3C_H #ifdef _WIN32 # ifdef SPMS3C_BUILD # define SPMS3C_API __declspec(dllexport) # else # define SPMS3C_API __declspec(dllimport) # endif #else # define SPMS3C_API __attribute__((visibility("default"))) #endif #ifdef __cplusplus extern "C" { #endif /* dialect ids (spms3c_connect's return; also in server_info_json) */ #define SPMS3C_D_NONE 0 /* not connected */ #define SPMS3C_D_CORE 1 /* Core Server.cs (iOS) — and our row 34 server */ #define SPMS3C_D_CLI 2 /* wpfcoretest.cli ("OK" handshake) */ #define SPMS3C_D_STORE 3 /* WPF/Store TCPIP.cs (JSON handshake, Core framing) */ #define SPMS3C_D_UNKNOWN 4 /* connected but the handshake was unusable (v1 fallback) */ /* error taxonomy (mirrors the fleet: 0 ok, negative codes) */ #define SPMS3C_OK 0 #define SPMS3C_E_ARG (-1) #define SPMS3C_E_STATE (-2) /* not connected / no open file where one is required */ #define SPMS3C_E_NET (-3) /* transport failure, timeout, refused */ #define SPMS3C_E_NOENT (-4) /* OpenFile says the file is not there (CLI 0-length; CORE Size 0) */ #define SPMS3C_E_NOMEM (-5) #define SPMS3C_E_LIMIT (-6) /* reply over the bounds (SPEC D5) */ #define SPMS3C_E_PROTO (-7) /* the server answered something this dialect does not produce */ /* ---- version / errors ---- */ SPMS3C_API const char *spms3c_version(void); SPMS3C_API int spms3c_last_code(void); SPMS3C_API const char *spms3c_last_error(void); /* ---- config (before use; NULL value = default) ---- * timeout_ms connect+call timeout (default 10000) * max_string longest reply string accepted (default 8 MiB, cap 64 MiB) * max_read largest single read_at (default 4 MiB, cap 32 MiB) * auto_open 1 = open() re-opens the CLI-dialect file when Size says 0 (default 1) */ SPMS3C_API int spms3c_config(const char *key, const char *value); /* ---- connections ---- * connect: dials host:port, sends HandShake, parses the dialect. Returns the dialect id * (>0) or <0. The dialect decides every later framing choice. */ SPMS3C_API int spms3c_connect(const char *host, int port, int timeout_ms); SPMS3C_API void spms3c_disconnect(void); SPMS3C_API int spms3c_is_connected(void); SPMS3C_API int spms3c_dialect(void); /* SPMS3C_D_* of the current conn */ /* {"dialect":"core","dialect_id":1,"version":3,"platform":"scorpiox-c","host":..,"port":..} */ SPMS3C_API const char *spms3c_server_info_json(void); /* the raw handshake string the server sent ("" when none) */ SPMS3C_API const char *spms3c_handshake_raw(void); /* ---- browse (each returns the item count >= 0, or <0) ---- * folders() the roots (Folders) * subfolders(id) folders inside id (SubFolders) * open_folder(id_or_name) the FILES of that folder (OpenFolder; legacy semantics) * files() files directly under the roots (Files) * item_json(i) the i-th item of the LAST listing as the wire JSON verbatim */ SPMS3C_API int spms3c_folders(void); SPMS3C_API int spms3c_subfolders(const char *fullname); SPMS3C_API int spms3c_open_folder(const char *fullname_or_name); SPMS3C_API int spms3c_files(void); SPMS3C_API int spms3c_item_count(void); SPMS3C_API const char *spms3c_item_json(int i); /* ---- open + read ---- * open(file_json): sends OpenFile with the caller's JSON (echo what the server gave), then * Size. Returns 0 ok; E_NOENT when the dialect's not-found signal appeared (CLI length 0 / * CORE size 0); E_NET on transport failure. size() is only valid after open(). */ SPMS3C_API int spms3c_open(const char *file_json); SPMS3C_API long long spms3c_size(void); SPMS3C_API long long spms3c_position(void); /* read_at: SeekRead when the dialect is v3-capable (CORE/STORE), else Seek+Read (CLI). * pos is clamped to [0, size]; count is capped at size-pos; the reply is trimmed to the * REAL bytes (zero-pad never reaches the caller). Returns bytes read (0 = EOF), <0 error. */ SPMS3C_API int spms3c_read_at(long long pos, void *buf, int count); /* subtitles(file_json): the Subtitle reply JSON ([] or [{"Name","Base64"}]) */ SPMS3C_API const char *spms3c_subtitles(const char *file_json); /* end the session politely (action 0) and close */ SPMS3C_API void spms3c_close(void); /* ---- fake transport (offline tests; the .console --test never opens a socket) ---- * A scripted byte-stream server: bytes the "server" writes, and what open() reports. */ SPMS3C_API int spms3c_use_fake(void); SPMS3C_API int spms3c_use_real(void); /* push bytes the fake server will send, in order (they are consumed by the reads) */ SPMS3C_API int spms3c_fake_server_bytes(const unsigned char *data, int n); /* the bytes the client sent, for framing assertions */ SPMS3C_API int spms3c_fake_client_bytes(unsigned char *out, int cap); SPMS3C_API void spms3c_fake_reset(void); /* make the next Nth wire op fail (transport error) — 0 disables */ SPMS3C_API int spms3c_fake_fail_after(int n); #ifdef __cplusplus } #endif #endif /* SPMS3C_H */