/net_framework

processing epoll model

Primary LanguageC++

net_framework



1 Introduction

  •   This framework processes details of net sockets.With it, you can be focus on the busines
      logics and do not care the issues of sockets and concurrences. You just need to 
      define some interfaces to deal with the business logics and add it to plugins, then 
      run the framework.
    

2 Interface

2.1 AP (TCP)

2.1.1 TcpHandleInit

  •   Define: INT32 TcpHandleInit(const CHAR * file);
    
  •   This interface initializes some resources for tcp handler. The parameter 'file'
      supports the path of configure file.
    

2.1.2 TcpHandlerConnected

  •   Define: INT32 TcpHandlerConnected(INT32 sock_fd, const CHAR * ip, INT32 port);
    
  •   This interface is called by frameworks when a socket connection is created.You can do 
      some logical check for the connection.
      For example: You can verify the connection. If the connection is invalid, the function returns
      non-zero, then the framework will disconnect.
    

2.1.3 TcpHandlerDisconnected

  •   Define: INT32 TcpHandlerDisconnected(INT32 sock_fd, const CHAR * ip, INT32 port);
    
  •   This interface is called by frameworks when a socket is disconnected,You can set free resources
      that have been allocated by TcpHandlerConnected.
    

2.1.4 FinishTcpHandle

  •   Define: INT32 FinishTcpHandle();
    
  •   This interface is called by frameworks when the AP is shutdown gracefully.You can set free 
      resources that have been allocated by TcpHandleInit.
    

2.1.5 MsgHandleUnpack

  •   Define: INT32 MsgHandleUnpack(const CHAR * data, INT32 size);
    
  •   This interface helps you to split package into multiple datagrams.The return 
      value indicates the length of one datagram.
    

2.1.6 MsgHandleRoute

  •   Define: INT32 MsgHandleRoute(const CHAR * data, INT32 size);
    
  •   This interface makes the load banlance of JOBs, the return value 
      indicates the ID of one JOB.
    

2.2 JOB

2.2.1 MsgHandleInit

  •   Define: INT32 MsgHandleInit(const CHAR * file);
    
  •   This interface is called by frameworks when a job is initialized.You can allocate 
      resources here.
    

2.2.2 MsgHandleProcessing

  •   Define: INT32 MsgHandleProcessing(const CHAR * data, INT32 size, CHAR * out, 
      									INT32 * out_size,
                                          AsyncCallBackWrite func, VOID * params, 
                                          INT32 params_size);
    
  •   This interface is the main entry of the business logics.You can implement it 
      synchronous and asynchronous.It returns zero that indicates success, and 
      non-zero indicates failure.
    

2.2.3 MsgHandleFinish

  •   Define: INT32 MsgHandleFinish();
    
  •   This interface is called by frameworks when a JOB is shutdown gracefully.You can set free 
      resources allocated by MsgHandleInit.
    

3 Dependence

  •   wget https://curl.haxx.se/download/curl-7.56.1.tar.gz 
      tar zxvf curl-7.56.1.tar.gz
      cd curl-7.56.1 
      ./configure
      make
      sudo make install
    

4 Compile

  •   git clone --recursive https://github.com/changyuezhou/net_framework.git
      OPTION: comment line 'add_subdirectory(db)' in net_framework/commlib/CMakeLists.txt
      mkdir build
      cd build
      cmake ../net_framework
      make
    

5 Install

5.1 Install Framework

  •   ${NET_FRAMWORK_DIR}/install/install.sh NSF [REQUIRED HOME_DIR] [REQUIRED LOG_DIR]
      HOME_DIR: framework install dir
      LOG_DIR: log dir
    

5.2 Install Plugins

  •   ${NET_FRAMWORK_DIR}/install/install.sh PLUGIN [REQUIRED HOME_DIR] [REQUIRED LOG_DIR] [REQUIRES PLUGIN_LIB] [OPTION: PLUGIN_ETC]
      HOME_DIR: framework install dir
      LOG_DIR: log dir
      PLUGIN_LIB: path of plugin lib
      PLUGIN_ETC: path of plugin config file
    

5.3 Install Both

  •   ${NET_FRAMWORK_DIR}/install/install.sh ALL [REQUIRED HOME_DIR] [REQUIRED LOG_DIR] [REQUIRES PLUGIN_LIB] [OPTION: PLUGIN_ETC]
      HOME_DIR: framework install dir
      LOG_DIR: log dir
      PLUGIN_LIB: path of plugin lib
      PLUGIN_ETC: path of plugin config file    
    

6 Dir Tree

  •   ${HOME_DIR}
      |
      |--etc--{ap.conf, ctrl.conf, job.conf}
      |--bin--{nsf_ctrl, nsf_ap, nsf_job}   
      |--plugins
         |
         |--libs--{${PLUGIN_LIB}}
         |--etc--{${PLUGIN_ETC}}
    

7 Configure

7.1 Framework Config

  •   see: https://github.com/changyuezhou/net_framework/tree/master/etc
    

7.2 Plugins Config

  •   Do something you like :-) ...............
    

8 Running

  •   ${HOME_DIR}/bin/service.sh START
    

9 Stop

  •   ${HOME_DIR}/bin/service.sh STOP
    

10 Performance