Browse Source

faux.async: New subsystem is added

Serj Kalichev 3 years ago
parent
commit
4b5ca8ff18
5 changed files with 145 additions and 0 deletions
  1. 3 0
      faux/Makefile.am
  2. 37 0
      faux/async.h
  3. 3 0
      faux/async/Makefile.am
  4. 86 0
      faux/async/async.c
  5. 16 0
      faux/async/private.h

+ 3 - 0
faux/Makefile.am

@@ -25,6 +25,7 @@ nobase_include_HEADERS += \
 	faux/net.h \
 	faux/msg.h \
 	faux/eloop.h \
+	faux/async.h \
 	faux/testc_helpers.h
 
 EXTRA_DIST += \
@@ -44,6 +45,7 @@ EXTRA_DIST += \
 	faux/net/Makefile.am \
 	faux/msg/Makefile.am \
 	faux/eloop/Makefile.am \
+	faux/async/Makefile.am \
 	faux/testc_helpers/Makefile.am
 
 include $(top_srcdir)/faux/base/Makefile.am
@@ -62,6 +64,7 @@ include $(top_srcdir)/faux/sched/Makefile.am
 include $(top_srcdir)/faux/net/Makefile.am
 include $(top_srcdir)/faux/msg/Makefile.am
 include $(top_srcdir)/faux/eloop/Makefile.am
+include $(top_srcdir)/faux/async/Makefile.am
 include $(top_srcdir)/faux/testc_helpers/Makefile.am
 
 if TESTC

+ 37 - 0
faux/async.h

@@ -0,0 +1,37 @@
+/** @file async.h
+ * @brief Public interface for ASYNChronous I/O class.
+ */
+
+#ifndef _faux_async_h
+#define _faux_async_h
+
+#include <faux/faux.h>
+#include <faux/sched.h>
+
+typedef struct faux_async_s faux_async_t;
+
+/*
+typedef enum {
+	FAUX_ELOOP_NULL = 0,
+	FAUX_ELOOP_SIGNAL = 1,
+	FAUX_ELOOP_SCHED = 2,
+	FAUX_ELOOP_FD = 3
+} faux_eloop_type_e;
+*/
+
+// Callback function prototypes
+typedef bool_t (*faux_async_read_cb_f)(faux_async_t *async,
+	void *data, size_t len, void *user_data);
+typedef bool_t (*faux_async_stall_cb_f)(faux_async_t *async,
+	size_t len, void *user_data);
+
+
+C_DECL_BEGIN
+
+faux_async_t *faux_async_new(int fd);
+void faux_async_free(faux_async_t *async);
+int faux_async_fd(const faux_async_t *async);
+
+C_DECL_END
+
+#endif // _faux_async_h

+ 3 - 0
faux/async/Makefile.am

@@ -0,0 +1,3 @@
+libfaux_la_SOURCES += \
+	faux/async/async.c \
+	faux/async/private.h

+ 86 - 0
faux/async/async.c

@@ -0,0 +1,86 @@
+/** @file async.c
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "faux/faux.h"
+#include "faux/str.h"
+#include "faux/net.h"
+#include "faux/async.h"
+
+#include "private.h"
+
+
+/** @brief Create new async I/O object.
+ *
+ * Constructor gets associated file descriptor to operate on it. File
+ * descriptor must be nonblocked. If not so then constructor will set
+ * nonblock flag itself.
+ *
+ * @param [in] fd File descriptor.
+ * @return Allocated object or NULL on error.
+ */
+faux_async_t *faux_async_new(int fd)
+{
+	faux_async_t *async = NULL;
+	int fflags = 0;
+
+	// Prepare FD
+	if (fd < 0) // Illegal fd
+		return NULL;
+	if ((fflags = fcntl(fd, F_GETFL)) == -1)
+		return NULL;
+	if (fcntl(fd, F_SETFL, fflags | O_NONBLOCK) == -1)
+		return NULL;
+
+	async = faux_zmalloc(sizeof(*async));
+	assert(async);
+	if (!async)
+		return NULL;
+
+	// Init
+	async->fd = fd;
+
+	return async;
+}
+
+
+/** @brief Free async I/O object.
+ *
+ * @param [in] Async I/O object.
+ */
+void faux_async_free(faux_async_t *async)
+{
+	if (!async)
+		return;
+
+	faux_free(async);
+}
+
+
+/** @brief Get file descriptor from async I/O object.
+ *
+ * @param [in] async Allocated and initialized async I/O object.
+ * @return Serviced file descriptor.
+ */
+int faux_async_fd(const faux_async_t *async)
+{
+	assert(async);
+	if (!async)
+		return -1;
+
+	return async->fd;
+}

+ 16 - 0
faux/async/private.h

@@ -0,0 +1,16 @@
+#include "faux/faux.h"
+#include "faux/list.h"
+#include "faux/net.h"
+
+
+struct faux_async_s {
+	int fd;
+	// Read
+	faux_async_read_cb_f read_cb; // Read callback
+	void *read_udata;
+	ssize_t min;
+	ssize_t max;
+	// Write
+	faux_async_stall_cb_f stall_cb; // Stall callback
+	void *stall_udata;
+};