数据中继epoll模型

数据中继epoll模型

示例:relay_epoll.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/epoll.h>

#define FILE1 "/dev/tty10"
#define FILE2 "/dev/tty11"
#define BUFFSIZE 1024

enum fsmstate  //状态机状态
{
  STATE_R,
  STATE_W,
  STATE_AUTO,
  STATE_EX,
  STATE_T
};

struct fsm_st  //状态机数据结构
{
  enum fsmstate state;
  int sfd;
  int dfd;
  int lenth;
  int pos;
  char *err;
  char buff[BUFFSIZE];
};

static void fsm_driver(struct fsm_st *fsm)  //状态机
{
  int ret;

  switch(fsm->state) //状态切换及事件处理
  {
    case STATE_R: //读状态
      fsm->lenth = read(fsm->sfd,fsm->buff,BUFFSIZE);
      if(fsm->lenth<0)
      {
        if(errno == EAGAIN)
          fsm->state = STATE_R;
        else
        {
          fsm->err = "read";
          fsm->state = STATE_EX;
        }
      }
      else if(fsm->lenth == 0)
        fsm->state = STATE_T;
      else
      {
        fsm->pos =0;
        fsm->state = STATE_W;
      }
      break;
    case STATE_W: //写状态
      ret = write(fsm->dfd,fsm->buff + fsm->pos,fsm->lenth);
      if(ret <0)
      {
        if(errno == EAGAIN)
          fsm->state = STATE_W;
        else
        {
          fsm->err = "write";
          fsm->state = STATE_EX;
        }
      }
      else
      {
        fsm->pos +=ret;
        fsm->lenth -=ret;
        if(fsm->lenth ==0)
          fsm->state = STATE_R;
        fsm->state = STATE_W;
      }
      break;
    case STATE_EX: //出错状态
      fprintf(stderr,"%s\n",fsm->err);
      fsm->state = STATE_T;
      break;
    case STATE_T:  //完成状态
      break;
    default:
      abort();
      break;
  }
}

static void relay(int fd1, int fd2)
{
  struct fsm_st fsm12,fsm21;
  int fd1_save,fd2_save;
  int epfd;  //创建epoll返回值
  struct epoll_event ev;   //epoll文件监测事件

  fd1_save = fcntl(fd1,F_GETFL);
  fsm12.state = STATE_R;
  fsm12.sfd = fd1;
  fsm12.dfd = fd2;

  fd2_save = fcntl(fd2,F_GETFL);
  fsm21.state = STATE_R;
  fsm21.sfd = fd2;
  fsm21.dfd = fd1;

  epfd = epoll_create(10); //创建epoll(10 是随便写的大于0 即可)
  if(epfd <0)
  {
    perror("epoll_create():");
    exit(0);
  }

  ev.events = 0;     //初始化epoll事件
  ev.data.fd = fd1;   //初始化epoll数据
  epoll_ctl(epfd, EPOLL_CTL_ADD, fd1, &ev);    //添加epoll监测文件及事件

  ev.data.fd = fd2;
  epoll_ctl(epfd, EPOLL_CTL_ADD, fd2, &ev);  //添加epoll监测文件及事件

  while(fsm12.state != STATE_T || fsm21.state != STATE_T)
  {
    if(fsm12.state == STATE_R)
      ev.events |= EPOLLIN;  //设置epoll监测事件
    if(fsm21.state == STATE_W)
      ev.events |= EPOLLOUT;
    ev.data.fd = fd1;      //设置epoll数据
    epoll_ctl(epfd, EPOLL_CTL_MOD, fd1, &ev); //修改epoll监测内容

    if(fsm12.state == STATE_W)
      ev.events |= EPOLLOUT;
    if(fsm21.state == STATE_R)
      ev.events |= EPOLLIN;
    ev.data.fd = fd2;
    epoll_ctl(epfd, EPOLL_CTL_MOD, fd2, &ev);  //修改epoll监测内容

    if(fsm12.state <STATE_AUTO || fsm21.state <STATE_AUTO)
    {
      while(epoll_wait(epfd, &ev, 1, -1)<0) //启动epoll监测事件   (监测那个epoll  , 监测返回事件, 一次最大返回个数, -1 阻塞等待)
      {
        if(errno == EINTR)
          continue;
        perror("epoll()");
        exit(1);
      }
    }

    if( ev.data.fd ==fd1 && ev.events&EPOLLIN || ev.data.fd == fd2 && ev.events&EPOLLOUT\      //事件判断及处理
      || fsm12.state > STATE_AUTO)
      fsm_driver(&fsm12);
    if( ev.data.fd ==fd2 && ev.events&EPOLLIN || ev.data.fd == fd1 && ev.events&EPOLLOUT\      //事件判断及处理
      || fsm21.state >STATE_AUTO)
      fsm_driver(&fsm21);
  }

  fcntl(fd1,F_SETFL,fd1_save);
  fcntl(fd2,F_SETFL,fd2_save);
  close(epfd);    //关闭epoll生成的文件
}

int main()
{
  int fd1,fd2;

  fd1 = open(FILE1,O_RDWR|O_NONBLOCK);  //阻塞方式打开
  if(fd1 <0)
  {
    perror("open()");
    exit(1);
  }

  fd2 = open(FILE2,O_RDWR|O_NONBLOCK);  //阻塞方式打开
  if(fd2 <0)
  {
    perror("open()");
    exit(1);
  }


  relay(fd1,fd2);

  while(1)
    pause();

  close(fd1);
  close(fd2);

  exit(0);
}

posted @ 2022-08-01 08:41  *^VV^*  阅读(31)  评论(0编辑  收藏  举报