Exemple #1
0
        private async Task ConnectMPS()
        {
            _utcConnectionState = DateTime.UtcNow;
            _mpsState           = MPSConnectionState.NO_CONNECTION;

            try
            {
                _mpsChannel = await _bootstrap.ConnectAsync(new IPEndPoint(Utils.GetIp(Program.Settings.MPS.Ip), Program.Settings.MPS.Port));
            }
            catch (Exception)
            {
                Logger.Error($"Failed to connect to MPS");
                TimeLostConnection = DateTime.UtcNow;
                return;
            }

            if (!_mpsChannel.Active)
            {
                return;
            }

            _mpsState = MPSConnectionState.CONNECTED;

            // Send hello
            await _mpsChannel.WriteAndFlushAsync(new RT_MSG_CLIENT_HELLO()
            {
                Parameters = new ushort[]
                {
                    2,
                    0x6e,
                    0x6d,
                    1,
                    1
                }
            });

            _mpsState = MPSConnectionState.HELLO;
        }
Exemple #2
0
        private void ProcessMediusMessage(BaseMediusMessage message, IChannel clientChannel)
        {
            if (message == null)
            {
                return;
            }

            switch (message)
            {
            //
            case MediusServerSetAttributesResponse setAttributesResponse:
            {
                if (_mpsState != MPSConnectionState.SET_ATTRIBUTES)
                {
                    throw new Exception($"Unexpected MediusServerSetAttributesResponse from server. {setAttributesResponse}");
                }

                if (setAttributesResponse.Confirmation == MGCL_ERROR_CODE.MGCL_SUCCESS)
                {
                    _mpsState = MPSConnectionState.AUTHENTICATED;
                }
                else
                {
                    _mpsState = MPSConnectionState.FAILED;
                }
                break;
            }

            //
            case MediusServerCreateGameWithAttributesRequest createGameWithAttributesRequest:
            {
                World world = new World(createGameWithAttributesRequest.MaxClients);
                _worlds.Add(world);

                Enqueue(new MediusServerCreateGameWithAttributesResponse()
                    {
                        MessageID    = createGameWithAttributesRequest.MessageID,
                        Confirmation = MGCL_ERROR_CODE.MGCL_SUCCESS,
                        WorldID      = world.WorldId
                    });
                break;
            }

            case MediusServerJoinGameRequest joinGameRequest:
            {
                var world = _worlds.FirstOrDefault(x => x.WorldId == joinGameRequest.ConnectInfo.WorldID);
                if (world == null)
                {
                    Enqueue(new MediusServerJoinGameResponse()
                        {
                            MessageID    = joinGameRequest.MessageID,
                            Confirmation = MGCL_ERROR_CODE.MGCL_INVALID_ARG,
                        });
                }
                else
                {
                    Enqueue(world.OnJoinGameRequest(joinGameRequest));
                }
                break;
            }

            case MediusServerEndGameRequest endGameRequest:
            {
                _worlds.FirstOrDefault(x => x.WorldId == endGameRequest.WorldID)?.OnEndGameRequest(endGameRequest);

                break;
            }

            default:
            {
                Logger.Warn($"UNHANDLED MESSAGE: {message}");

                break;
            }
            }
        }
Exemple #3
0
        private async Task ProcessMessage(BaseScertMessage message, IChannel serverChannel)
        {
            //
            switch (message)
            {
            // Authentication
            case RT_MSG_SERVER_HELLO serverHello:
            {
                if (_mpsState != MPSConnectionState.HELLO)
                {
                    throw new Exception($"Unexpected RT_MSG_SERVER_HELLO from server. {serverHello}");
                }

                // Send public key
                Enqueue(new RT_MSG_CLIENT_CRYPTKEY_PUBLIC()
                    {
                        Key = _clientKey.N.ToByteArrayUnsigned().Reverse().ToArray()
                    });

                _mpsState = MPSConnectionState.HANDSHAKE;
                break;
            }

            case RT_MSG_SERVER_CRYPTKEY_PEER serverCryptKeyPeer:
            {
                if (_mpsState != MPSConnectionState.HANDSHAKE)
                {
                    throw new Exception($"Unexpected RT_MSG_SERVER_CRYPTKEY_PEER from server. {serverCryptKeyPeer}");
                }

                await _mpsChannel.WriteAndFlushAsync(new RT_MSG_CLIENT_CONNECT_TCP()
                    {
                        AppId = Program.Settings.ApplicationId
                    });

                _mpsState = MPSConnectionState.CONNECT_TCP;
                break;
            }

            case RT_MSG_SERVER_CONNECT_ACCEPT_TCP serverConnectAcceptTcp:
            {
                if (_mpsState != MPSConnectionState.CONNECT_TCP)
                {
                    throw new Exception($"Unexpected RT_MSG_SERVER_CONNECT_ACCEPT_TCP from server. {serverConnectAcceptTcp}");
                }

                // Send attributes
                await _mpsChannel.WriteAndFlushAsync(new RT_MSG_CLIENT_APP_TOSERVER()
                    {
                        Message = new MediusServerSetAttributesRequest()
                        {
                            MessageID           = new MessageId(),
                            ListenServerAddress = new NetAddress()
                            {
                                Address = Program.SERVER_IP.ToString(),
                                Port    = (uint)Program.TcpServer.Port
                            }
                        }
                    });

                _mpsState = MPSConnectionState.SET_ATTRIBUTES;
                break;
            }

            //
            case RT_MSG_SERVER_ECHO serverEcho:
            {
                Enqueue(serverEcho);
                break;
            }

            case RT_MSG_CLIENT_ECHO clientEcho:
            {
                Enqueue(new RT_MSG_CLIENT_ECHO()
                    {
                        Value = clientEcho.Value
                    });
                break;
            }

            case RT_MSG_SERVER_APP serverApp:
            {
                ProcessMediusMessage(serverApp.Message, serverChannel);
                break;
            }

            case RT_MSG_SERVER_FORCED_DISCONNECT serverForcedDisconnect:
            case RT_MSG_CLIENT_DISCONNECT_WITH_REASON clientDisconnectWithReason:
            {
                await serverChannel.CloseAsync();

                _mpsState = MPSConnectionState.NO_CONNECTION;
                break;
            }

            default:
            {
                Logger.Warn($"UNHANDLED MESSAGE: {message}");

                break;
            }
            }

            return;
        }