public BestHttpClient(string serverIp) { this.m_serverIp = serverIp; this.m_retry_wrapper = new HttpRetry(); ResumeRetryMessage(); m_is_retry_ui_show = false; HttpSendingCahce.Clear(); HttpPairCahce.Clear(); S_ATOMIC_COUNTER = 0; HTTPManager.MaxConnectionPerServer = 4; }
private void CreateHttpRequest(RequestMessageWrapper sendMessageWrapper, OnRequestFinishedDelegate OnFinished_) { string serverTarget = MessageTargetDefine.GetServerUrl(sendMessageWrapper.MessageBody); if (string.IsNullOrEmpty(serverTarget)) { return; } Uri serverUri = new Uri(EngineCoreEvents.BridgeEvent.GetServerAddress.SafeInvoke() + "/" + serverTarget); //<best BestHTTP.HTTPRequest request = new HTTPRequest(serverUri, HTTPMethods.Post, OnFinished_); request.IsKeepAlive = true; request.RawData = sendMessageWrapper.MessageBody.ToByteArray();; request.SetHeader("Content-Type", "application/x-protobuf"); request.SetHeader("Accept", "application/x-protobuf"); string accountToken = EngineCoreEvents.SystemEvents.FetchUserIdentification(); if (!string.IsNullOrEmpty(accountToken)) { request.SetHeader("accessToken", accountToken); } int send_identifier = ++S_ATOMIC_COUNTER; request.SetHeader("sendTime", send_identifier.ToString()); #if UNITY_DEBUG DateTime t1 = DateTime.Now; TimeSpan t22 = new TimeSpan(t1.Ticks); long time2 = Convert.ToInt64(t22.TotalMilliseconds); //Debug.LogWarning($"SENDING TIME :{request.Uri} {t1.ToString("yyyy/MM/dd HH:mm:ss:fffffff")}"); sendMessageWrapper.Send_time = time2; sendMessageWrapper.Send_timestamp = t1.ToString("yyyy/MM/dd HH:mm:ss:fffffff"); #else sendMessageWrapper.Send_time = CommonUtils.GetCurTimeMillSenconds(); #endif HttpSendingCahce.AddToSendingCache(send_identifier.ToString(), sendMessageWrapper); request.Send(); }
private void OnASyncRequestFinished(HTTPRequest originalRequest, HTTPResponse response) { RequestMessageWrapper sendMessageWrapper = HttpSendingCahce.GetAndRemoveFromSendingCache(originalRequest); if (null == sendMessageWrapper) { Debug.LogError($"BEST HTTP : return unknown rsp {response.GetFirstHeaderValue("msgId")}"); return; } switch (originalRequest.State) { case HTTPRequestStates.Finished: //Debug.Log("Request Finished Successfully!\n" + response.DataAsText); OnRsp(sendMessageWrapper, response); break; case HTTPRequestStates.Error: Debug.LogError("BEST HTTP :Request Finished with Error! " + (originalRequest.Exception != null ? (originalRequest.Exception.Message + "\n" + originalRequest.Exception.StackTrace) : "No Exception")); BornErrorLog(sendMessageWrapper); break; case HTTPRequestStates.Aborted: Debug.LogWarning("BEST HTTP :Request Aborted!"); BornErrorLog(sendMessageWrapper); break; case HTTPRequestStates.ConnectionTimedOut: Debug.LogError("BEST HTTP :Connection Timed Out!"); BornErrorLog(sendMessageWrapper); break; case HTTPRequestStates.TimedOut: Debug.LogError("BEST HTTP :Processing the request Timed Out!"); BornErrorLog(sendMessageWrapper); break; } }
private void OnHalfSyncRequestFinished(HTTPRequest originalRequest, HTTPResponse response) { RequestMessageWrapper sendMessageWrapper = HttpSendingCahce.GetAndRemoveFromSendingCache(originalRequest); if (null == sendMessageWrapper) { Debug.LogError($"BEST HTTP : return unknown rsp {response.GetFirstHeaderValue("msgId")}"); return; } switch (originalRequest.State) { case HTTPRequestStates.Finished: //Debug.Log("Request Finished Successfully!\n" + response.DataAsText); OnRsp(sendMessageWrapper, response); WorkQueueCallback(new SendMessageCallback() { MessageId = sendMessageWrapper.MessageId, Status = NetworkModule.NetworkStatus.SYNC_SUCCEED }); break; case HTTPRequestStates.Error: { Debug.LogError("Request Finished with Error! " + (originalRequest.Exception != null ? (originalRequest.Exception.Message + "\n" + originalRequest.Exception.StackTrace) : "No Exception")); WorkQueueCallback(new SendMessageCallback() { MessageId = sendMessageWrapper.MessageId, Status = NetworkModule.NetworkStatus.OFFLINE_WARNNING }); } break; case HTTPRequestStates.Aborted: Debug.LogWarning("BEST HTTP :Request Aborted!"); WorkQueueCallback(new SendMessageCallback() { MessageId = sendMessageWrapper.MessageId, Status = NetworkModule.NetworkStatus.OFFLINE_WARNNING }); break; case HTTPRequestStates.ConnectionTimedOut: Debug.LogError("BEST HTTP :Connection Timed Out!"); WorkQueueCallback(new SendMessageCallback() { MessageId = sendMessageWrapper.MessageId, Status = NetworkModule.NetworkStatus.OFFLINE_WARNNING }); break; case HTTPRequestStates.TimedOut: Debug.LogError("BEST HTTP :Processing the request Timed Out!"); WorkQueueCallback(new SendMessageCallback() { MessageId = sendMessageWrapper.MessageId, Status = NetworkModule.NetworkStatus.OFFLINE_WARNNING }); break; default: break; } }