原创 A reliable communication protocol

2008-12-10 16:39 2559 11 11 分类: MCU/ 嵌入式




In
computer communication, there are many cases in which a protocol needs to cope
with packet loss. In order to provide a reliable channel, lost packets must be
retransmitted. This example shows a simple communication protocol that uses
(the absence of) acknowledgment packets to detect a lost packet. If a lost
packet is detected, the packet is retransmitted.



The
example shows two protothreads, one for the sender and one for the receiver.



i nclude "pt.h"



 



PT_THREAD(sender(struct
pt *pt))



{



  PT_BEGIN(pt); 



  do {



    send_packet();



    /* Wait until an ackowledgement has been
received, or until the timer expires. If the timer expires, we should send the
packet again. */



    timer_set(&timer, TIMEOUT);



    PT_WAIT_UNTIL(pt, acknowledgment_received()
|| timer_expired(&timer));



  } while(timer_expired(&timer)); 



  PT_END(pt);



}



 



PT_THREAD(receiver(struct
pt *pt))



{



  PT_BEGIN(pt);



  /* Wait until a packet has been received, and
send an acknowledgment. */



  PT_WAIT_UNTIL(pt, packet_received());



  send_acknowledgement(); 



  PT_END(pt);



}
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
11
关闭 站长推荐上一条 /3 下一条