|  |  |  | 
|---|
|  |  |  | #include <netinet/tcp.h> | 
|---|
|  |  |  | #include <arpa/inet.h> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #include <sys/epoll.h> | 
|---|
|  |  |  | #include <sys/resource.h> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #include "list.h" | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #define HOSTNAME_LEN         128 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | enum | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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                              | | 
|---|
|  |  |  | *+-------------------------------------------------------------------+*/ | 
|---|
|  |  |  | 
|---|
|  |  |  | /* 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_  ----- */ | 
|---|
|  |  |  |  | 
|---|