Exemplo n.º 1
0
            public void Create()
            {
                var log = LogManager.GetLogger(GlobalHelper.CallerName());

                try
                {
                    log.Info(string.Format("xhr open {0}: {1}", Method, Uri));
                    Xhr        = (HttpWebRequest)WebRequest.Create(Uri);
                    Xhr.Method = Method;
                    if (CookieHeaderValue != null)
                    {
                        Xhr.Headers.Add("Cookie", CookieHeaderValue);
                        log.Info("added header " + CookieHeaderValue);
                    }
                    else
                    {
                        log.Info("not added header " + CookieHeaderValue);
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                    OnError(e);
                    return;
                }

                if (Method == "POST")
                {
                    Xhr.ContentType = "application/octet-stream";
                }

                try
                {
                    if (Data != null)
                    {
                        Xhr.ContentLength = Data.Length;

                        using (var requestStream = Xhr.GetRequestStream())
                        {
                            requestStream.Write(Data, 0, Data.Length);
                        }
                    }

                    /// TODO
                    //Task.Factory.StartNew(() =>
                    //{
                    //    var log2 = LogManager.GetLogger(GlobalHelper.CallerName());
                    //    log2.Info("Task.Run Create start");
                    //    using (var res = Xhr.GetResponse())
                    //    {
                    //        log.Info("Xhr.GetResponse ");

                    //        var responseHeaders = new Dictionary<string, string>();
                    //        for (int i = 0; i < res.Headers.Count; i++)
                    //        {
                    //            responseHeaders.Add(res.Headers.Keys[i], res.Headers[i]);
                    //        }
                    //        OnResponseHeaders(responseHeaders);

                    //        var contentType = res.Headers["Content-Type"];



                    //        using (var resStream = res.GetResponseStream())
                    //        {
                    //            //Debug.Assert(resStream != null, "resStream != null");
                    //            if (contentType.Equals("application/octet-stream",
                    //                StringComparison.OrdinalIgnoreCase))
                    //            {
                    //                var buffer = new byte[16 * 1024];
                    //                using (var ms = new MemoryStream())
                    //                {
                    //                    int read;
                    //                    while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
                    //                    {
                    //                        ms.Write(buffer, 0, read);
                    //                    }
                    //                    var a = ms.ToArray();
                    //                    OnData(a);
                    //                }
                    //            }
                    //            else
                    //            {
                    //                using (var sr = new StreamReader(resStream))
                    //                {
                    //                    OnData(sr.ReadToEnd());
                    //                }
                    //            }
                    //        }
                    //    }
                    //    log2.Info("Task.Run Create finish");

                    //}, CancellationToken.None).Wait();

                    // var taskWorker = new TaskWorker(this);
                    //taskWorker.RunAndWait(
                    //    (x, e) =>
                    //    {
                    var log2 = LogManager.GetLogger(GlobalHelper.CallerName());
                    log2.Info("Task.Run Create start");
                    using (var res = Xhr.GetResponse())
                    {
                        log.Info("Xhr.GetResponse ");

                        var responseHeaders = new Dictionary <string, string>();
                        for (int i = 0; i < res.Headers.Count; i++)
                        {
                            responseHeaders.Add(res.Headers.Keys[i], res.Headers[i]);
                        }
                        OnResponseHeaders(responseHeaders);

                        var contentType = res.Headers["Content-Type"];

                        using (var resStream = res.GetResponseStream())
                        {
                            //Debug.Assert(resStream != null, "resStream != null");
                            if (contentType.Equals("application/octet-stream",
                                                   StringComparison.OrdinalIgnoreCase))
                            {
                                var buffer = new byte[16 * 1024];
                                using (var ms = new MemoryStream())
                                {
                                    int read;
                                    while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        ms.Write(buffer, 0, read);
                                    }
                                    var a = ms.ToArray();
                                    OnData(a);
                                }
                            }
                            else
                            {
                                using (var sr = new StreamReader(resStream))
                                {
                                    OnData(sr.ReadToEnd());
                                }
                            }
                        }
                    }
                    log2.Info("Task.Run Create finish");
                    //},
                    //null,
                    //20000);
                }
                catch (System.IO.IOException e)
                {
                    log.Error("Create call failed", e);
                    OnError(e);
                }
                catch (System.Net.WebException e)
                {
                    log.Error("Create call failed", e);
                    OnError(e);
                }
                catch (Exception e)
                {
                    log.Error("Create call failed", e);
                    OnError(e);
                }
            }
Exemplo n.º 2
0
            private Packet decodeString(string str)
            {
                Packet p = new Packet();
                int    i = 0;

                p.Type = int.Parse(str.Substring(0, 1));
                if (p.Type < 0 || p.Type > types.Count - 1)
                {
                    return(ErrorPacket);
                }

                if (BINARY_EVENT == p.Type || BINARY_ACK == p.Type)
                {
                    StringBuilder attachments = new StringBuilder();
                    while (str.Substring(++i, 1) != "-")
                    {
                        attachments.Append(str.Substring(i, 1));
                    }
                    p.Attachments = int.Parse(attachments.ToString());
                }

                if (str.Length > i + 1 && "/" == str.Substring(i + 1, 1))
                {
                    var nsp = new StringBuilder();
                    while (true)
                    {
                        ++i;
                        string c = str.Substring(i, 1);
                        if ("," == c)
                        {
                            break;
                        }
                        nsp.Append(c);
                        if (i + 1 == str.Length)
                        {
                            break;
                        }
                    }
                    p.Nsp = nsp.ToString();
                }
                else
                {
                    p.Nsp = "/";
                }

                var next = (i + 1) >= str.Length ? null : str.Substring(i + 1, 1);

                int unused;

                if (null != next && int.TryParse(next, out unused))
                {
                    var id = new StringBuilder();
                    while (true)
                    {
                        ++i;
                        var c = str.Substring(i, 1);

                        if (!int.TryParse(c, out unused))
                        {
                            --i;
                            break;
                        }
                        id.Append(c);
                        if (i + 1 >= str.Length)
                        {
                            break;
                        }
                    }
                    p.Id = int.Parse(id.ToString());
                }


                if (i++ < str.Length)
                {
                    try
                    {
                        var t = str.Substring(i);
                        p.Data = new JValue(t);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        // do nothing
                    }
                    catch (Exception)
                    {
                        return(ErrorPacket);
                    }
                }
                var log = LogManager.GetLogger(GlobalHelper.CallerName());

                log.Info(string.Format("decoded {0} as {1}", str, p));
                return(p);
            }