Skip to content

Commit

Permalink
BREAKING CHANGE: Default Reconnection Policy set to EXPONENTIAL.
Browse files Browse the repository at this point in the history
* File upload failure edge cases handling with proper exception messages.
* Refactored Check Internet status execution a bit (many more to come) to fix Unity engine impact.
* Old subscription logic cleanup: removing many state/internetstatus information store
  • Loading branch information
mohitpubnub committed Oct 25, 2024
1 parent 70011b8 commit c634e0b
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 383 deletions.
31 changes: 19 additions & 12 deletions src/Api/PubnubApi/ClientNetworkStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ private static async Task<bool> CheckClientNetworkAvailability<T>(Action<bool> i
}
}

InternetState<T> state = new InternetState<T>();
state.InternalCallback = internalCallback;
state.PubnubCallbacck = callback;
state.ResponseType = type;
state.Channels = channels;
state.ChannelGroups = channelGroups;
InternetState<T> state = new InternetState<T>
{
InternalCallback = internalCallback,
PubnubCallbacck = callback,
ResponseType = type,
Channels = channels,
ChannelGroups = channelGroups
};

networkStatus = await CheckSocketConnect<T>(state).ConfigureAwait(false);
return networkStatus;
Expand Down Expand Up @@ -128,22 +130,27 @@ private static void ParseCheckSocketConnectException<T>(Exception ex, PNOperatio
private static async Task<bool> GetTimeWithTaskFactoryAsync()
{
bool successFlag = false;
try {
try
{
var transportResponse = await TimeRequest().ConfigureAwait(false);
if (transportResponse.Error != null) throw transportResponse.Error;
successFlag = transportResponse.StatusCode == 200;
} finally {
if (transportResponse.Error == null)
{
successFlag = transportResponse.StatusCode == 200;
}
}
finally {
networkStatus = successFlag;
}
return successFlag;
}

private static async Task<TransportResponse> TimeRequest()
{
List<string> pathSegments = new List<string> {
List<string> pathSegments =
[
"time",
"0"
};
];
var requestParameter = new RequestParameter() {
RequestType = Constants.GET,
PathSegment = pathSegments
Expand Down
1 change: 1 addition & 0 deletions src/Api/PubnubApi/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public static class Constants

public const int ResourceNotFoundStatusCode = 404;
public const int HttpRequestSuccessStatusCode = 200;
public const int HttpRequestEntityTooLargeStatusCode = 413;
}
}
33 changes: 26 additions & 7 deletions src/Api/PubnubApi/EndPoint/Files/SendFileOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using PubnubApi.Security.Crypto;
using PubnubApi.Security.Crypto.Cryptors;
Expand Down Expand Up @@ -224,8 +225,7 @@ private async Task<PNResult<PNFileUploadResult>> ProcessFileUpload()
PNGenerateFileUploadUrlResult generateFileUploadUrlResult = generateFileUploadUrl.Result;
PNStatus generateFileUploadUrlStatus = generateFileUploadUrl.Status;
if (generateFileUploadUrlStatus.Error || generateFileUploadUrlResult == null) {
PNStatus errStatus = new PNStatus { Error = true, ErrorData = new PNErrorData("Error in GenerateFileUploadUrl.", new ArgumentException("Error in GenerateFileUploadUrl.")) };
returnValue.Status = errStatus;
returnValue.Status = generateFileUploadUrlStatus;
return returnValue;
}
LoggingMethod.WriteToLog(pubnubLog, string.Format(CultureInfo.InvariantCulture, "DateTime {0} GenerateFileUploadUrl executed.", DateTime.Now.ToString(CultureInfo.InvariantCulture)), config.LogVerbosity);
Expand All @@ -242,10 +242,13 @@ private async Task<PNResult<PNFileUploadResult>> ProcessFileUpload()
currentCryptoModule = !string.IsNullOrEmpty(currentFileCipherKey) ? new CryptoModule(new LegacyCryptor(currentFileCipherKey, true, pubnubLog), null) : (config.CryptoModule ??= new CryptoModule(new LegacyCryptor(config.CipherKey, true, pubnubLog), null));
}
byte[] postData = GetMultipartFormData(sendFileByteArray, generateFileUploadUrlResult.FileName, generateFileUploadUrlResult.FileUploadRequest.FormFields, dataBoundary, currentCryptoModule, config, pubnubLog);
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMinutes(5));
var transportRequest = new TransportRequest() {
RequestType = Constants.POST,
RequestUrl = generateFileUploadUrlResult.FileUploadRequest.Url,
BodyContentBytes = postData,
CancellationToken = cts.Token
};
transportRequest.Headers.Add("Content-Type", contentType);
Tuple<string, PNStatus> jsonAndStatusTuple;
Expand All @@ -260,11 +263,27 @@ private async Task<PNResult<PNFileUploadResult>> ProcessFileUpload()
} else {
jsonAndStatusTuple = new Tuple<string, PNStatus>(string.Empty, errStatus);
}
} else {
int statusCode = PNStatusCodeHelper.GetHttpStatusCode(transportResponse.Error.Message);
PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(statusCode, transportResponse.Error.Message);
PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNFileUploadOperation, category, requestState, statusCode, new PNException(transportResponse.Error.Message, transportResponse.Error));
jsonAndStatusTuple = new Tuple<string, PNStatus>(string.Empty, status);
} else
{
PNStatus error = new PNStatus();
if (transportResponse.Content != null)
{
error = GetStatusIfError(requestState, Encoding.UTF8.GetString(transportResponse.Content));
}
else
{
if (transportResponse.Error.Message != null)
{
error = GetStatusIfError(requestState, transportResponse.Error.Message);
}
else
{
PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(transportResponse.StatusCode, transportResponse.Error.Message);
error = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNFileUploadOperation, category, requestState, transportResponse.StatusCode, new PNException(transportResponse.Error.Message, transportResponse.Error));
}

}
jsonAndStatusTuple = new Tuple<string, PNStatus>(string.Empty, error);
}
returnValue.Status = jsonAndStatusTuple.Item2;
string json = jsonAndStatusTuple.Item1;
Expand Down
Loading

0 comments on commit c634e0b

Please sign in to comment.