Beispiel #1
0
        /// <summary>把消息写入流中,默认调用序列化框架</summary>
        /// <param name="stream">数据流</param>
        /// <param name="rwkind">序列化类型</param>
        protected virtual void OnWrite(Stream stream, RWKinds rwkind)
        {
            var writer = RWService.CreateWriter(rwkind);

            writer.Stream = stream;
            OnReadWriteSet(writer);
            writer.Settings.Encoding = new UTF8Encoding(false);

            if (Debug)
            {
                writer.Debug = true;
                writer.EnableTraceStream();
            }

            writer.WriteObject(this);
            writer.Flush();
        }
Beispiel #2
0
        ///// <summary>序列化当前消息到流中</summary>
        ///// <param name="stream"></param>
        //public void Write(Stream stream) { Write(stream, SerializationKinds.Binary); }

        /// <summary>序列化当前消息到流中</summary>
        /// <param name="stream"></param>
        /// <param name="rwkind"></param>
        public void Write(Stream stream, RWKinds rwkind = RWKinds.Binary)
        {
            //var writer = new BinaryWriterX(stream);
            var writer = RWService.CreateWriter(rwkind);

            writer.Stream = stream;
            Set(writer.Settings);
            writer.Settings.Encoding = new UTF8Encoding(false);

            if (Debug)
            {
                writer.Debug = true;
                writer.EnableTraceStream();
            }

            // 二进制增加头部
            if (rwkind == RWKinds.Binary)
            {
                // 判断并写入消息头
                if (_Header != null && _Header.UseHeader)
                {
                    Header.Write(writer.Stream);
                }

                // 基类写入编号,保证编号在最前面
                writer.Write((Byte)Kind);
            }
            else
            {
                var n   = (Byte)Kind;
                var bts = Encoding.ASCII.GetBytes(n.ToString());
                stream.Write(bts, 0, bts.Length);
            }

            writer.WriteObject(this);
            writer.Flush();
        }