예제 #1
0
        private static PacketBodyBase GetPacketBody(Type packetBodyType, int msgId, byte[] data)
        {
            PacketBodyBase _packetBody = null;

            try
            {
                PacketType _packetType = (PacketType)Enum.Parse(typeof(PacketType), msgId.ToString());
                if (typeof(RequestPacket).IsAssignableFrom(packetBodyType))
                {
                    switch (_packetType)
                    {
                    case PacketType.Register:
                        _packetBody = SerializationHelper.Decode <RegisterResponse>(data); break;

                    case PacketType.Login:
                        _packetBody = SerializationHelper.Decode <LoginRequest>(data); break;

                    default:
                        throw new NotSupportedPacketTypeException(string.Format("暂不支持类型为{0}的数据包", _packetType), packetBodyType);
                    }
                }
                else if (typeof(ResponsePacket).IsAssignableFrom(packetBodyType))
                {
                    switch (_packetType)
                    {
                    case PacketType.Register:
                        _packetBody = SerializationHelper.Decode <RegisterResponse>(data); break;

                    case PacketType.Login:
                        _packetBody = SerializationHelper.Decode <LoginResponse>(data); break;

                    default:
                        throw new NotSupportedPacketTypeException(string.Format("暂不支持类型为{0}的数据包", _packetType), packetBodyType);
                    }
                }
            }
            catch
            {
                throw;
            }

            return(_packetBody);
        }
예제 #2
0
 public PackException(string message, PacketBodyBase packetBody, Exception innerException)
     : base(message, innerException)
 {
     this.PacketBody = packetBody.ToString();
 }
예제 #3
0
        public static byte[] Pack(PacketBodyBase packetBody, bool compressed = true)
        {
            try
            {
                byte[] _head, _compress, _body;
                string _md5;

                //编码包体
                _body = SerializationHelper.Encode(packetBody);
                _md5  = SecurityHelper.GetMD5(_body);

                //编码压缩包头
                CompressHeader _compressHeader = new CompressHeader();
                _compressHeader.Compressed = (short)(compressed ? 1 : 0);
                _compressHeader.DataSize   = _body.Length;
                if (compressed)
                {
                    _body = CompressHelper.Compress(_body);
                }
                _compressHeader.CompressedSize = _body.Length;
                _compress = SerializationHelper.Encode(_compressHeader);
                _body     = _compress.Concat(_body).ToArray();

                //编码数据包包头
                if (typeof(RequestPacket).IsAssignableFrom(packetBody.GetType()))
                {
                    RequestHeader _requestHeader = new RequestHeader
                    {
                        MsgId   = packetBody.GetMsgId(),
                        PkgSize = _body.Length,
                        MD5     = _md5,
                    };
                    _head = SerializationHelper.Encode(_requestHeader);
                }
                else if (typeof(ResponsePacket).IsAssignableFrom(packetBody.GetType()))
                {
                    ResponseHeader _responseHeader = new ResponseHeader
                    {
                        MsgId   = packetBody.GetMsgId(),
                        PkgSize = _body.Length,
                        MD5     = _md5,
                    };
                    _head = SerializationHelper.Encode(_responseHeader);
                }
                else
                {
                    throw new NotSupportedPacketTypeException(string.Format("暂不支持类型为{0}的数据包", packetBody.GetType().ToString()), packetBody.GetType());
                }

                return(_head.Concat(_body).ToArray());
            }
            catch (NotSupportedPacketTypeException ex)
            {
                ex.Source += ".PacketUtil.Pack";
                throw;
            }
            catch (Exception ex)
            {
                ex.Source += ".PacketUtil.Pack";
                throw new PackException("数据包打包出现异常", packetBody, ex);
            }
        }
예제 #4
0
 public PackException(string message, PacketBodyBase packetBody)
     : base(message)
 {
     this.PacketBody = packetBody.ToString();
 }