private void SendResponse(HttpContext context, ServiceResponse response)
 {
     using (var poolItem = ByteBuffer.Borrow(Util.DefaultResponseSegmentSize))
     {
         var data = poolItem.Item;
         DataAnalyzer.SerializeResponse(response, data);
         data.Read(context.Response.OutputStream);
     }
 }
        public ServiceResponse Invoke(ServiceRequest request, string address)
        {
            ServiceResponse response;

            using (var requestDataItem = ByteBuffer.Borrow(Util.DefaultRequestSegmentSize))
            {
                var requestData = requestDataItem.Item;
                DataAnalyzer.SerializeRequest(request, requestData);
                response = HttpCommunicator.Instance.Send(address, requestData);
            }
            return(response);
        }
        private ServiceRequest GetServiceRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            var         length  = request.ContentLength;

            using (var poolItem = ByteBuffer.Borrow(length))
            {
                var data = poolItem.Item;
                data.Write(request.InputStream, length);
                return(DataAnalyzer.DeserializeRequest(data));
            }
        }
        /// <summary>
        /// 异步调用服务
        /// </summary>
        /// <param name="address"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <ServiceResponse> InvokeAsync(ServiceRequest request, string address)
        {
            ServiceResponse response;

            using (var requestDataItem = ByteBuffer.Borrow(SegmentSize.Byte256))
            {
                var requestData = requestDataItem.Item;
                DataAnalyzer.SerializeRequest(request, requestData);
                response = await HttpCommunicator.Instance.SendAsync(address, requestData);
            }
            return(response);
        }
Esempio n. 5
0
        public async Task <ServiceResponse> SendAsync(string address, ByteArray requestData)
        {
            HttpWebRequest  request = null;
            ServiceResponse response;

            try
            {
                request = CreateRequest(address);
                request.ContentLength = requestData.Length; //post方法需要手工指定

                //发送POST数据
                using (Stream stream = await request.GetRequestStreamAsync())
                {
                    requestData.Read(stream);
                }

                using (var httpResponse = await request.GetResponseAsync() as HttpWebResponse)
                {
                    int responseDataLength = (int)httpResponse.ContentLength;
                    using (Stream stream = httpResponse.GetResponseStream())
                    {
                        using (var responseDataItem = ByteBuffer.Borrow(responseDataLength))
                        {
                            var responseData = responseDataItem.Item;
                            responseData.Write(stream, responseDataLength);
                            response = DataAnalyzer.DeserializeResponse(responseData);
                        }
                    }
                }
                return(response);
            }
            catch (Exception ex)
            {
                throw new ServiceException("访问服务地址" + address + "出错", ex);
            }
            finally
            {
                ClearRequest(request);
            }
        }