资源简介
计算机网络数据链路层gobackn协议
typedef struct{
unsigned char kind; //FRAME_DATA
unsigned char ack; //ACK number
unsigned char seq; //seq bumber
unsigned char data[PKT_LEN]; //传输数据
unsigned int padding; //CRC校验和
}frame;
代码片段和文件信息
#include
#include
#include “protocol.h“
#include “datalink.h“
#define inc(k) if(k #define DATA_TIMER 2800
#define MAX_SEQ 7
#pragma comment(lib“Protocol.lib“)
typedef struct{
unsigned char kind; //frame_DATA
unsigned char ack; //ACK number
unsigned char seq; //seq bumber
unsigned char data[PKT_LEN]; //传输数据
unsigned int padding; //CRC校验和
}frame;
static unsigned char frame_nr = 0buffer[MAX_SEQ+1][PKT_LEN] nbuffered=0;
static unsigned char frame_expected = 0ack_expected=0next_frame_to_send=0;
static int phl_ready = 0;
int between(unsigned char aunsigned char bunsigned char c) //判断帧有没有在当前窗口中
{
if(((a<=b)&&(b return 1;
else
return 0;
}
static void put_frame(unsigned char *frame int len)
{
*(unsigned int *)(frame + len) = crc32(frame len);
send_frame(frame len + 4);
phl_ready = 0;
}
static void send_data_frame(void) //将帧传递给物理层传送,并开始计时器计时
{
frame s;
s.kind = frame_DATA;
s.seq = next_frame_to_send;
s.ack = (frame_expected+MAX_SEQ)%(MAX_SEQ+1);
memcpy(s.databuffer[next_frame_to_send]PKT_LEN);
dbg_frame(“Send DATA %d %d ID %dwindows %d\n“ s.seq s.ack *(short *)s.datanbuffered);
put_frame((unsigned char *)&s 3 + PKT_LEN);
start_timer(next_frame_to_send DATA_TIMER);
}
int main(int argc char **argv)
{
int event arg;
frame f;
int len = 0;
int i;
protocol_init(argc argv);
lprintf(“Designed by Su Shihao build: “ __DATE__“ “__TIME__“\n“);
disable_network_layer(); //开始状态,网络层禁止递交分组
for (;;) {
event = wait_for_event(&arg);
switch (event) {
case NETWORK_layer_READY: //网络层有分组要发送
get_packet(buffer[next_frame_to_send]);
nbuffered++;
send_data_frame();
inc(next_frame_to_send);
break;
case PHYSICAL_layer_READY: //物理层准备
phl_ready = 1;
break;
case frame_RECEIVED: //一个数据帧到达
len = recv_frame((unsigned char *)&f sizeof f);
if (len < 5 || crc32((unsigned char *)&f len) != 0) {
dbg_event(“**** Receiver Error Bad CRC Checksum\n“);
break;
}//CRC校验
if (f.kind == frame_DATA)
{
dbg_frame(“Recv DATA %d %d ID %d\n“ f.seq f.ack *(short *)f.data);
if (f.seq == frame_expected) //接收一帧
{
put_packet(f.data len - 7);
inc(frame_expected);
}
while(between(ack_expectedf.acknext_frame_to_send))// 检查n-1,n-2等ack并确认
{
nbuffered=nbuffered-1;
stop_timer(ack_expected);
inc(ack_expected);
}
}
break;
case DATA_TIMEOUT: //TIMER超时,重传发送窗口中所有帧
dbg_event(“---- DATA %d timeout\n“ arg);
next_frame_to_send=ack_expected;
for(i=1;i<=nbuffered;i++)
{
send_data_frame();
inc(next_frame_to_send);
}
break;
}
if (nbuffered < MAX_SEQ && phl_ready)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3181 2013-11-23 23:44 gobackn\datali
文件 3476 2009-10-19 16:46 gobackn\datali
文件 524 2009-10-16 10:23 gobackn\datali
文件 518 2009-10-16 12:34 gobackn\datali
文件 1831 2009-10-19 16:46 gobackn\protocol.h
文件 5932 2009-10-19 22:57 gobackn\Protocol.lib
目录 0 2013-11-25 13:22 gobackn
----------- --------- ---------- ----- ----
15462 7
- 上一篇:二叉树给期权定价.rar
- 下一篇:ABAP Editor Dark Theme
评论
共有 条评论