Beispiel #1
0
        /// <summary>
        /// 反序列化协议包。
        /// </summary>
        /// <param name="networkChannel">网络频道。</param>
        /// <param name="source">要反序列化的来源流。</param>
        /// <param name="customErrorData">用户自定义错误数据。</param>
        /// <returns>反序列化后的协议包。</returns>
        public override Packet Deserialize(INetworkChannel networkChannel, Stream source, out object customErrorData)
        {
            EuPacket p = new EuPacket();

            StreamReader sr   = new StreamReader(source);
            JsonData     data = JsonMapper.ToObject(sr);

            p.Tag  = data[0].ToString();
            p.Args = data.Cast <object>().Skip(1).ToArray();

            customErrorData = null;
            return(p);
        }
Beispiel #2
0
        /// <summary>
        /// 序列化协议包。
        /// </summary>
        /// <typeparam name="T">协议包类型。</typeparam>
        /// <param name="networkChannel">网络频道。</param>
        /// <param name="destination">要序列化的目标流。</param>
        /// <param name="packet">要序列化的协议包。</param>
        public override void Serialize <T>(INetworkChannel networkChannel, Stream destination, T packet)
        {
            EuPacket ep = packet as EuPacket;

            if (ep != null)
            {
                // Avoid create a new array
                string args = JsonMapper.ToJson(ep.Args);
                string json;
                if (ep.Args.Length == 0)
                {
                    json = string.Concat("[\"", ep.Tag, "\"]");
                }
                else
                {
                    json = string.Concat("[\"", ep.Tag, "\",", args.Substring(1));
                }
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                destination.Write(bytes, 0, bytes.Length);
            }
        }