コード例 #1
0
ファイル: NetworkClient.cs プロジェクト: simonwittber/Mirror
 internal void RegisterSystemHandlers(bool localClient)
 {
     ClientScene.RegisterSystemHandlers(this, localClient);
 }
コード例 #2
0
        internal virtual void Update()
        {
            if (m_ClientId == -1)
            {
                return;
            }

            // don't do anything if we aren't fully connected
            // -> we don't check Client.Connected because then we wouldn't
            //    process the last disconnect message.
            if (connectState != ConnectState.Connecting &&
                connectState != ConnectState.Connected)
            {
                return;
            }

            // pause message handling while a scene load is in progress
            //
            // problem:
            //   if we handle packets (calling the msgDelegates) while a
            //   scene load is in progress, then all the handled data and state
            //   will be lost as soon as the scene load is finished, causing
            //   state bugs.
            //
            // solution:
            //   don't handle messages until scene load is finished. the
            //   transport layer will queue it automatically.
            if (pauseMessageHandling)
            {
                Debug.Log("NetworkClient.Update paused during scene load...");
                return;
            }

            if (connectState == ConnectState.Connected)
            {
                NetworkTime.UpdateClient(this);
            }

            // any new message?
            // -> calling it once per frame is okay, but really why not just
            //    process all messages and make it empty..
            TransportEvent transportEvent;

            byte[] data;
            while (Transport.layer.ClientGetNextMessage(out transportEvent, out data))
            {
                switch (transportEvent)
                {
                case TransportEvent.Connected:
                    //Debug.Log("NetworkClient loop: Connected");

                    if (m_Connection != null)
                    {
                        // reset network time stats
                        NetworkTime.Reset();

                        // the handler may want to send messages to the client
                        // thus we should set the connected state before calling the handler
                        connectState = ConnectState.Connected;
                        m_Connection.InvokeHandlerNoData((short)MsgType.Connect);
                    }
                    else
                    {
                        Debug.LogError("Skipped Connect message handling because m_Connection is null.");
                    }

                    break;

                case TransportEvent.Data:
                    //Debug.Log("NetworkClient loop: Data: " + BitConverter.ToString(data));

                    if (m_Connection != null)
                    {
                        m_Connection.TransportReceive(data);
                    }
                    else
                    {
                        Debug.LogError("Skipped Data message handling because m_Connection is null.");
                    }

                    break;

                case TransportEvent.Disconnected:
                    //Debug.Log("NetworkClient loop: Disconnected");
                    connectState = ConnectState.Disconnected;

                    //GenerateDisconnectError(error); TODO which one?
                    ClientScene.HandleClientDisconnect(m_Connection);
                    if (m_Connection != null)
                    {
                        m_Connection.InvokeHandlerNoData((short)MsgType.Disconnect);
                    }
                    break;
                }
            }
        }
コード例 #3
0
        internal void OnStartServer(bool allowNonZeroNetId)
        {
            if (m_IsServer)
            {
                return;
            }
            m_IsServer     = true;
            m_HasAuthority = !m_LocalPlayerAuthority;

            m_Observers           = new List <NetworkConnection>();
            m_ObserverConnections = new HashSet <int>();
            CacheBehaviours();

            // If the instance/net ID is invalid here then this is an object instantiated from a prefab and the server should assign a valid ID
            if (netId.IsEmpty())
            {
                m_NetId = GetNextNetworkId();
            }
            else
            {
                if (!allowNonZeroNetId)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Object has non-zero netId " + netId + " for " + gameObject);
                    }
                    return;
                }
            }

            if (LogFilter.logDev)
            {
                Debug.Log("OnStartServer " + gameObject + " GUID:" + netId);
            }
            NetworkServer.SetLocalObjectOnServer(netId, gameObject);

            for (int i = 0; i < m_NetworkBehaviours.Length; i++)
            {
                NetworkBehaviour comp = m_NetworkBehaviours[i];
                try
                {
                    comp.OnStartServer();
                }
                catch (Exception e)
                {
                    Debug.LogError("Exception in OnStartServer:" + e.Message + " " + e.StackTrace);
                }
            }

            if (NetworkClient.active && NetworkServer.localClientActive)
            {
                // there will be no spawn message, so start the client here too
                ClientScene.SetLocalObject(netId, gameObject);
                OnStartClient();
            }

            if (m_HasAuthority)
            {
                OnStartAuthority();
            }
        }
