Beispiel #1
0
        public Task <bool> AddPlayer(Guid permanent_id, MTNetOutStream <Event> out_stream)
        {
            var t = new Task <bool>(() =>
            {
                try
                {
                    players_server_mapping.Add(permanent_id, new PlayerData
                    {
                        player_stream = out_stream
                    });
                    return(true);

#pragma warning disable CS0168 // Variable is declared but never used
                }
                catch (ArgumentException already_in_container_e)
#pragma warning restore CS0168 // Variable is declared but never used
                {
                    return(false);
                }
            });

            tasks.Enqueue(t);
            return(t);
        }
Beispiel #2
0
        public override async Task ConnectAndLogIn(LogInAttempt request, IServerStreamWriter <Event> responseStream, ServerCallContext context)
        {
            {
                var name = request.Username;
                var pw   = request.Password;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pw))
                {
                    await responseStream.WriteAsync(login_failed);

                    return;
                }

                Guid permanent_id;
                if (!await auth.GetPermanentId(name, pw, out permanent_id))
                {
                    await responseStream.WriteAsync(login_failed);

                    return;
                }

                var out_stream = MTNetOutStream <Event> .Create(context, responseStream);

                if (!await player_store.AddPlayer(permanent_id, out_stream))
                {
                    await responseStream.WriteAsync(login_failed);

                    return;
                }

                {
                    var ev = new Event
                    {
                        LoginResult = new EventLoginResult
                        {
                            Success = true,
                            Token   = permanent_id.ToString()
                        }
                    };
                    ev.LoginResult.Servers.AddRange(server_credentials);
                    await responseStream.WriteAsync(ev);
                }

                for (; ;)
                {
                    try
                    {
                        if (!await out_stream.SendCurrentEvents())
                        {
                            break;
                        }
                        await Task.Delay(100);
                    }
                    catch (InvalidOperationException)
                    {
                        //Happens when cancellation token is set to true
                        break;
                    }
                    catch (RpcException)
                    {
                        break; //This is expected to happen when user logs out
                    } catch (Exception e)
                    {
                        Log.Exception(e);
                        break;
                    }
                }

                out_stream.Nullify();
                await PlayerStore.RemovePlayerAndTellInstanceServer(player_store, permanent_id, instance_servers);
            }
        }