private void HandleTransmissionSentEvent(object sender, TransmissionProcessedEventArgs args) { if (args.Exception == null && (args.Response == null || args.Response.StatusCode == ResponseStatusCodes.Success)) { // We successfully sent transmittion this.backoffLogicManager.ResetConsecutiveErrors(); return; } if (args.Response != null && args.Response.StatusCode == ResponseStatusCodes.PartialSuccess) { int statusCode; string newTransmissions = this.ParsePartialSuccessResponse(args.Transmission, args, out statusCode); if (!string.IsNullOrEmpty(newTransmissions)) { this.DelayFutureProcessing(args.Response, statusCode); byte[] data = JsonSerializer.ConvertToByteArray(newTransmissions); Transmission newTransmission = new Transmission( args.Transmission.EndpointAddress, data, args.Transmission.ContentType, args.Transmission.ContentEncoding, args.Transmission.Timeout); this.Transmitter.Enqueue(newTransmission); } else { // We got 206 but there is no indication in response that something was not accepted. this.backoffLogicManager.ResetConsecutiveErrors(); } } }
private static Transmission SerializeNewTransmission(TransmissionProcessedEventArgs args, string newTransmissions) { byte[] data = JsonSerializer.ConvertToByteArray(newTransmissions); Transmission transmission = new Transmission( args.Transmission.EndpointAddress, data, args.Transmission.ContentType, args.Transmission.ContentEncoding, args.Transmission.Timeout); return(transmission); }
/// <summary> /// Splits the Transmission object into two pieces using a method /// to determine the length of the first piece based off of the length of the transmission. /// </summary> /// <returns> /// A tuple with the first item being a Transmission object with n ITelemetry objects /// and the second item being a Transmission object with the remaining ITelemetry objects. /// </returns> public virtual Tuple <Transmission, Transmission> Split(Func <int, int> calculateLength) { Transmission transmissionA = this; Transmission transmissionB = null; // We can be more efficient if we have a copy of the telemetry items still if (this.TelemetryItems != null) { // We don't need to deserialize, we have a copy of each telemetry item int numItems = calculateLength(this.TelemetryItems.Count); if (numItems != this.TelemetryItems.Count) { List <ITelemetry> itemsA = new List <ITelemetry>(); List <ITelemetry> itemsB = new List <ITelemetry>(); var i = 0; foreach (var item in this.TelemetryItems) { if (i < numItems) { itemsA.Add(item); } else { itemsB.Add(item); } i++; } transmissionA = new Transmission( this.EndpointAddress, itemsA); transmissionB = new Transmission( this.EndpointAddress, itemsB); } } else if (this.ContentType == JsonSerializer.ContentType) { // We have to decode the payload in order to split bool compress = this.ContentEncoding == JsonSerializer.CompressionType; string[] payloadItems = JsonSerializer .Deserialize(this.Content, compress) .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int numItems = calculateLength(payloadItems.Length); if (numItems != payloadItems.Length) { string itemsA = string.Empty; string itemsB = string.Empty; for (int i = 0; i < payloadItems.Length; i++) { if (i < numItems) { if (!string.IsNullOrEmpty(itemsA)) { itemsA += Environment.NewLine; } itemsA += payloadItems[i]; } else { if (!string.IsNullOrEmpty(itemsB)) { itemsB += Environment.NewLine; } itemsB += payloadItems[i]; } } transmissionA = new Transmission( this.EndpointAddress, JsonSerializer.ConvertToByteArray(itemsA, compress), this.ContentType, this.ContentEncoding); transmissionB = new Transmission( this.EndpointAddress, JsonSerializer.ConvertToByteArray(itemsB, compress), this.ContentType, this.ContentEncoding); } } else { // We can't deserialize it! // We can say it's of length 1 at the very least int numItems = calculateLength(1); if (numItems == 0) { transmissionA = null; transmissionB = this; } } return(Tuple.Create(transmissionA, transmissionB)); }