Esempio n. 1
0
 /// <summary>
 /// receive a packet from server from the peer
 /// 从对等服务器接收数据包
 /// </summary>
 /// <param name="p"></param>
 public void receive(UDTPacket p)
 {
     try
     {
         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.getSocket().getReceiver().resetEXPCount();
         }
         else if (p is DataPacket)
         {
             ////返回的确认数据包
             ////DataPacketAnswer Answer = (DataPacketAnswer)p;
             //if (largestSentSequenceNumber == (int)p.getPacketSequenceNumber())
             //{
             //    sendQueueStart.CountDown();
             //}
         }
     }
     catch (Exception ex)
     {
         Log.Write(this.ToString(), ex);
     }
 }
Esempio n. 2
0
 public void doSend(UDTPacket packet)
 {
     try
     {
         byte[] data = packet.getEncoded();
         byte[] dgp  = packet.getSession().getDatagram();
         this.dgSocket.SendTo(dgp, Remote);
     }
     catch
     { }
 }
Esempio n. 3
0
 /// <summary>
 /// packets are written by the endpoint
 /// </summary>
 /// <param name="p"></param>
 public void receive(UDTPacket p)
 {
     if (storeStatistics)
     {
         dgReceiveInterval.end();
     }
     handoffQueue.Enqueue(p);
     if (storeStatistics)
     {
         dgReceiveInterval.begin();
     }
 }
Esempio n. 4
0
 /// <summary>
 /// 发送udp数据包
 /// </summary>
 /// <param name="packet"></param>
 public void doSend(UDTPacket packet)
 {
     try
     {
         byte[]     data = packet.getEncoded();
         IPEndPoint dgp  = packet.getSession().getDatagram();
         this.SendInternal(data, dgp);
     }
     catch (Exception exc)
     {
         Log.Write(this.ToString(), "send data err", exc);
     }
 }
