示例#1
0
        public bool TryDecodePackage(ConnectionEventArgs e, out RequestPackage package)
        {
            byte[]         content = null;
            CTSPackageHead head    = ReadMessageHead(e.Data, out content);

            if (head == null)
            {
                var packageReader = new PackageReader(e.Data, Encoding.UTF8);
                if (TryBuildPackage(packageReader, out package))
                {
                    package.OpCode         = e.Meaage.OpCode;
                    package.CommandMessage = e.Socket.IsWebSocket && e.Meaage.OpCode == OpCode.Text
                    ? e.Meaage.Message
                    : null;
                    return(true);
                }
                package = null;
                return(false);
            }

            package = new RequestPackage(head.MsgId, head.SessionId, head.ActionId, head.UserId)
            {
                Message = content
            };
            return(true);
        }
示例#2
0
        public bool TryDecodePackage(HttpListenerRequest request, out RequestPackage package, out int statusCode)
        {
            statusCode = 200;
            package    = null;
            byte[]         content;
            var            bytes = GetRequestStream(request.InputStream);
            CTSPackageHead head  = ReadMessageHead(bytes, out content);

            if (head == null)
            {
                return(false);
            }
            package = new RequestPackage(head.MsgId, head.SessionId, head.ActionId, head.UserId)
            {
                Message = content
            };
            return(true);
        }
示例#3
0
        public bool TryDecodePackage(HttpContext context, out RequestPackage package)
        {
            package = null;
            CTSPackageHead head = null;

            byte[] content = new byte[0];
            var    bytes   = GetRequestStream(context.Request.InputStream);

            head = ReadMessageHead(bytes, out content);
            if (head == null)
            {
                return(false);
            }
            package = new RequestPackage(head.MsgId, head.SessionId, head.ActionId, head.UserId)
            {
                Message = content
            };
            return(true);
        }
示例#4
0
        private CTSPackageHead ReadMessageHead(byte[] data, out byte[] content)
        {
            CTSPackageHead headPackage = null;

            content = new byte[0];
            try
            {
                int    pos          = 0;
                byte[] headLenBytes = new byte[4];
                Buffer.BlockCopy(data, pos, headLenBytes, 0, headLenBytes.Length);
                pos += headLenBytes.Length;
                int headSize = BitConverter.ToInt32(headLenBytes, 0);
                if (headSize < data.Length)
                {
                    byte[] headBytes = new byte[headSize];
                    Buffer.BlockCopy(data, pos, headBytes, 0, headBytes.Length);
                    pos        += headBytes.Length;
                    headPackage = ProtoBufUtils.Deserialize <CTSPackageHead>(headBytes);

                    if (data.Length > pos)
                    {
                        int len = data.Length - pos;
                        content = new byte[len];
                        Buffer.BlockCopy(data, pos, content, 0, content.Length);
                    }
                }
                else
                {
                    TraceLog.ReleaseWriteFatal("Can not parse head package.");
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError(ex.Message);
            }
            return(headPackage);
        }