예제 #1
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;
            }
        }
        /// <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;
            }
        }
예제 #3
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;
            }
        }