把libevent 2.1.8源码的最小堆提取出来,自己封装成定时器使用(3)(★firecat推荐★)

Libevent中的timeout事件是使用最小堆来管理维护的.代码位于<minheap-internal.h>.

源码来源:https://github.com/libevent/libevent/blob/release-2.1.8-stable/minheap-internal.h


本篇在第2篇的基础之上进行优化升级,加上类似muduo活塞式的buffer。

本篇实现Linux网络库epoll+时间堆+buffer实现高性能服务器。

完整的工程下载:https://download.csdn.net/download/libaineu2004/10468714


1、CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(min_heap_libevent_epoll_buf)

AUX_SOURCE_DIRECTORY(. SRC_LIST)

ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST})

2、buffer.h

#ifndef MYREDISNET_AEBUFFER_H
#define MYREDISNET_AEBUFFER_H

/// 《Linux多线程服务端编程:使用muduo C++网络库》陈硕著 7.4章节,P204
/// https://github.com/chenshuo/muduo
/// muduo buf:A buffer class modeled after org.jboss.netty.buffer.ChannelBuffer
///
/// @code
/// +-------------------+------------------+------------------+
/// | prependable bytes |  readable bytes  |  writable bytes  |
/// |                   |     (CONTENT)    |                  |
/// +-------------------+------------------+------------------+
/// |                   |                  |                  |
/// 0      <=      readerIndex   <=   writerIndex    <=     size
///
/// @endcode

//#include "zmalloc.h"//不能直接包含reids这个头文件,编译会报错 basic_string.h:2423:7: error: ‘__str’ was not declared in this scope
//#include "myjemalloc.h"//要用这个
#include <sys/types.h>

//no use jemalloc,only libc
#define zmalloc malloc
#define zfree(p) if (p) { free(p); }
#define zrealloc realloc

#define DEFAULT_BUFF_SIZE        1024

typedef struct {
    unsigned char *buff;
    size_t size;
    size_t read_idx;
    size_t write_idx;
} buffer_t;

buffer_t *alloc_buffer();
void free_buffer(buffer_t *buffer);
void check_buffer_size(buffer_t *buffer, size_t avlid_size);
size_t get_readable_size(buffer_t *buffer);
size_t get_writeable_size(buffer_t *buffer);

#endif //MYREDISNET_AEBUFFER_H

3、buffer.c

#include "buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

buffer_t *alloc_buffer()
{
    buffer_t *buffer = zmalloc(sizeof(buffer_t));
    if (buffer == NULL) {
        goto err;
    }
    buffer->buff = zmalloc(DEFAULT_BUFF_SIZE);
    buffer->size = DEFAULT_BUFF_SIZE;
    buffer->read_idx = 0;
    buffer->write_idx = 0;

    return buffer;

err:
    if (buffer) {
        zfree(buffer->buff);
        buffer->buff = NULL;
        zfree(buffer);
        buffer = NULL;
    }

    return NULL;
}

void free_buffer(buffer_t *buffer)
{
    if (buffer) {
        zfree(buffer->buff);
        buffer->buff = NULL;
        zfree(buffer);
        buffer = NULL;
    }
}

void check_buffer_size(buffer_t *buffer, size_t avlid_size)
{
    if (buffer->read_idx > DEFAULT_BUFF_SIZE) {
        size_t data_len = get_readable_size(buffer);
        memmove(buffer->buff, buffer->buff + buffer->read_idx, data_len);
        buffer->read_idx = 0;
        buffer->write_idx = data_len;
    }

    if (get_writeable_size(buffer) < avlid_size) {
        size_t new_size = buffer->size + avlid_size;
        buffer->buff = zrealloc(buffer->buff, new_size);
        buffer->size = new_size;
    }
}

size_t get_readable_size(buffer_t *buffer)
{
    assert(buffer->size >= buffer->write_idx);
    assert(buffer->read_idx <= buffer->write_idx);
    return buffer->write_idx - buffer->read_idx;
}

size_t get_writeable_size(buffer_t *buffer)
{
    assert(buffer->size >= buffer->write_idx);
    assert(buffer->read_idx <= buffer->write_idx);
    return buffer->size - buffer->write_idx;
}

4、client.h

#ifndef CLIENT_H
#define CLIENT_H

