Exemple #1
0
        protected void ProcessUDTPacket(IUDTPacket p)
        {
            //(3).Check the packet type and process it according to this.

            if (!p.IsControlPacket())
            {
                DataPacket dp = (DataPacket)p;
                if (storeStatistics)
                {
                    dataPacketInterval.End();
                    dataProcessTime.Begin();
                }
                OnDataPacketReceived(dp);
                if (storeStatistics)
                {
                    dataProcessTime.End();
                    dataPacketInterval.Begin();
                }
            }

            else if (p.GetControlPacketType() == (int)ControlPacketType.ACK2)
            {
                Acknowledgment2 ack2 = (Acknowledgment2)p;
                onAck2PacketReceived(ack2);
            }

            else if (p is Shutdown)
            {
                OnShutdown();
            }
        }
Exemple #2
0
        /// <summary>
        /// 发送网络数据
        /// </summary>
        /// <param name="packet"></param>
        internal void DoSend(IUDTPacket packet)
        {
            byte[] data = packet.GetEncoded();
            var    dgp  = packet.GetSession().Datagram;

            dgSocket.SendTo(data, dgp.Remote);
        }
Exemple #3
0
 /*
  * packets are written by the endpoint
  */
 internal void Receive(IUDTPacket p)
 {
     if (storeStatistics)
     {
         dgReceiveInterval.End();
     }
     handoffQueue.Add(p);
     if (storeStatistics)
     {
         dgReceiveInterval.Begin();
     }
 }
Exemple #4
0
 //receive a packet from server from the peer
 internal void Receive(IUDTPacket p)
 {
     if (p is Acknowledgement)
     {
         Acknowledgement acknowledgement = (Acknowledgement)p;
         OnAcknowledge(acknowledgement);
     }
     else if (p is NegativeAcknowledgement)
     {
         NegativeAcknowledgement nak = (NegativeAcknowledgement)p;
         OnNAKPacketReceived(nak);
     }
     else if (p is KeepAlive)
     {
         session.Socket.GetReceiver().ResetEXPCount();
     }
 }
Exemple #5
0
        /**
         * receiver algorithm
         * see specification P11.
         */
        public void ReceiverAlgorithm()
        {
            //check ACK timer
            long currentTime = Util.getCurrentTime();

            if (nextACK < currentTime)
            {
                nextACK = currentTime + ackTimerInterval;
                ProcessACKEvent(true);
            }
            //check NAK timer
            if (nextNAK < currentTime)
            {
                nextNAK = currentTime + nakTimerInterval;
                ProcessNAKEvent();
            }

            //check EXP timer
            if (nextEXP < currentTime)
            {
                nextEXP = currentTime + expTimerInterval;
                ProcessEXPEvent();
            }
            //perform time-bounded UDP receive
            IUDTPacket packet = null;

            handoffQueue.TryTake(out packet, (int)Util.GetSYNTime());
            if (packet != null)
            {
                //reset exp count to 1
                expCount = 1;
                //If there is no unacknowledged data packet, or if this is an
                //ACK or NAK control packet, reset the EXP timer.
                bool needEXPReset = false;
                if (packet.IsControlPacket())
                {
                    ControlPacket cp     = (ControlPacket)packet;
                    int           cpType = cp.GetControlPacketType();
                    if (cpType == (int)ControlPacketType.ACK || cpType == (int)ControlPacketType.NAK)
                    {
                        needEXPReset = true;
                    }
                }
                if (needEXPReset)
                {
                    nextEXP = Util.getCurrentTime() + expTimerInterval;
                }
                if (storeStatistics)
                {
                    processTime.Begin();
                }

                ProcessUDTPacket(packet);

                if (storeStatistics)
                {
                    processTime.End();
                }
            }

            Thread.Yield();
        }
Exemple #6
0
 public abstract void Received(IUDTPacket packet, Destination peer);
Exemple #7
0
 public int CompareTo(IUDTPacket other)
 {
     return((int)(GetPacketSequenceNumber() - other.GetPacketSequenceNumber()));
 }
