Esempio n. 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ops"></param>
        /// <returns></returns>
        public HttpResult batch(string[] ops)
        {
            HttpResult batchResult = null;
            string batchUrl = string.Format("{0}{1}", Config.ZONE.RsHost, "/batch");

            StringBuilder opsBuilder = new StringBuilder();
            for (int i = 0; i < ops.Length; i++)
            {
                opsBuilder.AppendFormat("op={0}", ops[i]);
                if (i < ops.Length - 1)
                {
                    opsBuilder.Append("&");
                }
            }
            string accessToken = Auth.createManageToken(batchUrl, Encoding.UTF8.GetBytes(opsBuilder.ToString()), this.mac);
            Dictionary<string, string> batchHeaders = new Dictionary<string, string>();
            batchHeaders.Add("Authorization", accessToken);
            CompletionHandler batchCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                batchResult = new FetchResult();
                batchResult.Response = response;
                batchResult.ResponseInfo = respInfo;
            });

            Dictionary<string, string[]> batchParams = new Dictionary<string, string[]>();
            batchParams.Add("op", ops);
            this.mHttpManager.postForm(batchUrl, batchHeaders, batchParams, batchCompletionHandler);
            return batchResult;
        }
Esempio n. 2
0
        /// <summary>
        ///   Fetch from the <see cref = "Remote" />.
        /// </summary>
        /// <param name="remote">The remote to fetch</param>
        /// <param name="tagFetchMode">Optional parameter indicating what tags to download.</param>
        /// <param name="onProgress">Progress callback. Corresponds to libgit2 progress callback.</param>
        /// <param name="onCompletion">Completion callback. Corresponds to libgit2 completion callback.</param>
        /// <param name="onUpdateTips">UpdateTips callback. Corresponds to libgit2 update_tips callback.</param>
        /// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
        ///   Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
        /// <param name="credentials">Credentials to use for username/password authentication.</param>
        public virtual void Fetch(
            Remote remote,
            TagFetchMode tagFetchMode = TagFetchMode.Auto,
            ProgressHandler onProgress = null,
            CompletionHandler onCompletion = null,
            UpdateTipsHandler onUpdateTips = null,
            TransferProgressHandler onTransferProgress = null,
            Credentials credentials = null)
        {
            Ensure.ArgumentNotNull(remote, "remote");

            // We need to keep a reference to the git_cred_acquire_cb callback around
            // so it will not be garbage collected before we are done with it.
            // Note that we also have a GC.KeepAlive call at the end of the method.
            NativeMethods.git_cred_acquire_cb credentialCallback = null;

            using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
            {
                var callbacks = new RemoteCallbacks(onProgress, onCompletion, onUpdateTips);
                GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();

                Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode);

                if (credentials != null)
                {
                    credentialCallback = (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
                        NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);

                    Proxy.git_remote_set_cred_acquire_cb(
                        remoteHandle,
                        credentialCallback,
                        IntPtr.Zero);
                }

                // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
                // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
                // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
                // where the managed layer could move the git_remote_callbacks to a different location in memory,
                // but libgit2 would still reference the old address.
                //
                // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
                // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
                Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);

                try
                {
                    Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch);
                    Proxy.git_remote_download(remoteHandle, onTransferProgress);
                    Proxy.git_remote_update_tips(remoteHandle);
                }
                finally
                {
                    Proxy.git_remote_disconnect(remoteHandle);
                }
            }

            // To be safe, make sure the credential callback is kept until
            // alive until at least this point.
            GC.KeepAlive(credentialCallback);
        }
Esempio n. 3
0
 public void reqGetTest()
 {
     HttpManager target = new HttpManager();
     string url = "http://ip.taobao.com/service/getIpInfo.php?ip=100.123.199.44";
     Dictionary<string, string> pHeaders = new Dictionary<string, string>();
     pHeaders.Add("X-Reqid", "TestReqId");
     CompletionHandler pCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
     {
         Assert.AreEqual(respInfo.StatusCode, 200);
     });
     target.get(url, pHeaders, pCompletionHandler);
 }
        /// <summary>
        /// Process the specified inputFiles, using the other specified parameters.
        /// </summary>
        /// <param name="inputFiles">A list of one or more raw data text files.</param>
        /// <param name="startDate">Any timestamp prior to this ISO 8601 date will be trimmed.</param>
        /// <param name="endDate">Any timestamp after to this ISO 8601 date will be trimmed.</param>
        /// <param name="space">A smoothing value for x/y/z coordinates.</param>
        /// <param name="time">A smoothing value for time.</param>
        /// <param name="disaggregateTime">If set to <c>true</c> events that match in space but not time will not be aggregated.</param>
        /// <param name="events">A list of events to explicitly include.</param>
        public void Process(CompletionHandler completionHandler,
            List<string> inputFiles, DateTime startDate, DateTime endDate,
            float space, float time, float angle,
            bool disaggregateTime, bool disaggregateAngle,
            List<string> events = null)
        {
            m_CompletionHandler = completionHandler;

            string outputFileName = System.IO.Path.GetFileName(inputFiles[0]).Replace(".txt", ".json");
            var outputData = new Dictionary<string, List<Dictionary<string, float>>>();

            m_ReportFiles = 0;
            m_ReportLegalPoints = 0;
            m_ReportRows = 0;
            m_PointDict = new Dictionary<Tuplish, Dictionary<string, float>>();

            foreach (string file in inputFiles)
            {
                m_ReportFiles++;
                LoadStream(outputData, file, startDate, endDate, space, time, angle, disaggregateTime, disaggregateAngle, events, outputFileName);
            }

            // Test if any data was generated
            bool hasData = false;
            var reportList = new List<int>{ };
            foreach (var generated in outputData)
            {
                hasData = generated.Value.Count > 0;
                reportList.Add(generated.Value.Count);
                if (!hasData)
                {
                    break;
                }
            }
            if (hasData)
            {
                var reportArray = reportList.Select(x => x.ToString()).ToArray();

                //Output what happened
                string report = "Report of " + m_ReportFiles + " files:\n";
                report += "Total of " + reportList.Count + " groups numbering [" + string.Join(",", reportArray) + "]\n";
                report += "Total rows: " + m_ReportRows + "\n";
                report += "Total points analyzed: " + m_ReportLegalPoints;
                Debug.Log(report);

                SaveFile(outputFileName, outputData);
            }
            else
            {
                Debug.LogWarning("The aggregation process yielded no results.");
            }
        }
Esempio n. 5
0
        public void reqPostTest()
        {
            HttpManager target = new HttpManager();
            string pUrl = "http://ip.taobao.com/service/getIpInfo.php";
            Dictionary<string, string> pHeaders = new Dictionary<string, string>();
            Dictionary<string, string[]> pPostParams = new Dictionary<string, string[]>();
            pPostParams.Add("ip", new string[] { "100.123.199.44", "100.123.199.45" });

            CompletionHandler pCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                Assert.AreEqual(respInfo.StatusCode, 200);
            });
            target.postForm(pUrl, pHeaders, pPostParams, pCompletionHandler);
        }
Esempio n. 6
0
        public HttpResult batch(string ops)
        {
            HttpResult batchResult = null;
            string batchUrl = string.Format("{0}{1}", Config.ZONE.RsHost, "/batch");
            string accessToken = Auth.createManageToken(batchUrl, Encoding.UTF8.GetBytes(ops), this.mac);
            Dictionary<string, string> batchHeaders = new Dictionary<string, string>();
            batchHeaders.Add("Authorization", accessToken);
            CompletionHandler batchCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                batchResult = new FetchResult();
                batchResult.Response = response;
                batchResult.ResponseInfo = respInfo;
            });

            this.mHttpManager.postData(batchUrl, batchHeaders, Encoding.UTF8.GetBytes(ops),
                HttpManager.FORM_MIME_URLENCODED, batchCompletionHandler);
            return batchResult;
        }
Esempio n. 7
0
        public PfopResult pfop(string bucket, string key, string fops, string pipeline, string notifyUrl, bool force)
        {
            PfopResult pfopResult = null;

            Dictionary<string, string[]> pfopParams = new Dictionary<string, string[]>();
            pfopParams.Add("bucket", new string[] { bucket });
            pfopParams.Add("key", new string[] { key });
            pfopParams.Add("fops", new string[] { fops });
            if (!string.IsNullOrEmpty(notifyUrl))
            {
                pfopParams.Add("notifyURL", new string[] { notifyUrl });
            }
            if (force)
            {
                pfopParams.Add("force", new string[] { "1" });
            }
            if (!string.IsNullOrEmpty(pipeline))
            {
                pfopParams.Add("pipeline", new string[] { pipeline });
            }

            string pfopUrl = Config.ZONE.ApiHost + "/pfop/";

            string accessToken = Auth.createManageToken(pfopUrl, Encoding.UTF8.GetBytes(StringUtils.urlValuesEncode(pfopParams)), this.Mac);
            Dictionary<string, string> pfopHeaders = new Dictionary<string, string>();
            pfopHeaders.Add("Authorization", accessToken);

            CompletionHandler pfopCompletionHandler = new CompletionHandler(delegate (ResponseInfo respInfo, string response)
            {
                if (respInfo.isOk())
                {
                    pfopResult = StringUtils.jsonDecode<PfopResult>(response);
                }
                else
                {
                    pfopResult = new PfopResult();
                }
                pfopResult.ResponseInfo = respInfo;
                pfopResult.Response = response;
            });

            this.mHttpManager.postForm(pfopUrl, pfopHeaders, pfopParams, pfopCompletionHandler);
            return pfopResult;
        }
