/// <summary>
 /// 2nd handshake for connect
 /// </summary>
 /// <param name="hs"></param>
 protected void sendConfirmation(ConnectionHandshake hs)
 {
     try
     {
         ConnectionHandshake handshake = new ConnectionHandshake();
         handshake.setConnectionType(-1);
         handshake.setSocketType(ConnectionHandshake.SOCKET_TYPE_DGRAM);
         handshake.setInitialSeqNo(hs.getInitialSeqNo());
         handshake.setPacketSize(hs.getPacketSize());
         handshake.setSocketID(mySocketID);
         handshake.setMaxFlowWndSize(flowWindowSize);
         handshake.setSession(this);
         Log.Write(this.ToString(), "Sending confirmation " + handshake.toString());
         endPoint.doSend(handshake);
     }
     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. 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;
            }
        }