Beispiel #1
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);
        }