Browse Source

faux.msg: faux_msg_send_async()

Serj Kalichev 2 years ago
parent
commit
f65040fc37
3 changed files with 50 additions and 0 deletions
  1. 1 0
      faux/faux.map
  2. 2 0
      faux/msg.h
  3. 47 0
      faux/msg/msg.c

+ 1 - 0
faux/faux.map

@@ -194,6 +194,7 @@ FAUX_2.0 {
 		faux_msg_get_param_by_index;
 		faux_msg_get_param_by_type;
 		faux_msg_send;
+		faux_msg_send_async;
 		faux_msg_recv;
 		faux_msg_iov;
 		faux_msg_serialize;

+ 2 - 0
faux/msg.h

@@ -38,6 +38,7 @@
 #include <faux/faux.h>
 #include <faux/list.h>
 #include <faux/net.h>
+#include <faux/async.h>
 
 typedef struct faux_msg_s faux_msg_t;
 
@@ -119,6 +120,7 @@ faux_phdr_t *faux_msg_get_param_by_index(const faux_msg_t *msg, unsigned int ind
 faux_phdr_t *faux_msg_get_param_by_type(const faux_msg_t *msg,
 	uint16_t param_type, void **param_data, uint32_t *param_len);
 ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net);
+ssize_t faux_msg_send_async(const faux_msg_t *msg, faux_async_t *async);
 faux_msg_t *faux_msg_recv(faux_net_t *faux_net);
 bool_t faux_msg_iov(const faux_msg_t *msg, struct iovec **iov_out, size_t *iov_num_out);
 bool_t faux_msg_serialize(const faux_msg_t *msg, char **buf, size_t *len);

+ 47 - 0
faux/msg/msg.c

@@ -30,6 +30,7 @@
 #include <faux/str.h>
 #include <faux/list.h>
 #include <faux/net.h>
+#include <faux/async.h>
 #include <faux/msg.h>
 
 // Global variable to switch debug on/off (true/false)
@@ -660,6 +661,13 @@ ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net)
 	struct iovec *iov = NULL;
 	size_t ret = 0;
 
+	assert(msg);
+	if (!msg)
+		return -1;
+	assert(faux_net);
+	if (!faux_net)
+		return -1;
+
 	if (!faux_msg_iov(msg, &iov, &vec_entries_num))
 		return -1;
 
@@ -678,6 +686,45 @@ ssize_t faux_msg_send(const faux_msg_t *msg, faux_net_t *faux_net)
 }
 
 
+/** @brief Sends message to network in async mode.
+ *
+ * Function sends message to network using preinitialized faux_async_t object.
+ *
+ * @param [in] msg Allocated faux_msg_t object.
+ * @param [in] async Preinitialized faux_async_t object.
+ * @return Length of sent data or < 0 on error.
+ */
+ssize_t faux_msg_send_async(const faux_msg_t *msg, faux_async_t *async)
+{
+	size_t vec_entries_num = 0;
+	struct iovec *iov = NULL;
+	size_t ret = 0;
+
+	assert(msg);
+	if (!msg)
+		return -1;
+	assert(async);
+	if (!async)
+		return -1;
+
+	if (!faux_msg_iov(msg, &iov, &vec_entries_num))
+		return -1;
+
+	ret = faux_async_writev(async, iov, vec_entries_num);
+	faux_free(iov);
+
+#ifdef DEBUG
+	// Debug
+	if (msg && ret > 0 && faux_msg_debug_flag) {
+		printf("(o) ");
+		faux_msg_debug(msg);
+	}
+#endif
+
+	return ret;
+}
+
+
 /** @brief Serializes message.
  *
  * @param [in] msg Allocated faux_msg_t object.