示例#1
0
 /**
  * @param host
  * @param port
  * @param endpoint
  * @throws SocketException,UnknownHostException
  */
 public UDTSocket(UDPEndPoint endpoint, UDTSession session)
 {
     this.endpoint = endpoint;
     this.session  = session;
     this.receiver = new UDTReceiver(session, endpoint);
     this.sender   = new UDTSender(session, endpoint);
 }
示例#2
0
        /**
         * wait the given time for a new connection
         * @param timeout - the time to wait
         * @param unit - the {@link TimeUnit}
         * @return a new {@link UDTSession}
         * @throws InterruptedException
         */
        internal UDTSession Accept(int timeout)
        {
            UDTSession session = null;

            sessionHandoff.TryTake(out session, timeout);
            return(session);
        }
示例#3
0
 /**
  * listens and blocks until a new client connects and returns a valid {@link UDTSocket}
  * for the new connection
  * @return
  */
 public UDTSocket Accept()
 {
     lock (lock_obj)
     {
         if (!started)
         {
             endpoint.Start(true);
             started = true;
         }
         while (!shutdown)
         {
             UDTSession session = endpoint.Accept(10000);
             if (session != null)
             {
                 //wait for handshake to complete
                 while (!session.IsReady || session.Socket == null)
                 {
                     Thread.Sleep(100);
                 }
                 return(session.Socket);
             }
         }
         return(null);
     }
 }
示例#4
0
        public UDTSession GetSession(long destinationID)
        {
            UDTSession session = null;

            sessions.TryGetValue(destinationID, out session);
            return(session);
        }
示例#5
0
        /**
         * 移除session
         * cd
         * @param socketid
         * @return
         */
        public UDTSession RemoveSession(long socketid)
        {
            //cd
            sessionnum--;
            UDTSession session = null;

            FlashLogger.Info("Storing Sessionnum:" + sessionnum);
            sessions.TryRemove(socketid, out session);
            return(session);
        }
示例#6
0
 /**
  * create a receiver with a valid {@link UDTSession}
  * @param session
  */
 public UDTReceiver(UDTSession session, UDPEndPoint endpoint)
 {
     this.endpoint       = endpoint;
     this.session        = session;
     this.sessionUpSince = DateTime.Now.Ticks;
     this.statistics     = session.Statistics;
     if (!session.IsReady)
     {
         throw new IllegalStateException("UDTSession is not ready.");
     }
     ackHistoryWindow         = new AckHistoryWindow(16);
     packetHistoryWindow      = new PacketHistoryWindow(16);
     receiverLossList         = new ReceiverLossList();
     packetPairWindow         = new PacketPairWindow(16);
     largestReceivedSeqNumber = session.InitialSequenceNumber - 1;
     bufferSize   = session.ReceiveBufferSize;
     handoffQueue = new BlockingCollection <IUDTPacket>(4 * session.FlowWindowSize);
     //storeStatistics=bool.getbool("udt.receiver.storeStatistics");
     InitMetrics();
     Start();
 }
示例#7
0
        public UDTSender(UDTSession session, UDPEndPoint endpoint)
        {
            if (!session.IsReady)
            {
                throw new IllegalStateException("UDTSession is not ready.");
            }
            this.endpoint         = endpoint;
            this.session          = session;
            statistics            = session.Statistics;
            senderLossList        = new SenderLossList();
            sendBuffer            = new ConcurrentDictionary <long, DataPacket>(session.FlowWindowSize, 2);
            sendQueue             = new BlockingCollection <DataPacket>(1000);
            lastAckSequenceNumber = session.InitialSequenceNumber;
            currentSequenceNumber = session.InitialSequenceNumber - 1;
            //waitForAckLatch.set(new CountDownLatch(1));
            //waitForSeqAckLatch.set(new CountDownLatch(1));
            //storeStatistics=bool.getbool("udt.sender.storeStatistics");

            InitMetrics();
            DoStart();
        }
示例#8
0
 public UDTCongestionControl(UDTSession session)
 {
     this.session      = session;
     this.statistics   = session.Statistics;
     lastDecreaseSeqNo = session.InitialSequenceNumber - 1;
 }
示例#9
0
        /**
         * single receive, run in the receiverThread, see {@link #start()}
         * <ul>
         * <li>Receives UDP packets from the network</li>
         * <li>Converts them to UDT packets</li>
         * <li>dispatches the UDT packets according to their destination ID.</li>
         * </ul>
         * @throws IOException
         */


        protected void DoReceive()
        {
            EndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);

            while (!stopped)
            {
                try
                {
                    try
                    {
                        //v.end();

                        //will block until a packet is received or timeout has expired

                        int len = dgSocket.ReceiveFrom(dp, ref remotePoint);

                        //v.begin();

                        Destination peer   = new Destination(remotePoint);
                        IUDTPacket  packet = PacketFactory.CreatePacket(dp, len);
                        lastPacket = packet;

                        //handle connection handshake
                        if (packet.IsConnectionHandshake())
                        {
                            lock (lock_obj)
                            {
                                long       id      = packet.DestinationID;
                                UDTSession session = null;
                                sessions.TryGetValue(id, out session);
                                if (session == null)
                                {
                                    session = new ServerSession((IPEndPoint)remotePoint, this);
                                    AddSession(session.SocketID, session);
                                    //TODO need to check peer to avoid duplicate server session
                                    if (serverSocketMode)
                                    {
                                        FlashLogger.Info("Pooling new request.");
                                        sessionHandoff.Add(session);
                                        FlashLogger.Info("Request taken for processing.");
                                    }
                                }
                                peer.SocketID = ((ConnectionHandshake)packet).SocketID;
                                session.Received(packet, peer);
                            }
                        }
                        else
                        {
                            //dispatch to existing session
                            long       dest = packet.DestinationID;
                            UDTSession session;
                            if (dest == lastDestID)
                            {
                                session = lastSession;
                            }
                            else
                            {
                                // session =
                                sessions.TryGetValue(dest, out session);//cd 获取session
                                lastSession = session;
                                lastDestID  = dest;
                            }
                            if (session == null)
                            {
                                n++;
                                if (n % 100 == 1)
                                {
                                    FlashLogger.Warn("Unknown session <" + dest + "> requested from <" + peer + "> packet type " + packet);
                                }
                            }
                            else
                            {
                                Console.WriteLine("收到包");
                                session.Received(packet, peer);
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        if (ex.Message.Equals("socket closed") && stopped)
                        {
                            //cd
                            //已经正常关闭
                        }
                        else
                        {
                            FlashLogger.Info("SocketException: " + ex.Message);
                        }
                    }
                    catch (Exception ste)
                    {
                        //can safely ignore... we will retry until the endpoint is stopped
                    }
                }
                catch (Exception ex)
                {
                    FlashLogger.Warn("Got: " + ex.Message, ex);
                }
            }
        }
示例#10
0
 public void AddSession(long destinationID, UDTSession session)
 {
     FlashLogger.Info("Storing session <" + destinationID + ">");
     sessionnum++;
     sessions[destinationID] = session;
 }