public override void ClientConnect(string address)
        {
            // test
            Debug.LogError($"{Time.unscaledTime} | ClientConnect");

            if (!ENETInitialized)
            {
                if (InitializeENET())
                {
                    Debug.Log($"Ignorance successfully initialized ENET.");
                    ENETInitialized = true;
                }
                else
                {
                    Debug.LogError($"Ignorance failed to initialize ENET! Cannot continue.");
                    return;
                }
            }

            if (DebugEnabled)
            {
                Debug.Log($"Ignorance: DEBUGGING MODE - ClientConnect({address})");
            }

            if (Channels.Length > 255)
            {
                Debug.LogError($"Ignorance: Too many channels. Channel limit is 255, you have {Channels.Length}. This would probably crash ENET. Aborting connection.");
                return;
            }

            if (CommunicationPort < ushort.MinValue || CommunicationPort > ushort.MaxValue)
            {
                Debug.LogError($"Ignorance: Bad communication port number. You need to set it between port 0 and 65535. Aborting connection.");
                return;
            }

            if (ENETClientHost == null || !ENETClientHost.IsSet)
            {
                ENETClientHost.Create(null, 1, Channels.Length, 0, 0, PacketCache.Length);
            }
            if (DebugEnabled)
            {
                Debug.Log($"Ignorance: DEBUGGING MODE - Created ENET Host object");
            }

            ENETAddress.SetHost(address);
            ENETAddress.Port = (ushort)CommunicationPort;

            ENETPeer = ENETClientHost.Connect(ENETAddress, Channels.Length);
            if (CustomTimeoutLimit)
            {
                ENETPeer.Timeout(Library.throttleScale, CustomTimeoutBaseTicks, CustomTimeoutBaseTicks * CustomTimeoutMultiplier);
            }
            ClientStarted = true;

            if (DebugEnabled)
            {
                Debug.Log($"Ignorance: DEBUGGING MODE - Client has been started!");
            }
        }
Exemple #2
0
        public override void ClientDisconnect()
        {
            if (DebugEnabled)
            {
                Debug.Log($"[DEBUGGING MODE] Ignorance: ClientDisconnect()");
            }

            if (ServerStarted)
            {
                Debug.LogWarning("MIRROR BUG: ClientDisconnect called even when we're in HostClient/Dedicated Server mode");
                return;
            }

            if (!IsValid(ENETClientHost))
            {
                return;
            }
            if (ENETPeer.IsSet)
            {
                ENETPeer.DisconnectNow(0);
            }

            // Flush and free resources.
            if (IsValid(ENETClientHost))
            {
                ENETClientHost.Flush();
                ENETClientHost.Dispose();
            }
        }
        // client pump
        private bool ProcessClientMessages()
        {
            // Never do anything when ENET is not initialized
            if (!ENETInitialized)
            {
                return(false);
            }

            // Never do anything when ENET is in a different mode
            if (!IsValid(ENETClientHost) || ENETPeer.State == PeerState.Uninitialized || !ClientStarted)
            {
                return(false);
            }

            bool clientWasPolled = false;

            // Only process messages if the client is valid.
            while (!clientWasPolled)
            {
                if (!IsValid(ENETClientHost))
                {
                    return(false);
                }

                if (ENETClientHost.CheckEvents(out Event networkEvent) <= 0)
                {
                    if (ENETClientHost.Service(0, out networkEvent) <= 0)
                    {
                        break;
                    }
                    clientWasPolled = true;
                }

                switch (networkEvent.Type)
                {
                case EventType.Connect:
                    // Client connected.
                    // Debug.Log("Connect");
                    Debug.LogError($"{Time.unscaledTime} | Client Connected");
                    OnClientConnected.Invoke();
                    break;

                case EventType.Timeout:
                case EventType.Disconnect:
                    // Client disconnected.
                    // Debug.Log("Disconnect");
                    OnClientDisconnected.Invoke();
                    break;

                case EventType.Receive:
                    // Client recieving some data.
                    // Debug.Log("Data");
                    if (networkEvent.Packet.Length > PacketCache.Length)
                    {
                        if (DebugEnabled)
                        {
                            Debug.Log($"Ignorance: Packet too big to fit in buffer. {networkEvent.Packet.Length} packet bytes vs {PacketCache.Length} cache bytes {networkEvent.Peer.ID}.");
                        }
                        networkEvent.Packet.Dispose();
                    }
                    else
                    {
                        // invoke on the client.
                        networkEvent.Packet.CopyTo(PacketCache);
                        int spLength = networkEvent.Packet.Length;
                        networkEvent.Packet.Dispose();

                        OnClientDataReceived.Invoke(new ArraySegment <byte>(PacketCache, 0, spLength));
                    }
                    break;
                }
            }
            // We're done here. Return.
            return(true);
        }