Example #1
0
        private static void TimeoutCallback(object state, bool timedOut)
        {
            RpcHttpClientTransaction trans = null;

            try {
                trans = (RpcHttpClientTransaction)state;

                if (trans._registeredHandle != null)
                {
                    trans._registeredHandle.Unregister(null);
                }

                if (timedOut)                   // Timeout
                {
                    var response = RpcResponse.Create(RpcErrorCode.TransactionTimeout, null);
                    trans.OnCallback(response);
                }
            } catch (Exception ex) {
                SystemLog.Error(LogEventID.RpcFailed, ex, "TimeoutCallback");
            }
        }
Example #2
0
        private static void ResponseCallback(IAsyncResult asyncResult)
        {
            RpcHttpClientTransaction trans = (RpcHttpClientTransaction)asyncResult.AsyncState;
            RpcResponse response           = null;

            try {
                var webResponse = trans._webRequest.EndGetResponse(asyncResult);
                trans._webResponse = webResponse;

                string warn = webResponse.Headers.Get("Warning");

                if (!string.IsNullOrEmpty(warn))
                {
                    RpcErrorCode errCode = (RpcErrorCode)Enum.Parse(typeof(RpcErrorCode), warn);
                    if (errCode != RpcErrorCode.OK)
                    {
                        Exception ex = null;
                        if (webResponse.ContentLength > 0)
                        {
                            Stream stream = webResponse.GetResponseStream();
                            ex = BinarySerializer.Deserialize <Exception>(stream);
                        }
                        response = RpcResponse.Create(errCode, ex);
                    }
                    else
                    {
                        SystemLog.Error(LogEventID.RpcFailed, "Unexcepted Message");
                        response = RpcResponse.Create(RpcErrorCode.Unknown, null);
                    }
                }
                else
                {
                    bool hasBody = (webResponse.Headers["Null"] != "true");
                    if (hasBody)
                    {
                        if (webResponse.ContentLength == 0)
                        {
                            response = RpcResponse.Create(RpcNull.EmptyStream, 0);
                        }
                        else
                        {
                            Stream stream = webResponse.GetResponseStream();
                            response = RpcResponse.Create(stream, (int)webResponse.ContentLength);
                        }
                    }
                    else
                    {
                        response = RpcResponse.Create();
                    }
                }
            } catch (WebException ex) {
                if (ex.Status == WebExceptionStatus.Timeout)
                {
                    response = RpcResponse.Create(RpcErrorCode.TransactionTimeout, ex);
                }
                else
                {
                    response = RpcResponse.Create(RpcErrorCode.SendFailed, ex);
                }
            } catch (Exception ex) {
                response = RpcResponse.Create(RpcErrorCode.SendFailed, ex);
            }

            trans.OnCallback(response);
        }