数据中继poll模型

数据中继poll模型

示例:relay_poll.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.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 int max(int a,int b)
{
  if(a > b)
    return a;
  return b;
}

static void relay(int fd1, int fd2) 
{
  struct fsm_st fsm12,fsm21;
  int fd1_save,fd2_save;
  struct pollfd pfd[3];   //poll文件集数组

  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;

  pfd[0].fd = fd1;  //初始化struct pollfd
  pfd[0].events = 0;

  pfd[1].fd = fd2;
  pfd[1].events = 0;

  while(fsm12.state != STATE_T || fsm21.state != STATE_T)
  {
    if(fsm12.state == STATE_R)
      pfd[0].events |= POLLIN;  //设置监测事件
    if(fsm12.state == STATE_W)
      pfd[1].events |= POLLOUT;
    if(fsm21.state == STATE_R)
      pfd[1].events |= POLLIN;
    if(fsm21.state == STATE_W)
      pfd[0].events |= POLLOUT;

    if(fsm12.state <STATE_AUTO || fsm21.state <STATE_AUTO)
    {
      while(poll(pfd, 2, -1)<0)   //调用poll函数   监测文件集,监测2个文件,阻塞监控
      {
        if(errno == EINTR)
          continue;
        perror("poll()");
        exit(1);
      }
    }

    if( pfd[0].revents&POLLIN || pfd[1].revents&POLLOUT || fsm12.state > STATE_AUTO)  //判断状态启用状态机
      fsm_driver(&fsm12);
    if( pfd[1].revents&POLLIN || pfd[0].revents&POLLOUT || fsm21.state >STATE_AUTO)
      fsm_driver(&fsm21);
  }

  fcntl(fd1,F_SETFL,fd1_save);  //恢复文件属性
  fcntl(fd2,F_SETFL,fd2_save);
}

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:21  *^VV^*  阅读(34)  评论(0编辑  收藏  举报