ArrayList<SipcRequest> timeoutList = new ArrayList<SipcRequest>(); //超时的包的列表
Iterator<SipcRequest> it = this.requestQueue.iterator();
//首先把超时的包找出来
while(it.hasNext()){
SipcRequest request = it.next();
if(request.getAliveTime()<curTime){ //超过了存活期,
//因为这里如果判断超时并重发,这个包会重新添加到队列中,当前迭代的列表会增加元素,迭代器就会抛出更改异常
//所以这里重新建立了一个超时列表,并把超时的包放入列表中,在当前迭代结束后才处理超时包
it.remove(); //先从队列中移除
timeoutList.add(request); //添加到重发包列表
}else{
//未超过存活期,啥也不干
}
}
//遍历超时包列表,并根据条件判断是否重发或者通知超时
it = timeoutList.iterator();
while(it.hasNext()){
SipcRequest request = it.next();
if(request.getRetryTimes()<maxTryTimes){ //重发次数未超过指定的次数,重新发送这个包
logger.debug("Request was timeout, now sending it again... Request="+request);
request.incRetryTimes(); //递增重试次数
request.setAliveTime(curTime+aliveTime); //更新存活时间
this.processOutcoming(request); // 重新发送这个包
}else{ //这个包已经超过重发次数,通知超时
logger.warn("A request was sent three times, handle this timeout exception...Request="+request);
this.handleRequestTimeout(request);
}