Пример #1
0
 private void ReceivePacketHandler(Packet packet)
 {
     if (packet.type == PacketType.Message)
     {
         if (LSState == LockStepState.None)
         {
             LSState = LockStepState.Prepare;
         }
     }
     else if (packet.type == PacketType.LockStep)
     {
         PlayersInputBuffer pib = new PlayersInputBuffer();
         pib.Parse(packet.data);
         RUShortInt CFrame = LSFrame + RUShortInt.Reverse(NetConfig.PDBL);
         int        index  = pib.buffer.FindIndex((PlayersInput each) => each.frame == CFrame);
         if (index > 0)
         {
             pib.buffer.RemoveRange(0, index);
         }
         SLSBuffer.Merge(pib);
     }
 }
Пример #2
0
    private void OnGUI()
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label("ID");
        ID = uint.Parse(GUILayout.TextField(ID.ToString()));
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("server ip");
        byte[] bip = serverAddress.GetAddressBytes();
        for (int i = 0; i < bip.Length; i++)
        {
            bip[i] = System.Convert.ToByte(GUILayout.TextField(bip[i].ToString()));
        }
        serverAddress = new IPAddress(bip);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("server port");
        serverPort = int.Parse(GUILayout.TextField(serverPort.ToString()));
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("client port");
        clientPort = int.Parse(GUILayout.TextField(clientPort.ToString()));
        GUILayout.EndHorizontal();

        GUI.enabled = socket == null || socket.netState == NetState.None;
        if (GUILayout.Button("Connect", GUILayout.Width(200), GUILayout.Width(80)))
        {
            LSState        = LockStepState.None;
            LSTick         = 0;
            LSFrame        = 0;
            LSFrameAchieve = true;
            LSAccFrame     = 0;
            SLSBuffer      = new PlayersInputBuffer();
            socket         = new AppSocket(ID);
            socket.packetHandler[PacketType.Message]  = ReceivePacketHandler;
            socket.packetHandler[PacketType.LockStep] = ReceivePacketHandler;
            socket.Connect(serverAddress, serverPort, clientPort);
        }
        GUI.enabled = true;

        GUI.color = socket != null && socket.netState == NetState.Connected ? Color.green : Color.white;
        GUILayout.Label("状态: " + (socket != null ? socket.netState : NetState.None));
        GUI.color = Color.white;
        GUILayout.Label("延迟: " + (socket != null ? ((int)(socket.latency * 1000)).ToString() : ""));
        GUILayout.Label("速度: " + gameSpeed);

        if (socket != null)
        {
            // 已发送包
            Packet[] sends = socket.sends.ToArray();
            for (int i = 0; i < sends.Length; i++)
            {
                GUI.color = sends[i].arrived ? Color.green : Color.white;
                GUILayout.Label(sends[i].sequence + " " + sends[i].type);
                GUI.color = Color.white;
            }
            // 已接收包
            GUILayout.BeginArea(new Rect(300, 20, 200, 600));
            Packet[] receives = socket.receives.ToArray();
            for (int i = 0; i < receives.Length; i++)
            {
                GUILayout.Label(receives[i].sequence + " " + receives[i].type);
            }
            GUILayout.EndArea();
        }
    }