Indiefreaks.Xna.Sessions.Lidgren.LidgrenSession.ProcessClientMessages C# (CSharp) 메소드

ProcessClientMessages() 개인적인 메소드

private ProcessClientMessages ( ) : void
리턴 void
        private void ProcessClientMessages()
        {
            while ((_incomingMessage = LidgrenSessionManager.Client.ReadMessage()) != null)
            {
                switch (_incomingMessage.MessageType)
                {
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                        {
                            Console.WriteLine(_incomingMessage.ReadString());
                            break;
                        }
                    case NetIncomingMessageType.StatusChanged:
                        {
                            var status = (NetConnectionStatus)_incomingMessage.ReadByte();
                            switch (status)
                            {
                                case NetConnectionStatus.Connected:
                                    {
                                        SendAllLocalPlayersToServer();
                                        break;
                                    }
                                case NetConnectionStatus.Disconnected:
                                case NetConnectionStatus.Disconnecting:
                                case NetConnectionStatus.InitiatedConnect:
                                case NetConnectionStatus.None:
                                case NetConnectionStatus.RespondedAwaitingApproval:
                                case NetConnectionStatus.RespondedConnect:
                                default:
                                    {
                                        Console.WriteLine("Client Status changed  to " + status);
                                        break;
                                    }
                            }
                            break;
                        }
                    case NetIncomingMessageType.Data:
                        {
                            var msgType = (LidgrenMessages)_incomingMessage.ReadByte();

                            switch (msgType)
                            {


                                case LidgrenMessages.SendPlayersListToJustConnectedClient:
                                    {
                                        foreach (var localPlayer in LidgrenSessionManager.LocalPlayers.Values)
                                        {
                                            _localPlayers.Add(localPlayer);

                                            OnPlayerJoined(localPlayer);
                                        }

                                        RetrieveRemotePlayersFromServer();

                                        Application.SunBurn.GetManager<ISessionManager>(true).OnSessionCreated();
                                        break;
                                    }
                                case LidgrenMessages.SendNewPlayersToClients:
                                    {
                                        RetrieveNewPlayersFromServer();
                                        break;
                                    }
                                case LidgrenMessages.SendDisconnectedPlayersToClients:
                                    {
                                        RetrieveDisconnectedPlayersFromServer();
                                        break;
                                    }
                                case LidgrenMessages.SendSessionStateChangedToClients:
                                    {
                                        RetrieveSessionStateChangedFromServer();
                                        break;
                                    }
                                case LidgrenMessages.SendCommandsToClients:
                                    {
                                        RetrieveCommandsFromServer();
                                        break;
                                    }
                                case LidgrenMessages.SendSceneEntitiesToClients:
                                    {
                                        RetrieveSceneEntitiesFromServer();
                                        break;
                                    }
                                case LidgrenMessages.ExecuteServerCommandOnClientsDataExchanged:
                                case LidgrenMessages.ExecuteCommandOnServerDataExchanged:
                                    {
                                        ushort commandId = _incomingMessage.ReadUInt16();

                                        if (!Commands.ContainsKey(commandId))
                                            break;

                                        Command command = Commands[commandId];

                                        command.WaitingForServerReply = false;

                                        object networkValue = null;

                                        if (command.NetworkValueType == typeof(bool))
                                            networkValue = _incomingMessage.ReadBoolean();
                                        else if (command.NetworkValueType == typeof(byte))
                                            networkValue = _incomingMessage.ReadByte();
                                        else if (command.NetworkValueType == typeof(byte[]))
                                        {
                                            int tempNumberOfBytes = _incomingMessage.ReadInt32();
                                            networkValue = _incomingMessage.ReadBytes(tempNumberOfBytes);
                                        }
                                        else if (command.NetworkValueType == typeof(char))
                                            command.NetworkValue = Convert.ToChar(_incomingMessage.ReadByte());
                                        else if (command.NetworkValueType == typeof(char[]))
                                        {
                                            int tempNumberOfChars = _incomingMessage.ReadInt32();
                                            for (int i = 0; i < tempNumberOfChars; i++)
                                            {
                                                command.NetworkValue = _incomingMessage.ReadBytes(tempNumberOfChars * 8);
                                            }
                                        }
                                        else if (command.NetworkValueType == typeof(Color))
                                            command.NetworkValue = new Color(_incomingMessage.ReadVector4());
                                        else if (command.NetworkValueType == typeof(double))
                                            networkValue = _incomingMessage.ReadDouble();
                                        else if (command.NetworkValueType == typeof(float))
                                            networkValue = _incomingMessage.ReadSingle();
                                        else if (command.NetworkValueType == typeof(int))
                                            networkValue = _incomingMessage.ReadInt32();
                                        else if (command.NetworkValueType == typeof(long))
                                            networkValue = _incomingMessage.ReadInt64();
                                        else if (command.NetworkValueType == typeof(Matrix))
                                            networkValue = _incomingMessage.ReadMatrix();
                                        else if (command.NetworkValueType == typeof(Quaternion))
                                            networkValue = _incomingMessage.ReadRotation(24);
                                        else if (command.NetworkValueType == typeof(sbyte))
                                            networkValue = _incomingMessage.ReadSByte();
                                        else if (command.NetworkValueType == typeof(short))
                                            networkValue = _incomingMessage.ReadInt16();
                                        else if (command.NetworkValueType == typeof(string))
                                            networkValue = _incomingMessage.ReadString();
                                        else if (command.NetworkValueType == typeof(uint))
                                            networkValue = _incomingMessage.ReadUInt32();
                                        else if (command.NetworkValueType == typeof(ulong))
                                            networkValue = _incomingMessage.ReadInt64();
                                        else if (command.NetworkValueType == typeof(ushort))
                                            networkValue = _incomingMessage.ReadUInt16();
                                        else if (command.NetworkValueType == typeof(Vector2))
                                            networkValue = _incomingMessage.ReadVector2();
                                        else if (command.NetworkValueType == typeof(Vector3))
                                            networkValue = _incomingMessage.ReadVector3();
                                        else if (command.NetworkValueType == typeof(Vector4))
                                            networkValue = _incomingMessage.ReadVector4();
                                        //else
                                        //    throw new CoreException("Not supported NetworkValueType");

                                        //if (networkValue == null)
                                        //    throw new CoreException("No value transfered");

                                        if (command.ApplyServerResult != null)
                                            command.ApplyServerResult(command, networkValue);

                                        break;
                                    }
                                case LidgrenMessages.ExecuteServerCommandOnClientsNoDataExchanged:
                                case LidgrenMessages.ExecuteCommandOnServerNoDataExchanged:
                                    {
                                        ushort commandId = _incomingMessage.ReadUInt16();

                                        Command command = Commands[commandId];

                                        command.WaitingForServerReply = false;

                                        object networkValue = null;

                                        if (command.ApplyServerResult != null)
                                            command.ApplyServerResult(command, networkValue);

                                        break;
                                    }

                                default:
                                    break;
                            }
                            break;
                        }
                    case NetIncomingMessageType.DiscoveryResponse:
                        {
                            var sessionType = (SessionType)_incomingMessage.ReadByte();
                            int currentGamerCount = _incomingMessage.ReadVariableInt32();
                            string hostName = _incomingMessage.SenderEndPoint.Address.ToString();
                            int openPrivateSlots = _incomingMessage.ReadVariableInt32();
                            int openPublicSlots = _incomingMessage.ReadVariableInt32();

                            // TODO: this needs to be fixed, commented out because _incomingMessage.SenderConnection is null on remote discovery
                            var averageRoundtripTime = new TimeSpan(0, 0, 0, 0, 0);//(int) (_incomingMessage.SenderConnection.AverageRoundtripTime*1000));

                            var sessionProperties = new SessionProperties();
                            int sessionPropertiesCount = _incomingMessage.ReadVariableInt32();
                            if (sessionPropertiesCount > 0)
                            {
                                for (int i = 0; i < sessionPropertiesCount; i++)
                                {
                                    sessionProperties[i] = BitConverter.ToInt32(_incomingMessage.ReadBytes(4), 0);
                                }
                            }

                            var sessionFound = new LidgrenAvailableSession(sessionType, currentGamerCount, hostName, openPrivateSlots, openPublicSlots, sessionProperties, averageRoundtripTime);
                            LidgrenSessionsFound.Add(sessionFound);

                            break;
                        }
                    case NetIncomingMessageType.ConnectionApproval:
                    case NetIncomingMessageType.ConnectionLatencyUpdated:
                    case NetIncomingMessageType.DiscoveryRequest:
                    case NetIncomingMessageType.Error:
                    case NetIncomingMessageType.NatIntroductionSuccess:
                    case NetIncomingMessageType.Receipt:
                    case NetIncomingMessageType.UnconnectedData:
                    default:
                        {
                            Console.WriteLine("Client received " + _incomingMessage.MessageType);
                            break;
                        }
                }
            }
        }