Giter Site home page Giter Site logo

epoll_reactor_server's Introduction

epoll_reactor_server

基于epoll的reactor模式的TCP_Server

  • 目前,单进程执行callback_handler

  • 三个callback处理线程

    void callback_recvdata(int epfd, my_event_st *ev);
    void callback_accept(int epfd, my_event_st *ev);
    void callback_senddata(int epfd, my_event_st *ev);
  • Server主函数:

    my_event_st my_events[MAX_EVENTS + 1];
    int my_events_len = 0;  
    
    int main()
    {
      int epfd = epoll_create(MAX_EVENTS + 1);
      int ls_fd = create_ls_socket(SERV_PORT);
      my_event_st *ls_ev = create_my_event(ls_fd, EPOLLIN, (void *)callback_accept);
      register_event(epfd, ls_ev);
      epoll_event events[MAX_EVENTS + 1];
      while (1)
      {
        int nfd = epoll_wait(epfd, events, MAX_EVENTS + 1, ONE_MINUTE);
        perr(nfd <= 0, "epoll_wait");
        for (int i = 0; i < nfd; i++)
        {
          my_event_st *ev = events[i].data.ptr;
          ev->callback_fn(epfd, ev);
        }
      }
    }

    需要注意的坑:

    • 如果client关闭了socket,那么server会收到长度为0的msg,即会调用callback_recvdata,这个时候需要在函数里面对长度为0进行判断,并取消注册,关闭fd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.