This is a creation in Article, where the information may have evolved or changed.
#include <stdio.h> #include <string.h> #include "protocol.h" #include "datalink.h" #define MAX_SEQ 6#define Data_timer 2600#define Inc (K) if (k<max_seq) k=k + 1;else k = 0typedef struct//define packet packet {unsigned char Data[pkt_len] ;} Packet typedef enum {True,false} boolean; typedef unsigned int seq_nr; typedef struct//definition Frame frame{unsigned char kind;//frame_ DATA unsigned char ack; unsigned char seq; unsigned char Data[pkt_len]; unsigned int padding;} frame, static int phylsical_ready = 0;//The physical layer is ready for the flag, 1 indicates the physical layer is ready, and 0 indicates that the physical layer is not ready. SEQ_NR NEXT_FRAME_TO_SEND=0;SEQ_NR Ack_ Expected=0;seq_nr frame_expected=0;seq_nr nbuffered = 0;//in use cache frame F;seq_nr I;packet Buffer[max_seq+1];int event, arg,len;//event, Frame length static Boolean between (Seq_nr A, seq_nr B, Seq_nr c) {return ((a<=b) && (b<c)) | | ((C<a) && (a<=b)) | | ((b<c) && (C<a));} static void Put_frame (unsigned char *frame, int len)//Join checksum {* (unsigned int *) (frame + len) = Crc32 (frame, Len); Send_frame (frame, Len + 4); Phylsical_ready = 0;} static void Send_data (Seq_nr frame_nr,seq_nr frame_expected,packet buffer[]) {Frame F;f.kind = frame_data;//Frame Type F.SEQ = frame_nr;//to frame add serial number F.ack = (frame_expected + max_seq)% (max_seq + 1);//Piggyback Confirm memcpy (F.data, Buffer[frame_nr].data, Pkt_le N);//copy data from BUFFER[FRAME_NR] to S.data dbg_frame ("Send data%d%d, ID%d\n", F.seq, F.ack, * (short *) f.data);p Ut_frame (( unsigned char *) &f, 3 + pkt_len); Transmission frame Start_timer (FRAME_NR, Data_timer); Start Timer}int Main (int Argc,char **argv) {protocol_init (argc, argv); lprintf ("Designed by Nancyyihao, build:" __date__ "" __time__ "\ n");d Isable_network_layer (), while (1) {event = Wait_for_event (&arg), switch (event) {case Network_layer_ Ready:get_packet (buffer[next_frame_to_send].data);//received a new packet//lprintf ("Send data%d\n", sizeof (Buffer[next_ Frame_to_send].data)/sizeof (buffer[next_frame_to_send].data[0]); nbuffered = nbuffered + 1; Send_data (next_frame_to _send, Frame_expected,&amP;buffer); Inc (Next_frame_to_send);//Sender window forward (upper limit forward) break;case Physical_layer_ready:phylsical_ready = 1;break;case Frame_received:len = Recv_frame ((unsigned char *) &f, sizeof f);//Take data from physical layer if (Len < 5 | | (Crc32 ((unsigned char *) &f, len)! = 0)) {dbg_event ("Receiver Error, Bad CRC checksum\n"); if (F.kind = = Frame_data) {dbg_frame ("Recv data%d%d, ID%d\n", F.seq, F.ack, * (short *) f.data);//lprintf ("Recv data%d\n" , sizeof (F.data)/sizeof (f.data[0])), if (f.seq = = frame_expected)//received the desired frame {put_packet (f.data, len-7);//submit data to Network Layer Inc ( frame_expected)///Receiver window forward (lower bound forward)}}while (between (ack_expected, F.ack, next_frame_to_send)//Handle the piggyback acknowledgment {nbuffered = nbuffered-1; Stop_timer (ack_expected);//Receive Frame, Timer Stop Inc (ack_expected);//Reduce Sender window}break;case data_timeout:dbg_event (" ----DATA%d timeout\n ", ARG); Next_frame_to_send = ack_expected;//Start retransmission for (i=1; I <= nbuffered; i++) {Send_data (Next_frame_to_send, frame_expected , &buffer);//Retransmission Inc. (Next_frame_to_send);//Prepare the next DD frame to be transmitted}break;} if (NBuffered < Max_seq) && Phylsical_ready) Enable_network_layer (); Elsedisable_network_layer ();}}