From b0053f87034ff358b7ccadc7f2d9643e9a7767e7 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Thu, 30 Apr 2020 16:15:09 +0800 Subject: [PATCH] update tlv project for server --- prj1_tlv/lylib/socket.h | 32 ++++++++++++++++++++++++++++++-- 1 files changed, 30 insertions(+), 2 deletions(-) diff --git a/prj1_tlv/lylib/socket.h b/prj1_tlv/lylib/socket.h index 1b5a043..b472ae6 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 @@ -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