Esempio n. 8
0
        public PrefopResult prefop()
        {
            PrefopResult prefopResult = null;

            CompletionHandler prefopCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.isOk())
                {
                    prefopResult = StringUtils.jsonDecode<PrefopResult>(response);
                }
                else
                {
                    prefopResult = new PrefopResult();
                }
                prefopResult.ResponseInfo = respInfo;
                prefopResult.Response = response;
            });
            string qUrl = string.Format(Config.ZONE.ApiHost + "/status/get/prefop?id={0}", this.PersistentId);
            this.mHttpManager.get(qUrl, null, prefopCompletionHandler);
            return prefopResult;
        }
 public void DownloadFileAsync(string url, string filePath, CompletionHandler handler)
 {
     this.downloadCompletionHandler = handler;
     base.DownloadFileAsync(new Uri(url), filePath);
 }
Esempio n. 10
0
 private void makeBlock(string upHost, long offset, int blockSize, int chunkSize,
     ProgressHandler progressHandler, CompletionHandler completionHandler)
 {
     string url = string.Format("{0}/mkblk/{1}", upHost, blockSize);
     try
     {
         this.fileStream.Read(this.chunkBuffer, 0, chunkSize);
     }
     catch (Exception ex)
     {
         this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), "");
         return;
     }
     this.crc32 = CRC32.CheckSumBytes(this.chunkBuffer, chunkSize);
     post(url, this.chunkBuffer, chunkSize, progressHandler, completionHandler);
 }
Esempio n. 11
0
        public ServerCapabilities GetCapabilities(ClientCapabilities clientCapabilities)
        {
            var capabilities = new ServerCapabilities();

            if (clientCapabilities is VSInternalClientCapabilities vsClientCapabilities && vsClientCapabilities.SupportsVisualStudioExtensions)
            {
                capabilities = GetVSServerCapabilities();
            }

            var commitCharacters  = CompletionRules.Default.DefaultCommitCharacters.Select(c => c.ToString()).ToArray();
            var triggerCharacters = _completionProviders.SelectMany(
                lz => CompletionHandler.GetTriggerCharacters(lz.Value)).Distinct().Select(c => c.ToString()).ToArray();

            capabilities.DefinitionProvider     = true;
            capabilities.RenameProvider         = true;
            capabilities.ImplementationProvider = true;
            capabilities.CodeActionProvider     = new CodeActionOptions {
                CodeActionKinds = new[] { CodeActionKind.QuickFix, CodeActionKind.Refactor }, ResolveProvider = true
            };
            capabilities.CompletionProvider = new VisualStudio.LanguageServer.Protocol.CompletionOptions
            {
                ResolveProvider     = true,
                AllCommitCharacters = commitCharacters,
                TriggerCharacters   = triggerCharacters,
            };

            capabilities.SignatureHelpProvider = new SignatureHelpOptions {
                TriggerCharacters = new[] { "(", "," }
            };
            capabilities.DocumentSymbolProvider           = true;
            capabilities.WorkspaceSymbolProvider          = true;
            capabilities.DocumentFormattingProvider       = true;
            capabilities.DocumentRangeFormattingProvider  = true;
            capabilities.DocumentOnTypeFormattingProvider = new DocumentOnTypeFormattingOptions {
                FirstTriggerCharacter = "}", MoreTriggerCharacter = new[] { ";", "\n" }
            };
            capabilities.ReferencesProvider     = true;
            capabilities.FoldingRangeProvider   = true;
            capabilities.ExecuteCommandProvider = new ExecuteCommandOptions();
            capabilities.TextDocumentSync       = new TextDocumentSyncOptions
            {
                Change    = TextDocumentSyncKind.Incremental,
                OpenClose = true
            };

            capabilities.HoverProvider = true;

            capabilities.SemanticTokensOptions = new SemanticTokensOptions
            {
                Full = new SemanticTokensFullOptions {
                    Delta = true
                },
                Range  = true,
                Legend = new SemanticTokensLegend
                {
                    TokenTypes     = SemanticTokenTypes.AllTypes.Concat(SemanticTokensHelpers.RoslynCustomTokenTypes).ToArray(),
                    TokenModifiers = new string[] { SemanticTokenModifiers.Static }
                }
            };

            return(capabilities);
        }
Esempio n. 12
0
        private void makeFile(string upHost, CompletionHandler completionHandler)
        {
            string fname = this.key;
            if (!string.IsNullOrEmpty(this.filePath))
            {
                fname = Path.GetFileName(this.filePath);
            }

            string fnameStr = "";
            if (!string.IsNullOrEmpty(fname))
            {
                fnameStr = string.Format("/fname/{0}", StringUtils.urlSafeBase64Encode(fname));
            }

            string mimeTypeStr = string.Format("/mimeType/{0}", StringUtils.urlSafeBase64Encode(this.uploadOptions.MimeType));

            string keyStr = "";
            if (this.key != null)
            {
                keyStr = string.Format("/key/{0}", StringUtils.urlSafeBase64Encode(this.key));
            }

            string paramsStr = "";
            if (this.uploadOptions.ExtraParams.Count > 0)
            {
                string[] paramArray = new string[this.uploadOptions.ExtraParams.Count];
                int j = 0;
                foreach (KeyValuePair<string, string> kvp in this.uploadOptions.ExtraParams)
                {
                    paramArray[j++] = string.Format("{0}/{1}", kvp.Key, StringUtils.urlSafeBase64Encode(kvp.Value));
                }
                paramsStr = "/" + StringUtils.join(paramArray, "/");
            }

            string url = string.Format("{0}/mkfile/{1}{2}{3}{4}{5}", upHost, this.size, mimeTypeStr, fnameStr, keyStr, paramsStr);
            string postBody = StringUtils.join(this.contexts, ",");
            byte[] postBodyData = Encoding.UTF8.GetBytes(postBody);
            this.mHttpManager.postData(url, upHeaders, postBodyData, HttpManager.FORM_MIME_URLENCODED, completionHandler);
        }
Esempio n. 13
0
 private void putChunk(string upHost, long offset, int chunkSize, string context,
     ProgressHandler progressHandler, CompletionHandler completionHandler)
 {
     int chunkOffset = (int)(offset % Config.BLOCK_SIZE);
     string url = string.Format("{0}/bput/{1}/{2}", upHost, context, chunkOffset);
     try
     {
         this.fileStream.Read(this.chunkBuffer, 0, chunkSize);
     }
     catch (Exception ex)
     {
         this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), "");
         return;
     }
     this.crc32 = CRC32.CheckSumSlice(this.chunkBuffer, 0, chunkSize);
     this.mHttpManager.postData(url, this.upHeaders, this.chunkBuffer, 0, chunkSize,
         HttpManager.FORM_MIME_OCTECT, new CompletionHandler(delegate(ResponseInfo respInfo,string response)
         {
             progressHandler(offset, this.size);
             completionHandler(respInfo, response);
         }));
 }
Esempio n. 14
0
        public HttpResult updateLifecycle(string bucket,string key,int deleteAfterDays)
        {
            HttpResult updateResult = null;

            string updateUrl = string.Format("{0}{1}", Config.ZONE.RsHost, updateLifecycleOp(bucket, key, deleteAfterDays));
            string accessToken = Auth.createManageToken(updateUrl, null, this.mac);
            Dictionary<string, string> updateHeaders = new Dictionary<string, string>();
            updateHeaders.Add("Authorization", accessToken);
            CompletionHandler updateCompletionHandler = new CompletionHandler(delegate (ResponseInfo respInfo, string response)
            {
                updateResult = new HttpResult();
                updateResult.Response = response;
                updateResult.ResponseInfo = respInfo;
            });

            this.mHttpManager.postForm(updateUrl, updateHeaders, null, updateCompletionHandler);
            return updateResult;
        }
Esempio n. 15
0
        public BucketsResult buckets()
        {
            BucketsResult bucketsResult = null;
            List<string> buckets = new List<string>();
            string bucketsUrl = string.Format("{0}/buckets", Config.ZONE.RsHost);
            string accessToken = Auth.createManageToken(bucketsUrl, null, this.mac);
            Dictionary<string, string> bucketsHeaders = new Dictionary<string, string>();
            bucketsHeaders.Add("Authorization", accessToken);
            CompletionHandler bucketsCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                bucketsResult = new BucketsResult();
                bucketsResult.Response = response;
                bucketsResult.ResponseInfo = respInfo;
                if (respInfo.isOk())
                {
                    buckets = JsonConvert.DeserializeObject<List<string>>(response);
                    bucketsResult.Buckets = buckets;
                }
            });

            this.mHttpManager.get(bucketsUrl, bucketsHeaders, bucketsCompletionHandler);
            return bucketsResult;
        }
Esempio n. 16
0
 static StoreItemFactory()
 {
     // Always null out the thread when the job is done to ensure cleanup
     OnTaskCompleted += items => _backgroundThread = null;
 }
