5 months ago

限制状态机:在某个状态下就会产生某种行为,然后到下一个状态。
写限制状态机的基本逻辑:
分配一个内存
造一推指针
给每一个指针赋值
返回这片内存

//得到编码
short get_2518(char *state)
{
   short *_4053;

   _4053 = (short *) state;

   return *_4053;
}

//得到传输接口的状态(0/1)
char get_1385(char *state)
{
   char *_1330;

   _1330 = (char *) (state + 2);

   return *_1330;
}

//得到端口的前半部分
char get_2321(char *state)
{
   char *_7402;

   _7402 = (char *) (state + 3);

   return *_7402;
}

//得到端口的后半部分
char get_3818(char *state)
{
   char *_9253;

   _9253 = (char *) (state + 4);

   return *_9253;
}

//状态编码为5860的状态函数,在这个状态下,会执行的动作
char *create_9473(char _8928, char _5979, char _1702)
{
   char *_8525, *_8068, *_1882, *_4854;
   short *_4034;

   _8525 = malloc(5);

   _4034 = (short *) _8525;
   _8068 = (char *) (_8525 + 2);
   _1882 = (char *) (_8525 + 3);
   _4854 = (char *) (_8525 + 4);

   *_4034 = 5860;
   *_8068 = _8928;
   *_1882 = _5979;
   *_4854 = _1702;

   return _8525;
}

//状态编码为9029的状态函数,在这个状态下,会执行的动作
char *create_5603()
{
   char *_5714;
   short *_5028;

   _5714 = malloc(2);

   _5028 = (short *) _5714;

   *_5028 = 9029;

   return _5714;
}

//状态编码为6804的状态函数,在这个状态下,会执行的动作
char *create_4351()
{
   char *_1516;
   short *_4883;

   _1516 = malloc(2);

   _4883 = (short *) _1516;

   *_4883 = 6804;

   return _1516;
}

//状态编码为6254的状态函数,在这个状态下,会执行的动作
char *create_9159(char _1968, char _9353)
{
   char *_1568, *_3720, *_6614;
   short *_5647;

   _1568 = malloc(4);

   _5647 = (short *) _1568;
   _3720 = (char *) (_1568 + 2);
   _6614 = (char *) (_1568 + 3);

   *_5647 = 6254;
   *_3720 = _1968;
   *_6614 = _9353;

   return _1568;
}

//信息传输,参数:状态,字节,与连接者通讯的套接字【对方端口】(请看最下面的一张图,accept会产生一个新的套接字来与连接者通讯)
char *transit(char *state, char byte, int other) 
{
   printf("transit has been entered%c", 10);

//当状态为9029并且字节为59时,跳到5860这个状态,参考create_9473这个状态创造函数。
   if (get_2518(state) == 9029 && byte == 59) {

      printf("state is %d%c", 9029, 10);

      return create_9473(0, 0, 0);//参数为0,为初始状态
   }

//当状态为9029时,跳到6804这个状态,参考create_4351这个状态创造函数。(6804这个状态没有动作)
   if (get_2518(state) == 9029) {

      printf("state is %d%c", 9029, 10);

      return create_4351();
   }

//当状态为5860且传输进度为0时(初始化状态是0),跳到传输进度为1的状态下,调用create_9473这个状态创造函数。
   if (get_2518(state) == 5860 && get_1385(state) == 0) {

      printf("state is %d, progress is %d%c", 5860, 0, 10);

      return create_9473(1, byte, 0);//获得端口的前半部分
   }

//当状态为5860且传输状态为1时,跳到6254这个状态,调用create_9159这个状态创造函数,获得一个4字节的内存指针,2个字节放6254这个编码,一个字节放端口的前半部分,一个字节放端口的后半部分。
   if (get_2518(state) == 5860 && get_1385(state) == 1) {

      printf("state is %d, progress is %d%c", 5860, 1, 10);

      return create_9159(get_2321(state), get_3818(state));//获得端口的后半部分
   }
}

void handle(char *conservation, char *private, int self)
{
   struct sockaddr_in _7231;
   int _8352 = 16;
   char _3049, *state;
   int other, _6888;
      char *_9710;

   while (1) {

      other = accept(self, (struct sockaddr *) &_7231, &_8352);
      _9710 = (char *) &(_7231.sin_addr.s_addr);
      state = create_5603();

printf("new connection: %hhu.%hhu.%hhu.%hhu%c", _9710[0], _9710[1], _9710[2], _9710[3], 10);

      while (1) {

         _6888 = recv(other, &_3049, 1, 0);

         if (_6888 == -1 || _6888 == 0) {

            printf("conection broken%c", 10);
            break;
         }

         state = transit(state, _3049, other);
      }
   }
}

信息补充,以下是监听者的流程,帮助你了解每个函数的作用:

← 节点之间传输区块 制作TCP程序在两个机器间传输文件 →
 
comments powered by Disqus