1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Made the DLL handle-based: several clients per process

- BakaShrot_CreateClient() returns a handle, the first argument of every
  other export (same names); BakaShrot_FreeClient() frees it. No default
  client, no exports without a handle: apps built for the old DLL need
  updating
- Each client has its own settings, connection, answers and errors;
  different clients run at once from threads. A bad handle (freed, made
  up, 0) fails with InvalidArgument, never crashes; handles are never
  reused
- Python: each Client has its own handle, so several can be connected at
  once (the one-Client restriction is gone)
- PureBasic: the prototypes take the handle first; the example runs a
  second client beside the first. Passed with 5.73 LTS and 6.41
- Tests: exports.rs `clients` (two identities at once, bad and freed
  handles, 4 threads on 4 clients, a client freed mid-call); the Python
  check connects two Clients and pushes from two threads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 23:17:48 +01:00
parent 5b41a06d0c
commit d061ff6f80
10 changed files with 1074 additions and 561 deletions
+94 -61
View File
@@ -18,67 +18,79 @@ extern "C" {
#endif // __cplusplus
/**
* Why the last call failed: a protocol error code (1-99 a server's
* refusal, the connection still open; 100 and up the client's own), 0 if
* it succeeded. Getters don't change it.
* Makes a client: its handle (1 and up, never reused), 0 if it failed.
*/
int32_t BakaShrot_GetLastError(void);
int64_t BakaShrot_CreateClient(void);
/**
* Why the last call failed, in words: the server's reason for a refusal,
* the client's otherwise. Empty if it succeeded.
* Frees a client and closes its connection (once a call on it in another
* thread ends): 1, or 0 if it wasn't one.
*/
ptrdiff_t BakaShrot_GetRefusalReasonA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_FreeClient(int64_t client);
/**
* Why the client's last call failed: a protocol error code (1-99 a
* server's refusal, the connection still open; 100 and up the client's
* own), 0 if it succeeded. Getters don't change it. `InvalidArgument` for
* a bad handle.
*/
int32_t BakaShrot_GetLastError(int64_t client);
/**
* Why the client's last call failed, in words: the server's reason for a
* refusal, the client's otherwise. Empty if it succeeded.
*/
ptrdiff_t BakaShrot_GetRefusalReasonA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetRefusalReasonA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetRefusalReasonW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetRefusalReasonW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* Sets the CA's certificate, which the server's must be issued by: PEM
* text, or the path of a file holding it. Before connecting.
*/
ptrdiff_t BakaShrot_SetCaCertificateA(const char *certificate);
ptrdiff_t BakaShrot_SetCaCertificateA(int64_t client, const char *certificate);
/**
* The same as `BakaShrot_SetCaCertificateA`, in UTF-16.
*/
ptrdiff_t BakaShrot_SetCaCertificateW(const uint16_t *certificate);
ptrdiff_t BakaShrot_SetCaCertificateW(int64_t client, const uint16_t *certificate);
/**
* Sets this client's certificate, which says its node and its roles: PEM
* Sets the client's certificate, which says its node and its roles: PEM
* text, or the path of a file holding it. Before connecting.
*/
ptrdiff_t BakaShrot_SetCertificateA(const char *certificate);
ptrdiff_t BakaShrot_SetCertificateA(int64_t client, const char *certificate);
/**
* The same as `BakaShrot_SetCertificateA`, in UTF-16.
*/
ptrdiff_t BakaShrot_SetCertificateW(const uint16_t *certificate);
ptrdiff_t BakaShrot_SetCertificateW(int64_t client, const uint16_t *certificate);
/**
* Sets this client's private key, its certificate's: PEM text, or the path
* Sets the client's private key, its certificate's: PEM text, or the path
* of a file holding it. Before connecting.
*/
ptrdiff_t BakaShrot_SetPrivateKeyA(const char *key);
ptrdiff_t BakaShrot_SetPrivateKeyA(int64_t client, const char *key);
/**
* The same as `BakaShrot_SetPrivateKeyA`, in UTF-16.
*/
ptrdiff_t BakaShrot_SetPrivateKeyW(const uint16_t *key);
ptrdiff_t BakaShrot_SetPrivateKeyW(int64_t client, const uint16_t *key);
/**
* Sets the CA's CRL, optional: servers it revokes are refused. PEM text,
* or the path of a file holding it (one that isn't there is a
* `FileError`); null or empty for none. Before connecting.
*/
ptrdiff_t BakaShrot_SetCrlA(const char *crl);
ptrdiff_t BakaShrot_SetCrlA(int64_t client, const char *crl);
/**
* The same as `BakaShrot_SetCrlA`, in UTF-16.
*/
ptrdiff_t BakaShrot_SetCrlW(const uint16_t *crl);
ptrdiff_t BakaShrot_SetCrlW(int64_t client, const uint16_t *crl);
/**
* Connects to the server at `host` (a name or an address) and `port`:
@@ -87,57 +99,59 @@ ptrdiff_t BakaShrot_SetCrlW(const uint16_t *crl);
* the server refused the certificate, `ServerNotVerified` this client
* refused the server's, `NotAllowed` none of its roles is the server's).
*/
ptrdiff_t BakaShrot_ConnectA(const char *host, int32_t port);
ptrdiff_t BakaShrot_ConnectA(int64_t client, const char *host, int32_t port);
/**
* The same as `BakaShrot_ConnectA`, in UTF-16.
*/
ptrdiff_t BakaShrot_ConnectW(const uint16_t *host, int32_t port);
ptrdiff_t BakaShrot_ConnectW(int64_t client, const uint16_t *host, int32_t port);
/**
* 1 while connected: notices when the server closed the connection
* (forgotten then: connect again).
*/
ptrdiff_t BakaShrot_IsConnected(void);
ptrdiff_t BakaShrot_IsConnected(int64_t client);
/**
* Closes the connection: 0 if there was none. The settings stay.
*/
ptrdiff_t BakaShrot_Disconnect(void);
ptrdiff_t BakaShrot_Disconnect(int64_t client);
/**
* The node this client is, as the server said when connecting: its
* certificate's name. Empty when not connected.
*/
ptrdiff_t BakaShrot_GetNodeA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetNodeA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetNodeA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetNodeW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetNodeW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* Pushes a message (JSON or not) to `topic`, as this client's node: its
* id (1 and up), -1 on failure. A push whose answer was lost
* (`ConnectionLost`) may have been stored all the same.
*/
int64_t BakaShrot_PushMessageToA(const char *topic, const char *data);
int64_t BakaShrot_PushMessageToA(int64_t client, const char *topic, const char *data);
/**
* The same as `BakaShrot_PushMessageToA`, in UTF-16.
*/
int64_t BakaShrot_PushMessageToW(const uint16_t *topic, const uint16_t *data);
int64_t BakaShrot_PushMessageToW(int64_t client, const uint16_t *topic, const uint16_t *data);
/**
* Gets the latest message of `topic` and `node`, then read with the
* `BakaShrot_GetMessage*()` getters. `NotFound` if there's none yet.
*/
ptrdiff_t BakaShrot_GetLatestMessageOfA(const char *topic, const char *node);
ptrdiff_t BakaShrot_GetLatestMessageOfA(int64_t client, const char *topic, const char *node);
/**
* The same as `BakaShrot_GetLatestMessageOfA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetLatestMessageOfW(const uint16_t *topic, const uint16_t *node);
ptrdiff_t BakaShrot_GetLatestMessageOfW(int64_t client,
const uint16_t *topic,
const uint16_t *node);
/**
* Gets the messages of `topic` and `node` after id `after` (0 for the
@@ -146,12 +160,17 @@ ptrdiff_t BakaShrot_GetLatestMessageOfW(const uint16_t *topic, const uint16_t *n
* first is selected; `BakaShrot_SelectMessage()` selects the others,
* `BakaShrot_HasMoreMessages()` says whether more follow the last one.
*/
int32_t BakaShrot_GetMessagesA(const char *topic, const char *node, int64_t after, int32_t limit);
int32_t BakaShrot_GetMessagesA(int64_t client,
const char *topic,
const char *node,
int64_t after,
int32_t limit);
/**
* The same as `BakaShrot_GetMessagesA`, in UTF-16.
*/
int32_t BakaShrot_GetMessagesW(const uint16_t *topic,
int32_t BakaShrot_GetMessagesW(int64_t client,
const uint16_t *topic,
const uint16_t *node,
int64_t after,
int32_t limit);
@@ -159,119 +178,119 @@ int32_t BakaShrot_GetMessagesW(const uint16_t *topic,
/**
* How many messages the last `GetMessages` or `GetLatestMessageOf` got.
*/
int32_t BakaShrot_GetMessageCount(void);
int32_t BakaShrot_GetMessageCount(int64_t client);
/**
* Selects the message at `index` (0 to the count - 1) for the getters: 0
* if there's none there (`InvalidArgument`), the selection unchanged.
*/
ptrdiff_t BakaShrot_SelectMessage(int32_t index);
ptrdiff_t BakaShrot_SelectMessage(int64_t client, int32_t index);
/**
* 1 if more messages follow the last one the last `GetMessages` got: ask
* again after its id.
*/
ptrdiff_t BakaShrot_HasMoreMessages(void);
ptrdiff_t BakaShrot_HasMoreMessages(int64_t client);
/**
* The selected message's id, 0 if none.
*/
int64_t BakaShrot_GetMessageId(void);
int64_t BakaShrot_GetMessageId(int64_t client);
/**
* When the selected message was pushed, by the server it was pushed to:
* milliseconds since 1970-01-01 UTC, 0 if none.
*/
int64_t BakaShrot_GetMessagePushedAt(void);
int64_t BakaShrot_GetMessagePushedAt(int64_t client);
/**
* The selected message's attachment's size, 0 without one.
*/
int64_t BakaShrot_GetMessageAttachmentSize(void);
int64_t BakaShrot_GetMessageAttachmentSize(int64_t client);
/**
* 1 if the selected message's attachment is all on this server (a
* replicated one may still be on its way).
*/
ptrdiff_t BakaShrot_IsMessageAttachmentReady(void);
ptrdiff_t BakaShrot_IsMessageAttachmentReady(int64_t client);
/**
* The selected message's data, as pushed (JSON or not).
*/
ptrdiff_t BakaShrot_GetMessageDataA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetMessageDataA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetMessageDataA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetMessageDataW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetMessageDataW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* The topic the last `GetMessages` or `GetLatestMessageOf` asked for.
*/
ptrdiff_t BakaShrot_GetMessageTopicA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetMessageTopicA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetMessageTopicA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetMessageTopicW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetMessageTopicW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* The node the last `GetMessages` or `GetLatestMessageOf` asked for.
*/
ptrdiff_t BakaShrot_GetMessageNodeA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetMessageNodeA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetMessageNodeA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetMessageNodeW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetMessageNodeW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* Lists the topics with messages that this client may push to or read
* (every one if its roles have `list_topics` or `query`): how many, -1 on
* failure. Then `BakaShrot_GetNamesList()`.
*/
int32_t BakaShrot_ListTopics(void);
int32_t BakaShrot_ListTopics(int64_t client);
/**
* Lists the nodes with messages in the topics this client may read (every
* one if its roles have `list_nodes` or `query`): how many, -1 on failure. Then
* `BakaShrot_GetNamesList()`.
*/
int32_t BakaShrot_ListNodes(void);
int32_t BakaShrot_ListNodes(int64_t client);
/**
* The last list, as JSON:
* `{"kind":"topic","truncated":false,"names":["logs",...]}`, `truncated`
* if some didn't fit.
*/
ptrdiff_t BakaShrot_GetNamesListA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetNamesListA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetNamesListA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetNamesListW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetNamesListW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* Runs read-only SQL on the server (one SELECT or WITH), if this client
* may query. Then `BakaShrot_GetQueryResult()`.
*/
ptrdiff_t BakaShrot_QueryA(const char *sql);
ptrdiff_t BakaShrot_QueryA(int64_t client, const char *sql);
/**
* The same as `BakaShrot_QueryA`, in UTF-16.
*/
ptrdiff_t BakaShrot_QueryW(const uint16_t *sql);
ptrdiff_t BakaShrot_QueryW(int64_t client, const uint16_t *sql);
/**
* The last query's result, as JSON:
* `{"columns":[...],"rows":[[...],...]}`, every value a string or null.
*/
ptrdiff_t BakaShrot_GetQueryResultA(char *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetQueryResultA(int64_t client, char *buffer, ptrdiff_t size);
/**
* The same as `BakaShrot_GetQueryResultA`, in UTF-16.
*/
ptrdiff_t BakaShrot_GetQueryResultW(uint16_t *buffer, ptrdiff_t size);
ptrdiff_t BakaShrot_GetQueryResultW(int64_t client, uint16_t *buffer, ptrdiff_t size);
/**
* Pushes a message with the file at `FilePath` as its attachment (1 byte
@@ -280,12 +299,16 @@ ptrdiff_t BakaShrot_GetQueryResultW(uint16_t *buffer, ptrdiff_t size);
* read twice (its SHA-256 first): one changed in between is refused
* (`DataMismatch`).
*/
int64_t BakaShrot_PushMessageWithFileA(const char *topic, const char *data, const char *file_path);
int64_t BakaShrot_PushMessageWithFileA(int64_t client,
const char *topic,
const char *data,
const char *file_path);
/**
* The same as `BakaShrot_PushMessageWithFileA`, in UTF-16.
*/
int64_t BakaShrot_PushMessageWithFileW(const uint16_t *topic,
int64_t BakaShrot_PushMessageWithFileW(int64_t client,
const uint16_t *topic,
const uint16_t *data,
const uint16_t *file_path);
@@ -293,7 +316,8 @@ int64_t BakaShrot_PushMessageWithFileW(const uint16_t *topic,
* Pushes a message with the `Size` bytes at `Data` as its attachment, as
* `BakaShrot_PushMessageWithFileA` does.
*/
int64_t BakaShrot_PushMessageWithDataA(const char *topic,
int64_t BakaShrot_PushMessageWithDataA(int64_t client,
const char *topic,
const char *data,
const void *attachment,
int64_t size);
@@ -301,7 +325,8 @@ int64_t BakaShrot_PushMessageWithDataA(const char *topic,
/**
* The same as `BakaShrot_PushMessageWithDataA`, in UTF-16.
*/
int64_t BakaShrot_PushMessageWithDataW(const uint16_t *topic,
int64_t BakaShrot_PushMessageWithDataW(int64_t client,
const uint16_t *topic,
const uint16_t *data,
const void *attachment,
int64_t size);
@@ -313,7 +338,8 @@ int64_t BakaShrot_PushMessageWithDataW(const uint16_t *topic,
* `NotReady` if it isn't on this server yet, `AttachmentMismatch`). A
* failed one leaves no file.
*/
int64_t BakaShrot_SaveAttachmentA(const char *topic,
int64_t BakaShrot_SaveAttachmentA(int64_t client,
const char *topic,
const char *node,
int64_t message_id,
const char *file_path);
@@ -321,7 +347,8 @@ int64_t BakaShrot_SaveAttachmentA(const char *topic,
/**
* The same as `BakaShrot_SaveAttachmentA`, in UTF-16.
*/
int64_t BakaShrot_SaveAttachmentW(const uint16_t *topic,
int64_t BakaShrot_SaveAttachmentW(int64_t client,
const uint16_t *topic,
const uint16_t *node,
int64_t message_id,
const uint16_t *file_path);
@@ -331,24 +358,30 @@ int64_t BakaShrot_SaveAttachmentW(const uint16_t *topic,
* memory, checked with its SHA-256 (`BakaShrot_SaveAttachmentA` for big
* ones): its size, -1 on failure. Then `BakaShrot_GetAttachmentData()`.
*/
int64_t BakaShrot_LoadAttachmentA(const char *topic, const char *node, int64_t message_id);
int64_t BakaShrot_LoadAttachmentA(int64_t client,
const char *topic,
const char *node,
int64_t message_id);
/**
* The same as `BakaShrot_LoadAttachmentA`, in UTF-16.
*/
int64_t BakaShrot_LoadAttachmentW(const uint16_t *topic, const uint16_t *node, int64_t message_id);
int64_t BakaShrot_LoadAttachmentW(int64_t client,
const uint16_t *topic,
const uint16_t *node,
int64_t message_id);
/**
* Copies what the last `BakaShrot_LoadAttachment()` read into `Buffer`, if
* `BufferSize` is enough: returns its size (0 if none), copies nothing if
* it doesn't fit (a null `Buffer` asks for the size).
*/
int64_t BakaShrot_GetAttachmentData(void *buffer, int64_t buffer_size);
int64_t BakaShrot_GetAttachmentData(int64_t client, void *buffer, int64_t buffer_size);
/**
* Frees what `BakaShrot_LoadAttachment()` read: 1.
*/
ptrdiff_t BakaShrot_FreeAttachmentData(void);
ptrdiff_t BakaShrot_FreeAttachmentData(int64_t client);
#ifdef __cplusplus
} // extern "C"