Esempio n. 17
0
        public VSServerCapabilities GetCapabilities()
        {
            var commitCharacters = CompletionRules.Default.DefaultCommitCharacters
                                   .Select(c => c.ToString())
                                   .ToArray();
            var triggerCharacters = _completionProviders
                                    .SelectMany(lz => CompletionHandler.GetTriggerCharacters(lz.Value))
                                    .Distinct()
                                    .Select(c => c.ToString())
                                    .ToArray();

            return(new VSServerCapabilities
            {
                DefinitionProvider = true,
                RenameProvider = true,
                ImplementationProvider = true,
                CodeActionProvider = new CodeActionOptions
                {
                    CodeActionKinds = new[] { CodeActionKind.QuickFix, CodeActionKind.Refactor }
                },
                CodeActionsResolveProvider = true,
                CompletionProvider = new LanguageServer.Protocol.CompletionOptions
                {
                    ResolveProvider = true,
                    AllCommitCharacters = commitCharacters,
                    TriggerCharacters = triggerCharacters
                },
                SignatureHelpProvider = new SignatureHelpOptions
                {
                    TriggerCharacters = new[] { "(", "," }
                },
                DocumentSymbolProvider = true,
                WorkspaceSymbolProvider = true,
                DocumentFormattingProvider = true,
                DocumentRangeFormattingProvider = true,
                DocumentOnTypeFormattingProvider = new DocumentOnTypeFormattingOptions
                {
                    FirstTriggerCharacter = "}",
                    MoreTriggerCharacter = new[] { ";", "\n" }
                },
                OnAutoInsertProvider = new DocumentOnAutoInsertOptions
                {
                    TriggerCharacters = new[] { "'", "/", "\n" }
                },
                DocumentHighlightProvider = true,
                ReferencesProvider = true,
                ProjectContextProvider = true,
                FoldingRangeProvider = true,
                SemanticTokensOptions = new SemanticTokensOptions
                {
                    DocumentProvider = new SemanticTokensDocumentProviderOptions {
                        Edits = true
                    },
                    RangeProvider = true,
                    Legend = new SemanticTokensLegend
                    {
                        TokenTypes = SemanticTokenTypes.AllTypes
                                     .Concat(SemanticTokensHelpers.RoslynCustomTokenTypes)
                                     .ToArray(),
                        TokenModifiers = new string[] { SemanticTokenModifiers.Static }
                    }
                },
                ExecuteCommandProvider = new ExecuteCommandOptions(),
                TextDocumentSync = new TextDocumentSyncOptions
                {
                    Change = TextDocumentSyncKind.Incremental,
                    OpenClose = true
                },
                // Always support hover - if any LSP client for a content type advertises support,
                // then the liveshare provider is disabled.  So we must provide for both C# and razor
                // until https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1106064/ is fixed
                // or we have different content types.
                HoverProvider = true,
                // Diagnostic requests are only supported from PullDiagnosticsInProcLanguageClient.
                SupportsDiagnosticRequests = false,
            });
        }
Esempio n. 18
0
        public void DidReceivePushNotification(MSNotificationHub notificationHub, MSNotificationHubMessage message, CompletionHandler completionHandler)
        {
            homeViewController.ProcessNotification(message.Title, message.Body);

            Console.WriteLine("Notification Title: " + message.Title);
            Console.WriteLine("Notification Body: " + message.Body);

            completionHandler(UIBackgroundFetchResult.NoData);
        }
Esempio n. 19
0
        /// <summary>
        /// Process the specified inputFiles, using the other specified parameters.
        /// </summary>
        /// <param name="inputFiles">A list of one or more raw data text files.</param>
        /// <param name="startDate">Any timestamp prior to this ISO 8601 date will be trimmed.</param>
        /// <param name="endDate">Any timestamp after to this ISO 8601 date will be trimmed.</param>
        /// <param name="aggregateOn">A list of properties on which to specify point uniqueness.</param>
        /// <param name="smoothOn">A dictionary of properties that are smoothable, along with their smoothing values. <b>Must be a subset of aggregateOn.</b></param>
        /// <param name="groupOn">A list of properties on which to group resulting lists (supports arbitrary data, plus 'eventName', 'userID', 'sessionID', 'platform', and 'debug').</param>
        /// <param name="remapDensityToField">If not blank, remaps density (aka color) to the value of the field.</param>
        /// <param name="aggregationMethod">Determines the calculation with which multiple points aggregate (default is Increment).</param>
        /// <param name="percentile">For use with the AggregationMethod.Percentile, a value between 0-1 indicating the percentile to use.</param>
        public void Process(CompletionHandler completionHandler,
                            List <string> inputFiles, DateTime startDate, DateTime endDate,
                            List <string> aggregateOn,
                            Dictionary <string, float> smoothOn,
                            List <string> groupOn,
                            string remapDensityToField,
                            AggregationMethod aggregationMethod = AggregationMethod.Increment,
                            float percentile = 0)
        {
            m_CompletionHandler = completionHandler;

            string outputFileName = System.IO.Path.GetFileName(inputFiles[0]).Replace(".gz", ".json");

            // Histograms stores all the data
            // Tuplish is the key, holding the combo that makes the point unique(often, x/y/z/t)
            // List maintains the individual lists of points
            // The HistogramHeatPoint stores all the data in the list, compacted right before saving
            var histograms = new Dictionary <Tuplish, List <HistogramHeatPoint> >();

            m_ReportFiles       = 0;
            m_ReportLegalPoints = 0;
            m_ReportRows        = 0;
            m_PointDict         = new Dictionary <Tuplish, HistogramHeatPoint>();

            var headers = GetHeaders();

            if (headers["name"] == -1 || headers["submit_time"] == -1 || headers["custom_params"] == -1)
            {
                Debug.LogWarning("No headers found. The likeliest cause of this is that you have no custom_headers.gz file. Perhaps you need to download a raw data job?");
            }
            else
            {
                foreach (string file in inputFiles)
                {
                    m_ReportFiles++;
                    LoadStream(histograms, headers, file, startDate, endDate,
                               aggregateOn, smoothOn, groupOn,
                               remapDensityToField);
                }

                // Test if any data was generated
                bool       hasData    = false;
                List <int> reportList = new List <int>();

                Dictionary <Tuplish, List <Dictionary <string, float> > > outputData = new Dictionary <Tuplish, List <Dictionary <string, float> > >();

                foreach (var generated in histograms)
                {
                    // Convert to output. Perform accretion calcs
                    var list       = generated.Value;
                    var outputList = new List <Dictionary <string, float> >();
                    foreach (var pt in list)
                    {
                        var outputPt = new Dictionary <string, float>();
                        outputPt["x"]  = (float)pt["x"];
                        outputPt["y"]  = (float)pt["y"];
                        outputPt["z"]  = (float)pt["z"];
                        outputPt["rx"] = (float)pt["rx"];
                        outputPt["ry"] = (float)pt["ry"];
                        outputPt["rz"] = (float)pt["rz"];
                        outputPt["dx"] = (float)pt["dx"];
                        outputPt["dy"] = (float)pt["dy"];
                        outputPt["dz"] = (float)pt["dz"];
                        outputPt["t"]  = (float)pt["t"];
                        outputPt["d"]  = Accrete(pt, aggregationMethod, percentile);

                        outputList.Add(outputPt);
                    }
                    outputData.Add(generated.Key, outputList);

                    hasData = generated.Value.Count > 0;
                    reportList.Add(generated.Value.Count);
                    if (!hasData)
                    {
                        break;
                    }
                }

                if (hasData)
                {
                    var reportArray = reportList.Select(x => x.ToString()).ToArray();

                    //Output what happened
                    string report = "Report of " + m_ReportFiles + " files:\n";
                    report += "Total of " + reportList.Count + " groups numbering [" + string.Join(",", reportArray) + "]\n";
                    report += "Total rows: " + m_ReportRows + "\n";
                    report += "Total points analyzed: " + m_ReportLegalPoints;
                    Debug.Log(report);

                    SaveFile(outputFileName, outputData);
                }
                else
                {
                    Debug.LogWarning("The aggregation process yielded no results.");
                }
            }
        }
Esempio n. 20
0
 public static void StartRecording(int width, int height, float framerate, int sampleRate, int channelCount, int bitrate, CompletionHandler callback, IntPtr context)
 {
 }