コード例 #4
0
        void OnGUI()
        {
            if (!showGUI)
            {
                return;
            }

            GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999));
            if (!NetworkClient.isConnected && !NetworkServer.active)
            {
                if (!NetworkClient.active)
                {
                    // LAN Host
                    if (Application.platform != RuntimePlatform.WebGLPlayer)
                    {
                        if (GUILayout.Button("LAN Host"))
                        {
                            manager.StartHost();
                        }
                    }

                    // LAN Client + IP
                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("LAN Client"))
                    {
                        manager.StartClient();
                    }
                    manager.networkAddress = GUILayout.TextField(manager.networkAddress);
                    GUILayout.EndHorizontal();

                    // LAN Server Only
                    if (Application.platform == RuntimePlatform.WebGLPlayer)
                    {
                        // cant be a server in webgl build
                        GUILayout.Box("(  WebGL cannot be server  )");
                    }
                    else
                    {
                        if (GUILayout.Button("LAN Server Only"))
                        {
                            manager.StartServer();
                        }
                    }
                }
                else
                {
                    // Connecting
                    GUILayout.Label("Connecting to " + manager.networkAddress + "..");
                    if (GUILayout.Button("Cancel Connection Attempt"))
                    {
                        manager.StopClient();
                    }
                }
            }
            else
            {
                // server / client status message
                if (NetworkServer.active)
                {
                    GUILayout.Label("Server: active. Transport: " + Transport.activeTransport);
                }
                if (NetworkClient.isConnected)
                {
                    GUILayout.Label("Client: address=" + manager.networkAddress);
                }
            }

            // client ready
            if (NetworkClient.isConnected && !ClientScene.ready)
            {
                if (GUILayout.Button("Client Ready"))
                {
                    ClientScene.Ready(NetworkClient.connection);

                    if (ClientScene.localPlayer == null)
                    {
                        ClientScene.AddPlayer();
                    }
                }
            }

            // stop
            if (NetworkServer.active || NetworkClient.isConnected)
            {
                if (GUILayout.Button("Stop"))
                {
                    manager.StopHost();
                }
            }

            GUILayout.EndArea();
        }
コード例 #5
0
        void OnGUI()
        {
            if (!showGUI)
            {
                return;
            }

            GUI.contentColor    = Color.white;
            GUI.backgroundColor = Color.black;

            int  HEIGHT = Screen.height;
            int  WIDTH  = Screen.width;
            Rect areaRect;
            {
                int w = WIDTH / 5;
                int h = (int)(HEIGHT * 0.9f);
                int x = (WIDTH - w) / 2;
                int y = (HEIGHT - h) / 2;
                areaRect = new Rect(x, y, w, h);
            }
            int MIN                 = 18;
            var newLineHeigth       = GUILayout.Height(Mathf.Max(HEIGHT / 10, MIN));
            var newLineHeigthHalved = GUILayout.Height(Mathf.Max(HEIGHT / 20, MIN));

            GUILayout.BeginArea(areaRect);
            if (!NetworkClient.isConnected && !NetworkServer.active)
            {
                if (!NetworkClient.active)
                {
                    // // LAN Host
                    // if (Application.platform != RuntimePlatform.WebGLPlayer)
                    // {
                    //     if (GUILayout.Button("LAN Host"))
                    //     {
                    //         manager.StartHost();
                    //     }
                    // }

                    // LAN Client + IP
                    //GUILayout.BeginHorizontal();
                    {
                        const string defaulthost       = "localhost";
                        const string networkAddressKey = "manager.networkAddress";
                        manager.networkAddress = GUILayout.TextField(PlayerPrefs.GetString("manager.networkAddress", defaulthost), newLineHeigthHalved);
                        if (manager.networkAddress.Length == 0)
                        {
                            manager.networkAddress = defaulthost;
                        }
                        PlayerPrefs.SetString(networkAddressKey, manager.networkAddress);

                        if (GUILayout.Button("START", newLineHeigth))//if (GUILayout.Button("LAN Client"))
                        {
                            manager.StartClient();
                        }
                    }
                    //GUILayout.EndHorizontal();

                    // LAN Server Only
                    if (Application.platform == RuntimePlatform.WebGLPlayer)
                    {
                        // cant be a server in webgl build
                        GUILayout.Box("(  WebGL cannot be server  )");
                    }
                    else
                    {
                        GUILayout.FlexibleSpace();
                        if (GUILayout.Button("Start Server", newLineHeigthHalved))
                        {
                            manager.StartServer();
                        }
                        // if (GUILayout.Button("LAN Server Only")) manager.StartServer();
                    }
                }
                else
                {
                    // Connecting
                    GUILayout.Label("Connecting to " + manager.networkAddress + "..");
                    if (GUILayout.Button("Cancel Connection Attempt", newLineHeigth))
                    {
                        manager.StopClient();
                    }
                }
            }
            else
            {
                // server / client status message
                if (NetworkServer.active)
                {
                    GUILayout.Label("Server: active. Transport: " + Transport.activeTransport);
                }
                if (NetworkClient.isConnected)
                {
                    GUILayout.Label("Client: address=" + manager.networkAddress);
                }
            }

            // client ready
            if (NetworkClient.isConnected && !ClientScene.ready)
            {
                if (GUILayout.Button("Client Ready", newLineHeigth))
                {
                    ClientScene.Ready(NetworkClient.connection);

                    if (ClientScene.localPlayer == null)
                    {
                        ClientScene.AddPlayer();
                    }
                }
            }

            GUILayout.EndArea();

            // stop
            //if (NetworkServer.active || NetworkClient.isConnected)
            if (NetworkClient.isConnected)
            {
                if (GUILayout.Button("Exit", newLineHeigthHalved, GUILayout.Width(WIDTH / 10)))
                {
                    manager.StopHost();
                }
            }
            if (NetworkServer.active)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(" GET MY IP: ", newLineHeigthHalved);
                GUILayout.TextField("https://www.whatismybrowser.com/detect/what-is-my-local-ip-address", newLineHeigthHalved);
                // GUILayout.Label(" LOCAL IPv4: ",newLineHeigthHalved);
                // GUILayout.Label(_localIPv4,newLineHeigthHalved);
                GUILayout.EndHorizontal();
            }
        }