Browse Source

faux.async: faux_async_writev()

Serj Kalichev 2 years ago
parent
commit
769d70e35c
3 changed files with 51 additions and 0 deletions
  1. 2 0
      faux/async.h
  2. 48 0
      faux/async/async.c
  3. 1 0
      faux/faux.map

+ 2 - 0
faux/async.h

@@ -39,6 +39,8 @@ void faux_async_set_stall_cb(faux_async_t *async,
 void faux_async_set_write_overflow(faux_async_t *async, size_t overflow);
 void faux_async_set_read_overflow(faux_async_t *async, size_t overflow);
 ssize_t faux_async_write(faux_async_t *async, void *data, size_t len);
+ssize_t faux_async_writev(faux_async_t *async,
+	const struct iovec *iov, int iovcnt);
 ssize_t faux_async_out(faux_async_t *async);
 ssize_t faux_async_in(faux_async_t *async);
 

+ 48 - 0
faux/async/async.c

@@ -266,6 +266,54 @@ ssize_t faux_async_write(faux_async_t *async, void *data, size_t len)
 }
 
 
+/** @brief Async "struct iovec" write.
+ *
+ * This function is like a faux_async_write() function but uses scatter/gather.
+ *
+ * @see faux_async_write().
+ * @param [in] async Allocated and initialized async I/O object.
+ * @param [in] iov Array of "struct iovec" structures.
+ * @param [in] iovcnt Number of iov array members.
+ * @return Length of stored/writed data or < 0 on error.
+ */
+ssize_t faux_async_writev(faux_async_t *async,
+	const struct iovec *iov, int iovcnt)
+{
+	size_t total_written = 0;
+	int i = 0;
+
+	assert(async);
+	if (!async)
+		return -1;
+	if (!iov)
+		return -1;
+	if (iovcnt == 0)
+		return 0;
+
+	for (i = 0; i < iovcnt; i++) {
+		ssize_t bytes_written = 0;
+		if (iov[i].iov_len == 0)
+			continue;
+		bytes_written = faux_buf_write(async->obuf,
+			iov[i].iov_base, iov[i].iov_len);
+		if (bytes_written < 0) { // Error
+			if (total_written != 0)
+				break;
+			return -1;
+		}
+		if (0 == bytes_written) // Insufficient space
+			break;
+		total_written += bytes_written;
+	}
+
+	// Try to real write data to fd in nonblocked mode
+	if (total_written > 0)
+		faux_async_out(async);
+
+	return total_written;
+}
+
+
 /** @brief Write output buffer to fd in non-blocking mode.
  *
  * Previously data must be written to internal buffer by faux_async_write()

+ 1 - 0
faux/faux.map

@@ -22,6 +22,7 @@ FAUX_2.0 {
 		faux_async_set_write_overflow;
 		faux_async_set_read_overflow;
 		faux_async_write;
+		faux_async_writev;
 		faux_async_out;
 		faux_async_in;