Exemple #1
0
        public static async Task SendMessageAsync(string targetServer, string nodeName, string groupName, HttpRequest.PaxosMessage msg)
        {
            try
            {
                string         uri = HttpConfiguration.FormClientUriPrefix(targetServer, nodeName, groupName);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
                req.Method      = "PUT";
                req.ContentType = "application/octet-stream";
                MemoryStream ms = DxSerializationUtil.SerializeMessage(msg);
                req.ContentLength = ms.Length;
                ms.Position       = 0L;
                Stream outStream = await req.GetRequestStreamAsync();

                using (outStream)
                {
                    await ms.CopyToAsync(outStream);
                }
                ExTraceGlobals.PaxosMessageTracer.TraceDebug <long>(0L, "Sent PaxosMessage len={0}", ms.Length);
                using ((HttpWebResponse)(await req.GetResponseAsync()))
                {
                }
            }
            catch (Exception ex)
            {
                ExTraceGlobals.PaxosMessageTracer.TraceError <string, Exception>(0L, "SendMessageAsync failed to node {0} caught: {1}", nodeName, ex);
                throw new DxStoreInstanceClientException(ex.Message, ex);
            }
        }
Exemple #2
0
        public static async Task <InstanceStatusInfo> GetStatusAsync(string targetServer, string targetNodeName, string groupName, string sendingNodeName)
        {
            InstanceStatusInfo reply;

            try
            {
                string         uri = HttpConfiguration.FormClientUriPrefix(targetServer, targetNodeName, groupName);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
                req.Method      = "PUT";
                req.ContentType = "application/octet-stream";
                HttpRequest.GetStatusRequest reqMsg = new HttpRequest.GetStatusRequest(sendingNodeName);
                MemoryStream ms = DxSerializationUtil.SerializeMessage(reqMsg);
                req.ContentLength = ms.Length;
                ms.Position       = 0L;
                Stream outStream = await req.GetRequestStreamAsync();

                using (outStream)
                {
                    await ms.CopyToAsync(outStream);
                }
                using (HttpWebResponse httpResponse = (HttpWebResponse)(await req.GetResponseAsync()))
                {
                    using (Stream responseStream = httpResponse.GetResponseStream())
                    {
                        HttpReply httpReply = DxSerializationUtil.Deserialize <HttpReply>(responseStream);
                        if (httpReply is HttpReply.GetInstanceStatusReply)
                        {
                            reply = (httpReply as HttpReply.GetInstanceStatusReply).Reply;
                        }
                        else
                        {
                            if (httpReply is HttpReply.ExceptionReply)
                            {
                                Exception exception = (httpReply as HttpReply.ExceptionReply).Exception;
                                throw new DxStoreInstanceServerException(exception.Message, exception);
                            }
                            throw new DxStoreInstanceClientException(string.Format("unexpected reply: {0}", httpReply.GetType().FullName));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExTraceGlobals.InstanceClientTracer.TraceError <string, Exception>(0L, "GetStatusAsync to {0} caught: {1}", targetNodeName, ex);
                if (ex is DxStoreInstanceClientException || ex is DxStoreInstanceServerException)
                {
                    throw;
                }
                throw new DxStoreInstanceClientException(ex.Message, ex);
            }
            return(reply);
        }
Exemple #3
0
 public static T TryDeserialize <T>(Stream stream, out Exception ex) where T : class
 {
     ex = null;
     try
     {
         return(DxSerializationUtil.Deserialize <T>(stream));
     }
     catch (Exception ex2)
     {
         ex = ex2;
     }
     return(default(T));
 }
        private TReply Execute <TRequest, TReply>(TRequest request, TimeSpan?timeout = null) where TRequest : DxStoreRequestBase where TReply : DxStoreReplyBase
        {
            string text = null;
            TReply result;

            try
            {
                text = HttpConfiguration.FormClientUriPrefix(this.TargetInfo.TargetHost, this.TargetInfo.TargetNode, this.TargetInfo.GroupName);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(text);
                if (timeout != null)
                {
                    httpWebRequest.Timeout = (int)timeout.Value.TotalMilliseconds;
                }
                else
                {
                    httpWebRequest.Timeout = this.TimeoutInMsec;
                }
                httpWebRequest.Method      = "PUT";
                httpWebRequest.ContentType = "application/octet-stream";
                HttpRequest.DxStoreRequest msg = new HttpRequest.DxStoreRequest(this.Self, request);
                MemoryStream memoryStream      = DxSerializationUtil.SerializeMessage(msg);
                httpWebRequest.ContentLength = memoryStream.Length;
                memoryStream.Position        = 0L;
                Stream requestStream = httpWebRequest.GetRequestStream();
                using (requestStream)
                {
                    memoryStream.CopyTo(requestStream);
                }
                using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                {
                    using (Stream responseStream = httpWebResponse.GetResponseStream())
                    {
                        HttpReply httpReply = DxSerializationUtil.Deserialize <HttpReply>(responseStream);
                        HttpReply.DxStoreReply dxStoreReply = httpReply as HttpReply.DxStoreReply;
                        if (dxStoreReply != null)
                        {
                            TReply treply = dxStoreReply.Reply as TReply;
                            if (treply == null)
                            {
                                throw new DxStoreAccessClientException(string.Format("Unexpected DxStoreReply {0}", dxStoreReply.Reply.GetType().FullName));
                            }
                            result = treply;
                        }
                        else
                        {
                            HttpReply.ExceptionReply exceptionReply = httpReply as HttpReply.ExceptionReply;
                            if (exceptionReply != null)
                            {
                                Exception exception = exceptionReply.Exception;
                                throw new DxStoreServerException(exception.Message, exception);
                            }
                            throw new DxStoreServerException(string.Format("unexpected reply: {0}", httpReply.GetType().FullName));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExTraceGlobals.AccessClientTracer.TraceError <string, string>(0L, "HttpSend failed. Uri={0} Req={1} Ex={2}", text, request.GetType().FullName);
                if (ex is DxStoreAccessClientException || ex is DxStoreServerException)
                {
                    throw;
                }
                throw new DxStoreAccessClientException(ex.Message, ex);
            }
            return(result);
        }
Exemple #5
0
 public static MemoryStream SerializeMessage(HttpRequest msg)
 {
     return(DxSerializationUtil.Serialize <HttpRequest>(msg));
 }
Exemple #6
0
        private void ListenerCallback(IAsyncResult asyncContext)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.ListenForAnotherRequest));
            Exception arg = null;

            try
            {
                HttpListenerContext httpListenerContext = this.listener.EndGetContext(asyncContext);
                HttpListenerRequest request             = httpListenerContext.Request;
                using (HttpListenerResponse response = httpListenerContext.Response)
                {
                    using (Stream inputStream = request.InputStream)
                    {
                        int          num          = (int)request.ContentLength64;
                        MemoryStream memoryStream = new MemoryStream(num);
                        inputStream.CopyTo(memoryStream);
                        memoryStream.Position = 0L;
                        HttpRequest httpRequest = DxSerializationUtil.TryDeserialize <HttpRequest>(memoryStream, out arg);
                        if (httpRequest != null)
                        {
                            ExTraceGlobals.InstanceTracer.TraceDebug <string, int>(0L, "Listener got {0} of size {1}", httpRequest.GetType().FullName, num);
                            HttpReply httpReply = null;
                            try
                            {
                                httpReply = this.msgHandler(httpRequest);
                            }
                            catch (Exception ex)
                            {
                                ExTraceGlobals.InstanceTracer.TraceError <Exception>(0L, "Listener handler threw {0}", ex);
                                httpReply = new HttpReply.ExceptionReply(ex);
                            }
                            if (httpReply == null)
                            {
                                response.ContentLength64 = 0L;
                                response.Close();
                                return;
                            }
                            using (MemoryStream memoryStream2 = DxSerializationUtil.Serialize <HttpReply>(httpReply))
                            {
                                response.ContentLength64 = memoryStream2.Length;
                                memoryStream2.Position   = 0L;
                                using (Stream outputStream = response.OutputStream)
                                {
                                    memoryStream2.CopyTo(outputStream);
                                }
                                ExTraceGlobals.InstanceTracer.TraceDebug <string, long>(0L, "Listener returns {0} of size {1}", httpReply.GetType().FullName, memoryStream2.Length);
                                response.Close();
                                return;
                            }
                        }
                        response.StatusCode      = 400;
                        response.ContentLength64 = 0L;
                        response.Close();
                        EventLogger.LogErr("msg unhandled: {0}", new object[]
                        {
                            (httpRequest == null) ? "UnknownMsg" : httpRequest.GetType().FullName
                        });
                    }
                }
            }
            catch (Exception ex2)
            {
                arg = ex2;
                ExTraceGlobals.InstanceTracer.TraceError <Exception>(0L, "Listener caught {0}", arg);
            }
        }