private PushResponse PushOneChunk(HgResumeApiParameters request, byte[] dataToPush) { var pushResponse = new PushResponse(PushStatus.Fail); try { HgResumeApiResponse response = _apiServer.Execute("pushBundleChunk", request, dataToPush, TimeoutInSeconds); if (response == null) { _progress.WriteVerbose("API REQ: {0} Timeout"); pushResponse.Status = PushStatus.Timeout; return(pushResponse); } /* API returns the following HTTP codes: * 200 OK (SUCCESS) * 202 Accepted (RECEIVED) * 412 Precondition Failed (RESEND) * 400 Bad Request (FAIL, UNKNOWNID, and RESET) */ _progress.WriteVerbose("API REQ: {0} RSP: {1} in {2}ms", _apiServer.Url, response.HttpStatus, response.ResponseTimeInMilliseconds); if (response.HttpStatus == HttpStatusCode.ServiceUnavailable && response.Content.Length > 0) { var msg = String.Format("Server temporarily unavailable: {0}", Encoding.UTF8.GetString(response.Content)); _progress.WriteError(msg); pushResponse.Status = PushStatus.NotAvailable; return(pushResponse); } if (response.ResumableResponse.HasNote) { _progress.WriteWarning(String.Format("Server replied: {0}", response.ResumableResponse.Note)); } // the chunk was received successfully if (response.HttpStatus == HttpStatusCode.Accepted) { pushResponse.StartOfWindow = response.ResumableResponse.StartOfWindow; pushResponse.Status = PushStatus.Received; pushResponse.ChunkSize = CalculateChunkSize(request.ChunkSize, response.ResponseTimeInMilliseconds); return(pushResponse); } // the final chunk was received successfully if (response.HttpStatus == HttpStatusCode.OK) { pushResponse.Status = PushStatus.Complete; return(pushResponse); } if (response.HttpStatus == HttpStatusCode.BadRequest) { if (response.ResumableResponse.Status == "UNKNOWNID") { _progress.WriteError("The server {0} does not have the project '{1}'", _targetLabel, _apiServer.ProjectId); return(pushResponse); } if (response.ResumableResponse.Status == "RESET") { _progress.WriteError("Push failed: All chunks were pushed to the server, but the unbundle operation failed. Try again later."); _progress.WriteVerbose(string.Format("Additional error info: {0}", response.ResumableResponse.Error)); pushResponse.Status = PushStatus.Reset; return(pushResponse); } if (response.ResumableResponse.HasError) { if (response.ResumableResponse.Error == "invalid baseHash") { pushResponse.Status = PushStatus.InvalidHash; } else { _progress.WriteError("Server Error: {0}", response.ResumableResponse.Error); } return(pushResponse); } } _progress.WriteWarning("Invalid Server Response '{0}'", response.HttpStatus); return(pushResponse); } catch (WebException e) { _progress.WriteWarning(String.Format("Push data chunk failed: {0}", e.Message)); return(pushResponse); } }
private PushResponse PushOneChunk(HgResumeApiParameters request, byte[] dataToPush) { var pushResponse = new PushResponse(PushStatus.Fail); try { HgResumeApiResponse response = _apiServer.Execute("pushBundleChunk", request, dataToPush, TimeoutInSeconds); if (response == null) { _progress.WriteVerbose("API REQ: {0} Timeout"); pushResponse.Status = PushStatus.Timeout; return pushResponse; } /* API returns the following HTTP codes: * 200 OK (SUCCESS) * 202 Accepted (RECEIVED) * 412 Precondition Failed (RESEND) * 400 Bad Request (FAIL, UNKNOWNID, and RESET) */ _progress.WriteVerbose("API REQ: {0} RSP: {1} in {2}ms", _apiServer.Url, response.HttpStatus, response.ResponseTimeInMilliseconds); if (response.HttpStatus == HttpStatusCode.ServiceUnavailable && response.Content.Length > 0) { var msg = String.Format("Server temporarily unavailable: {0}", Encoding.UTF8.GetString(response.Content)); _progress.WriteError(msg); pushResponse.Status = PushStatus.NotAvailable; return pushResponse; } if (response.ResumableResponse.HasNote) { _progress.WriteWarning(String.Format("Server replied: {0}", response.ResumableResponse.Note)); } // the chunk was received successfully if (response.HttpStatus == HttpStatusCode.Accepted) { pushResponse.StartOfWindow = response.ResumableResponse.StartOfWindow; pushResponse.Status = PushStatus.Received; pushResponse.ChunkSize = CalculateChunkSize(request.ChunkSize, response.ResponseTimeInMilliseconds); return pushResponse; } // the final chunk was received successfully if (response.HttpStatus == HttpStatusCode.OK) { pushResponse.Status = PushStatus.Complete; return pushResponse; } if (response.HttpStatus == HttpStatusCode.BadRequest) { if (response.ResumableResponse.Status == "UNKNOWNID") { _progress.WriteError("The server {0} does not have the project '{1}'", _targetLabel, _apiServer.ProjectId); return pushResponse; } if (response.ResumableResponse.Status == "RESET") { _progress.WriteError("Push failed: All chunks were pushed to the server, but the unbundle operation failed. Try again later."); pushResponse.Status = PushStatus.Reset; return pushResponse; } if (response.ResumableResponse.HasError) { if (response.ResumableResponse.Error == "invalid baseHash") { pushResponse.Status = PushStatus.InvalidHash; } else { _progress.WriteError("Server Error: {0}", response.ResumableResponse.Error); } return pushResponse; } } _progress.WriteWarning("Invalid Server Response '{0}'", response.HttpStatus); return pushResponse; } catch (WebException e) { _progress.WriteWarning(String.Format("Push data chunk failed: {0}", e.Message)); return pushResponse; } }