#include <stdio.h>
#include <stdint.h> //eg. uint64_t
#include "buffer.h"

typedef struct {
    int fd;
    int epollfd;
    int timerId;
    uint64_t last_recv_tick;
    buffer_t *read_buffer;
    buffer_t *write_buffer;
} client_t;

typedef struct fileEvent {
    client_t *clientData;
} fileEvent;

extern fileEvent *fileev;
extern client_t *alloc_client();
extern uint64_t get_tick_count();
extern void free_client(client_t *client);
extern void create_fileEvent(int setsize);
extern void destroy_fileEvent(int setsize);

#endif // CLIENT_H

5、client.c

#include "client.h"

fileEvent *fileev = NULL;

uint64_t get_tick_count() //come from /teamtalk/util.cpp
{
#ifdef _WIN32
    LARGE_INTEGER liCounter;
    LARGE_INTEGER liCurrent;

    if (!QueryPerformanceFrequency(&liCounter))
        return GetTickCount();

    QueryPerformanceCounter(&liCurrent);
    return (uint64_t)(liCurrent.QuadPart * 1000 / liCounter.QuadPart);
#else
    struct timeval tval;
    uint64_t ret_tick;
    gettimeofday(&tval, NULL);
    ret_tick = tval.tv_sec * 1000L + tval.tv_usec / 1000L;
    return ret_tick;
#endif
}

client_t *alloc_client()
{
    client_t * client = zmalloc(sizeof(client_t));
    if (client == NULL) {
        goto err;
    }

    client->fd = -1;
    client->timerId = -1;
    client->last_recv_tick = get_tick_count();
    client->read_buffer = alloc_buffer();
    client->write_buffer = alloc_buffer();
    if (client->read_buffer == NULL || client->write_buffer == NULL) {
        goto err;
    }

    return client;

err:
    if (client) {
        free_client(client);
    }

    return NULL;
}

void free_client(client_t *client)
{
    if (client) {
        if (client->fd > 0) {
            close(client->fd);
        }

        free_buffer(client->read_buffer);
        free_buffer(client->write_buffer);
        zfree(client);
    }
}

void create_fileEvent(int setsize)
{
     fileev = zmalloc(sizeof(fileEvent) * setsize);
}

void destroy_fileEvent(int setsize)
{
    int i = 0;
    for (i = 0; i < setsize; i++)
    {
        if (fileev->clientData != NULL)
        {
            free_client(fileev->clientData);
        }
    }

    zfree(fileev);
}


6、minheap-event-firecat.h

#ifndef MINHEAPEVENTFIRECAT_H
#define MINHEAPEVENTFIRECAT_H

#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

//come from https://github.com/libevent/libevent/blob/release-2.1.8-stable/mm-internal.h
#define mm_malloc(sz) malloc(sz)
#define mm_calloc(n, sz) calloc((n), (sz))
#define mm_strdup(s) strdup(s)
#define mm_realloc(p, sz) realloc((p), (sz))
#define mm_free(p) free(p)

//come from https://github.com/libevent/libevent/blob/release-2.1.8-stable/include/event2/util.h
#define evutil_timercmp(tvp, uvp, cmp)                          \
    (((tvp)->tv_sec == (uvp)->tv_sec) ?                           \
    ((tvp)->tv_usec cmp (uvp)->tv_usec) :                     \
    ((tvp)->tv_sec cmp (uvp)->tv_sec))

