浏览代码

faux.net: Add events parameter to faux_pollfd_add() function

Serj Kalichev 3 年之前
父节点
当前提交
2d12df2345
共有 2 个文件被更改,包括 13 次插入11 次删除
  1. 1 1
      faux/net.h
  2. 12 10
      faux/net/pollfd.c

+ 1 - 1
faux/net.h

@@ -62,7 +62,7 @@ struct pollfd *faux_pollfd_vector(faux_pollfd_t *faux_pollfd);
 size_t faux_pollfd_len(faux_pollfd_t *faux_pollfd);
 struct pollfd *faux_pollfd_item(faux_pollfd_t *faux_pollfd, unsigned int index);
 struct pollfd *faux_pollfd_find(faux_pollfd_t *faux_pollfd, int fd);
-struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd);
+struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd, short events);
 int faux_pollfd_del_by_fd(faux_pollfd_t *faux_pollfd, int fd);
 int faux_pollfd_del_by_index(faux_pollfd_t *faux_pollfd, unsigned int index);
 void faux_pollfd_init_iterator(faux_pollfd_t *faux_pollfd, faux_pollfd_iterator_t *iterator);

+ 12 - 10
faux/net/pollfd.c

@@ -145,9 +145,10 @@ struct pollfd *faux_pollfd_find(faux_pollfd_t *faux_pollfd, int fd)
  *
  * @param [in] faux_pollfd Allocated faux_pollfd_t object.
  * @param [in] fd File descriptor to set to newly created item.
+ * @param [in] events The events interested in.
  * @return Pointer to new item or NULL on error.
  */
-struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd)
+struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd, short events)
 {
 	struct pollfd *pollfd = NULL;
 
@@ -158,17 +159,18 @@ struct pollfd *faux_pollfd_add(faux_pollfd_t *faux_pollfd, int fd)
 	if (fd < 0)
 		return NULL;
 
-	// Don't add duplicate fd
+	// Don't add duplicated fd
 	pollfd = faux_pollfd_find(faux_pollfd, fd);
-	if (pollfd)
-		return pollfd;
+	if (!pollfd) {
+		// Create new item
+		pollfd = faux_vec_add(faux_pollfd->vec);
+		assert(pollfd);
+		if (!pollfd)
+			return NULL;
+		pollfd->fd = fd;
+	}
 
-	// Create new item
-	pollfd = faux_vec_add(faux_pollfd->vec);
-	assert(pollfd);
-	if (!pollfd)
-		return NULL;
-	pollfd->fd = fd;
+	pollfd->events = events;
 
 	return pollfd;
 }