Exemplo n.º 1
0
        /// <summary>
        /// 转换为ByteRange
        /// </summary>
        /// <exception cref="ProtocolException"></exception>
        /// <returns></returns>
        public ByteRange ToByteRange()
        {
            var apiNameBytes = Encoding.UTF8.GetBytes(this.ApiName);
            var headLength   = apiNameBytes.Length + 15;

            this.TotalBytes = this.Body == null ? headLength : headLength + this.Body.Length;

            const int packegMaxSize = 10 * 1204 * 1024; // 10M

            if (this.TotalBytes > packegMaxSize)
            {
                throw new ProtocolException("数据包太大");
            }

            this.ApiNameLength = (byte)apiNameBytes.Length;
            var builder = new ByteBuilder(Endians.Big);

            builder.Add(this.TotalBytes);
            builder.Add(this.ApiNameLength);
            builder.Add(apiNameBytes);
            builder.Add(this.Id);
            builder.Add(this.IsFromClient);
            builder.Add(this.IsException);
            builder.Add(this.Body);
            return(builder.ToByteRange());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 输出文本内容
        /// </summary>
        /// <param name="content">内容</param>
        public bool Write(string content)
        {
            if (content == null)
            {
                content = string.Empty;
            }
            var buffer       = new ByteBuilder(Endians.Little);
            var contentBytes = this.Charset.GetBytes(content);

            if (this.wroteHeader == false)
            {
                this.wroteHeader = true;
                var headerByes = this.GetHeaderBytes(contentBytes.Length);
                buffer.Add(headerByes);
            }

            buffer.Add(contentBytes);
            return(this.TrySend(buffer.ToByteRange()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 转换为ByteRange
        /// </summary>
        /// <returns></returns>
        public ByteRange ToByteRange()
        {
            var apiNameBytes = Encoding.UTF8.GetBytes(this.ApiName);
            var headLength   = apiNameBytes.Length + 16;

            this.TotalBytes = this.Body == null ? headLength : headLength + this.Body.Length;

            this.ApiNameLength = (byte)apiNameBytes.Length;
            var builder = new ByteBuilder(Endians.Big);

            builder.Add(FastPacket.Mark);
            builder.Add(this.TotalBytes);
            builder.Add(this.ApiNameLength);
            builder.Add(apiNameBytes);
            builder.Add(this.Id);
            builder.Add(this.IsFromClient);
            builder.Add(this.IsException);
            builder.Add(this.Body);
            return(builder.ToByteRange());
        }
Exemplo n.º 4
0
        /// <summary>
        /// 转换为ByteRange
        /// </summary>
        /// <returns></returns>
        public override ByteRange ToByteRange()
        {
            var builder = new ByteBuilder(Endians.Big);

            builder.Add((byte)((byte)this.Frame + 128));

            if (this.Content.Length > UInt16.MaxValue)
            {
                builder.Add((byte)127);
                builder.Add((ulong)this.Content.Length);
            }
            else if (this.Content.Length > 125)
            {
                builder.Add((byte)126);
                builder.Add((ushort)this.Content.Length);
            }
            else
            {
                builder.Add((byte)this.Content.Length);
            }
            builder.Add(this.Content);
            return(builder.ToByteRange());
        }
Exemplo n.º 5
0
        /// <summary>
        /// 转换为ByteRange
        /// </summary>
        /// <returns></returns>
        public override ByteRange ToByteRange()
        {
            var builder = new ByteBuilder(Endians.Big);

            builder.Add((byte)((byte)this.Frame + 128));

            if (this.Content.Length > UInt16.MaxValue)
            {
                builder.Add((byte)127);
                builder.Add((ulong)this.Content.Length);
            }
            else if (this.Content.Length > 125)
            {
                builder.Add((byte)126);
                builder.Add((ushort)this.Content.Length);
            }
            else
            {
                builder.Add((byte)this.Content.Length);
            }
            builder.Add(this.Content);
            return builder.ToByteRange();
        }
Exemplo n.º 6
0
        /// <summary>
        /// 输出文本内容
        /// </summary>      
        /// <param name="content">内容</param>
        public bool Write(string content)
        {
            if (content == null)
            {
                content = string.Empty;
            }
            var buffer = new ByteBuilder(Endians.Little);
            var contentBytes = this.Charset.GetBytes(content);

            if (this.wroteHeader == false)
            {
                this.wroteHeader = true;
                var headerByes = this.GetHeaderBytes(contentBytes.Length);
                buffer.Add(headerByes);
            }

            buffer.Add(contentBytes);
            return this.TrySend(buffer.ToByteRange());
        }
Exemplo n.º 7
0
        /// <summary>
        /// 转换为ByteRange
        /// </summary>
        /// <exception cref="ProtocolException"></exception>
        /// <returns></returns>
        public ByteRange ToByteRange()
        {
            var apiNameBytes = Encoding.UTF8.GetBytes(this.ApiName);
            var headLength = apiNameBytes.Length + 15;
            this.TotalBytes = this.Body == null ? headLength : headLength + this.Body.Length;

            const int packegMaxSize = 10 * 1204 * 1024; // 10M
            if (this.TotalBytes > packegMaxSize)
            {
                throw new ProtocolException();
            }

            this.ApiNameLength = (byte)apiNameBytes.Length;
            var builder = new ByteBuilder(Endians.Big);
            builder.Add(this.TotalBytes);
            builder.Add(this.ApiNameLength);
            builder.Add(apiNameBytes);
            builder.Add(this.Id);
            builder.Add(this.IsFromClient);
            builder.Add(this.IsException);
            builder.Add(this.Body);
            return builder.ToByteRange();
        }