int Mac802_11::check_pktRTS() //check_pktRTS():check if we have RTS packet to send
{ //if we have,send it then return 0; otherwise return -1
struct hdr_mac802_11 *mh;
double timeout;
assert(mhBackoff_.busy() == 0);
if(pktRTS_ == 0) // 1. have RTS packet to send?
return -1; //NO: return -1
mh = HDR_MAC802_11(pktRTS_);
switch(mh->dh_fc.fc_subtype) { //yes:
case MAC_Subtype_RTS:
if(! is_idle()) { // 2. channel is idle?
inc_cw(); //NO: increase contention window
mhBackoff_.start(cw_, is_idle()); //start backoff timer, time out=CW
return 0;
}
setTxState(MAC_RTS); //yes: 將狀態設置爲MAC_RTS,後面send_timer()函數根據這個狀態判斷是否需要重傳
timeout = txtime(phymib_.getRTSlen(), basicRate_) //timeout: the time that send the RTS out
+ DSSS_MaxPropagationDelay // 0.000002:2us
+ phymib_.getSIFS()
+ txtime(phymib_.getCTSlen(), basicRate_)
+ DSSS_MaxPropagationDelay;
break;
default:
fprintf(stderr, "check_pktRTS:Invalid MAC Control subtype\n");
exit(1);
}
transmit(pktRTS_, timeout); // 3.call transmit() ,send the RTS packet
return 0;
}
inline void Mac802_11::transmit(Packet *p, double timeout) { tx_active_ = 1; .... downtarget_->recv(p->copy(), this); mhSend_.start(timeout); //在timeout時間之後,調用send_time()函數,在send_time()中判斷是否需要重傳 // 4.start TxTimer(TxTimer::handle:mac->sendHandler(), call send_timer()): judge whether need for re-transmission. mhIF_.start(txtime(p)); // 5.start interface timer, timerout=transmit time (call txHandler() ) }
timeout時間的意義:
在802.11的握手機制中,發送節點在發送時利用TxTimer定時器給後續響應幀的預定截止到達時刻進行定時。當定時時間timeout結束而發送節點沒有接收到響應幀,表示發送失敗,發送節點的sendHandler()被調用進行重傳操作。
在上面的check_pktRTS()函數中,timeout = txtime(phymib_.getRTSlen(), basicRate_)
+ DSSS_MaxPropagationDela
+ phymib_.getSIFS()
+ txtime(phymib_.getCTSlen(), basicRate_)
+ DSSS_MaxPropagationDelay;
timeout = RTS包的傳輸時間 + 最大傳播時延 + SIFS + CTS包的傳輸時間 + 最大傳播時延
表示後續的CTS響應幀預定截止的到達時刻,如果timeout結束之後,發送節點還沒有收到CTS包,則表示RTS發送失敗,請求重傳RTS包。
即在 transmit(pktRTS_, timeout)之後,發送節點返回rece()函數等待CTS包的接收,接收到CTS包之後,會將狀態設置爲setTxState(MAC_IDLE),(在recvCTS()->tx_resume()函數中設置),在send_timer()函數中通過這個狀態判斷髮送節點已經成功收到CTS包,不需要重傳RTS。如果狀態仍然爲發送RTS後的狀態(setTxState:MAC_RTS),則表示發送節點沒有成功收到CTS包,此時重傳RTS(RetransmitRTS())。