コード例 #1
0
        /// <summary>
        /// Handles the player kicked message
        /// </summary>
        /// <param name="inMsg">The message to decode</param>
        private void HandlePlayerKicked(NetIncomingMessage inMsg)
        {
            byte   playerId = inMsg.ReadByte();
            string reason   = inMsg.ReadString();

            if (playerId != myPlayerId)
            {
                OnPlayerKicked?.Invoke(this, myKnownPlayers[playerId], reason);
                OnPlayerLeft?.Invoke(this, myKnownPlayers[playerId], reason);

                myKnownPlayers[playerId] = null;
            }
            else
            {
                myConnectedServer = null;

                OnKicked?.Invoke(this, reason);
            }
        }
コード例 #2
0
 public void KickAnimationEvent()
 {
     OnKicked?.Invoke();
 }
コード例 #3
0
        /// <summary>
        /// Invoked when the network peer has received a message
        /// </summary>
        /// <param name="state">The object that invoked this (NetPeer)</param>
        private void MessageReceived(object state)
        {
            // Get the incoming message
            NetIncomingMessage inMsg = ((NetPeer)state).ReadMessage();

            // We don't want the server to crash on one bad packet
            try
            {
                // Determine the message type to correctly handle it
                switch (inMsg.MessageType)
                {
                // Handle when a client's status has changed
                case NetIncomingMessageType.StatusChanged:
                    // Gets the status and reason
                    NetConnectionStatus status = (NetConnectionStatus)inMsg.ReadByte();
                    string reason = inMsg.ReadString();

                    // Depending on the status, we handle players joining or leaving
                    switch (status)
                    {
                    case NetConnectionStatus.Disconnected:

                        // If the reason the is shutdown message, we're good
                        if (reason.Equals(NetSettings.DEFAULT_SERVER_SHUTDOWN_MESSAGE))
                        {
                            OnDisconnected?.Invoke(this, EventArgs.Empty);
                        }
                        // Otherwise if the reason is that \/ , then we timed out
                        else if (reason.Equals("Failed to establish connection - no response from remote host", StringComparison.InvariantCultureIgnoreCase))
                        {
                            OnConnectionTimedOut?.Invoke(this, EventArgs.Empty);

                            OnDisconnected?.Invoke(this, EventArgs.Empty);
                        }
                        else if (reason.StartsWith("You have been kicked:"))
                        {
                            OnKicked?.Invoke(this, reason);

                            OnDisconnected?.Invoke(this, EventArgs.Empty);
                        }
                        // Otherwise the connection failed for some other reason
                        else
                        {
                            OnDisconnected?.Invoke(this, EventArgs.Empty);
                        }

                        if (isApproving)
                        {
                            OnConnectionFailed?.Invoke(this, reason);
                        }

                        isApproving = false;

                        // Clear local state and forget connected server tag
                        myLocalState.Clear();
                        myConnectedServer = null;
                        isReady           = false;
                        isHost            = false;
                        myKnownPlayers.Clear();
                        myLocalState.Clear();
                        myHand.Clear();

                        break;

                    case NetConnectionStatus.RespondedAwaitingApproval:
                        isApproving = true;
                        break;

                    // We connected
                    case NetConnectionStatus.Connected:
                        isApproving = false;
                        // invoked the onConnected event
                        OnConnected?.Invoke(this, EventArgs.Empty);
                        break;
                    }

                    break;

                // Handle when the server has received a discovery request
                case NetIncomingMessageType.DiscoveryResponse:
                    // Read the server tag from the packet
                    ServerTag serverTag = ServerTag.ReadFromPacket(inMsg);

                    // Notify that we discovered a server
                    OnServerDiscovered?.Invoke(this, serverTag);

                    break;

                // Handles when the server has received data
                case NetIncomingMessageType.Data:
                    HandleMessage(inMsg);
                    break;
                }
            }
            // An exception has occured parsing the packet
            catch (Exception e)
            {
                //Log the exception
                Logger.Write(e);
            }
        }