/** * handle the connection handshake:<br/> * <ul> * <li>set initial sequence number</li> * <li>send response handshake</li> * </ul> * @param handshake * @param peer * @throws IOException */ protected void handleHandShake(ConnectionHandshake handshake) { try { ConnectionHandshake responseHandshake = new ConnectionHandshake(); //compare the packet size and choose minimun long clientBufferSize = handshake.getPacketSize(); long myBufferSize = getDatagramSize(); long bufferSize = Math.Min(clientBufferSize, myBufferSize); long initialSequenceNumber = handshake.getInitialSeqNo(); setInitialSequenceNumber(initialSequenceNumber); setDatagramSize((int)bufferSize); responseHandshake.setPacketSize(bufferSize); responseHandshake.setUdtVersion(4); responseHandshake.setInitialSeqNo(initialSequenceNumber); responseHandshake.setConnectionType(-1); responseHandshake.setMaxFlowWndSize(handshake.getMaxFlowWndSize()); //tell peer what the socket ID on this side is responseHandshake.setSocketID(mySocketID); responseHandshake.setDestinationID(this.getDestination().getSocketID()); responseHandshake.setSession(this); Log.Write(this.ToString(), "Sending reply " + responseHandshake); endPoint.doSend(responseHandshake); } catch (Exception exc) { Log.Write(this.ToString(), exc); } }
/// <summary> /// handshake for connect /// </summary> protected void sendHandShake() { try { ConnectionHandshake handshake = new ConnectionHandshake(); handshake.setConnectionType(ConnectionHandshake.CONNECTION_TYPE_REGULAR); handshake.setSocketType(ConnectionHandshake.SOCKET_TYPE_DGRAM); long initialSequenceNo = SequenceNumber.random(); setInitialSequenceNumber(initialSequenceNo); handshake.setInitialSeqNo(initialSequenceNo); handshake.setPacketSize(getDatagramSize()); handshake.setSocketID(mySocketID); handshake.setMaxFlowWndSize(flowWindowSize); handshake.setSession(this); Log.Write(this.ToString(), "Sending " + handshake); endPoint.doSend(handshake); } catch (Exception exc) { Log.Write(this.ToString(), exc); } }
/// <summary> /// sends the given data packet, storing the relevant information /// 发送存储相关信息的数据包 /// </summary> /// <param name="p"></param> private void send(DataPacket p) { lock (sendLock) { if (storeStatistics) { dgSendInterval.end(); dgSendTime.begin(); } endpoint.doSend(p); if (storeStatistics) { dgSendTime.end(); dgSendInterval.begin(); throughput.end(); throughput.begin(); } sendBuffer[p.getPacketSequenceNumber()] = p; unacknowledged.IncrementAndGet(); } statistics.incNumberOfSentDataPackets(); }
public void shutdown() { if (clientSession.isReady() && clientSession.active == true) { Shutdown shutdown = new Shutdown(); shutdown.setDestinationID(clientSession.getDestination().getSocketID()); shutdown.setSession(clientSession); try{ clientEndpoint.doSend(shutdown); } catch (Exception e) { Log.Write(this.ToString(), "SEVERE ERROR: Connection could not be stopped!", e); } clientSession.getSocket().getReceiver().stop(); clientEndpoint.stop(); } }
/** * 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); } }