Example #1
0
        public void DoTest()
        {
            //ESP_Type lastObjDateType = typeof(byte[]);
            //byte[] lastDat = new byte[] { 0x00, 0x01 };
            //long lastOffset = Convert.ToInt64(lastObjDateType.GetProperty("ESP_Length").GetValue(lastDat, null));
            //Console.WriteLine(lastOffset);


            MixedResponse instance = new MixedResponse();

            instance.ESP_Header = ResponseHeader.ResponseHeader4Test;
            instance.ESP_Code   = StatusCode.Success;
            byte[] msgBytes = EaseString.DefaultEncoding.GetBytes("OK");
            instance.ESP_Message = new EaseString {
                ESP_Data = msgBytes, ESP_Length = (ushort)msgBytes.Length
            };

            instance.ESP_Method = CommandType.None;

            byte[] cmdBytes = EaseString.DefaultEncoding.GetBytes("GET / HTTP/1.1");
            instance.ESP_Command = new EaseString {
                ESP_Data = cmdBytes, ESP_Length = (ushort)cmdBytes.Length
            };

            instance.ESP_PageDocCount = 2;
            instance.ESP_Docs         = new EaseDocument[] { EaseDocument.EaseDocument4Test, EaseDocument.EaseDocument4Test };

            instance.ESP_PageResCount = 2;
            instance.ESP_Resources    = new EaseResource[] { EaseResource.EaseResource4Test, EaseResource.EaseResource4Test };


            byte[] testBytes = instance.GetNetworkBytes();
            //Console.WriteLine("Total:{0}\r\n{1}", testBytes.ESP_Length, testBytes.GetHexViewString());

            MixedResponse instanceCmp = new MixedResponse();

            System.IO.MemoryStream ms = new System.IO.MemoryStream(testBytes);
            ms.Position = 0;
            instanceCmp.BindFromNetworkStream(ms, 0, false);

            byte[] bytes2cmp = instanceCmp.GetNetworkBytes();
            //Console.WriteLine("Cmp Total:{0}\r\n{1}", bytes2cmp.ESP_Length, bytes2cmp.GetHexViewString());

            Assert.That(SpecUtil.AreEqual(testBytes, bytes2cmp));
        }
Example #2
0
        /// <summary>
        /// 获取返回包装对象子类(6种Response对象中的一种)
        /// </summary>
        /// <returns></returns>
        public ESPDataBase GetSubResponse(RequestBase request)
        {
            RequestType requestType = request.ESP_Header.ESP_Protocol;
            ESPDataBase resp        = null;

            System.IO.MemoryStream ms = ESP_TransferData.AsMemoryStream();
            switch (requestType)
            {
            case RequestType.PageV21:
                resp = new PageV21Response(Context);
                break;

            case RequestType.Mixed:
                resp = new MixedResponse(Context);
                break;

            case RequestType.Page:
                resp = new PageResponse(Context);
                break;

            case RequestType.Resource:
                ResourceRequest resReq = request as ResourceRequest;
                if (!resReq.IsPackageReqeust())
                {
                    resp = new ResourcePartialResponse(Context);
                }
                else
                {
                    resp = new ResourceResponse(Context);
                }
                break;

            case RequestType.Application:
                ApplicationRequest appReq = request as ApplicationRequest;
                if (!appReq.IsPackageReqeust())
                {
                    resp = new ApplicationResponse(Context);
                }
                else
                {
                    resp = new ApplicationPartialResponse(Context);
                }
                break;

            case RequestType.UpdateCenter:
                resp = new GatewayUpdateResponse(Context);
                break;

            case RequestType.SynServerAddress:
                resp = new SynServerAddressResponse(Context);
                break;

            default:
                break;
            }

            SpecUtil.BindFromNetworkStream(resp, ms, ms.Position, false, 0);
            resp.ContentRange[0] = 0;
            resp.ContentRange[1] = ESP_TransferData.LongLength - 1;
            ms.Close();
            ms.Dispose();

            return(resp);
        }