Ejemplo n.º 1
0
        private static void ProcessDepotCommand(byte[] data, DepotClient client)
        {
            var group = RdlCommandGroup.FromBytes(data);

            foreach (var cmd in group)
            {
                if (cmd.TypeName.ToUpper().Equals("MAPNAMES"))
                {
                    client.Send(Depot.GetMapNames().ToBytes());
                }
                else if (cmd.TypeName.ToUpper().Equals("MAPCHUNK"))
                {
                    var mapName       = cmd.GetArg <string>(0);
                    var startX        = cmd.GetArg <int>(1);
                    var startY        = cmd.GetArg <int>(2);
                    var includeActors = cmd.GetArg <bool>(3);

                    var result = Depot.GetMapChunk(mapName, startX, startY, includeActors).Tags;
                    client.Send(Encoding.UTF8.GetBytes(result));
                }
                else
                {
                    client.Send(RdlTag.Empty.ToBytes());
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sends the specified command to the server to be executed.
 /// </summary>
 /// <param name="commands">The command to execute.</param>
 /// <param name="responseCallback">The CommunicatorResponseEventHandler to use for handling the
 /// response from this command execution. If this value is provided the Response event will not be raised.</param>
 public void Execute(RdlCommandGroup commands, CommunicatorResponseEventHandler responseCallback)
 {
     this.Buffer      = commands.ToBytes();
     this.AltCallback = responseCallback;
     this.CreateRequest();
     this.Request.BeginGetRequestStream(new AsyncCallback(HttpCommunicator.BeginRequest), this);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends the specified command to the server to be executed.
        /// </summary>
        /// <param name="commands">The command to execute.</param>
        /// <param name="responseCallback">The CommunicatorResponseEventHandler to use for handling the
        /// response from this command execution. If this value is provided the Response event will not be raised.</param>
        public void Execute(RdlCommandGroup commands, CommunicatorResponseEventHandler responseCallback)
        {
            _altResponse = responseCallback;
            // Create a message with the appropriate SOAPAction and asynchronously send it on the channel with the serialized Stock as the body of the envelope
            Message message = Message.CreateMessage(MessageVersion.Soap11, "Radiance.Contract.IGameServer/Process", commands.ToBytes());

            Send(_channel, message);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sends the specified command to the server using the current CommunicationProtocol and provides an
        /// alternate handler for the server response.
        /// </summary>
        /// <param name="command">The CommandTag to send to the server.</param>
        /// <param name="serverResponseCallback">The CommunicatorResponseEventHandler that will handle the
        /// response from this command.</param>
        public void SendCommand(RdlCommand command, CommunicatorResponseEventHandler serverResponseCallback)
        {
            RdlCommandGroup group = new RdlCommandGroup(new RdlCommand[] { command });

            group.AuthKey     = this.AuthKey;
            group.AuthKeyType = this.AuthKeyType;

            this.Init();
            _communicator.Execute(group, serverResponseCallback);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes commands in the context of the virtual world.
        /// </summary>
        /// <param name="requestHandler">The ICommunicationHandler processing the request from the client.</param>
        /// <param name="commands">The commands to process.</param>
        /// <returns>The IClient instance of the current requestor.</returns>
        public IClient ProcessCommands(ICommunicationHandler requestHandler, RdlCommandGroup commands, Guid sessionId, string address)
        {
            bool    addClient = false;
            IClient client    = null;

            if (commands.Count > 0)
            {
                if (!String.IsNullOrEmpty(commands.AuthKey))
                {
                    AuthKey key = AuthKey.Get(commands.AuthKey);
                    if (this.ValidateAuthKey(key))
                    {
                        // Clients are stored with the UserName of the current user as the key. This
                        // should prevent a user from playing multiple characters at the same time unless
                        // they have multiple user names, which violates the TOS.
                        if (!this.Clients.ContainsKey(key.SessionId))
                        {
                            client          = requestHandler.CreateClient(key.SessionId);
                            client.Address  = address;
                            client.UserName = key.UserName;
                            addClient       = true;
                        }
                        else
                        {
                            client = this.Clients[key.SessionId];
                        }
                        if (client != null)
                        {
                            //client.LastHeartbeatDate = DateTime.Now;
                            client.AuthKey = key;
                        }
                    }
                }
                else
                {
                    // New client will implement the LoginCommandHandler.
                    client         = requestHandler.CreateClient(sessionId);
                    client.Address = address;
                    addClient      = true;
                }

                if (addClient)
                {
                    this.Clients.Add(client);
                    this.World.Provider.CreateSession(client, client.SessionId.ToString());
                    Logger.LogDebug("SERVER: New client connected from: {0}", client.SessionId.ToString());
                }

                if (client != null)
                {
                    ProcessCommands(client, commands);
                }
            }
            return(client);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Validates that the specified command can be executed by this handler.
        /// </summary>
        /// <param name="server">The current server instance.</param>
        /// <param name="commands">The commands being executed.</param>
        /// <returns>True if the commands can be executed by the current handler; otherwise false.</returns>
        protected override bool ValidateCommands(Server server, RdlCommandGroup commands)
        {
            if (this.Client.AuthKey.ID == 0)
            {
                this.Client.Handler = new UserCommandHandler(this.Client);
                this.Client.Handler.ProcessCommands(server, commands);
                return(false);
            }

            return(this.Client.Player != null && this.Client.Player.UserName == this.Client.AuthKey.UserName);
        }
 /// <summary>
 /// Processes the commands in the RdlCommandGroup.
 /// </summary>
 /// <param name="server">The current server instance.</param>
 /// <param name="commands">The RdlCommandGroup containing the commands to process.</param>
 public override void ProcessCommands(Server server, RdlCommandGroup commands)
 {
     if (this.ValidateCommands(server, commands))
     {
         int count = commands.Count;
         for (int i = 0; i < count; i++)
         {
             this.ProcessCommand(server, commands[i], this.Client.Context);
         }
     }
 }
Ejemplo n.º 8
0
        public void Process(byte[] data)
        {
            var session = OperationContext.Current.GetPollingDuplexSession();

            Logger.LogDebug("SERVER: Recieved data from {0} on Thread [ {1} ]", session.SessionId, Thread.CurrentThread.ManagedThreadId);
            var commands = RdlCommandGroup.FromBytes(data);

            if (commands != null && commands.Count > 0)
            {
                Game.Server.ProcessCommands(this, commands, new Guid(session.SessionId), session.Address);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Processes commands in the context of the virtual world.
        /// </summary>
        /// <param name="client">The IClient processing the request.</param>
        /// <param name="commands">The commands to process.</param>
        /// <returns>The IClient instance of the current requestor.</returns>
        public IClient ProcessCommands(IClient client, RdlCommandGroup commands)
        {
            if (!(client.Handler is LoginCommandHandler))
            {
                client.LastCommandGroup = commands;
            }

            bool processCommands = this.World.IsOnline;

            if (!processCommands)
            {
                processCommands = this.World.Provider.ValidateRole(client.UserName, RoleNames.God);
            }

            if (processCommands)
            {
                if (this.World.EnableCommandLogging)
                {
                    if (!(client.Handler is LoginCommandHandler))
                    {
                        Lionsguard.Logger.LogCommand("CMD LOG: SessionId: {0}, UserName: {1}, Commands: {2}",
                                                     client.SessionId, client.UserName, commands.ToString());
                    }
                }

                // TODO: Spam filter, record the commands to this client, if the same command chat
                // command is sent more than twice in a row then abandon this command group.

                // Queue commands.
                lock (_receivedPackets)
                {
                    Logger.LogDebug("SERVER: Queue Commands: {0}, for Client: {1}", commands, client.UserName);
                    _receivedPackets.Enqueue(new ClientPacket {
                        Client = client, Commands = commands
                    });
                }

                // Update client session.
                this.World.Provider.UpdateSession(client);

                this.Signal();
            }
            else
            {
                client.Context.Add(new RdlErrorMessage(SR.WorldOffline(this.World.Name)));
                // Send down a command to refresh the web page where the client sits so
                // it will redirect to the app offline page.
                client.Context.Add(new RdlCommand("EXIT"));
            }
            return(client);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Processes the commands in the RdlCommandGroup.
        /// </summary>
        /// <param name="server">The current server instance.</param>
        /// <param name="commands">The RdlCommandGroup containing the commands to process.</param>
        public override void ProcessCommands(Server server, RdlCommandGroup commands)
        {
            if (String.IsNullOrEmpty(commands.AuthKey))
            {
                // If authkey is null but a login command exists then process the login command.
                // Check for a login command.
                var cmds = commands.Where(c => c.TypeName == KnownCommands.Login ||
                                          c.TypeName == KnownCommands.Heartbeat ||
                                          c.TypeName == KnownCommands.ForgotPassword ||
                                          c.TypeName == KnownCommands.SignUp);
                if (cmds != null)
                {
                    foreach (var cmd in cmds)
                    {
                        CommandManager.ProcessCommand(server, cmd, this.Client);
                    }
                    //string username = cmd.GetArg<string>(0);
                    //AuthKey authKey = server.World.Provider.AuthenticateUser(username, cmd.GetArg<string>(1));
                    //RdlAuthKey user = new RdlAuthKey(authKey.ToString(), authKey.Type.ToString());
                    //this.Client.Context.Add(user);

                    //if (!String.IsNullOrEmpty(authKey.ToString()))
                    //{
                    //    this.Client.Handler = new UserCommandHandler(this.Client);
                    //    this.Client.UserName = username;
                    //    this.Client.AuthKey = authKey;
                    //    //if (!server.Clients.ContainsKey(username))
                    //    //{
                    //    ////    // Expire the current client.
                    //    ////    server.Clients[username].Expire();
                    //    ////    // Reset the current client.
                    //    ////    server.Clients[username] = this.Client;
                    //    ////}
                    //    ////else
                    //    ////{
                    //    //    server.Clients.Add(this.Client);
                    //    //}
                    //}
                }
            }
            else
            {
                this.Client.Handler = new UserCommandHandler(this.Client);
                this.Client.Handler.ProcessCommands(server, commands);
            }
        }
Ejemplo n.º 11
0
        public string Process(string data)
        {
            var response = String.Empty;

            var client = Game.Server.ProcessCommands(this, RdlCommandGroup.FromString(data), Guid.NewGuid(), OperationContext.Current.Channel.LocalAddress.ToString());

            if (client != null)
            {
                var    tags = new RdlTagCollection();
                RdlTag tag;
                while (client.Context.Read(out tag))
                {
                    tags.Add(tag);
                }
                response = tags.ToString();
            }

            return(response);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Processes the packet data for a connected client.
        /// </summary>
        /// <param name="data">The packet data to process.</param>
        public void Process(byte[] data)
        {
            Logger.LogDebug("SERVER: DuplexService.Process Entered.");
            ThreadPool.QueueUserWorkItem((o) =>
            {
                var info = (SessionInfo)o;

                Logger.LogDebug("SERVER: Executing DuplexService.Process on Thread {0}", Thread.CurrentThread.ManagedThreadId);
                var commands = RdlCommandGroup.FromBytes(info.Data);
                if (commands.Count > 0)
                {
                    var client = Game.Server.Clients.Where(c => c.SessionId.ToString().Equals(info.Session.SessionId)).FirstOrDefault();
                    if (client == null)
                    {
                        Game.Server.ProcessCommands(this, commands, info.Session);
                    }
                    else
                    {
                        Game.Server.QueueRequest(this, commands, info.Session);
                    }
                }
                Logger.LogDebug("SERVER: Completed DuplexService.Process.");
            }, new SessionInfo {
                Data = data, Session = OperationContext.Current.GetPollingDuplexSession()
            });

            //var commands = RdlCommandGroup.FromBytes(data);
            //if (commands.Count > 0)
            //{
            //    var session = OperationContext.Current.GetPollingDuplexSession();
            //    var client = Game.Server.Clients.Where(c => c.SessionId.ToString().Equals(session.SessionId)).FirstOrDefault();
            //    if (client == null)
            //    {
            //        Game.Server.ProcessCommands(this, commands, OperationContext.Current.GetPollingDuplexSession());
            //    }
            //    else
            //    {
            //        Game.Server.QueueRequest(this, commands, OperationContext.Current.GetPollingDuplexSession());
            //    }
            //}

            Logger.LogDebug("SERVER: DuplexService.Process Exited.");
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Validates that the specified command can be executed by this handler.
        /// </summary>
        /// <param name="server">The current server instance.</param>
        /// <param name="commands">The commands being executed.</param>
        /// <returns>True if the commands can be executed by the current handler; otherwise false.</returns>
        protected override bool ValidateCommands(Server server, RdlCommandGroup commands)
        {
            if (this.Client.AuthKey.ID > 0)
            {
                this.Client.Handler = new PlayerCommandHandler(this.Client);
                this.Client.Handler.ProcessCommands(server, commands);
                return(false);
            }

            RdlCommand cmd = commands.Where(c => c.TypeName == "ADMINBUILD").FirstOrDefault();

            if (cmd != null)
            {
                this.Client.Handler = new AdminCommandHandler(this.Client);
                this.Client.Handler.ProcessCommands(server, commands);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Validates that the specified command can be executed by this handler.
 /// </summary>
 /// <param name="server">The current server instance.</param>
 /// <param name="commands">The commands being executed.</param>
 /// <returns>True if the commands can be executed by the current handler; otherwise false.</returns>
 protected override bool ValidateCommands(Server server, RdlCommandGroup commands)
 {
     return(true);
 }
Ejemplo n.º 15
0
        public void ProcessRequest(HttpContext context)
        {
            var response = new List <byte>();

            try
            {
                // Read the bytes from the request stream and create a Message from it.
                var buffer = new byte[context.Request.ContentLength];
                context.Request.InputStream.Read(buffer, 0, context.Request.ContentLength);
                if (buffer != null && buffer.Length > 0)
                {
                    var perenthiaSessionId = context.Request.Headers.Get(HttpHeaders.RadianceSessionIdHeaderKey);

                    Guid sessionId;
                    if (!Lionsguard.Util.GuidTryParse(perenthiaSessionId, out sessionId))
                    {
                        sessionId = Guid.NewGuid();
                    }

                    IClient client      = null;
                    var     waitForTags = false;
                    var     commands    = RdlCommandGroup.FromBytes(buffer);
                    if (commands.Count == 1 && commands[0].TypeName == RdlCommand.Heartbeat.TypeName)
                    {
                        Game.Server.Clients.TryGetClient(sessionId, out client);
                    }
                    else
                    {
                        waitForTags = true;
                        client      = Game.Server.ProcessCommands(this, commands, sessionId, context.Request.UserHostAddress);
                    }
                    if (client != null)
                    {
                        // Wait for the client to get some tags.
                        if (waitForTags)
                        {
                            while (client.Context.Count == 0)
                            {
                                _waitHandle.WaitOne(TimeSpan.FromSeconds(1));
                            }
                        }

                        RdlTag tag;
                        while (client.Context.Read(out tag))
                        {
                            response.AddRange(tag.ToBytes());
                        }
                        Logger.LogDebug("SERVER: Sending {0} bytes to the client.", response.Count);
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                throw ex;
#else
                Lionsguard.Log.Write(ex.ToString(), true);
#endif
            }

            context.Response.ContentType = "binary/octet-stream";

            if (response.Count > 0)
            {
                context.Response.BinaryWrite(response.ToArray());
            }
            else
            {
                context.Response.Write(String.Empty);
            }
        }
Ejemplo n.º 16
0
        private static void OnBeginReceiveComplete(IAsyncResult ar)
        {
            var context = (SocketMessageContext)ar.AsyncState;

            int bytesReceived = 0;

            try
            {
                // Complete the call.
                bytesReceived = context.Socket.EndReceive(ar);
            }
            catch (SocketException)
            {
                // Disconnect the current client.
                context.Socket.Close();
                if (context.Client != null)
                {
                    context.Client.Expire();
                }
                return;
            }
            catch (ObjectDisposedException)
            {
                if (context.Client != null)
                {
                    context.Client.Expire();
                }
                return;
            }

            try
            {
                if (bytesReceived > 0)
                {
                    context.PacketData.AddRange(context.Buffer.Take(bytesReceived));

                    if (context.PacketData.Count >= 4)
                    {
                        int index = 0;

                        // Convert the current packet data into a byte array.
                        byte[] data = context.PacketData.ToArray();

                        bool readComplete = false;
                        while (!readComplete)
                        {
                            // Get the length of the bytes to process. Length does do not include
                            // the 4 bytes required for the actual length int value itself.
                            int length = BitConverter.ToInt32(data, index);

                            // Advance the index 4 bytes to account for the length value.
                            index += 4;

                            if (data.Length >= length + 4)                             // Account for the length value
                            {
                                byte[] buffer = new byte[length];
                                Array.Copy(data, index, buffer, 0, length);

                                // Completed adding packet data, create the reader and allow the network to process the packet.
                                Game.Server.ProcessCommands(context.Client, RdlCommandGroup.FromBytes(buffer));

                                index += length;

                                if (index >= data.Length)
                                {
                                    context.PacketData.Clear();
                                    readComplete = true;
                                }
                            }
                            else
                            {
                                readComplete = true;
                            }
                        }
                    }
                }

                context.BeginReceive();
            }
            catch (SocketException)
            {
                context.Socket.Close();
                if (context.Client != null)
                {
                    context.Client.Expire();
                }
            }
        }
 /// <summary>
 /// Validates that the specified command can be executed by this handler.
 /// </summary>
 /// <param name="server">The current server instance.</param>
 /// <param name="commands">The commands being executed.</param>
 /// <returns>True if the commands can be executed by the current handler; otherwise false.</returns>
 protected abstract bool ValidateCommands(Server server, RdlCommandGroup commands);
Ejemplo n.º 18
0
 /// <summary>
 /// Processes the commands in the RdlCommandGroup.
 /// </summary>
 /// <param name="server">The current server instance.</param>
 /// <param name="commands">The RdlCommandGroup containing the commands to process.</param>
 public abstract void ProcessCommands(Server server, RdlCommandGroup commands);
Ejemplo n.º 19
0
 /// <summary>
 /// Sends the specified command to the server to be executed.
 /// </summary>
 /// <param name="commands">The command to execute.</param>
 /// <param name="responseCallback">The CommunicatorResponseEventHandler to use for handling the
 /// response from this command execution. If this value is provided the Response event will not be raised.</param>
 public void Execute(RdlCommandGroup commands, CommunicatorResponseEventHandler responseCallback)
 {
     this.AltCallback = responseCallback;
     this.Send(commands.ToBytes());
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Sends the specified command to the server to be executed.
 /// </summary>
 /// <param name="commands">The command to execute.</param>
 public void Execute(RdlCommandGroup commands)
 {
     Execute(commands, null);
 }