aboutsummaryrefslogtreecommitdiff
path: root/client/libacron/include/net.h
diff options
context:
space:
mode:
authorTrumeet <yuuta@yuuta.moe>2022-07-23 11:03:20 -0700
committerTrumeet <yuuta@yuuta.moe>2022-07-23 11:03:20 -0700
commit6c49e2e93b23defd51d4b342baad5a9c3f843ea1 (patch)
treeccf1c90934571fe9472c85ea86ee1c5112ce3e65 /client/libacron/include/net.h
parent4337729c2fa77871e3b10cbe329f286c01ddc01f (diff)
downloadacron-6c49e2e93b23defd51d4b342baad5a9c3f843ea1.tar
acron-6c49e2e93b23defd51d4b342baad5a9c3f843ea1.tar.gz
acron-6c49e2e93b23defd51d4b342baad5a9c3f843ea1.tar.bz2
acron-6c49e2e93b23defd51d4b342baad5a9c3f843ea1.zip
feat(libacron/acronc): delegate socket IO to the client
API:CHANGE Signed-off-by: Trumeet <yuuta@yuuta.moe>
Diffstat (limited to 'client/libacron/include/net.h')
-rw-r--r--client/libacron/include/net.h51
1 files changed, 49 insertions, 2 deletions
diff --git a/client/libacron/include/net.h b/client/libacron/include/net.h
index 5cf8f67..03be12f 100644
--- a/client/libacron/include/net.h
+++ b/client/libacron/include/net.h
@@ -10,6 +10,8 @@
#include "events.h"
#include "requests.h"
+#include <stddef.h>
+
/* Connection */
typedef struct ac_connection_parameters {
@@ -18,8 +20,38 @@ typedef struct ac_connection_parameters {
unsigned int version;
char *id;
char *token;
+ /**
+ * An established socket using the host and port parameters above.
+ * The type of the value is opaque to libac, and it will pass
+ * the value as-is to the on_send function pointer parameter.
+ *
+ * The user must keep the socket to disconnect manuall after
+ * calling ac_disconnect().
+ */
+ void *sock;
+
+ /**
+ * Called when ac_request needs to write to the socket.
+ * This function is called on the same thread calling ac_*.
+ * Any functions related to the connection may call on_send.
+ * These functions do not have internal locking, and concurrent
+ * accesses to the same connection object is a racing condition.
+ * Applications are required to do their own locking to guard
+ * the connection.
+ * @param sock The same sock passed in.
+ * @param buffer The buffer of data.
+ * @param len The length to the buffer.
+ * @return 0 on success. Otherwise error.
+ */
+ int (*on_send)(const void *sock, const void *buffer, const size_t len);
} ac_connection_parameters_t;
+enum ac_connection_state {
+ AC_STATE_INIT,
+ AC_STATE_READY,
+ AC_STATE_CLOSED
+};
+
/**
* Connect to the server.
* @param parameters Connection parameters. Untouched.
@@ -31,6 +63,7 @@ LIBAC_EXPORT int ac_connect(ac_connection_parameters_t parameters, void **out);
/**
* Disconnect the connection.
+ * The user must manually disconnect the socket passed in during ac_connect.
* @param connection A non-NULL and connected connection passed as-is from ac_connect.
* @return AC_E_OK or an error code. When failed, connection is undefined. When succeeds, connection is freed and invalid.
* MT-Unsafe
@@ -40,10 +73,24 @@ LIBAC_EXPORT int ac_disconnect(void *connection);
/**
* Blocks the current thread until a new response or event arrives.
* @param connection A non-NULL and connected connection passed as-is from ac_connect.
+ * @param buffer Buffer of data read from the socket.
+ * @param len Length to buffer.
* @param response Output response of either an event or a response. May be NULL even if it succeeds.
- * @param timeout Read timeout in seconds. Set to 0 to wait infinitely.
* @return AC_E_OK or an error code. When failed, *response is NULL.
+ * Notes: if the state is changed to CLOSED after receiving, this function will return AC_E_NET.
+ */
+LIBAC_EXPORT int ac_receive(void *connection,
+ const void *buffer,
+ size_t len,
+ ac_obj_t **response);
+
+/**
+ * Get the current state of the connection.
+ * @param connection A non-NULL and connected connection passed as-is from ac_connect.
+ * @param out Output state.
+ * @return AC_E_OK or an error code. When failed, *out is untouched.
*/
-LIBAC_EXPORT int ac_receive(void *connection, ac_obj_t **response, long timeout);
+LIBAC_EXPORT int ac_get_state(void *connection,
+ enum ac_connection_state *out);
#endif /* LIBAC_NET_H */