APUE Learning Example Source Code
guowenxue
2020-06-06 cd10eef059e9d9c34c927f3ac53ac43f4a9716c1
prj1_tlv/lylib/socket.c
@@ -22,7 +22,7 @@
 *               $create: call socket() create or not
 * return value: <0: failure  0: successfully
 */                           
int socket_ctx_init(socket_t *sock, uint8_t type, int create)
int socket_init(socket_t *sock, uint8_t type, int create)
{
    int        fd = -1;
@@ -116,7 +116,6 @@
int socket_listen(socket_t *sock, char *ipaddr, int port)
{
    int                 rv = 0; 
    int                 fd = -1;
    struct sockaddr_in  addr;
    int                 backlog = 13;
@@ -126,7 +125,7 @@
        return -1;
    }
    if( sock->status != SOCK_STAT_INIT )
    if( sock->status != SOCK_STAT_UNINIT )
    {
        socket_close(sock);
    }
@@ -149,7 +148,7 @@
    }
    /* initial socket context and create socket fd  */
    if( socket_ctx_init(sock, SOCK_TYPE_LISTEN, SOCK_CREATE) < 0)
    if( socket_init(sock, SOCK_TYPE_LISTEN, SOCK_CREATE) < 0)
    {
        log_err("socket context initial failure\n");
        return -2;
@@ -179,8 +178,6 @@
        goto cleanup;
    }
    log_nrml("create socket and listen on [%s:%d] already\n", sock->laddr, sock->lport);
cleanup: 
    if( rv ) 
    { 
@@ -190,7 +187,7 @@
    else 
    { 
        sock->status = SOCK_STAT_LISTENED; 
        log_nrml("Create socket[%p:%d] listen [%s:%d] ok\n", sock, sock->fd, sock->laddr, sock->lport);
        log_nrml("Create socket[%d] listen [%s:%d] ok\n", sock->fd, sock->laddr, sock->lport);
    }
    return rv; 
@@ -236,7 +233,7 @@
    /* socket not initial before */
    if( SOCK_STAT_UNINIT== sock->status ) 
    {
        if( socket_ctx_init(sock, SOCK_TYPE_LISTEN, SOCK_NOT_CREATE) < 0)
        if( socket_init(sock, SOCK_TYPE_LISTEN, SOCK_NOT_CREATE) < 0)
        {
            log_err("ERROR: initial socket context failure\n");
            return -2;
@@ -559,6 +556,72 @@
}
/*  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)
{
    int                       epollfd;
    struct epoll_event        event;
    if( max_evts<=0 || listenfd<0 )
    {
        log_err("Invalid input arguments\n");
        return -1;
    }
    if( (epollfd=epoll_create(max_evts)) < 0 )
    {
        log_err("epoll_create() failure: %s\n", strerror(errno));
        return -2;
    }
    if( epoll_add(epollfd, listenfd) < 0 )
    {
        log_err("epoll add listen socket[%d] failure: %s\n", listenfd, strerror(errno));
        close(epollfd);
        return -2;
    }
    log_nrml("epoll add listen socket[%d] ok\n", listenfd);
    return epollfd;
}
/*  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)
{
    struct epoll_event        event;
    if( epollfd<0 || fd<0 )
    {
        log_err("Invalid input arguments\n");
        return -1;
    }
    //event.events = EPOLLIN | EPOLLET;  /* Edge Triggered  */
    event.events = EPOLLIN; /* default Level Triggered */
    event.data.fd = fd;
    if( epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event) < 0)
    {
        log_err("epoll add fd[%d] failure: %s\n", fd, strerror(errno));
        return -2;
    }
    return 0;
}
/*+-------------------------------------------------------------------+
 *|                socket utils function                              |
 *+-------------------------------------------------------------------+*/
/*  description: parser hostname and port from $host and set it into $sock 
 *   input args: $sock: socket context
@@ -787,3 +850,16 @@
}
/*  Set open file description count to max */
void set_socket_rlimit(void)
{
    struct rlimit limit = {0};
    getrlimit(RLIMIT_NOFILE, &limit );
    limit.rlim_cur  = limit.rlim_max;
    setrlimit(RLIMIT_NOFILE, &limit );
    log_nrml("set socket open fd max count to %d\n", limit.rlim_max);
}