Пример #1
0
        public GameServer(IGameAdapter gameAdapter)
        {
            _gameAdapter   = gameAdapter;
            _stopWatch     = new Stopwatch();
            _networkServer = new NetworkServer(this, HandshakeState.Instance);

            WorldManager  = new WorldManager(_gameAdapter.BlockRegistry, _gameAdapter.EntityRegistry);
            ClientManager = new ClientManager(this);
        }
Пример #2
0
        /// <summary>
        /// Register a game adapter
        /// </summary>
        /// <param name="gameAdapter">The game adapter</param>
        /// <remarks>Once <see cref="Lock"/>ed, the game adapter won't update the <see cref="CurrentVersion"/> property</remarks>
        public static void Register(IGameAdapter gameAdapter)
        {
            var version = gameAdapter.Version;

            Logger.Info("Using Minecraft version {0}", version);

            Versions[version.Protocol] = gameAdapter;

            if (!_locked && version >= CurrentVersion)
            {
                CurrentVersion = version;
            }
        }
Пример #3
0
        private void RequestSession(string ip = null)
        {
            var serverHash = Cryptography.GetServerHash(
                EmptyArrays.EmptyBytes,
                _sharedSecret,
                Cryptography.PublicKey
                );

            var urlBuilder = new UriBuilder(HasJoinedUrl);

            var queryString = new NameValueCollection
            {
                ["username"] = _username,
                ["serverId"] = serverHash
            };

            if (ip != null)
            {
                queryString["ip"] = ip;
            }
            urlBuilder.Query = string.Join("&", queryString.AllKeys.Select(key => key + '=' + queryString.Get(key)));

            Logger.Debug("Requesting player profile for user \"{0}\" (query: {1})", _username, urlBuilder.Query);

            HttpClient
            .GetAsync(urlBuilder.Uri)
            .ContinueWith(requestTask =>
            {
                if (requestTask.IsFaulted)
                {
                    throw requestTask.Exception;
                }

                var responseMessage = requestTask.Result;
                if (responseMessage.StatusCode != HttpStatusCode.OK)
                {
                    NetworkClient.Disconnect(new TextComponentTranslate("multiplayer.disconnect.authservers_down")
                    {
                        Color = TextColor.Red
                    });
                    return;
                }

                var httpContent = responseMessage.Content;
                httpContent
                .ReadAsStringAsync()
                .ContinueWith(readTask =>
                {
                    _profile = JsonConvert.DeserializeObject <GameProfile>(readTask.Result);

                    if (!_username.Equals(_profile.Name))
                    {
                        NetworkClient.Disconnect(
                            new TextComponentTranslate("multiplayer.disconnect.unverified_username")
                        {
                            Color = TextColor.Red
                        });
                        return;
                    }

                    Logger.Info("UUID of player {0} is {1}", _profile.Name, _profile.Id);

                    NetworkClient
                    .Send(new MessageClientEnableCompression.Message(CompressionThreshold))
                    .ContinueWith(CheckErrors)
                    .ContinueWith(t1 =>
                    {
                        if (NetworkClient.Closed)             // encryption gone wrong
                        {
                            return;
                        }

                        NetworkClient.EnableCompression(CompressionThreshold);

                        NetworkClient
                        .Send(new MessageClientLoginResponse.Message(
                                  _profile.Id.ToString(),
                                  _profile.Name
                                  ))
                        .ContinueWith(CheckErrors)
                        .ContinueWith(t2 =>
                        {
                            if (NetworkClient.Closed)                 // compression gone wrong
                            {
                                return;
                            }

                            _adapter            = GameAdapters.Resolve(NetworkClient.Version.Protocol);
                            NetworkClient.State = _adapter.NetworkState;
                            NetworkClient.AddTask(FinalizeLogin);
                        });
                    });
                }).ContinueWith(CheckErrors);
            }).ContinueWith(CheckErrors);
        }