Esempio n. 21
0
        private void upload(HttpFormFile fFile, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            // 使用uploadHost -- REMINDME-0
            // 是否使用CDN(默认:是)
            string uploadHost = Config.UploadFromCDN ? Config.ZONE.UploadHost : Config.ZONE.UpHost;

            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary <string, string> vPostParams = new Dictionary <string, string>();

            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                case HttpFileType.DATA_SLICE:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                    break;

                case HttpFileType.DATA_BYTES:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                    break;

                case HttpFileType.FILE_STREAM:
                    long   streamLength = fFile.BodyStream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                    fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    break;

                case HttpFileType.FILE_PATH:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                    break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });


            // [x]第一次失败后使用备用域名重试一次
            // FIX 2016-11-22 17:11
            // 网络错误(网络断开)恢复正常后,重试会出现“流不可读”的错误,因此原域名和重试域名一致
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}", respInfo.StatusCode);
                if (respInfo.needRetry())
                {
                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });

                    // 使用uploadHost -- REMINDME-1
                    this.mHttpManager.postMultipartDataForm(uploadHost, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            // // 使用uploadHost -- REMINDME-2
            this.mHttpManager.postMultipartDataForm(uploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
 public void SubCompletion(CompletionHandler dl)
 {
     _audioPlayer.ComplectionEvent += dl;
 }
Esempio n. 23
0
        /// <summary>
        /// post binary data to remote server
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="pPostData"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <param name="pCompletionHandler"></param>
        public void postData(string pUrl, Dictionary<string, string> pHeaders,
            byte[] pPostData, int offset, int count, string contentType,
            CompletionHandler pCompletionHandler)
        {
            HttpWebRequest vWebReq = null;
            HttpWebResponse vWebResp = null;
            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method = "POST";
                if (!string.IsNullOrEmpty(contentType))
                {
                    vWebReq.ContentType = contentType;
                }
                else
                {
                    vWebReq.ContentType = FORM_MIME_OCTECT;
                }
                if (pHeaders != null)
                {
                    foreach (KeyValuePair<string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                vWebReq.AllowWriteStreamBuffering = true;
                // write data
                using (Stream vWebReqStream = vWebReq.GetRequestStream())
                {
                    vWebReqStream.Write(pPostData, offset, count);
                    vWebReqStream.Flush();
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
            finally
            {
                if (vWebResp != null)
                {
                    vWebResp.Close();
                    vWebResp = null;
                }

                if (vWebReq != null)
                {
                    vWebReq.Abort();
                    vWebReq = null;
                }
            }
        }
Esempio n. 24
0
        private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            postArgs.Params = new Dictionary <string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                postArgs.Params.Add("key", key);
            }
            //设置token
            postArgs.Params.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (httpManager.FileContentType)
                {
                case PostContentType.BYTES:
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length)));
                    break;

                case PostContentType.STREAM:
                    long   streamLength = postArgs.Stream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = postArgs.Stream.Read(buffer, 0, (int)streamLength);
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt)));
                    postArgs.Stream.Seek(0, SeekOrigin.Begin);
                    break;

                case PostContentType.FILE:
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File)));
                    break;
                }
            }

            //设置MimeType
            postArgs.MimeType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                postArgs.Params.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            httpManager.CancellationSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });
            httpManager.PostArgs = postArgs;
            //第一次失败后使用备用域名重试一次
            httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.needRetry())
                {
                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin);
                    }
                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        uploadOptions.ProgressHandler(key, 1.0);

                        if (httpManager.PostArgs.Stream != null)
                        {
                            httpManager.PostArgs.Stream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            upCompletionHandler(key, retryRespInfo, retryResponse);
                        }
                    });
                    httpManager.CompletionHandler = retried;
                    httpManager.multipartPost(Config.UP_HOST);
                }
                else
                {
                    uploadOptions.ProgressHandler(key, 1.0);

                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
            });
            httpManager.multipartPost(Config.UPLOAD_HOST);
        }
        private void nextTask(long offset, int retried, string upHost)
        {
            //上传中途触发停止
            if (this.isCancelled())
            {
                this.upCompletionHandler(this.key, ResponseInfo.cancelled(), null);
                return;
            }
            //所有分片已上传
            if (offset == this.size)
            {
                this.makeFile(upHost, new CompletionHandler(delegate(ResponseInfo respInfo, string response)
                {
                    //makeFile成功
                    if (respInfo.isOk())
                    {
                        removeRecord();
                        Console.WriteLine("mkfile ok, upload done!");
                        this.upCompletionHandler(this.key, respInfo, response);
                        return;
                    }

                    //失败重试,如果614,则不重试
                    if (respInfo.StatusCode != 614)
                    {
                        if (respInfo.needRetry() && retried < Config.RETRY_MAX)
                        {
                            Console.WriteLine("mkfile retrying due to {0}...", respInfo.StatusCode);
                            string upHost2 = Config.ZONE.UploadHost;
                            nextTask(offset, retried + 1, upHost2);
                            return;
                        }
                    }

                    Console.WriteLine("mkfile error, upload failed due to {0}", respInfo.StatusCode);
                    this.upCompletionHandler(key, respInfo, response);
                }));
                return;
            }

            //创建块或上传分片
            int             chunkSize       = calcBPutChunkSize(offset);
            ProgressHandler progressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)(offset) / this.size;
                Console.WriteLine("resumable upload progress {0}", percent);
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                this.uploadOptions.ProgressHandler(this.key, percent);
            });

            CompletionHandler completionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (offset % Config.BLOCK_SIZE == 0)
                {
                    Console.WriteLine("mkblk result {0}, offset {1}", respInfo.StatusCode, offset);
                }
                else
                {
                    Console.WriteLine("bput result {0}, offset {1}", respInfo.StatusCode, offset);
                }
                if (!respInfo.isOk())
                {
                    //如果是701错误,为mkblk的ctx过期
                    if (respInfo.StatusCode == 701)
                    {
                        nextTask((offset / Config.BLOCK_SIZE) * Config.BLOCK_SIZE, retried, upHost);
                        return;
                    }

                    if (retried >= Config.RETRY_MAX || !respInfo.needRetry())
                    {
                        this.upCompletionHandler(key, respInfo, response);
                        return;
                    }

                    String upHost2 = upHost;
                    if (respInfo.needRetry())
                    {
                        upHost2 = Config.ZONE.UploadHost;
                    }
                    nextTask(offset, retried + 1, upHost2);
                    return;
                }

                //请求成功
                string chunkContext = null;
                if (response == null || string.IsNullOrEmpty(response))
                {
                    nextTask(offset, retried + 1, upHost);
                    return;
                }

                long chunkCrc32 = 0;
                Dictionary <string, string> respDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(response);
                if (respDict.ContainsKey("ctx"))
                {
                    chunkContext = respDict["ctx"];
                }
                if (respDict.ContainsKey("crc32"))
                {
                    chunkCrc32 = Convert.ToInt64(respDict["crc32"]);
                }

                if (chunkContext == null || chunkCrc32 != this.crc32)
                {
                    nextTask(offset, retried + 1, upHost);
                    return;
                }

                this.contexts[offset / Config.BLOCK_SIZE] = chunkContext;
                record(offset + chunkSize);
                nextTask(offset + chunkSize, retried, upHost);
            });

            //创建块
            if (offset % Config.BLOCK_SIZE == 0)
            {
                int blockSize = calcMakeBlockSize(offset);
                this.makeBlock(upHost, offset, blockSize, chunkSize, progressHandler, completionHandler);
                return;
            }

            //上传分片
            string context = this.contexts[offset / Config.BLOCK_SIZE];

            this.putChunk(upHost, offset, chunkSize, context, progressHandler, completionHandler);
        }
Esempio n. 26
0
 public override void DidReceivePushNotification(MSNotificationHub notificationHub, MSNotificationHubMessage message, CompletionHandler completionHandler)
 {
     OnNotificationMessageReceivedAction?.Invoke(message);
 }
Esempio n. 27
0
        private void handleErrorWebResponse(HttpWebResponse pWebResp, CompletionHandler pCompletionHandler, Exception pExp)
        {
            DateTime startTime  = DateTime.Now;
            int      statusCode = ResponseInfo.NetworkError;
            //parse self defined code from the error message
            string expMsg     = pExp.Message;
            int    indexStart = expMsg.IndexOf("(");

            if (indexStart != -1)
            {
                int indexEnd = expMsg.IndexOf(")", indexStart);
                if (indexStart != -1 && indexEnd != -1)
                {
                    string statusCodeStr = expMsg.Substring(indexStart + 1, indexEnd - indexStart - 1);
                    try
                    {
                        statusCode = Convert.ToInt32(statusCodeStr);
                    }
                    catch (Exception) { }
                }
            }
            //check for exception
            string reqId         = null;
            string xlog          = null;
            string ip            = null;
            string xvia          = null;
            string error         = null;
            string host          = null;
            string respData      = null;
            int    contentLength = -1;
            bool   recvInvalid   = false;

            if (pWebResp != null)
            {
                if (pWebResp.Headers != null)
                {
                    WebHeaderCollection respHeaders = pWebResp.Headers;
                    foreach (string headerName in respHeaders.AllKeys)
                    {
                        if (headerName.Equals("X-Reqid"))
                        {
                            reqId = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("X-Log"))
                        {
                            xlog = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("X-Via"))
                        {
                            xvia = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("X-Px"))
                        {
                            xvia = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("Fw-Via"))
                        {
                            xvia = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("Host"))
                        {
                            host = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("Content-Length"))
                        {
                            contentLength = int.Parse(respHeaders[headerName].ToString());
                        }
                    }
                }

                using (StreamReader respStream = new StreamReader(pWebResp.GetResponseStream()))
                {
                    respData = respStream.ReadToEnd();

                    if (contentLength > 0)
                    {
                        if (respData.Length != contentLength)
                        {
                            recvInvalid = true;
                        }
                    }
                }

                try
                {
                    /////////////////////////////////////////////////////////////
                    // 改进Response的error解析, 根据HttpStatusCode
                    // @fengyh 2016-08-17 18:29
                    /////////////////////////////////////////////////////////////
                    if (statusCode != (int)HCODE.OK)
                    {
                        bool isOtherCode = HttpCode.GetErrorMessage(statusCode, out error);

                        if (isOtherCode)
                        {
                            Dictionary <string, string> errorDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(respData);
                            error = errorDict["error"];
                        }
                    }
                }
                catch (Exception) { }

                if (recvInvalid)
                {
                    statusCode = -1;
                    string err = string.Format("response-recv is not complete RECV={0},TOTAL={1} {2}", respData.Length, contentLength, error);
                    Console.WriteLine(err);
                    error = err;
                }

                pWebResp.Close();
            }
            else
            {
                error = pExp.Message;
            }

            double       duration = DateTime.Now.Subtract(startTime).TotalSeconds;
            ResponseInfo respInfo = new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, duration, error);

            if (pCompletionHandler != null)
            {
                pCompletionHandler(respInfo, respData);
            }
        }
