From 7856bf3569cc448fe3b360c3fca836593a0c0c7d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 06 Nov 2023 17:01:27 +0800
Subject: [PATCH] format c source code

---
 prj1_tlv/lylib/socket.h |   34 +++++++++++++++++++++++++++++++---
 1 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/prj1_tlv/lylib/socket.h b/prj1_tlv/lylib/socket.h
index 1b5a043..4e8bd61 100644
--- a/prj1_tlv/lylib/socket.h
+++ b/prj1_tlv/lylib/socket.h
@@ -32,6 +32,11 @@
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 
+#include <sys/epoll.h>
+#include <sys/resource.h>
+
+#include "list.h"
+
 #define HOSTNAME_LEN         128
 
 enum
@@ -56,7 +61,7 @@
 typedef struct socket_s
 {
     int            fd;          /* socket fd  */ 
-    uint8_t        type;        /* socket type: listen, accept or accept */
+    uint8_t        type;        /* socket type: listen, accept or connect */
     uint8_t        status;      /* socket status: SOCK_STAT_INIT,SOCK_STAT_CONNECTED... */
     
     /* socket server/client IP address and port */
@@ -69,7 +74,9 @@
     int            keepintvl;   /* keepalive detect interval */ 
     int            keepcnt;     /* keepalive count */ 
 
-    struct sockaddr  saddr;              /*  sockaddr for connect */ 
+    struct sockaddr  saddr;     /* sockaddr for connect */ 
+
+    struct list_head list;      /* socket server manage client link list */
 } socket_t;
 
 
@@ -88,7 +95,7 @@
     SOCK_NOT_CREATE,
     SOCK_CREATE,
 };
-int socket_ctx_init(socket_t *sock, uint8_t type, int create);
+int socket_init(socket_t *sock, uint8_t type, int create);
 
 
 /*  description: close socket and set socket status as SOCK_STAT_INIT 
@@ -148,6 +155,25 @@
 int socket_recv(socket_t *sock, char *buf, int size, int timeout);
 
 
+/*  description: create epoll for socket server and add listenfd into it
+ *   input args: $max_evts:  max events for epoll_create()
+ *               $listenfd:  listen socket fd
+ * return value: <0: failure  >=0: epollfd
+ */ 
+int epoll_init(int max_evts, int listenfd);
+
+/*  description: add new fd into epoll to monitor
+ *   input args: $epollfd:  epoll fd
+ *               $fd:       socket fd need added into epoll
+ * return value: <0: failure  0: successfully
+ */ 
+inline int epoll_add(int epollfd, int fd);
+
+static inline int epoll_del(int epollfd, int fd)
+{
+    return epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
+}
+
 /*+-------------------------------------------------------------------+
  *|                socket utils function                              |
  *+-------------------------------------------------------------------+*/
@@ -171,6 +197,8 @@
 /* set heartbeat keepalive  */
 int socket_set_keepalive(int sockfd, int keepintvl, int keepcnt);
 
+/*  Set open file description count to max */
+void set_socket_rlimit(void);
 
 #endif   /* ----- #ifndef _SOCKET_H_  ----- */
 

--
Gitblit v1.9.1