#define evutil_timersub(tvp, uvp, vvp)                      \
    do {                                                    \
    (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;     \
    (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;  \
    if ((vvp)->tv_usec < 0) {                         \
    (vvp)->tv_sec--;                             \
    (vvp)->tv_usec += 1000000;                       \
    }                                                   \
    } while (0)

#define evutil_timeradd(tvp, uvp, vvp)                          \
    do {                                                        \
    (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;         \
    (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
    if ((vvp)->tv_usec >= 1000000) {                      \
    (vvp)->tv_sec++;                                 \
    (vvp)->tv_usec -= 1000000;                           \
    }                                                       \
    } while (0)

//come from https://github.com/libevent/libevent/blob/release-2.1.8-stable/include/event2/event_struct.h
struct event
{
    /* for managing timeouts */
    union {
        //TAILQ_ENTRY(event) ev_next_with_common_timeout;
        int min_heap_idx;
    } ev_timeout_pos;

    unsigned int timer_id;
    struct timeval ev_interval;
    struct timeval ev_timeout;
    int ev_exe_num;

    int (*ev_callback)(void *arg);
    int ev_arg;

    int ev_res; /* result passed to event callback */
    int ev_flags;
};

//static inline void gettime(struct timeval *tm);
static void gettime(struct timeval *tm)
{
    gettimeofday(tm, NULL);
}

//come from redis src "ae.c"
static void aeGetTime(long *seconds, long *milliseconds)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);
    *seconds = tv.tv_sec;
    *milliseconds = tv.tv_usec/1000;
}

#endif // MINHEAPEVENTFIRECAT_H

7、minheap-internal.h

/*
 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
 *
 * Copyright (c) 2006 Maxim Yegorushkin <maxim.yegorushkin@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef MINHEAP_INTERNAL_H_INCLUDED_
#define MINHEAP_INTERNAL_H_INCLUDED_

//come from https://github.com/libevent/libevent/blob/release-2.1.8-stable/minheap-internal.h

/*firecat del
#include "event2/event-config.h"
#include "evconfig-private.h"
#include "event2/event.h"
#include "event2/event_struct.h"
#include "event2/util.h"
#include "util-internal.h"
#include "mm-internal.h"
*/

#include "minheap-event-firecat.h" //firecat add

typedef struct min_heap
{
    struct event** p;
    unsigned n, a;
} min_heap_t;

static inline void	     min_heap_ctor_(min_heap_t* s);
static inline void	     min_heap_dtor_(min_heap_t* s);
static inline void	     min_heap_elem_init_(struct event* e);
static inline int	     min_heap_elt_is_top_(const struct event *e);
static inline int	     min_heap_empty_(min_heap_t* s);
static inline unsigned	     min_heap_size_(min_heap_t* s);
static inline struct event*  min_heap_top_(min_heap_t* s);
static inline int	     min_heap_reserve_(min_heap_t* s, unsigned n);
static inline int	     min_heap_push_(min_heap_t* s, struct event* e);
static inline struct event*  min_heap_pop_(min_heap_t* s);
static inline int	     min_heap_adjust_(min_heap_t *s, struct event* e);
static inline int	     min_heap_erase_(min_heap_t* s, struct event* e);
static inline void	     min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
static inline void	     min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e);
static inline void	     min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e);

#define min_heap_elem_greater(a, b) \
    (evutil_timercmp(&(a)->ev_timeout, &(b)->ev_timeout, >))

void min_heap_ctor_(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
void min_heap_dtor_(min_heap_t* s) { if (s->p) mm_free(s->p); }
void min_heap_elem_init_(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; }
int min_heap_empty_(min_heap_t* s) { return 0u == s->n; }
unsigned min_heap_size_(min_heap_t* s) { return s->n; }
struct event* min_heap_top_(min_heap_t* s) { return s->n ? *s->p : 0; }

int min_heap_push_(min_heap_t* s, struct event* e)
{
    if (min_heap_reserve_(s, s->n + 1))
        return -1;
    min_heap_shift_up_(s, s->n++, e);
    return 0;
}

struct event* min_heap_pop_(min_heap_t* s)
{
    if (s->n)
    {
        struct event* e = *s->p;
        min_heap_shift_down_(s, 0u, s->p[--s->n]);
        e->ev_timeout_pos.min_heap_idx = -1;
        return e;
    }
    return 0;
}

int min_heap_elt_is_top_(const struct event *e)
{
    return e->ev_timeout_pos.min_heap_idx == 0;
}

int min_heap_erase_(min_heap_t* s, struct event* e)
{
    if (-1 != e->ev_timeout_pos.min_heap_idx)
    {
        struct event *last = s->p[--s->n];
        unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
        /* we replace e with the last element in the heap.  We might need to
           shift it upward if it is less than its parent, or downward if it is
           greater than one or both its children. Since the children are known
           to be less than the parent, it can't need to shift both up and
           down. */
        if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
            min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, last);
        else
            min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
        e->ev_timeout_pos.min_heap_idx = -1;
        return 0;
    }
    return -1;
}