Esempio n. 28
0
        private void upload(HttpFormFile fFile, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            string uploadHost      = "<UPLOAD_HOST>";
            string uploadHostRetry = "<UPLOAD_HOST_RETRY>";

            if (Config.UploadFromCDN)
            {
                uploadHost      = Config.ZONE.UploadHost;
                uploadHostRetry = Config.ZONE.UpHost;
            }
            else
            {
                uploadHost      = Config.ZONE.UpHost;
                uploadHostRetry = Config.ZONE.UploadHost;
            }

            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary <string, string> vPostParams = new Dictionary <string, string>();

            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                case HttpFileType.DATA_SLICE:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                    break;

                case HttpFileType.DATA_BYTES:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                    break;

                case HttpFileType.FILE_STREAM:
                    long   streamLength = fFile.BodyStream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                    fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    break;

                case HttpFileType.FILE_PATH:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                    break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });


            // 第一次失败后使用备用域名重试一次
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}", respInfo.StatusCode);

                if (respInfo.needRetry()) // 需要重试
                {
                    Console.WriteLine(string.Format("form upload retry"));

                    if (Config.RetryWaitForNext)
                    {
                        Console.WriteLine(string.Format("wait for {0} milisecond(s)", Config.RETRY_INTERVAL_MILISEC));
                        System.Threading.Thread.Sleep(Config.RETRY_INTERVAL_MILISEC);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });

                    // 使用UPLOAD_HOST_RETRY重试
                    this.mHttpManager.postMultipartDataForm(uploadHostRetry, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else // 不需要重试
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            // 使用UPLOAD_HOST上传
            this.mHttpManager.postMultipartDataForm(uploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
Esempio n. 29
0
 private void makeBlock(string upHost, long offset, int blockSize, int chunkSize,
     ProgressHandler progressHandler, CompletionHandler completionHandler)
 {
     string url = string.Format("{0}/mkblk/{1}", upHost, blockSize);
     try
     {
         this.fileStream.Read(this.chunkBuffer, 0, chunkSize);
     }
     catch (Exception ex)
     {
         this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), "");
         return;
     }
     this.crc32 = CRC32.CheckSumSlice(this.chunkBuffer, 0, chunkSize);
     this.mHttpManager.postData(url, this.upHeaders, this.chunkBuffer, 0, chunkSize,
         HttpManager.FORM_MIME_OCTECT, new CompletionHandler(delegate (ResponseInfo respInfo, string response)
         {
             progressHandler(offset, this.size);
             completionHandler(respInfo, response);
         }));
 }
Esempio n. 30
0
 public override void DidFinishEventsForBackgroundSession(NSUrlSession session)
 {
     this.manager.Session = null;
     CompletionHandler?.Invoke();
 }
Esempio n. 31
0
        private void nextTask(long offset, int retried, string upHost)
        {
            //上传中途触发停止
            if (this.isCancelled())
            {
                this.upCompletionHandler(this.key, ResponseInfo.cancelled(), null);
                return;
            }
            //所有分片已上传
            if (offset == this.size)
            {
                this.makeFile(upHost, new CompletionHandler(delegate(ResponseInfo respInfo, string response)
                {
                    //makeFile成功
                    if (respInfo.isOk())
                    {
                        removeRecord();
                        Console.WriteLine("mkfile ok, upload done!");
                        this.upCompletionHandler(this.key, respInfo, response);
                        return;
                    }

                    //失败重试,如果614,则不重试
                    if (respInfo.StatusCode != 614)
                    {
                        if (respInfo.needRetry() && retried < Config.RETRY_MAX)
                        {
                            Console.WriteLine("mkfile retrying due to {0}...", respInfo.StatusCode);
                            //string upHost2 = Config.ZONE.UploadHost;
                            fileStream.Seek(offset, SeekOrigin.Begin);
                            nextTask(offset, retried + 1, upHost);
                            return;
                        }
                    }

                    Console.WriteLine("mkfile error, upload failed due to {0}", respInfo.StatusCode);
                    this.upCompletionHandler(key, respInfo, response);
                }));
                return;
            }

            //创建块或上传分片
            int chunkSize = calcBPutChunkSize(offset);
            ProgressHandler progressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)(offset) / this.size;
                Console.WriteLine("resumable upload progress {0}", percent);
                // why 0.95
                //if (percent > 0.95)
                //{
                //    percent = 0.95;
                //}
                this.uploadOptions.ProgressHandler(this.key, percent);
            });

            CompletionHandler completionHandler = new CompletionHandler(delegate (ResponseInfo respInfo, string response)
            {
                if (offset % Config.BLOCK_SIZE == 0)
                {
                    Console.WriteLine("mkblk result {0}, offset {1}", respInfo.StatusCode, offset);
                }
                else
                {
                    Console.WriteLine("bput result {0}, offset {1}", respInfo.StatusCode, offset);
                }

                if (!respInfo.isOk())
                {
                    //如果是701错误,为mkblk的ctx过期
                    if (respInfo.StatusCode == 701)
                    {
                        Console.WriteLine("mkblk ctx is out of date, re-do upload");
                        offset = (offset / Config.BLOCK_SIZE) * Config.BLOCK_SIZE;
                        fileStream.Seek(offset, SeekOrigin.Begin);
                        nextTask(offset, retried, upHost);
                        return;
                    }

                    if (retried >= Config.RETRY_MAX || !respInfo.needRetry())
                    {
                        Console.WriteLine("retried " + retried + " times, all failed");
                        this.upCompletionHandler(key, respInfo, response);
                        return;
                    }

                    // 下一个任务,使用uploadHost
                    string uploadHost = upHost;
                    if (respInfo.needRetry())
                    {
                        Console.WriteLine(string.Format("upload-retry #{0}", retried + 1));

                        if (Config.RetryWaitForNext)
                        {
                            Console.WriteLine(string.Format("wait for {0} milisecond(s)", Config.RETRY_INTERVAL_MILISEC));
                            // 如果需要重试,并且设置了多次重试之间的时间间隔
                            System.Threading.Thread.Sleep(Config.RETRY_INTERVAL_MILISEC);
                        }

                        //// 交替使用两个域名重试
                        //if (retried % 2 != 0)
                        //{
                        //    uploadHost = Config.UploadFromCDN ? Config.ZONE.UploadHost : Config.ZONE.UpHost;
                        //}
                        //else
                        //{
                        //    uploadHost = Config.UploadFromCDN ? Config.ZONE.UpHost : Config.ZONE.UploadHost;
                        //}
                    }
                    fileStream.Seek(offset, SeekOrigin.Begin);
                    nextTask(offset, retried + 1, uploadHost);
                    return;
                }

                //请求成功
                string chunkContext = null;
                if (response == null || string.IsNullOrEmpty(response))
                {
                    fileStream.Seek(offset, SeekOrigin.Begin);
                    nextTask(offset, retried + 1, upHost);
                    return;
                }

                long chunkCrc32 = 0;

                Dictionary<string, string> respDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
                if (respDict.ContainsKey("ctx"))
                {
                    chunkContext = respDict["ctx"];
                }
                if (respDict.ContainsKey("crc32"))
                {
                    chunkCrc32 = Convert.ToInt64(respDict["crc32"]);
                }
                //if(respDict.ContainsKey("host"))
                //{
                //    upHost = respDict["host"];
                //}

                if (chunkContext == null || chunkCrc32 != this.crc32)
                {
                    fileStream.Seek(offset, SeekOrigin.Begin);
                    nextTask(offset, retried + 1, upHost);
                    return;
                }

                this.contexts[offset / Config.BLOCK_SIZE] = chunkContext;
                record(offset + chunkSize);
                nextTask(offset + chunkSize, retried, upHost);
            });

            //创建块
            if (offset % Config.BLOCK_SIZE == 0)
            {
                int blockSize = calcMakeBlockSize(offset);
                this.makeBlock(upHost, offset, blockSize, chunkSize, progressHandler, completionHandler);
                return;
            }

            //上传分片
            string context = this.contexts[offset / Config.BLOCK_SIZE];
            this.putChunk(upHost, offset, chunkSize, context, progressHandler, completionHandler);
        }
