コード例 #1
0
        public static void SendPacketsSafely(P2PPackage data, bool IncludeSelf = true)
        {
            var ms = new MemoryStream();

            new BinaryFormatter().Serialize(ms, data);
            SendPacketsToAll(ms.GetBuffer(), EP2PSend.k_EP2PSendReliable, IncludeSelf);
        }
コード例 #2
0
        public static void SendPackets(P2PPackage data, EP2PSend send, bool IncludeSelf = true)
        {
            //SendUnreliable – 小包,可以丢失,不需要依次发送,但要快
            //SendUnreliableNoDelay –     跟上面一样,但是不做链接检查,因为这样,它可能被丢失,但是这种方式是最快的传送方式。
            //SendReliable – 可靠的信息,大包,依次收发。
            //SendReliableWithBuffering –     跟上面一样,但是在发送前会缓冲数据,如果你发送大量的小包,它不会那么及时。(可能会延迟200ms)
            var ms = new MemoryStream();

            new BinaryFormatter().Serialize(ms, data);
            SendPacketsToAll(ms.GetBuffer(), send, IncludeSelf);
        }
コード例 #3
0
        // Analyze the P2Ppackage
        public virtual void Analyze(P2PPackage package, CSteamID steamid)
        {
            if (package.type != P2PPackageType.Instantiate)
            {
                if (package.Object_identity != -1)
                {
                    while (instance.OnlineObjects.Count <= package.Object_identity)
                    {
                        instance.OnlineObjects.Add(null);
                    }
                    if (instance.OnlineObjects[package.Object_identity] == null)
                    {
                        var obj = Instantiate(
                            NetworkLobbyManager.instance.SpawnablePrefab[package.ObjectSpawnID]);
                        obj.TargetID = package.Object_identity;
                        obj.host     = steamid;
                        obj.Init();
                    }
                }
            }
            switch (package.type)
            {
            case P2PPackageType.SyncTransform:
                instance.OnlineObjects[package.Object_identity].GetComponent <SynTransform>()
                .Receive(Lib.To_Vector3((Lib.M_Vector3)package.value));
                break;

            case P2PPackageType.SeverClose:
                Debug.Log("服务器关闭");
                DestroyConnections(true);
                break;

            case P2PPackageType.Instantiate:
                var info = (SpawnInfo)package.value;
                var obj  = Instantiate(
                    NetworkLobbyManager.instance.SpawnablePrefab[info.SpawnID],
                    Lib.To_Vector3(info.pos), Lib.To_Quaternion(info.rot));
                obj.TargetID = package.Object_identity;
                obj.host     = steamid;
                obj.Init();
                break;

            case P2PPackageType.Broadcast:
                if (NetworkLobbyManager.instance.lobby.m_SteamID == 0)
                {
                    break;
                }
                //Debug.Log("从 "+SteamFriends.GetFriendPersonaName(steamid)+" 收到应答数据包");
                if (!PlayerList.Contains(steamid))
                {
                    PlayerList.Add(steamid);
                    CreateConnection(steamid);
                    //PlayerNeedToConnect=new bool[PlayerList.Count];
                    //PlayerNeedToConnect[PlayerList.IndexOf(SelfID)] = true;
                }
                if (checking_joined)
                {
                    var index = PlayerList.IndexOf(steamid);
                    if (!player_need_to_connect[index])
                    {
                        player_need_to_connect[index] = true;
                    }
                }
                break;

            case P2PPackageType.SendMessage:
                var rpc_info = (SendMessageInfo)package.value;
                if (rpc_info.have_value)
                {
                    instance.OnlineObjects[package.Object_identity].SendMessage(rpc_info.FuncName, rpc_info.Values);
                }
                else
                {
                    instance.OnlineObjects[package.Object_identity].SendMessage(rpc_info.FuncName);
                }
                break;

            case P2PPackageType.Sync:
                var sync = instance.OnlineObjects[package.Object_identity].sync;
                if (sync != null)
                {
                    sync.OnSync((object[])package.value);
                }
                break;

            case P2PPackageType.DeleteObject:
                if (instance.OnlineObjects[package.Object_identity] != null)
                {
                    Destroy(instance.OnlineObjects[package.Object_identity].gameObject);
                }
                break;

            case P2PPackageType.LoadScene:
                SceneManager.LoadScene((string)package.value);
                break;

            case P2PPackageType.RPC:
                var callInfo = (RPCInfo)package.value;
                instance.OnlineObjects[package.Object_identity].rpc.Call(callInfo.FuncIndex, callInfo.Values);
                break;

            case P2PPackageType.Custom:
                var CustomPacket = (CustomPacket)package.value;
                instance.events.CustomPacket.Invoke(CustomPacket.tag, CustomPacket.value);
                break;

            case P2PPackageType.AnimatorState:
                instance.OnlineObjects[package.Object_identity].anim.SetAnimState((SteamAnimator.MyAniationMessage)package.value);
                break;

            case P2PPackageType.AnimatorParamter:
                instance.OnlineObjects[package.Object_identity].anim.SetParamter((SteamAnimator.MyAniationParamterMessage[])package.value);
                break;

            case P2PPackageType.LeftLobby:
                if (PlayerList.Contains(steamid))
                {
                    PlayerList.Remove(steamid);
                }
                foreach (var t in instance.OnlineObjects)
                {
                    if (!t.DestoryOnQuit)
                    {
                        continue;
                    }
                    if (t.host == steamid)
                    {
                        Destroy(t.gameObject);
                    }
                }
                break;

            default:
            {
                throw new ArgumentOutOfRangeException();
            }
            }
        }