模仿数据链路层的gobackn协议/*该协议是搭载ack的回退n步协议*/#include#include"protocol.h"#definemax_seq7#defineflag126#defineESC100#definewait_time2700 //发送计时器等待的时间#defineack_wait_time280staticintphl_ready=0;unsignedcharbuf[max_seq+1][270];unsignedcharack[8];//发送空的ack帧unsignedcharin_buf[600],last_buf[520];//接收时的缓冲区;去掉冗余之后的缓冲区,为防备因误码两帧合并为一帧而定义了很大一个数组intnbuffered=0;//发送的帧数intbuf_size[max_seq+1];//记下以发送各帧的帧长intnext_frame_to_send=0;intframe_in_phl=0;//用于成帧intframe_expected=0;intack_expected=0;intbetween(inta,intb,intc){ if(((a<=b)&&(b<c))||((c<a)&&(a<=b))||((b<c)&&(c<a))) return1; elsereturn0;}//判断帧尾,防止出现误判escescflag为数据的情况intend_flag(intin_len){ intcount=0; inti; if(in_len=0;i--)//记录flag前的esc数目 count++; returncount%2;//若flag前的esc为偶数,则为帧尾}//成帧函数--数据帧voidsend_frame(char*my_buf,intlen){ intn; buf[frame_in_phl][0]=(frame_expected+max_seq)%(max_seq+1); //ack buf[frame_in_phl][1]=frame_in_phl; //发送帧的帧号 for(n=0;n<len;n++) buf[frame_in_phl][n+2]=my_buf[n]; //将处理过的新帧赋值到缓冲区中 len=len+2; *(unsignedint*)(buf[frame_in_phl]+len)=crc32(buf[frame_in_phl],len); //在原始帧的基础上加检验和 buf_size[frame_in_phl]=len+4; //记录当前帧的长度,包括3个帧头,4个检验和 nbuffered=nbuffered+1; //缓冲区占用数加一 frame_in_phl=(frame_in_phl+1)%(max_seq+1);}//成帧函数--ack帧voidsend_ack() //ack帧的处理{ ack[0]=(frame_expected+max_seq)%(max_seq+1); ack[1]=max_seq+10; //ack帧的序号位,使ack[1]==frame_expected恒不成立 *(unsignedint*)(ack+2)=crc32(ack,2); //在原始帧的基础上加检验和}//主函数intmain(intargc,char**argv){intevent,arg,n,m,i,j,len=0,in_len=0; unsignedcharmy_buf[260]; intphl_wait=0;//在物理层中还没有被发送的帧protocol_init(argc,argv);enable_network_layer();for(;;){event=wait_for_event(&arg);switch(event){caseNETWORK_LAYER_READY:
1