Esempio n. 32
0
        private void nextTask(long offset, int retried, string upHost)
        {
            //上传中途触发停止
            if (this.isCancelled())
            {
                this.upCompletionHandler(this.key, ResponseInfo.cancelled(), null);
                return;
            }
            //所有分片已上传
            if (offset == this.size)
            {
                this.makeFile(upHost, new CompletionHandler(delegate(ResponseInfo respInfo, string response)
                {
                    //makeFile成功
                    if (respInfo.isOk())
                    {
                        removeRecord();
                        this.upCompletionHandler(this.key, respInfo, response);
                        return;
                    }

                    //失败重试
                    if (respInfo.needRetry() && retried < Config.RETRY_MAX)
                    {
                        string upHost2 = Config.UP_HOST;
                        nextTask(offset, retried + 1, upHost2);
                        return;
                    }

                    this.upCompletionHandler(key, respInfo, response);
                }));
                return;
            }

            //创建块或上传分片
            int chunkSize = calcBPutChunkSize(offset);
            ProgressHandler progressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)(offset + bytesWritten) / this.size;
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                this.uploadOptions.ProgressHandler(this.key, percent);
            });

            CompletionHandler completionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (!respInfo.isOk())
                {
                    //如果是701错误,为mkblk的ctx过期
                    if (respInfo.StatusCode == 701)
                    {
                        nextTask((offset / Config.BLOCK_SIZE) * Config.BLOCK_SIZE, retried, upHost);
                        return;
                    }

                    if (retried >= Config.RETRY_MAX || !respInfo.needRetry())
                    {
                        this.upCompletionHandler(key, respInfo, response);
                        return;
                    }

                    String upHost2 = upHost;
                    if (respInfo.needRetry())
                    {
                        upHost2 = Config.UP_HOST;
                    }
                    nextTask(offset, retried + 1, upHost2);
                    return;
                }

                //请求成功
                string chunkContext = null;
                if (response == null || string.IsNullOrEmpty(response))
                {
                    nextTask(offset, retried + 1, upHost);
                    return;
                }

                long chunkCrc32 = 0;
                Dictionary<string, string> respDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
                if (respDict.ContainsKey("ctx"))
                {
                    chunkContext = respDict["ctx"];
                }
                if (respDict.ContainsKey("crc32"))
                {
                    chunkCrc32 = Convert.ToInt64(respDict["crc32"]);
                }

                if (chunkContext == null || chunkCrc32 != this.crc32)
                {
                    nextTask(offset, retried + 1, upHost);
                    return;
                }

                this.contexts[offset / Config.BLOCK_SIZE] = chunkContext;
                record(offset + chunkSize);
                nextTask(offset + chunkSize, retried, upHost);
            });

            //创建块
            if (offset % Config.BLOCK_SIZE == 0)
            {
                int blockSize = calcMakeBlockSize(offset);
                this.makeBlock(upHost, offset, blockSize, chunkSize, progressHandler, completionHandler);
                return;
            }

            //上传分片
            string context = this.contexts[offset / Config.BLOCK_SIZE];
            this.putChunk(upHost, offset, chunkSize, context, progressHandler, completionHandler);
        }
Esempio n. 33
0
 internal RemoteCallbacks(ProgressHandler onProgress = null, CompletionHandler onCompletion = null, UpdateTipsHandler onUpdateTips = null)
 {
     Progress = onProgress;
     Completion = onCompletion;
     UpdateTips = onUpdateTips;
 }
Esempio n. 34
0
 private void putChunk(string upHost, long offset, int chunkSize, string context,
     ProgressHandler progressHandler, CompletionHandler completionHandler)
 {
     int chunkOffset = (int)(offset % Config.BLOCK_SIZE);
     string url = string.Format("{0}/bput/{1}/{2}", upHost, context, chunkOffset);
     try
     {
         this.fileStream.Read(this.chunkBuffer, 0, chunkSize);
     }
     catch (Exception ex)
     {
         this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), "");
         return;
     }
     this.crc32 = CRC32.CheckSumBytes(this.chunkBuffer, chunkSize);
     post(url, this.chunkBuffer, chunkSize, progressHandler, completionHandler);
 }
Esempio n. 35
0
        private void makeFile(string upHost, CompletionHandler completionHandler)
        {
            string mimeTypeStr = string.Format("/mimeType/{0}", StringUtils.urlSafeBase64Encode(this.uploadOptions.MimeType));

            string keyStr = "";
            if (this.key != null)
            {
                keyStr = string.Format("/key/{0}", StringUtils.urlSafeBase64Encode(this.key));
            }

            string paramsStr = "";
            if (this.uploadOptions.ExtraParams.Count > 0)
            {
                string[] paramArray = new string[this.uploadOptions.ExtraParams.Count];
                int j = 0;
                foreach (KeyValuePair<string, string> kvp in this.uploadOptions.ExtraParams)
                {
                    paramArray[j++] = string.Format("{0}/{1}", kvp.Key, StringUtils.urlSafeBase64Encode(kvp.Value));
                }
                paramsStr = "/" + StringUtils.join(paramArray, "/");
            }

            string url = string.Format("{0}/mkfile/{1}{2}{3}{4}", upHost, this.size, mimeTypeStr, keyStr, paramsStr);
            string postBody = StringUtils.join(this.contexts, ",");
            byte[] postBodyData = Encoding.UTF8.GetBytes(postBody);
            post(url, postBodyData, postBodyData.Length, null, completionHandler);
        }
Esempio n. 36
0
        // Override this to handle received RPC requests & notifications.
        // Call this method with `await super.DidReceiveCall(...)` to implement default calls like `getVersion`.
        // Call the completion handler when done with a request:
        // - pass your call's "return value" (or null) as `result` on success
        // - pass an instance of `JsonRpcException` for `error` on failure
        // You may also throw a `JsonRpcException` (or any other `Exception`) to signal failure.
        // Exceptions are caught even when thrown in an `async` method after `await`:
        // http://www.interact-sw.co.uk/iangblog/2010/11/01/csharp5-async-exceptions
        protected virtual async Task DidReceiveCall(string method, JObject parameters, CompletionHandler completion)
        {
            switch (method)
            {
            case "pingMe":
                await completion("willPing", null);

                SendRemoteRequest("ping", null, (result, error) =>
                {
                    Debug.Print($"Got result from ping: {result}");
                    return(Task.CompletedTask);
                });
                break;

            case "getVersion":
                await completion(GetVersion(), null);

                break;

            default:
                // unrecognized method
                throw JsonRpcException.MethodNotFound(method);
            }
        }
Esempio n. 37
0
 private void post(string url, byte[] data, int chunkSize, ProgressHandler progressHandler, CompletionHandler completionHandler)
 {
     byte[] uploadData = new byte[chunkSize];
     Array.Copy(data, uploadData, chunkSize);
     PostArgs postArgs = new PostArgs();
     postArgs.Data = uploadData;
     this.httpManager.PostArgs = postArgs;
     this.httpManager.ProgressHandler = progressHandler;
     this.httpManager.CompletionHandler = completionHandler;
     this.httpManager.postData(url);
 }
