Exemplo n.º 1
0
        private void T_RawSendInternal(object state, bool retry)
        {
            RequestMessageWrapper sendMessageWrapper = state as RequestMessageWrapper;

            if (!retry && sendMessageWrapper.RequestType == RequestMessageType.SYNC)
            {
                Debug.Log(string.Format("{0} retry count left {1}", sendMessageWrapper.MessageBody.Descriptor.Name, sendMessageWrapper.m_retry_count));
            }
            string serverTarget = MessageTargetDefine.GetServerUrl(sendMessageWrapper.MessageBody);

            if (string.IsNullOrEmpty(serverTarget))
            {
                return;
            }

            Uri            serverUri = new Uri(this.m_serverIp + "/" + serverTarget);
            HttpWebRequest request   = null;

            if (serverUri.Scheme == "https")
            {
                ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { return(true); };
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;//配置协议 4.6 好像默认不设SecurityProtocolType.Ssl3
                request = WebRequest.Create(serverUri) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = WebRequest.Create(serverUri) as HttpWebRequest;
            }

            request.Timeout = 15000;
            //request.ReadWriteTimeout = 10000;
            request.ContentType = "application/x-protobuf";
            request.Accept      = "application/x-protobuf";
            request.Method      = "POST";
            //ForceIpv6(request);

            string accountToken = EngineCoreEvents.SystemEvents.FetchUserIdentification();

            if (!string.IsNullOrEmpty(accountToken))
            {
                request.Headers["accessToken"] = accountToken;
            }

            //byte[] requestData = sendMessageWrapper.MessageBody.ToByteArray();
            if (sendMessageWrapper.RequestType == RequestMessageType.SYNC)
            {
                T_DoSyncSend(sendMessageWrapper, request, retry);
            }
            else
            {
                sendMessageWrapper.m_unique_identifier = (++m_atomic_sending_counter).ToString();
                m_sending_times.Add(sendMessageWrapper.m_unique_identifier, CommonUtils.GetCurTimeMillSenconds());

                T_DoASyncSend(sendMessageWrapper, request);
            }
        }
Exemplo n.º 2
0
        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();
        }