/// <summary> /// create a receiver with a valid {@link UDTSession} /// </summary> /// <param name="session"></param> /// <param name="endpoint"></param> public UDTReceiver(UDTSession session, UDPEndPoint endpoint) { this.roundTripTimeVar = roundTripTime / 2; this.endpoint = endpoint; this.session = session; //( System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks)/10000; //如果要得到Java中 System.currentTimeMillis() 一样的结果,就可以写成 上面那样,也可以这样写: // TimeSpan ts=new TimeSpan( System.DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks); //(long)ts.TotalMilliseconds; this.sessionUpSince = (long)ts.TotalMilliseconds; this.statistics = session.getStatistics(); if (!session.isReady()) { Log.Write(this.ToString(), "UDTSession is not ready."); } ackHistoryWindow = new AckHistoryWindow(16); packetHistoryWindow = new PacketHistoryWindow(16); receiverLossList = new ReceiverLossList(); packetPairWindow = new PacketPairWindow(16); largestReceivedSeqNumber = (int)session.getInitialSequenceNumber() - 1; bufferSize = session.getReceiveBufferSize(); handoffQueue = new Queue <UDTPacket>(4 * session.getFlowWindowSize()); storeStatistics = false; //Boolean.getBoolean("udt.receiver.storeStatistics"); initMetrics(); start(); }
public UDTSocket Accept() { try { if (!started) { this.endpoint.start(true);//启动监听 this.started = true; } while (!shutdown) { UDTSession session = endpoint.accept(); if (session != null) { //等待握手完成 while (!session.isReady() || session.getSocket() == null) { Thread.Sleep(100); } return(session.getSocket()); } Thread.Sleep(400); } return(null); } catch (Exception exc) { Log.Write(this.ToString(), "listens and blocks until a new client connects and returns a valid {@link UDTSocket} for the new connection", exc); return(null); } }
/// <summary> /// * @param host /// * @param port /// * @param endpoint /// * @throws SocketException,UnknownHostException /// </summary> /// <param name="endpoint"></param> /// <param name="session"></param> public UDTSocket(UDPEndPoint endpoint, UDTSession session) { this.endpoint = endpoint; this.session = session; this.receiver = new UDTReceiver(session, endpoint); this.sender = new UDTSender(session, endpoint); }
public UDTSession getSession(long destinationID) { UDTSession _udtsession = null; this.sessions.TryGetValue(destinationID, out _udtsession); return(_udtsession); }
/// <summary> /// 存储UDTSession会话 /// </summary> /// <param name="destinationID"></param> /// <param name="session"></param> public void addSession(long destinationID, UDTSession session) { try { this.sessions.Add(destinationID, session); Log.Write(this.ToString(), "Storing session <" + destinationID + ">"); } catch (Exception exc) { Log.Write(this.ToString(), "Storing session", exc); } }
public UDTSender(UDTSession session, UDPEndPoint endpoint) { if (!session.isReady()) { Log.Write(this.ToString(), "UDTSession is not ready."); } this.endpoint = endpoint; this.session = session; statistics = session.getStatistics(); senderLossList = new SenderLossList(); sendBuffer = new Dictionary <long, DataPacket>(session.getFlowWindowSize()); sendQueue = new Queue <DataPacket>(1000); lastAckSequenceNumber = (int)session.getInitialSequenceNumber(); currentSequenceNumber = (int)session.getInitialSequenceNumber() - 1; waitForAckLatch.Set(new CountDownLatch(1)); waitForSeqAckLatch.Set(new CountDownLatch(1)); storeStatistics = false; //Boolean.getBoolean("udt.sender.storeStatistics"); initMetrics(); doStart(); }
protected void doReceive() { while (!stopped) { try{ try{ //will block until a packet is received or timeout has expired IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)sender; //dgSocket.Bind(_ipep); dgSocket.ReceiveFrom(dp, ref Remote); Remoteinfo = (IPEndPoint)Remote; Destination peer = new Destination(Remoteinfo.Address, Remoteinfo.Port); int l = dp.Length; UDTPacket packet = PacketFactory.createPacket(dp, l); lastPacket = packet; //handle connection handshake if (packet.isConnectionHandshake()) { lock (thisLock) { long id = packet.getDestinationID(); UDTSession session; sessions.TryGetValue(id, out session); if (session == null) { session = new ServerSession(dp, this); addSession(session.getSocketID(), session); //TODO need to check peer to avoid duplicate server session if (serverSocketMode) { Log.Write(this.ToString(), "Pooling new request."); sessionHandoff.Enqueue(session); Log.Write(this.ToString(), "Request taken for processing."); } } peer.setSocketID(((ConnectionHandshake)packet).getSocketID()); session.received(packet, peer); } } else { //dispatch to existing session long dest = packet.getDestinationID(); UDTSession session; if (dest == lastDestID) { session = lastSession; } else { sessions.TryGetValue(dest, out session); lastSession = session; lastDestID = dest; } if (session == null) { n++; if (n % 100 == 1) { Log.Write(this.ToString(), "Unknown session <" + dest + "> requested from <" + peer + "> packet type " + packet.ToString()); } } else { session.received(packet, peer); } } } catch (SocketException ex) { Log.Write(this.ToString(), "INFO", ex); } }catch (Exception ex) { Log.Write(this.ToString(), "WARNING", ex); } } }
/// <summary> /// 存储UDTSession会话 /// </summary> /// <param name="destinationID"></param> /// <param name="session"></param> public void addSession(long destinationID, UDTSession session) { Log.Write(this.ToString(), "Storing session <" + destinationID + ">"); this.sessions.Add(destinationID, session); }
/// <summary> /// 处理接收的数据 /// </summary> /// <param name="dp"></param> /// <param name="_remoteIP"></param> private void ReceiveData(byte[] dp, IPEndPoint _remoteIP) { try { Destination peer = new Destination(_remoteIP.Address, _remoteIP.Port); int l = dp.Length; UDTPacket packet = PacketFactory.createPacket(dp, l); lastPacket = packet; //handle connection handshake 处理连接握手 if (packet.isConnectionHandshake()) { lock (thisLock) { long id = packet.getDestinationID(); UDTSession session; sessions.TryGetValue(id, out session);//ClientSession 或是 ServerSession if (session == null) { session = new ServerSession(dp, this); addSession(session.getSocketID(), session); //TODO need to check peer to avoid duplicate server session if (serverSocketMode) { sessionHandoff.Enqueue(session); Log.Write(this.ToString(), "Pooling new request, request taken for processing."); } } try { peer.setSocketID(((ConnectionHandshake)packet).getSocketID()); session.received(packet, peer);//ClientSession 或是 ServerSession } catch (Exception ex) { Log.Write(this.ToString(), "WARNING", ex); } } } else { //dispatch to existing session long dest = packet.getDestinationID(); UDTSession session; if (dest == lastDestID) { session = lastSession; } else { sessions.TryGetValue(dest, out session); lastSession = session; lastDestID = dest; } if (session == null) { n++; if (n % 100 == 1) { Log.Write(this.ToString(), "Unknown session <" + dest + "> requested from <" + peer + "> packet type " + packet.ToString()); } } else { session.received(packet, peer); } } } catch (SocketException ex) { Log.Write(this.ToString(), "INFO", ex); } }
public UDTCongestionControl(UDTSession session) { this.session = session; this.statistics = session.getStatistics(); lastDecreaseSeqNo = session.getInitialSequenceNumber() - 1; }