APUE Learning Example Source Code
guowenxue
2020-04-30 9d2a15e034348a38a569d4ff91047363f608ee79
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_  ----- */