int min_heap_adjust_(min_heap_t *s, struct event *e)
{
    if (-1 == e->ev_timeout_pos.min_heap_idx) {
        return min_heap_push_(s, e);
    } else {
        unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
        /* The position of e has changed; we shift it up or down
         * as needed.  We can't need to do both. */
        if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], e))
            min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, e);
        else
            min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, e);
        return 0;
    }
}

int min_heap_reserve_(min_heap_t* s, unsigned n)
{
    if (s->a < n)
    {
        struct event** p;
        unsigned a = s->a ? s->a * 2 : 8;
        if (a < n)
            a = n;
        if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p)))
            return -1;
        s->p = p;
        s->a = a;
    }
    return 0;
}

void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e)
{
    unsigned parent = (hole_index - 1) / 2;
    do
    {
        (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
        hole_index = parent;
        parent = (hole_index - 1) / 2;
    } while (hole_index && min_heap_elem_greater(s->p[parent], e));
    (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}

void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
{
    unsigned parent = (hole_index - 1) / 2;
    while (hole_index && min_heap_elem_greater(s->p[parent], e))
    {
        (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
        hole_index = parent;
        parent = (hole_index - 1) / 2;
    }
    (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}

void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
{
    unsigned min_child = 2 * (hole_index + 1);
    while (min_child <= s->n)
    {
        min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
        if (!(min_heap_elem_greater(e, s->p[min_child])))
            break;
        (s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
        hole_index = min_child;
        min_child = 2 * (hole_index + 1);
    }
    (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
}

#endif /* MINHEAP_INTERNAL_H_INCLUDED_ */

8、mytimer.h

#ifndef MYTIMER_H
#define MYTIMER_H

#include "buffer.h"
#include "minheap-internal.h"

#define LIMIT_TIMER 1 //有限次数定时器
#define CYCLE_TIMER 2 //循环定时器

//typedef enum {false, true} bool;
extern struct min_heap _min_heap;
extern void timer_init();
extern void timer_destroy();
extern struct event* timer_top();
extern unsigned int timer_add(unsigned int timer_id, int interval, int (*fun)(void*), int arg, int flag, int exe_num);//interval:ms
extern int timer_remove(unsigned int timer_id);
extern int timer_process();

#endif // MYTIMER_H



9、mytimer.c

#include "mytimer.h"

struct min_heap _min_heap;

void timer_init()
{
    min_heap_ctor_(&_min_heap);
}

void timer_destroy()
{
    int i = 0;
    for (i = 0; i < _min_heap.n; i++)
    {
        zfree(_min_heap.p[i]);
    }
    min_heap_dtor_(&_min_heap);
}

struct event* timer_top()
{
    return min_heap_top_(&_min_heap);
}

unsigned int timer_add(unsigned int timer_id, int interval, int(*fun)(void*), int arg,
                              int flag /* = CYCLE_TIMER */, int exe_num /* =  0 */)//interval:ms
{
    struct event * ev = (struct event*) zmalloc(sizeof(struct event));
    min_heap_elem_init_(ev);
    if (NULL == ev)
        return NULL;
    struct timeval now;
    gettime(&now);
    ev->ev_interval.tv_sec = interval / 1000;
    ev->ev_interval.tv_usec = (interval % 1000) * 1000;
    evutil_timeradd(&now, &(ev->ev_interval), &(ev->ev_timeout));
    ev->ev_flags = flag;
    ev->ev_callback = fun;
    ev->ev_arg = arg;
    ev->ev_exe_num = exe_num;
    ev->timer_id = timer_id;

    min_heap_push_(&_min_heap, ev);

    return ev->timer_id;
}

int timer_remove(unsigned int timer_id)
{
    int i = 0;
    for (i = 0; i < _min_heap.n; i++)
    {
        if (timer_id == _min_heap.p[i]->timer_id)
        {
            struct event * event = _min_heap.p[i];
            min_heap_erase_(&_min_heap, _min_heap.p[i]);
            zfree(event);
            return 1;
        }
    }
    return 0;
}

int timer_process()
{
    struct event *event;
    struct timeval now;
    int ret = 0;
    int processed = 0;

    while ((event = min_heap_top_(&_min_heap)) != NULL)
    {
        gettime(&now);
        if (evutil_timercmp(&now, &(event->ev_timeout), < ))
            break;

        processed++;
        min_heap_pop_(&_min_heap);
        ret = event->ev_callback(event->ev_arg);
        if (ret == 0)//kill timer
        {
            event->ev_flags = LIMIT_TIMER;
            event->ev_exe_num = 0;
        }

        if (event->ev_flags == CYCLE_TIMER
                || (event->ev_flags == LIMIT_TIMER && --event->ev_exe_num > 0))
        {
            evutil_timeradd(&(event->ev_timeout), &(event->ev_interval), &(event->ev_timeout));
            min_heap_push_(&_min_heap, event);
        }
        else
        {
            zfree(event);
        }
    }

    return processed;
}



10、main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/resource.h>    /*setrlimit */

#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <signal.h>
#include <pthread.h>

#include "mytimer.h"
#include "client.h"

#define IPADDRESS               "127.0.0.1"
#define PORT                    1883
#define LISTENQ                 512
#define FDSIZE                  80000
#define EPOLLEVENTS             100
#define CONFIG_MIN_RESERVED_FDS 32 //come from redis src
#define CONFIG_FDSET_INCR       (CONFIG_MIN_RESERVED_FDS+96)
#define CLIENT_TIMEOUT	        60 * 1000//ms

int stop_server = 0;
int timer_id = 0;

//函数声明
//创建套接字并进行绑定
static int socket_bind(const char* ip,int port);
//IO多路复用epoll
static void do_epoll(int listenfd);
//事件处理函数
static void handle_events(int epollfd,struct epoll_event *events,int num,int listenfd);
//处理接收到的连接
static void handle_accpet(int epollfd,int listenfd);
//读处理
static void do_read(int epollfd,int fd);
//写处理
static void do_write(int epollfd,int fd);
//添加事件
static void add_event(int epollfd,int fd,int state);
//修改事件
static void modify_event(int epollfd,int fd,int state);
//删除事件
static void delete_event(int epollfd,int fd,int state);
//other
static int do_error(int fd, int *error);
static int setnonblocking(int fd);
static void daemonize(void);
static int set_fdlimit();
static void signal_exit_handler();
static void signal_exit_func(int signo);
static void handle_timer();
static int timerfun_callback(int arg);
static int user_read(client_t *client, buffer_t *rbuffer);
static int user_write(client_t *client, unsigned char* data, int len);

int main(int argc,char *argv[])
{
    //设置每个进程允许打开的最大文件数,socket
    if (set_fdlimit() < 0)
    {
        return -1;
    }

    int background = 0;
    if (background)
    {
        daemonize();
    }

    //设置信号处理,SIG_IGN表示忽略信号,SIG_DFL表示使用信号的默认处理方式
    //signal(SIGHUP, SIG_IGN); //开启的话,就捕获不到终端窗口关闭的信号了。即窗口关闭,进程仍然进行。
    signal(SIGPIPE, SIG_IGN);

    /*
    if (argc != 2) {
        fprintf(stderr, "Usage: %s port\n", argv[0]);
        return 1;
    }

    int port = atoi(argv[1]);*/

    create_fileEvent(FDSIZE + CONFIG_FDSET_INCR);
    int  listenfd;
    listenfd = socket_bind(IPADDRESS,PORT);
    listen(listenfd,LISTENQ);
    printf("start listening...\n");
    signal_exit_handler();
    timer_init();
    do_epoll(listenfd);
    timer_destroy();
    destroy_fileEvent(FDSIZE + CONFIG_FDSET_INCR);
    return 0;
}

static int socket_bind(const char* ip,int port)
{
    int  listenfd;
    struct sockaddr_in servaddr;
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    if (listenfd == -1)
    {
        perror("socket error:");
        exit(1);
    }
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    //inet_pton(AF_INET,ip,&servaddr.sin_addr);
    servaddr.sin_port = htons(port);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    int error;
    int reuse = 1;
    int ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    if (ret == -1)
    {
        return do_error(listenfd, &error);
    }

    if (bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) == -1)
    {
        perror("bind error: ");
        exit(1);
    }

    return listenfd;
}

static void do_epoll(int listenfd)
{
    int epollfd;
    struct epoll_event events[EPOLLEVENTS];
    int ret;

    //创建一个描述符
    int error;
    epollfd = epoll_create(1024);//1024 is just a hint for the kernel
    if (epollfd == -1)
    {
        return do_error(epollfd, &error);
    }

    //添加监听描述符事件
    add_event(epollfd,listenfd,EPOLLIN);

    struct event *event;
    //struct timeval now;
    struct timeval tv;
    struct timeval *tvp = NULL;

    while ( stop_server == 0 )
    {
        if ((event = timer_top()) != NULL)
        {
            long now_sec, now_ms;//come from redis src "ae.c", int aeProcessEvents(aeEventLoop *eventLoop, int flags)

            aeGetTime(&now_sec, &now_ms);
            tvp = &tv;

            /* How many milliseconds we need to wait for the next
             * time event to fire? */
            long long ms = (event->ev_timeout.tv_sec - now_sec) * 1000 +
                    (event->ev_timeout.tv_usec / 1000 - now_ms);

            if (ms > 0) {
                tvp->tv_sec = ms / 1000;
                tvp->tv_usec = (ms % 1000) * 1000;
            } else {
                tvp->tv_sec = 0;
                tvp->tv_usec = 0;
            }

            /* maybe error
            gettime(&now);

            tv.tv_sec = event->ev_timeout.tv_sec - now.tv_sec;;
            tv.tv_usec = event->ev_timeout.tv_usec - now.tv_usec;
            if ( tv.tv_usec < 0 )
            {
                tv.tv_usec += 1000000;
                tv.tv_sec--;
                printf("tv.tv_usec < 0\n");
            }
            tvp = &tv;
            */
        }
        else
        {
            tvp = NULL;
            printf("tvp == NULL\n");
        }

        //获取已经准备好的描述符事件
        if (tvp == NULL)
        {
            ret = epoll_wait(epollfd, events, EPOLLEVENTS, -1);
        }
        else
        {
            printf("timer_wait:%d\n", tvp->tv_sec*1000 + tvp->tv_usec/1000);//ms
            ret = epoll_wait(epollfd, events, EPOLLEVENTS, tvp->tv_sec*1000 + tvp->tv_usec/1000);//ms
        }

        handle_events(epollfd,events,ret,listenfd);
        handle_timer();
    }
    close(epollfd);
}

static void handle_events(int epollfd,struct epoll_event *events,int num,int listenfd)
{
    int i;
    int fd;
    //进行选好遍历
    for (i = 0;i < num;i++)
    {
        fd = events[i].data.fd;
        //根据描述符的类型和事件类型进行处理
        if ((fd == listenfd) &&(events[i].events & EPOLLIN))
            handle_accpet(epollfd,listenfd);
        else if (events[i].events & EPOLLIN)
            do_read(epollfd,fd);
        else if (events[i].events & EPOLLOUT)
            do_write(epollfd,fd);
    }
}

static void handle_timer()
{
    timer_process();
}

static void handle_accpet(int epollfd,int listenfd)
{
    int clifd;
    struct sockaddr_in cliaddr;
    socklen_t  cliaddrlen = sizeof(cliaddr);
    clifd = accept(listenfd,(struct sockaddr*)&cliaddr,&cliaddrlen);
    if (clifd == -1)
        perror("accpet error:");
    else
    {
        printf("accept a new client: %s:%d\n",inet_ntoa(cliaddr.sin_addr),cliaddr.sin_port);

        client_t *client = alloc_client();
        if (!client) {
            printf("alloc client error...close socket\n");
            close(clifd);
            return;
        }

        client->fd = clifd;
        client->epollfd = epollfd;
        client->timerId = timer_id;
        fileev[clifd].clientData = client;

        //添加一个客户描述符和事件
        add_event(epollfd,clifd,EPOLLIN);

        //add timer
        timer_add(timer_id, 1000, timerfun_callback, clifd, CYCLE_TIMER, 0);//ms
        timer_id++;
    }
}

static void do_read(int epollfd,int fd)
{
    client_t *client = fileev[fd].clientData;

    buffer_t *rbuffer = client->read_buffer;

    check_buffer_size(rbuffer, DEFAULT_BUFF_SIZE / 2);
    size_t avlid_size = rbuffer->size - rbuffer->write_idx;

    //ssize_t readn = anetRead(fd, rbuffer->buff + rbuffer->write_idx, avlid_size);
    //不能调用anetRead这个函数
    //1.客户端下线不好判断
    //2.该函数适合linux epoll是边缘模式(ET),数据一定要一次性收完,anetRead里面有while循环
    //3.redis源码自身也没有调用anetRead

    //把读到的网络数据写入活塞缓存
    ssize_t readn = read(fd, rbuffer->buff + rbuffer->write_idx, avlid_size);
    if (readn > 0)
    {
        rbuffer->write_idx += readn;
        user_read(client, rbuffer);

        client->last_recv_tick = get_tick_count();
    }
    else if (readn == 0)
    {
        printf("fd=%d, client disconnect, close it.\n", client->fd);
        delete_event(epollfd,fd,EPOLLIN);
        delete_event(epollfd,fd,EPOLLOUT);
        timer_remove(client->timerId);
        free_client(client);
    }
    else if (readn == -1)
    {
        if (errno == EAGAIN) {
            return;
        } else {
            printf("read error,%s.\n", strerror(errno));
            delete_event(epollfd,fd,EPOLLIN);
            delete_event(epollfd,fd,EPOLLOUT);
            timer_remove(client->timerId);
            free_client(client);
        }
    }
}

static void do_write(int epollfd,int fd)
{
    client_t *client = fileev[fd].clientData;
    buffer_t *wbuffer = client->write_buffer;

    int data_size = (int)get_readable_size(wbuffer);
    if (data_size == 0) {
        delete_event(epollfd,fd,EPOLLOUT);
        return;
    }

    //int writen = anetWrite(client->fd, (char *)wbuffer->buff + wbuffer->read_idx, data_size);
    int writen = write(client->fd, (char *)wbuffer->buff + wbuffer->read_idx, data_size);
    if (writen > 0) {
        wbuffer->read_idx += writen;
    } else if (writen == 0) {
        printf("Writing 0\n");
    } else { //-1
        if (errno != EWOULDBLOCK) {
            printf("Writing error: %s\n", strerror(errno));
        } else {
            printf("Writing EWOULDBLOCK\n");
        }
    }

    if (get_readable_size(wbuffer) == 0) {
        delete_event(epollfd,fd,EPOLLOUT);
    }
}

static int user_read(client_t *client, buffer_t *rbuffer)
{
    size_t len = get_readable_size(rbuffer);
    unsigned char data[DEFAULT_BUFF_SIZE];
    if (len > DEFAULT_BUFF_SIZE)
    {
        len = DEFAULT_BUFF_SIZE;
    }

    //把活塞缓存读取出来,作为用户数据
    memcpy(data, rbuffer->buff + rbuffer->read_idx, len);
    rbuffer->read_idx += len;

    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%c", data[i]);
    }

    printf("\n");

    user_write(client, data, len);//echo

    return 0;
}

static int user_write(client_t *client, unsigned char* data, int len)
{
    //把用户数据写入活塞缓存
    buffer_t *wbuffer = client->write_buffer;
    check_buffer_size(wbuffer, len);
    memcpy((char *)wbuffer->buff + wbuffer->write_idx, data, len);
    wbuffer->write_idx += len;

    //把活塞缓存的有效数据通过网络发送出去
    //int writen = anetWrite(client->fd, (char *)wbuffer->buff + wbuffer->read_idx, (int)get_readable_size(wbuffer));
    int writen = write(client->fd, (char *)wbuffer->buff + wbuffer->read_idx, (int)get_readable_size(wbuffer));
    if (writen > 0) {
        wbuffer->read_idx += writen;
    } else if (writen == 0) {
        printf("Writing 0\n");
    } else { //-1
        if (errno != EWOULDBLOCK) {
            printf("Writing error: %s\n", strerror(errno));
        } else {
            printf("Writing EWOULDBLOCK\n");
        }
    }

    //如果writen==-1,表示当前tcp窗口容量不够,需要等待下次机会再发,errno == EWOULDBLOCK
    //因为活塞缓存的有效数据没有发完,遗留部分需要再给机会
    if (get_readable_size(wbuffer) != 0) {
        /*
        if (aeCreateFileEvent(client->loop, client->fd,
                              AE_WRITABLE, writeEventHandler, client) == AE_ERR) {
            printf("create socket writeable event error, close it.\n");
            free_client(client);
        }*/

        //modify_event(client->epollfd,client->fd,EPOLLOUT);
        add_event(client->epollfd,client->fd,EPOLLOUT);
    }

    return 0;
}

static void add_event(int epollfd,int fd,int state)
{
    struct epoll_event ev;
    ev.events = state;
    ev.data.fd = fd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&ev);

    setnonblocking(fd);
}

static void delete_event(int epollfd,int fd,int state)
{
    struct epoll_event ev;
    ev.events = state;
    ev.data.fd = fd;
    epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,&ev);
}

static void modify_event(int epollfd,int fd,int state)
{
    struct epoll_event ev;
    ev.events = state;
    ev.data.fd = fd;
    epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&ev);
}

static int do_error(int fd, int *error)
{
    fprintf(stderr, "error: %s\n", strerror(errno));
    *error = errno;
    while ((close(fd) == -1) && (errno == EINTR));
    errno = *error;
    return 1;
}

static int setnonblocking(int fd)
{
    int old_option = fcntl(fd, F_GETFL);
    int new_option = old_option | O_NONBLOCK;
    fcntl(fd, F_SETFL, new_option);
    return old_option;
}

static void daemonize(void) { //come from /redis/server.c/daemonize()
    int fd;

    if (fork() != 0) exit(0); /* parent exits */
    setsid(); /* create a new session */

    /* Every output goes to /dev/null. If Redis is daemonized but
     * the 'logfile' is set to 'stdout' in the configuration file
     * it will not log at all. */
    if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
        if (fd > STDERR_FILENO) close(fd);
    }
}

