Exemple #1
0
        internal void HandleReg <TPlayer>(TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, IRegistration reg)
            where TPlayer : Player, new()
        {
            if (reg.Username != null && reg.Password != null && reg.Email != null)
            {
                // ok we want to register & login

                if (ws.Username == null || ws.Password == null)
                {
                    var res = auther.Register(new Credentials(reg.Username, ws.Socket, new DefaultPassword(reg.Password), reg.Email), out var token);

                    if (res.Successful)
                    {
                        // they've been registered, let's log them in now

                        Login(ws, new Messages.v1.Authentication {
                            Username = reg.Username,
                            Password = reg.Password,
                        });
                    }
                    else
                    {
                        // send an error message
                    }
                }
                else
                {
                    // send an error message
                }
            }
            else
            {
                // send an error message
            }
        }
Exemple #2
0
        internal void HandlePacket <TPlayer>(Type type, TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, Stream data)
            where TPlayer : Player, new()
        {
            switch (Activator.CreateInstance(type))
            {
            case IAuthentication auth: {
                if (ProtoSerializer.Deserialize <v1.Authentication>(data, out var __))
                {
                    auth = __;

                    HandleAuth(ws, auther, rooms, auth);
                }
                else
                {
                    ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your authentication packet.")));
                }
            } break;

            case IRegistration reg: {
                if (ProtoSerializer.Deserialize <v1.Registration>(data, out var __))
                {
                    reg = __;

                    HandleReg(ws, auther, rooms, reg);
                }
                else
                {
                    ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your registration packet.")));
                }
            } break;

            case IRoomRequest req: {
                if (ProtoSerializer.Deserialize <v1.RoomRequest>(data, out var __))
                {
                    req = __;

                    HandleRoom(ws, rooms, req);
                }
                else
                {
                    ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your roomrequest packet.")));
                }
            } break;

            case IMessage msg: {
                if (ProtoSerializer.Deserialize <v1.Message>(data, out var __))
                {
                    msg = __;

                    HandleMessage(ws, rooms, msg);
                }
                else
                {
                    ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your message packet.")));
                }
            } break;

            default: break;
            }
        }
Exemple #3
0
        public static void Handle <TPlayer>(TPlayer player, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, MemoryStream ms)
            where TPlayer : Player, new()
        {
            //TODO: handle different protocol versions

            // :p

            V1.Handle(player, auther, rooms, ms);
        }
Exemple #4
0
        public Server(string[] commandLineArgs, Authentication.IAuthModule auther, ushort port, params Type[] enabledRooms)
        {
            this._auth = auther;

            Message.Constructor();

            this._players = new PlayerManager <TPlayer>();
            this._rooms   = new RoomManager <TPlayer>(enabledRooms);

            this._server = new WebSocketServer($"ws://0.0.0.0:{port}");
            this._server.RestartAfterListenError = true;
        }
Exemple #5
0
        internal void HandleAuth <TPlayer>(TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, IAuthentication auth)
            where TPlayer : Player, new()
        {
            if (auth.Username != null && auth.Password != null)
            {
                // ok we want to login

                if (ws.Username == null || ws.Password == null)
                {
                    var res = auther.Login(new Credentials(auth.Username, ws.Socket, new DefaultPassword(auth.Password)), out var token);

                    if (res.Successful)
                    {
                        Login(ws, auth);
                    }
                    else
                    {
                        // send an error message
                    }
                }
                else
                {
                    // send an error message
                }
            }
            else
            {
                //logout

                if (ws.Username != null && ws.Password != null)
                {
                    // the player is authenticated, so we'll log them out

                    ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(true)));
                }
                else
                {
                    // send an error message
                }
            }
        }
Exemple #6
0
        public override async void Handle <TPlayer>(TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, Stream data)
        {
            using (var t = new MemoryStream()) {
                data.CopyTo(t);
                data.Seek(0, SeekOrigin.Begin);
                Console.WriteLine(Convert.ToBase64String(t.ToArray()));
            }
            if (!ProtoSerializer.Deserialize <SimpleIProtoMessageInheriter>(data, out var res))
            {
                await ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to deserialize your message.")));

                return;
            }

            if (!ProtocolDefinition.FindTypeFor(res, out var type))
            {
                await ws.Socket.Send(ProtoSerializer.Serialize(GetSuccess(false, "Unable to find the type for your message.")));

                return;
            }

            HandlePacket(type, ws, auther, rooms, data);
        }
Exemple #7
0
 public abstract void Handle <TPlayer>(TPlayer ws, Authentication.IAuthModule auther, RoomManager <TPlayer> rooms, Stream data)
     where TPlayer : Player, new();