Exemplo n.º 1
0
        /**
         * handle the connection handshake:<br/>
         * <ul>
         * <li>set initial sequence number</li>
         * <li>send response handshake</li>
         * </ul>
         * @param handshake
         * @param peer
         * @throws IOException
         */
        protected void HandleHandShake(ConnectionHandshake handshake)
        {
            ConnectionHandshake responseHandshake = new ConnectionHandshake();
            //compare the packet size and choose minimun
            long clientBufferSize      = handshake.PacketSize;
            long myBufferSize          = datagramSize;
            long bufferSize            = Math.Min(clientBufferSize, myBufferSize);
            long initialSequenceNumber = handshake.InitialSeqNo;

            this.initialSequenceNumber = initialSequenceNumber;
            this.DatagramSize          = (int)bufferSize;
            // setDatagramSize((int) bufferSize);
            responseHandshake.PacketSize     = bufferSize;
            responseHandshake.UdtVersion     = 4;
            responseHandshake.InitialSeqNo   = initialSequenceNumber;
            responseHandshake.ConnectionType = -1;
            responseHandshake.MaxFlowWndSize = handshake.MaxFlowWndSize;
            //tell peer what the socket ID on this side is
            responseHandshake.SocketID      = mySocketID;
            responseHandshake.DestinationID = this.destination.SocketID;
            //cd 2018-08-28
            if (this.cookie == 0)
            {
                this.cookie = CreateCookie();
            }
            responseHandshake.Cookie = cookie;

            responseHandshake.SetSession(this);
            FlashLogger.Info("Sending reply " + responseHandshake);
            endPoint.DoSend(responseHandshake);
        }
Exemplo n.º 2
0
 /**
  * handle the connection handshake:<br/>
  * <ul>
  * <li>set initial sequence number</li>
  * <li>send response handshake</li>
  * </ul>
  * @param handshake
  * @param peer
  * @throws IOException
  */
 protected void handleHandShake(ConnectionHandshake handshake)
 {
     try
     {
         ConnectionHandshake responseHandshake = new ConnectionHandshake();
         //compare the packet size and choose minimun
         long clientBufferSize      = handshake.getPacketSize();
         long myBufferSize          = getDatagramSize();
         long bufferSize            = Math.Min(clientBufferSize, myBufferSize);
         long initialSequenceNumber = handshake.getInitialSeqNo();
         setInitialSequenceNumber(initialSequenceNumber);
         setDatagramSize((int)bufferSize);
         responseHandshake.setPacketSize(bufferSize);
         responseHandshake.setUdtVersion(4);
         responseHandshake.setInitialSeqNo(initialSequenceNumber);
         responseHandshake.setConnectionType(-1);
         responseHandshake.setMaxFlowWndSize(handshake.getMaxFlowWndSize());
         //tell peer what the socket ID on this side is
         responseHandshake.setSocketID(mySocketID);
         responseHandshake.setDestinationID(this.getDestination().getSocketID());
         responseHandshake.setSession(this);
         Log.Write(this.ToString(), "Sending reply " + responseHandshake);
         endPoint.doSend(responseHandshake);
     }
     catch (Exception exc)
     {
         Log.Write(this.ToString(), exc);
     }
 }
Exemplo n.º 3
0
        //2nd handshake for connect
        protected void SendConfirmation(ConnectionHandshake hs)
        {
            ConnectionHandshake handshake = new ConnectionHandshake();

            handshake.ConnectionType = -1;
            handshake.SocketID       = ConnectionHandshake.SOCKET_TYPE_DGRAM;
            handshake.InitialSeqNo   = hs.InitialSeqNo;
            handshake.PacketSize     = hs.PacketSize;
            handshake.SocketID       = mySocketID;
            handshake.MaxFlowWndSize = flowWindowSize;
            //cd 2018-08-28
            handshake.Cookie = hs.Cookie;
            handshake.PeerIP = hs.PeerIP;
            //
            handshake.SetSession(this);
            FlashLogger.Info("Sending confirmation " + handshake);
            endPoint.DoSend(handshake);
        }
 /// <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);
     }
 }
Exemplo n.º 5
0
        //handshake for connect
        protected void SendHandShake()
        {
            ConnectionHandshake handshake = new ConnectionHandshake();

            handshake.ConnectionType = ConnectionHandshake.CONNECTION_TYPE_REGULAR;
            handshake.SocketType     = ConnectionHandshake.SOCKET_TYPE_DGRAM;
            long initialSequenceNo = SequenceNumber.Random();

            initialSequenceNumber    = initialSequenceNo;
            handshake.InitialSeqNo   = initialSequenceNo;
            handshake.PacketSize     = datagramSize;
            handshake.SocketID       = mySocketID;
            handshake.MaxFlowWndSize = flowWindowSize;
            //cd 2018-08-28
            handshake.SetPeerIP(this.endPoint.GetLocalAddress().Address.ToString());
            handshake.SetSession(this);
            FlashLogger.Info("Sending " + handshake);
            endPoint.DoSend(handshake);
            connectNum++;
        }
 /// <summary>
 /// handshake for connect
 /// </summary>
 protected void sendHandShake()
 {
     try
     {
         ConnectionHandshake handshake = new ConnectionHandshake();
         handshake.setConnectionType(ConnectionHandshake.CONNECTION_TYPE_REGULAR);
         handshake.setSocketType(ConnectionHandshake.SOCKET_TYPE_DGRAM);
         long initialSequenceNo = SequenceNumber.random();
         setInitialSequenceNumber(initialSequenceNo);
         handshake.setInitialSeqNo(initialSequenceNo);
         handshake.setPacketSize(getDatagramSize());
         handshake.setSocketID(mySocketID);
         handshake.setMaxFlowWndSize(flowWindowSize);
         handshake.setSession(this);
         Log.Write(this.ToString(), "Sending " + handshake);
         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;
            }
        }