static int set_fdlimit()
{
    //设置每个进程允许打开的最大文件数
    //这项功能等价于linux终端命令 "ulimit -n 102400"
    struct rlimit rt;
    rt.rlim_max = rt.rlim_cur = FDSIZE + CONFIG_FDSET_INCR;
    if (setrlimit(RLIMIT_NOFILE, &rt) == -1)
    {
        perror("setrlimit error");
        return -1;
    }

    return 0;
}

static void signal_exit_handler()
{
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = signal_exit_func;
    sigaction(SIGINT, &sa, NULL);//当按下ctrl+c时,它的效果就是发送SIGINT信号
    sigaction(SIGTERM, &sa, NULL);//kill pid
    sigaction(SIGQUIT, &sa, NULL);//ctrl+\代表退出SIGQUIT

    //SIGSTOP和SIGKILL信号是不可捕获的,所以下面两句话写了等于没有写
    sigaction(SIGKILL, &sa, NULL);//kill -9 pid
    sigaction(SIGSTOP, &sa, NULL);//ctrl+z代表停止

    //#define    SIGTERM        15
    //#define    SIGKILL        9
    //kill和kill -9,两个命令在linux中都有杀死进程的效果,然而两命令的执行过程却大有不同,在程序中如果用错了,可能会造成莫名其妙的现象。
    //执行kill pid命令,系统会发送一个SIGTERM信号给对应的程序。
    //执行kill -9 pid命令,系统给对应程序发送的信号是SIGKILL,即exit。exit信号不会被系统阻塞,所以kill -9能顺利杀掉进程。
}

static void signal_exit_func(int signo)
{
    printf("exit signo is %d\n", signo);
    stop_server = 1;
}

static int timerfun_callback(int arg)
{
    client_t *client = fileev[arg].clientData;
    assert(arg == client->fd);
    printf("timer_id=%d, fd=%d\n", client->timerId, client->fd);

    //心跳机制:定时检测,如果没有数据来则踢除客户端
    uint64_t curr_tick = get_tick_count();
    if (curr_tick > client->last_recv_tick + CLIENT_TIMEOUT)
    {
        printf("timeout, fd=%d\n", client->fd);
        //timer_remove(arg);//不能在这里删除timer,因为是回调函数,timer其实已经先pop掉了,我们要做的是不让它再次加入堆
        delete_event(client->epollfd, client->fd, EPOLLIN);
        delete_event(client->epollfd, client->fd, EPOLLOUT);
        free_client(client);
        return 0;//kill timer,返回值为0,目的是不让它再次加入堆
    }

    return 1;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值