9 months ago

table_create这个函数在内存中的图形展示:

//创造table
char *table_create(int limit)
{
   char *A;
   int *B;

   A = malloc(16 * limit + 8);
   B = (int *) A;
   *B = 0;
   B = (int *) (A + 4);
   *B = limit;

   return A;
}

//将其他节点信息逐一增加到这个table中
int table_add(char *table, int address, int port, int recent, int expectation)
{
   int *A, *B, *C, *D, *E, *F;

   A = (int *) table;
   B = (int *) (table + 4);
   C = (int *) (table + 8 + *A * 16);
   D = (int *) (table + 8 + *A * 16 + 4);
   E = (int *) (table + 8 + *A * 16 + 8);
   F = (int *) (table + 8 + *A * 16 + 12);

   *C = address;
   *D = port;
   *E = recent;
   *F = expectation;
   
   *A = *A + 1;

   return 1;
}

//获取table的row,得到每一行的IP地址和端口,即其他节点的身份号
int table_get_row(char *table, int sequence, int *address, int *port)
{
   int *A, *B, *C, *D;

   A = (int *) table;
   B = (int *) (table + 4);

   if (sequence >= *A)
      return 0;

   C = (int *) (table + 8 + 16 * sequence);
   D = (int *) (table + 8 + 16 * sequence + 4);

   *address = *C;
   *port = *D;

   return 1;
}

//获取已经有几个节点在维持中
int table_get_attainment(char *table)
{
   int *B;
   B = (int *) table;
   
   return *B;
}

//获取这片内存能最多能维持几个节点
int table_get_limit(char *table)
{
   int *B;
   B = (int *) (table + 4);

   return *B;
}

← 制作TCP程序在两个机器间传输文件 私钥-->公钥-->地址 →
 
comments powered by Disqus