Esempio n. 38
0
        /// <summary>
        /// get info from remote server
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="pCompletionHandler"></param>
        public void get(string pUrl, Dictionary<string, string> pHeaders,
            CompletionHandler pCompletionHandler)
        {
            HttpWebRequest vWebReq = null;
            HttpWebResponse vWebResp = null;
            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
            }
            catch(WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method = "GET";
                vWebReq.UserAgent = this.getUserAgent();
                if (pHeaders != null)
                {
                    foreach (KeyValuePair<string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
            finally
            {
                if(vWebResp!=null)
                {
                    vWebResp.Close();
                    vWebResp = null;
                }

                if(vWebReq!=null)
                {
                    vWebReq.Abort();
                    vWebReq = null;
                }
            }
        }
Esempio n. 39
0
        /// <summary>
        /// post multi-part data form to remote server
        /// used to upload data
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="httpFormFile"></param>
        /// <param name="pProgressHandler"></param>
        /// <param name="pCompletionHandler"></param>
        public void postMultipartDataRaw(string pUrl, Dictionary <string, string> pHeaders,
                                         HttpFormFile pFormFile, ProgressHandler pProgressHandler, CompletionHandler pCompletionHandler)
        {
            if (pFormFile == null)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.fileError(new Exception("no file specified")), "");
                }
                return;
            }

            HttpWebRequest  vWebReq  = null;
            HttpWebResponse vWebResp = null;

            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
                vWebReq.ServicePoint.Expect100Continue = false;
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent         = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method            = "POST";

                //create boundary
                string formBoundaryStr = this.createFormDataBoundary();
                string contentType     = string.Format("multipart/form-data; boundary={0}", formBoundaryStr);
                vWebReq.ContentType = contentType;
                if (pHeaders != null)
                {
                    foreach (KeyValuePair <string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                //write post body
                vWebReq.AllowWriteStreamBuffering = true;

                byte[] formBoundaryBytes = Encoding.UTF8.GetBytes(string.Format("{0}{1}\r\n",
                                                                                FORM_BOUNDARY_TAG, formBoundaryStr));
                byte[] formBoundaryEndBytes = Encoding.UTF8.GetBytes(string.Format("\r\n{0}{1}{2}\r\n",
                                                                                   FORM_BOUNDARY_TAG, formBoundaryStr, FORM_BOUNDARY_TAG));

                using (Stream vWebReqStream = vWebReq.GetRequestStream())
                {
                    vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length);

                    //write file name
                    string filename = pFormFile.Filename;
                    if (string.IsNullOrEmpty(filename))
                    {
                        filename = this.createRandomFilename();
                    }
                    byte[] filePartTitleData = Encoding.UTF8.GetBytes(
                        string.Format("Content-Disposition: form-data; name=\"data\"; filename=\"{0}\"\r\n", filename));
                    vWebReqStream.Write(filePartTitleData, 0, filePartTitleData.Length);
                    //write content type
                    string mimeType = FORM_MIME_OCTECT;  //!!!注意这里 @fengyh 2016-08-17 15:00
                    if (!string.IsNullOrEmpty(pFormFile.ContentType))
                    {
                        mimeType = pFormFile.ContentType;
                    }
                    byte[] filePartMimeData = Encoding.UTF8.GetBytes(string.Format("Content-Type: {0}\r\n\r\n", mimeType));
                    vWebReqStream.Write(filePartMimeData, 0, filePartMimeData.Length);

                    //write file data
                    switch (pFormFile.BodyType)
                    {
                    case HttpFileType.FILE_PATH:
                        try
                        {
                            FileStream fs = File.Open(pFormFile.BodyFile, FileMode.Open, FileAccess.Read);
                            this.writeHttpRequestBody(fs, vWebReqStream);
                        }
                        catch (Exception fex)
                        {
                            if (pCompletionHandler != null)
                            {
                                pCompletionHandler(ResponseInfo.fileError(fex), "");
                            }
                        }
                        break;

                    case HttpFileType.FILE_STREAM:
                        this.writeHttpRequestBody(pFormFile.BodyStream, vWebReqStream);
                        break;

                    case HttpFileType.DATA_BYTES:
                        vWebReqStream.Write(pFormFile.BodyBytes, 0, pFormFile.BodyBytes.Length);
                        break;

                    case HttpFileType.DATA_SLICE:
                        vWebReqStream.Write(pFormFile.BodyBytes, pFormFile.Offset, pFormFile.Count);
                        break;
                    }

                    vWebReqStream.Write(formBoundaryEndBytes, 0, formBoundaryEndBytes.Length);
                    vWebReqStream.Flush();
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
        }
Esempio n. 40
0
        private void handleWebResponse(HttpWebResponse pWebResp, CompletionHandler pCompletionHandler)
        {
            DateTime startTime = DateTime.Now;
            //check for exception
            int statusCode = ResponseInfo.NetworkError;
            string reqId = null;
            string xlog = null;
            string ip = null;
            string xvia = null;
            string error = null;
            string host = null;
            string respData = null;
            int contentLength = -1;

            if (pWebResp != null)
            {
                statusCode = (int)pWebResp.StatusCode;

                if (pWebResp.Headers != null)
                {
                    WebHeaderCollection respHeaders = pWebResp.Headers;
                    foreach (string headerName in respHeaders.AllKeys)
                    {
                        if (headerName.Equals("X-Reqid"))
                        {
                            reqId = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("X-Log"))
                        {
                            xlog = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("X-Via"))
                        {
                            xvia = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("X-Px"))
                        {
                            xvia = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("Fw-Via"))
                        {
                            xvia = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("Host"))
                        {
                            host = respHeaders[headerName].ToString();
                        }
                        else if (headerName.Equals("Content-Length"))
                        {
                            contentLength = int.Parse(respHeaders[headerName].ToString());
                        }
                    }
                }

                if (contentLength > 0)
                {
                    Stream ps = pWebResp.GetResponseStream();
                    byte[] raw = new byte[contentLength];
                    int bytesRead = 0; // 已读取的字节数
                    int bytesLeft = contentLength; // 剩余字节数
                    while (bytesLeft > 0)
                    {
                        bytesRead = ps.Read(raw, contentLength - bytesLeft, bytesLeft);
                        bytesLeft -= bytesRead;
                    }

                    respData = Encoding.UTF8.GetString(raw);

                    try
                    {
                        /////////////////////////////////////////////////////////////
                        // 改进Response的error解析, 根据HttpStatusCode
                        // @fengyh 2016-08-17 18:29
                        /////////////////////////////////////////////////////////////
                        if (statusCode != (int)HCODE.OK)
                        {
                            bool isOtherCode = HttpCode.GetErrorMessage(statusCode, out error);

                            if (isOtherCode)
                            {
                                Dictionary<string, string> errorDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(respData);
                                error = errorDict["error"];
                            }
                        }
                    }
                    catch (Exception) { }
                }
                else
                {
                    //statusCode = -1;
                    //error = "response err";
                }
            }
            else
            {
                error = "invalid response";
                statusCode = -1;
            }

            double duration = DateTime.Now.Subtract(startTime).TotalSeconds;
            ResponseInfo respInfo = new ResponseInfo(statusCode, reqId, xlog, xvia, host, ip, duration, error);
            if (pCompletionHandler != null)
            {
                pCompletionHandler(respInfo, respData);
            }
        }
Esempio n. 41
0
 public PaypalDelegate(CompletionHandler del)
 {
     _del = del;
 }
Esempio n. 42
0
        /// <summary>
        /// post binary data to remote server
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="pPostData"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <param name="pCompletionHandler"></param>
        public void postData(string pUrl, Dictionary <string, string> pHeaders,
                             byte[] pPostData, int offset, int count, string contentType,
                             CompletionHandler pCompletionHandler)
        {
            HttpWebRequest  vWebReq  = null;
            HttpWebResponse vWebResp = null;

            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent         = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method            = "POST";
                if (!string.IsNullOrEmpty(contentType))
                {
                    vWebReq.ContentType = contentType;
                }
                else
                {
                    vWebReq.ContentType = FORM_MIME_OCTECT;
                }
                if (pHeaders != null)
                {
                    foreach (KeyValuePair <string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                vWebReq.AllowWriteStreamBuffering = true;
                // write data
                using (Stream vWebReqStream = vWebReq.GetRequestStream())
                {
                    vWebReqStream.Write(pPostData, offset, count);
                    vWebReqStream.Flush();
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
        }
Esempio n. 43
0
        /// <summary>
        /// post the url encoded form to remote server
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="pParamDict"></param>
        /// <param name="pCompletionHandler"></param>
        public void postForm(string pUrl, Dictionary<string, string> pHeaders,
            Dictionary<string, string[]> pPostParams, CompletionHandler pCompletionHandler)
        {
            HttpWebRequest vWebReq = null;
            HttpWebResponse vWebResp = null;
            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method = "POST";
                vWebReq.ContentType = FORM_MIME_URLENCODED;
                if (pHeaders != null)
                {
                    foreach (KeyValuePair<string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                // format the post body
                StringBuilder vPostBody = new StringBuilder();
                if (pPostParams != null)
                {
                    foreach (KeyValuePair<string, string[]> kvp in pPostParams)
                    {
                        foreach (string vVal in kvp.Value)
                        {
                            vPostBody.AppendFormat("{0}={1}&",
                                Uri.EscapeDataString(kvp.Key), Uri.EscapeDataString(vVal));
                        }
                    }
                    // write data
                    vWebReq.AllowWriteStreamBuffering = true;
                    using (Stream vWebReqStream = vWebReq.GetRequestStream())
                    {
                        vWebReqStream.Write(Encoding.UTF8.GetBytes(vPostBody.ToString()),
                            0, vPostBody.Length - 1);
                        vWebReqStream.Flush();
                    }
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
            finally
            {
                if (vWebResp != null)
                {
                    vWebResp.Close();
                    vWebResp = null;
                }

                if (vWebReq != null)
                {
                    vWebReq.Abort();
                    vWebReq = null;
                }
            }
        }
Esempio n. 44
0
        private void post(string url, byte[] data, int chunkSize, ProgressHandler progressHandler, CompletionHandler completionHandler)
        {
            byte[] uploadData = new byte[chunkSize];
            Array.Copy(data, uploadData, chunkSize);
            PostArgs postArgs = new PostArgs();

            postArgs.Data                      = uploadData;
            this.httpManager.PostArgs          = postArgs;
            this.httpManager.ProgressHandler   = progressHandler;
            this.httpManager.CompletionHandler = completionHandler;
            this.httpManager.postData(url);
        }
Esempio n. 45
0
        /// <summary>
        /// post multi-part data form to remote server
        /// used to upload file
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="pPostParams"></param>
        /// <param name="httpFormFile"></param>
        /// <param name="pProgressHandler"></param>
        /// <param name="pCompletionHandler"></param>
        public void postMultipartDataForm(string pUrl, Dictionary<string, string> pHeaders,
           Dictionary<string, string> pPostParams, HttpFormFile pFormFile,
            ProgressHandler pProgressHandler, CompletionHandler pCompletionHandler)
        {
            if (pFormFile == null)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.fileError(new Exception("no file specified")), "");
                }
                return;
            }

            HttpWebRequest vWebReq = null;
            HttpWebResponse vWebResp = null;
            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
                vWebReq.ServicePoint.Expect100Continue = false;
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method = "POST";

                //create boundary
                string formBoundaryStr = this.createFormDataBoundary();
                string contentType = string.Format("multipart/form-data; boundary={0}", formBoundaryStr);
                vWebReq.ContentType = contentType;
                if (pHeaders != null)
                {
                    foreach (KeyValuePair<string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                //write post body
                vWebReq.AllowWriteStreamBuffering = true;

                byte[] formBoundaryBytes = Encoding.UTF8.GetBytes(string.Format("{0}{1}\r\n",
                    FORM_BOUNDARY_TAG, formBoundaryStr));
                byte[] formBoundaryEndBytes = Encoding.UTF8.GetBytes(string.Format("\r\n{0}{1}{2}\r\n",
                    FORM_BOUNDARY_TAG, formBoundaryStr, FORM_BOUNDARY_TAG));

                using (Stream vWebReqStream = vWebReq.GetRequestStream())
                {
                    //write params
                    if (pPostParams != null)
                    {
                        foreach (KeyValuePair<string, string> kvp in pPostParams)
                        {
                            vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length);

                            byte[] formPartTitleData = Encoding.UTF8.GetBytes(
                                string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n", kvp.Key));
                            vWebReqStream.Write(formPartTitleData, 0, formPartTitleData.Length);

                            byte[] formPartBodyData = Encoding.UTF8.GetBytes(string.Format("\r\n{0}\r\n", kvp.Value));
                            vWebReqStream.Write(formPartBodyData, 0, formPartBodyData.Length);
                        }
                    }

                    vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length);

                    //write file name
                    string filename = pFormFile.Filename;
                    if (string.IsNullOrEmpty(filename))
                    {
                        filename = this.createRandomFilename();
                    }
                    byte[] filePartTitleData = Encoding.UTF8.GetBytes(
                                string.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n", filename));
                    vWebReqStream.Write(filePartTitleData, 0, filePartTitleData.Length);
                    //write content type
                    string mimeType = FORM_MIME_OCTECT;  //!!!注意这里 @fengyh 2016-08-17 15:00
                    if (!string.IsNullOrEmpty(pFormFile.ContentType))
                    {
                        mimeType = pFormFile.ContentType;
                    }
                    byte[] filePartMimeData = Encoding.UTF8.GetBytes(string.Format("Content-Type: {0}\r\n\r\n", mimeType));
                    vWebReqStream.Write(filePartMimeData, 0, filePartMimeData.Length);

                    //write file data
                    switch (pFormFile.BodyType)
                    {
                        case HttpFileType.FILE_PATH:
                            try
                            {
                                FileStream fs = File.Open(pFormFile.BodyFile, FileMode.Open, FileAccess.Read);
                                this.writeHttpRequestBody(fs, vWebReqStream);
                            }
                            catch (Exception fex)
                            {
                                if (pCompletionHandler != null)
                                {
                                    pCompletionHandler(ResponseInfo.fileError(fex), "");
                                }
                            }
                            break;
                        case HttpFileType.FILE_STREAM:
                            this.writeHttpRequestBody(pFormFile.BodyStream, vWebReqStream);
                            break;
                        case HttpFileType.DATA_BYTES:
                            vWebReqStream.Write(pFormFile.BodyBytes, 0, pFormFile.BodyBytes.Length);
                            break;
                        case HttpFileType.DATA_SLICE:
                            vWebReqStream.Write(pFormFile.BodyBytes, pFormFile.Offset, pFormFile.Count);
                            break;
                    }

                    vWebReqStream.Write(formBoundaryEndBytes, 0, formBoundaryEndBytes.Length);
                    vWebReqStream.Flush();
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
            finally
            {
                if (vWebResp != null)
                {
                    vWebResp.Close();
                    vWebResp = null;
                }

                if (vWebReq != null)
                {
                    vWebReq.Abort();
                    vWebReq = null;
                }
            }
        }
Esempio n. 46
0
        // ADD 'force' param
        // 2016-08-22 14:58 @fengyh
        public HttpResult move(string srcBucket, string srcKey, string destBucket, string destKey, bool force)
        {
            HttpResult moveResult = null;
            string moveUrl = string.Format("{0}{1}", Config.ZONE.RsHost, moveOp(srcBucket, srcKey, destBucket, destKey, force));
            string accessToken = Auth.createManageToken(moveUrl, null, this.mac);
            Dictionary<string, string> moveHeaders = new Dictionary<string, string>();
            moveHeaders.Add("Authorization", accessToken);
            CompletionHandler moveCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                moveResult = new HttpResult();
                moveResult.Response = response;
                moveResult.ResponseInfo = respInfo;
            });

            this.mHttpManager.postForm(moveUrl, moveHeaders, null, moveCompletionHandler);
            return moveResult;
        }
Esempio n. 47
0
        private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            postArgs.Params = new Dictionary<string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                postArgs.Params.Add("key", key);
            }
            //设置token
            postArgs.Params.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (httpManager.FileContentType)
                {
                    case PostContentType.BYTES:
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length)));
                        break;
                    case PostContentType.STREAM:
                        long streamLength = postArgs.Stream.Length;
                        byte[] buffer = new byte[streamLength];
                        int cnt = postArgs.Stream.Read(buffer, 0, (int)streamLength);
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt)));
                        postArgs.Stream.Seek(0, SeekOrigin.Begin);
                        break;
                    case PostContentType.FILE:
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File)));
                        break;
                }
            }

            //设置MimeType
            postArgs.MimeType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams)
            {
                postArgs.Params.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            httpManager.CancellationSignal = new CancellationSignal(delegate()
            {
                return uploadOptions.CancellationSignal();
            });
            httpManager.PostArgs = postArgs;
            //第一次失败后使用备用域名重试一次
            httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.needRetry())
                {
                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin);
                    }
                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        uploadOptions.ProgressHandler(key, 1.0);

                        if (httpManager.PostArgs.Stream != null)
                        {
                            httpManager.PostArgs.Stream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            upCompletionHandler(key, retryRespInfo, retryResponse);
                        }
                    });
                    httpManager.CompletionHandler = retried;
                    httpManager.multipartPost(Config.UP_HOST);
                }
                else
                {
                    uploadOptions.ProgressHandler(key, 1.0);

                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
            });
            httpManager.multipartPost(Config.UPLOAD_HOST);
        }