Exemplo 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;
            }
        }
Exemplo n.º 9
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;
            }
        }
Exemplo n.º 10
0
 protected virtual void OnConnectionHandshake(ConnectionHandshakeArgs e)
 {
     ConnectionHandshake?.Invoke(this, e);
 }
Exemplo n.º 11
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;
            }
        }
Exemplo n.º 12
0
        public static void HandlePacket(byte[] Data, byte Module, PacketType Type, UInt32 Unk01, TORGameClient Client)
        {
            // TODO: Check for packet drop

            IPacket     iPacket = null;
            ClientState State   = Client.State;

            switch (State)
            {
            case ClientState.CONNECTING:
                switch (Type)
                {
                case PacketType.ConnectionHandshake:
                    iPacket = new ConnectionHandshake();
                    break;

                default:
                    Log.Write(LogLevel.Warning, "Received Unknown Packet [{0:X}] on State '{1}'\n{2}", Type, State.ToString(), Data.ToHEX());
                    break;
                }
                break;

            case ClientState.CONNECTED:
                switch (Type)
                {
                case PacketType.ObjectReply:
                    iPacket = new ObjectReply();
                    break;

                case PacketType.Ping:
                    iPacket = new Ping();
                    break;

                default:
                    Log.Write(LogLevel.Warning, "Received Unknown Packet [{0:X}] on State '{1}'\n{2}", Type, State.ToString(), Data.ToHEX());
                    break;
                }
                break;

            case ClientState.AUTHED:
                switch (Type)
                {
                case PacketType.CreateCharacterRequest:
                    Log.Write(LogLevel.Warning, "\n{0}", Data.ToHEX());
                    iPacket = new CreateCharacterRequest();
                    break;

                case PacketType.ObjectReply:
                    iPacket = new ObjectReply();
                    break;

                case PacketType.ServiceRequest:
                    iPacket = new ServiceRequest();
                    break;

                case PacketType.RequestClose:
                    iPacket = new RequestClose();
                    break;

                case PacketType.TimeRequesterRequest:
                    iPacket = new TimeRequesterRequest();
                    break;

                case PacketType.HackNotifyData:
                    iPacket = new HackNotifyData();
                    break;

                case PacketType.CMsgC26464A9:
                    iPacket = new CMsgC26464A9();
                    break;

                case PacketType.CMsg7CB9A193:
                    iPacket = new CMsg7CB9A193();
                    break;

                case PacketType.ModulesList:
                    iPacket = new ModulesList();
                    break;

                case PacketType.AreaModulesList:
                    iPacket = new AreaModulesList();
                    break;

                case PacketType.SetTrackingInfo:
                    iPacket = new SetTrackingInfo();
                    break;

                case PacketType.CharacterListRequest:
                    iPacket = new CharacterListRequest();
                    break;

                case PacketType.SelectCharacterRequest:
                    iPacket = new SelectCharacterRequest();
                    break;

                case PacketType.Ping:
                    iPacket = new Ping();
                    break;

                default:
                    Log.Write(LogLevel.Warning, "Received Unknown Packet [{0:X}] on State '{1}'\n{2}", Type, State.ToString(), Data.ToHEX());
                    break;
                }
                break;

            case ClientState.IN_GAME:
                switch (Type)
                {
                case PacketType.RequestClose:
                    iPacket = new RequestClose();
                    break;

                case PacketType.SetTrackingInfo:
                    iPacket = new SetTrackingInfo();
                    break;

                case PacketType.Ping:
                    iPacket = new Ping();
                    break;

                // TODO: Implement Game Packets
                default:
                    Log.Write(LogLevel.Warning, "Received Unknown Packet [{0:X}] on State '{1}'\n{2}", Type, State.ToString(), Data.ToHEX());
                    break;
                }
                break;
            }

            if (iPacket != null)
            {
                iPacket.SetClient(Client);
                iPacket.SetBuffers(Data);
                //Log.Write(LogLevel.EDebug, "{0}", Data.ToHEX());
                iPacket.Read();
                iPacket.Run();
            }
        }
Exemplo n.º 13
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;
            }
        }