/** * write a NAK triggered by a received sequence number that is larger than * the largestReceivedSeqNumber + 1 * @param currentSequenceNumber - the currently received sequence number * @throws IOException */ protected void sendNAK(long currentSequenceNumber) { try { NegativeAcknowledgement nAckPacket = new NegativeAcknowledgement(); nAckPacket.addLossInfo(largestReceivedSeqNumber + 1, currentSequenceNumber); nAckPacket.setSession(session); nAckPacket.setDestinationID(session.getDestination().getSocketID()); //put all the sequence numbers between (but excluding) these two values into the //receiver loss list for (long i = largestReceivedSeqNumber + 1; i < currentSequenceNumber; i++) { ReceiverLossListEntry detectedLossSeqNumber = new ReceiverLossListEntry(i); receiverLossList.insert(detectedLossSeqNumber); } endpoint.doSend(nAckPacket); //logger.info("NAK for "+currentSequenceNumber); statistics.incNumberOfNAKSent(); } catch (Exception exc) { Log.Write(this.ToString(), exc); } }
protected void sendAck2(long ackSequenceNumber) { try { Acknowledgment2 ackOfAckPkt = new Acknowledgment2(); ackOfAckPkt.setAckSequenceNumber(ackSequenceNumber); ackOfAckPkt.setSession(session); ackOfAckPkt.setDestinationID(session.getDestination().getSocketID()); endpoint.doSend(ackOfAckPkt); } catch (Exception ex) { Log.Write(this.ToString(), ex); } }
/** * write the given data, waiting at most for the specified time if the queue is full * @param data * @param offset * @param length * @param timeout * @param units * @throws IOException - if data cannot be sent * @throws InterruptedException */ //protected void doWrite(byte[]data, int offset, int length, int timeout, TimeUnit units) /// <summary> /// write the given data, waiting at most for the specified time if the queue is full /// 写入给定数据,如果队列已满,将最多等待指定的超时时间 /// </summary> /// <param name="data"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <param name="timeout"></param> public void doWrite(byte[] data, int offset, int length, int timeout) { int chunksize = session.getDatagramSize() - 24;//need some bytes for the header 1400-24=1376 每一次发送的最大字节数 ByteBuffer bb = new ByteBuffer(data, offset, length); long seqNo = 0; while (bb.Remaining() > 0) { try { int len = Math.Min(bb.Remaining(), chunksize); byte[] chunk = new byte[len]; bb.Get(ref chunk); DataPacket packet = new DataPacket(); seqNo = sender.getNextSequenceNumber(); packet.setPacketSequenceNumber(seqNo); packet.setSession(session); packet.setDestinationID(session.getDestination().getSocketID()); packet.setData(chunk); //put the packet into the send queue if (!sender.sendUdtPacket(packet, timeout)) { Log.Write(this.ToString(), "Queue full"); } Thread.Sleep(50); } catch (Exception exc) { Log.Write(this.ToString(), "write the given data, waiting at most for the specified time if the queue is full ", exc); } } if (length > 0) { active = true; } }