Beispiel #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());
                }
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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.");
        }
        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);
            }
        }
        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();
                }
            }
        }