Exemple #8
0
        public override void Received(IUDTPacket packet, Destination peer)
        {
            lastPacket = packet;

            if (packet is ConnectionHandshake)
            {
                ConnectionHandshake hs = (ConnectionHandshake)packet;
                FlashLogger.Info("Received connection handshake from " + peer + "\n" + hs);
                if (State != ready)
                {
                    if (hs.ConnectionType == 1)
                    {
                        try {
                            //TODO validate parameters sent by peer
                            // long peerSocketID = hs.SocketID;
                            // DestinationID = peerSocketID;
                            // destination(peerSocketID);
                            destination.SocketID = hs.SocketID;
                            SendConfirmation(hs);
                        } catch (Exception ex) {
                            FlashLogger.Warn("Error creating socket", ex);

                            State = invalid;
                        }
                        return;
                    }
                    else
                    {
                        try {
                            //TODO validate parameters sent by peer
                            //理论上这里是getConnectionType==-1
                            // long peerSocketID = hs.getSocketID();
                            // destination.SetSocketID(peerSocketID);
                            // setState(ready);
                            destination.SocketID = hs.SocketID;
                            State = ready;
                            Thread.Sleep(50);
                            //多个握手序列,使用接收的第一个
                            this.initialSequenceNumber = hs.InitialSeqNo;//cd 必须重置
                            socket = new UDTSocket(endPoint, this);
                        } catch (Exception ex) {
                            FlashLogger.Warn("Error creating socket", ex);

                            State = invalid;
                        }
                        return;
                    }
                }
            }

            if (State == ready)
            {
                if (packet is Shutdown)
                {
                    State  = shutdown;
                    active = false;
                    FlashLogger.Info("Connection shutdown initiated by the other side.");
                    return;
                }
                active = true;
                try {
                    if (packet.ForSender())
                    {
                        socket.GetSender().Receive(lastPacket);
                    }
                    else
                    {
                        socket.GetReceiver().Receive(lastPacket);
                    }
                } catch (Exception ex) {
                    //session is invalid
                    FlashLogger.Error("Error in " + toString(), ex);
                    //setState(invalid);
                    State = invalid;
                }
                return;
            }
        }
Exemple #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);
                }
            }
        }
Exemple #10
0
        public override void Received(IUDTPacket packet, Destination peer)
        {
            lastPacket = packet;
            if (packet is ConnectionHandshake)
            {
                ConnectionHandshake connectionHandshake = (ConnectionHandshake)packet;
                FlashLogger.Info("Received " + connectionHandshake);


                if (State <= ready)
                {
                    destination.SocketID = connectionHandshake.SocketID;
                    if (State <= handshaking)
                    {
                        State = handshaking;
                    }
                    try {
                        HandleHandShake(connectionHandshake);
                        n_handshake++;
                        try {
                            //理论上应该先检验cookie

                            State  = ready;
                            socket = new UDTSocket(endPoint, this);
                            cc.Init();
                        } catch (Exception uhe) {
                            //session is invalid

                            FlashLogger.Error("", uhe);

                            State = invalid;
                        }
                    } catch (IOException ex) {
                        //session invalid

                        FlashLogger.Warn("Error processing ConnectionHandshake", ex);

                        State = invalid;
                    }
                    return;
                }
                else
                {
                    //cd  回复
                    try {
                        HandleHandShake(connectionHandshake);
                    } catch (IOException e) {
                    }
                }
            }
            else if (packet is KeepAlive)
            {
                socket.GetReceiver().ResetEXPTimer();
                active = true;
                return;
            }

            if (State == ready)
            {
                active = true;

                if (packet is KeepAlive)
                {
                    //nothing to do here
                    return;
                }
                else if (packet is Shutdown)
                {
                    try {
                        socket.GetReceiver().Stop();
                    } catch (IOException ex) {
                        FlashLogger.Warn("", ex);
                    }
                    State = shutdown;
                    Console.WriteLine("SHUTDOWN ***");
                    active = false;
                    FlashLogger.Info("Connection shutdown initiated by the other side.");
                    return;
                }

                else
                {
                    try {
                        Console.WriteLine("收到数据包");
                        if (packet.ForSender())
                        {
                            socket.GetSender().Receive(packet);
                        }
                        else
                        {
                            socket.GetReceiver().Receive(packet);
                        }
                    } catch (Exception ex) {
                        //session invalid
                        FlashLogger.Error("", ex);
                        State = invalid;
                        // setState(invalid);
                    }
                }
                return;
            }
        }