Bläddra i källkod

faux.net: faux_net_set_fd()

Serj Kalichev 3 år sedan
förälder
incheckning
ecc831138e
3 ändrade filer med 26 tillägg och 4 borttagningar
  1. 3 1
      faux/net.h
  2. 17 3
      faux/net/net.c
  3. 6 0
      faux/net/net_io.c

+ 3 - 1
faux/net.h

@@ -35,8 +35,10 @@ ssize_t faux_recvv_block(int fd, struct iovec *iov, int iovcnt,
 	int (*isbreak_func)(void));
 
 // Network class
-faux_net_t *faux_net_new_by_fd(int fd);
+faux_net_t *faux_net_new(void);
 void faux_net_free(faux_net_t *faux_net);
+void faux_net_set_fd(faux_net_t *faux_net, int fd);
+void faux_net_reset_fd(faux_net_t *faux_net);
 void faux_net_set_send_timeout(faux_net_t *faux_net, struct timespec *send_timeout);
 void faux_net_set_recv_timeout(faux_net_t *faux_net, struct timespec *recv_timeout);
 void faux_net_set_timeout(faux_net_t *faux_net, struct timespec *timeout);

+ 17 - 3
faux/net/net.c

@@ -39,7 +39,7 @@ static faux_net_t *faux_net_allocate(void)
 }
 
 
-faux_net_t *faux_net_new_by_fd(int fd)
+faux_net_t *faux_net_new(void)
 {
 	faux_net_t *faux_net = NULL;
 
@@ -48,8 +48,6 @@ faux_net_t *faux_net_new_by_fd(int fd)
 	if (!faux_net)
 		return NULL;
 
-	faux_net->fd = fd;
-
 	return faux_net;
 }
 
@@ -62,6 +60,22 @@ void faux_net_free(faux_net_t *faux_net)
 }
 
 
+void faux_net_set_fd(faux_net_t *faux_net, int fd)
+{
+	if (!faux_net)
+		return;
+	faux_net->fd = fd;
+}
+
+
+void faux_net_reset_fd(faux_net_t *faux_net)
+{
+	if (!faux_net)
+		return;
+	faux_net->fd = -1;
+}
+
+
 void faux_net_set_send_timeout(faux_net_t *faux_net, struct timespec *send_timeout)
 {
 	assert(faux_net);

+ 6 - 0
faux/net/net_io.c

@@ -175,6 +175,9 @@ ssize_t faux_sendv(int fd, const struct iovec *iov, int iovcnt,
 	struct timespec now = {};
 	struct timespec deadline = {};
 
+	assert(fd != -1);
+	if (fd == -1)
+		return -1;
 	if (!iov)
 		return -1;
 	if (iovcnt == 0)
@@ -372,6 +375,9 @@ ssize_t faux_recvv(int fd, struct iovec *iov, int iovcnt,
 	struct timespec now = {};
 	struct timespec deadline = {};
 
+	assert(fd != -1);
+	if (fd == -1)
+		return -1;
 	if (!iov)
 		return -1;
 	if (iovcnt == 0)