Esempio n. 5
0
        protected void processUDTPacket(UDTPacket p)
        {
            //(3).Check the packet type and process it according to this.
            try
            {
                if (!p.isControlPacket())
                {
                    DataPacket dp = (DataPacket)p;
                    if (storeStatistics)
                    {
                        dataPacketInterval.end();
                        dataProcessTime.begin();
                    }
                    onDataPacketReceived(dp);//解析数据包将数据存在AppData类中
                    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();
                }
            }
            catch (Exception exc)
            {
                Log.Write(this.ToString(), exc);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// receiver algorithm
        /// see specification P11.
        /// </summary>
        public void receiverAlgorithm()
        {
            try
            {
                if (handoffQueue.Count <= 0)
                {
                    Thread.Yield();
                }
                else
                {
                    //check ACK timer
                    long currentTime = Util.getCurrentTime();

                    #region 暂时,屏蔽不然会一直发包(不知道这段是启什么作用)
                    //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();
                    //}
                    #endregion

                    //perform time-bounded UDP receive
                    UDTPacket packet = handoffQueue.Dequeue();
                    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();
                        }
                        //解析数据包将数据存在AppData类中
                        processUDTPacket(packet);

                        if (storeStatistics)
                        {
                            processTime.end();
                        }
                    }
                    Thread.Yield();
                }
            }
            catch (Exception exc)
            {
                Log.Write(this.ToString(), exc);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="peer"></param>
        public override void received(UDTPacket packet, Destination peer)
        {
            lastPacket = packet;

            if (packet is ConnectionHandshake)
            {
                ConnectionHandshake hs = (ConnectionHandshake)packet;

                Log.Write(this.ToString(), "Received connection handshake from " + peer.getAddress() + " " + peer.getPort() + "\n" + hs.toString());

                if (getState() != ready)
                {
                    if (hs.getConnectionType() == 1)
                    {
                        try
                        {
                            //TODO validate parameters sent by peer
                            long peerSocketID = hs.getSocketID();
                            destination.setSocketID(peerSocketID);
                            sendConfirmation(hs);
                        }
                        catch (Exception ex) {
                            Log.Write(this.ToString(), "WARNING:Error creating socket", ex);
                            setState(invalid);
                        }
                        return;
                    }
                    else
                    {
                        try{
                            //TODO validate parameters sent by peer
                            long peerSocketID = hs.getSocketID();
                            destination.setSocketID(peerSocketID);
                            setState(ready);
                            socket = new UDTSocket(endPoint, this);
                        }catch (Exception ex) {
                            Log.Write(this.ToString(), "WARNING:Error creating socket", ex);
                            setState(invalid);
                        }
                        return;
                    }
                }
            }

            if (getState() == ready)
            {
                if (packet is Shutdown)
                {
                    setState(shutdown);
                    active = false;
                    Log.Write(this.ToString(), "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
                    Log.Write(this.ToString(), "SEVERE:Error in " + toString(), ex);
                    setState(invalid);
                }
                return;
            }
        }
Esempio n. 8
0
        public override void received(UDTPacket packet, Destination peer)
        {
            lastPacket = packet;

            if (packet is ConnectionHandshake)
            {
                ConnectionHandshake connectionHandshake = (ConnectionHandshake)packet;
                Log.Write(this.ToString(), "Received " + connectionHandshake);

                if (getState() <= ready)
                {
                    destination.setSocketID(connectionHandshake.getSocketID());

                    if (getState() <= handshaking)
                    {
                        setState(handshaking);
                    }
                    try
                    {
                        handleHandShake(connectionHandshake);
                        n_handshake++;
                        try{
                            socket = new UDTSocket(endPoint, this);
                            setState(ready);
                            cc.init();
                        }catch (Exception uhe) {
                            //session is invalid
                            Log.Write(this.ToString(), "SEVERE", uhe);
                            setState(invalid);
                        }
                    }catch (Exception ex) {
                        //session invalid
                        Log.Write(this.ToString(), "WARNING:Error processing ConnectionHandshake", ex);
                        setState(invalid);
                    }
                    return;
                }
            }
            else if (packet is KeepAlive)
            {
                socket.getReceiver().resetEXPTimer();
                active = true;
                return;
            }

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

                if (packet is KeepAlive)
                {
                    //nothing to do here
                    return;
                }
                else if (packet is Shutdown)
                {
                    try
                    {
                        socket.getReceiver().stop();
                    }
                    catch (Exception ex)
                    {
                        Log.Write(this.ToString(), "WARNING", ex);
                    }
                    setState(shutdown);
                    Log.Write(this.ToString(), "SHUTDOWN ***");
                    active = false;
                    Log.Write(this.ToString(), "Connection shutdown initiated by the other side.");
                    return;
                }
                else
                {
                    try{
                        if (packet.forSender())
                        {
                            socket.getSender().receive(packet);
                        }
                        else
                        {
                            if (packet.getMessageNumber() == 9999)//作为返回确认数据包
                            {
                                //通知可以继续发送数据
                                socket.getSender().receive(packet);
                            }
                            else
                            {
                                socket.getReceiver().receive(packet);
                            }
                        }
                    }catch (Exception ex)
                    {
                        //session invalid
                        Log.Write(this.ToString(), "SEVERE", ex);
                        setState(invalid);
                    }
                }
                return;
            }
        }
Esempio n. 9
0
 public abstract void received(UDTPacket packet, Destination peer);
Esempio n. 10
0
        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);
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="peer"></param>
        public override void received(UDTPacket packet, Destination peer)
        {
            lastPacket = packet;

            if (packet is ConnectionHandshake)
            {
                ConnectionHandshake hs = (ConnectionHandshake)packet;

                Log.Write(this.ToString(), "Received connection handshake from " + peer.getAddress() + " " + peer.getPort() + "\n" + hs.toString());

                if (getState() != ready)
                {
                    if (hs.getConnectionType() == 1)
                    {
                        try
                        {
                            //TODO validate parameters sent by peer
                            long peerSocketID = hs.getSocketID();
                            destination.setSocketID(peerSocketID);
                            sendConfirmation(hs);
                        }
                        catch (Exception ex) {
                            Log.Write(this.ToString(), "WARNING:Error creating socket", ex);
                            setState(invalid);
                        }
                        return;
                    }
                    else
                    {
                        try
                        {
                            //TODO validate parameters sent by peer
                            long peerSocketID = hs.getSocketID();
                            destination.setSocketID(peerSocketID);
                            socket = new UDTSocket(endPoint, this);
                            setState(ready);
                        }catch (Exception ex) {
                            Log.Write(this.ToString(), "WARNING:Error creating socket", ex);
                            setState(invalid);
                        }
                        return;
                    }
                }
            }

            if (getState() == ready)
            {
                if (packet is Shutdown)
                {
                    setState(shutdown);
                    active = false;
                    Log.Write(this.ToString(), "Connection shutdown initiated by the other side.");
                    return;
                }
                active = true;
                try
                {
                    if (packet.forSender())
                    {
                        socket.getSender().receive(lastPacket);
                    }
                    else
                    {
                        DataPacket dtemp = (DataPacket)lastPacket;
                        //Log.Write(this.ToString(), "receive data PacketSequenceNumber:" + dtemp.getPacketSequenceNumber() + "  length:" + dtemp.getData().Length);
                        //if (!lastPacket.isControlPacket())
                        //{
                        //    Thread.Sleep(50);
                        //    //向发送端反回数据包
                        //    sendDataPacketAnswer((DataPacket)lastPacket);
                        //    Log.Write(this.ToString(), "return data PacketSequenceNumber" + lastPacket.getPacketSequenceNumber() + "  length:" + dtemp.getData().Length + "  MessageNumber:9999");
                        //}
                        socket.getReceiver().receive(lastPacket);//将数据包存在UDTReceiver类的队列中
                    }
                }
                catch (Exception ex)
                {
                    //session is invalid
                    Log.Write(this.ToString(), "SEVERE:Error in " + toString(), ex);
                    setState(invalid);
                }
                return;
            }
        }
Esempio n. 12
0
        /// <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 int compareTo(UDTPacket other)
 {
     return((int)(getPacketSequenceNumber() - other.getPacketSequenceNumber()));
 }