Esempio n. 48
0
        public HttpResult prefetch(string bucket, string key)
        {
            HttpResult prefetchResult = null;
            string prefetchUrl = string.Format("{0}{1}", Config.ZONE.IovipHost, prefetchOp(bucket, key));
            string accessToken = Auth.createManageToken(prefetchUrl, null, this.mac);
            Dictionary<string, string> prefetchHeaders = new Dictionary<string, string>();
            prefetchHeaders.Add("Authorization", accessToken);
            CompletionHandler prefetchCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                prefetchResult = new FetchResult();
                prefetchResult.Response = response;
                prefetchResult.ResponseInfo = respInfo;
            });

            this.mHttpManager.postForm(prefetchUrl, prefetchHeaders, null, prefetchCompletionHandler);
            return prefetchResult;
        }
Esempio n. 49
0
        public StatResult stat(string bucket, string key)
        {
            StatResult statResult = null;
            string statUrl = string.Format("{0}{1}", Config.ZONE.RsHost, statOp(bucket, key));
            string accessToken = Auth.createManageToken(statUrl, null, this.mac);

            Dictionary<string, string> statHeaders = new Dictionary<string, string>();
            statHeaders.Add("Authorization", accessToken);
            CompletionHandler statCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.isOk())
                {
                    statResult = StringUtils.jsonDecode<StatResult>(response);
                }
                else
                {
                    statResult = new StatResult();
                }
                statResult.Response = response;
                statResult.ResponseInfo = respInfo;
            });

            this.mHttpManager.get(statUrl, statHeaders, statCompletionHandler);
            return statResult;
        }
Esempio n. 50
0
        /// <summary>
        /// post the url encoded form to remote server
        /// </summary>
        /// <param name="pUrl"></param>
        /// <param name="pHeaders"></param>
        /// <param name="pParamDict"></param>
        /// <param name="pCompletionHandler"></param>
        public void postForm(string pUrl, Dictionary <string, string> pHeaders,
                             Dictionary <string, string[]> pPostParams, CompletionHandler pCompletionHandler)
        {
            HttpWebRequest  vWebReq  = null;
            HttpWebResponse vWebResp = null;

            try
            {
                vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
            }
            catch (Exception ex)
            {
                if (pCompletionHandler != null)
                {
                    pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
                }
                return;
            }

            try
            {
                vWebReq.UserAgent         = this.getUserAgent();
                vWebReq.AllowAutoRedirect = false;
                vWebReq.Method            = "POST";
                vWebReq.ContentType       = FORM_MIME_URLENCODED;
                if (pHeaders != null)
                {
                    foreach (KeyValuePair <string, string> kvp in pHeaders)
                    {
                        if (!kvp.Key.Equals("Content-Type"))
                        {
                            vWebReq.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                }

                // format the post body
                StringBuilder vPostBody = new StringBuilder();
                if (pPostParams != null)
                {
                    foreach (KeyValuePair <string, string[]> kvp in pPostParams)
                    {
                        foreach (string vVal in kvp.Value)
                        {
                            vPostBody.AppendFormat("{0}={1}&",
                                                   Uri.EscapeDataString(kvp.Key), Uri.EscapeDataString(vVal));
                        }
                    }
                    // write data
                    vWebReq.AllowWriteStreamBuffering = true;
                    using (Stream vWebReqStream = vWebReq.GetRequestStream())
                    {
                        vWebReqStream.Write(Encoding.UTF8.GetBytes(vPostBody.ToString()),
                                            0, vPostBody.Length - 1);
                        vWebReqStream.Flush();
                    }
                }

                //fire request
                vWebResp = (HttpWebResponse)vWebReq.GetResponse();
                handleWebResponse(vWebResp, pCompletionHandler);
            }
            catch (WebException wexp)
            {
                // FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
                HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
                handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
            }
            catch (Exception exp)
            {
                handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
            }
        }