Esempio n. 1
0
        internal void GetOrPostOrDelete(ref UnityWebRequestWrapper unityWebRequestWrapper)
        {
            if (unityWebRequestWrapper.CurrentRequestState.httpMethod.Equals(HTTPMethod.Post))
            {
                unityWebRequestWrapper.CurrentUnityWebRequest = new UnityWebRequest(unityWebRequestWrapper.URL);
                unityWebRequestWrapper.CurrentUnityWebRequest.uploadHandler   = new UploadHandlerRaw(Encoding.UTF8.GetBytes(unityWebRequestWrapper.CurrentRequestState.POSTData));
                unityWebRequestWrapper.CurrentUnityWebRequest.downloadHandler = new DownloadHandlerBuffer();
                unityWebRequestWrapper.CurrentUnityWebRequest.method          = UnityWebRequest.kHttpVerbPOST;
                unityWebRequestWrapper.CurrentUnityWebRequest.SetRequestHeader("Content-Type", "application/json");

                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("POST Data : {1} \nURL:{0}", unityWebRequestWrapper.URL, unityWebRequestWrapper.CurrentRequestState.POSTData), PNLoggingMethod.LevelInfo);
                #endif
            }
            else if (unityWebRequestWrapper.CurrentRequestState.httpMethod.Equals(HTTPMethod.Delete))
            {
                unityWebRequestWrapper.CurrentUnityWebRequest = new UnityWebRequest(unityWebRequestWrapper.URL);
                unityWebRequestWrapper.CurrentUnityWebRequest.downloadHandler = new DownloadHandlerBuffer();
                unityWebRequestWrapper.CurrentUnityWebRequest.method          = UnityWebRequest.kHttpVerbDELETE;
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("Delete \nURL:{0}", unityWebRequestWrapper.URL), PNLoggingMethod.LevelInfo);
                #endif
            }
            else
            {
                unityWebRequestWrapper.CurrentUnityWebRequest = UnityWebRequest.Get(unityWebRequestWrapper.URL);
            }
        }
Esempio n. 2
0
        internal CurrentRequestTypeEventArgs CreateCurrentRequestTypeEventArgs(UnityWebRequestWrapper unityWebRequestWrapper, bool isTimeout)
        {
            CurrentRequestTypeEventArgs crtEa = new CurrentRequestTypeEventArgs();

            crtEa.WebRequestWrapper = unityWebRequestWrapper;
            crtEa.IsTimeout         = isTimeout;
            return(crtEa);
        }
Esempio n. 3
0
        internal void RaiseEvents(bool isTimeout, UnityWebRequestWrapper unityWebRequestWrapper, string key)
        {
            StopTimeoutsAndComplete(unityWebRequestWrapper, key, false);

            if (isTimeout)
            {
                ProcessTimeout(unityWebRequestWrapper, key, unityWebRequestWrapper.IsComplete);
            }
            else
            {
                ProcessResponse(unityWebRequestWrapper, key);
            }
        }
Esempio n. 4
0
        internal void CheckPauseTime(UnityWebRequestWrapper unityWebRequestWrapper, string key)
        {
            if (unityWebRequestWrapper.PauseTimer <= 0)
            {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("pause timeout {0} {1}", unityWebRequestWrapper.CurrentRequestType.ToString(), unityWebRequestWrapper.PauseTimer.ToString()), PNLoggingMethod.LevelInfo);
                #endif

                StopTimeoutsAndComplete(unityWebRequestWrapper, key, true);

                StartWebRequests(unityWebRequestWrapper, key);
            }
        }
Esempio n. 5
0
        internal void StopTimeoutsAndComplete(UnityWebRequestWrapper unityWebRequestWrapper, string key, bool pauseOnly)
        {
            unityWebRequestWrapper.PauseTimer    = 0;
            unityWebRequestWrapper.RunPauseTimer = false;

            if (!pauseOnly)
            {
                unityWebRequestWrapper.RunTimer   = false;
                unityWebRequestWrapper.Timer      = 0;
                unityWebRequestWrapper.IsComplete = true;
            }

            currentWebRequests[key] = unityWebRequestWrapper;
        }
Esempio n. 6
0
        internal void StartWebRequests(UnityWebRequestWrapper unityWebRequestWrapper, string key)
        {
            #if (ENABLE_PUBNUB_LOGGING)
            this.PNLog.WriteToLog(string.Format("StartWebRequestsByName: URL {1} {0}", unityWebRequestWrapper.URL, unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
            #endif

            unityWebRequestWrapper.CurrentRequestState.StartRequestTicks = DateTime.UtcNow.Ticks;
            unityWebRequestWrapper.RunTimer   = true;
            unityWebRequestWrapper.IsComplete = false;
            GetOrPostOrDelete(ref unityWebRequestWrapper);

            UnityWebRequestAsyncOperation asyncOp = unityWebRequestWrapper.CurrentUnityWebRequest.SendWebRequest();
            currentWebRequests[key] = unityWebRequestWrapper;

            #if (ENABLE_PUBNUB_LOGGING)
            this.PNLog.WriteToLog(string.Format("StartWebRequestsByName: {0} running", unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
            this.PNLog.WriteToLog(string.Format("StartWebRequests POST Data : {1} \nURL : {0}", unityWebRequestWrapper.URL, unityWebRequestWrapper.CurrentRequestState.POSTData), PNLoggingMethod.LevelInfo);
            #endif
        }
Esempio n. 7
0
 internal void CheckElapsedTime(UnityWebRequestWrapper unityWebRequestWrapper, string key)
 {
     if (unityWebRequestWrapper.Timer <= 0)
     {
         #if (ENABLE_PUBNUB_LOGGING)
         this.PNLog.WriteToLog(string.Format("CheckElapsedTime: timeout {0}", unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
         #endif
         RaiseEvents(true, unityWebRequestWrapper, key);
     }
     else if ((unityWebRequestWrapper != null) && ((unityWebRequestWrapper.CurrentUnityWebRequest != null) && (unityWebRequestWrapper.CurrentUnityWebRequest.isDone)))
     {
         #if (ENABLE_PUBNUB_LOGGING)
         this.PNLog.WriteToLog(string.Format("CheckElapsedTime: done {0}", unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
         #endif
         RaiseEvents(false, unityWebRequestWrapper, key);
     }
     else if ((unityWebRequestWrapper.Timer > 0) && ((unityWebRequestWrapper.CurrentUnityWebRequest != null) && (unityWebRequestWrapper.CurrentUnityWebRequest.isDone)))
     {
         #if (ENABLE_PUBNUB_LOGGING)
         this.PNLog.WriteToLog(string.Format("CheckElapsedTime: www null timer running {0}", unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
         #endif
     }
     else if ((unityWebRequestWrapper.Timer > 0) && (unityWebRequestWrapper == null))
     {
         #if (ENABLE_PUBNUB_LOGGING)
         this.PNLog.WriteToLog(string.Format("CheckElapsedTime: www null request not running timer running {0}", unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
         #endif
     }
     else if ((unityWebRequestWrapper.Timer > 0) && ((unityWebRequestWrapper.CurrentUnityWebRequest != null) && (!unityWebRequestWrapper.CurrentUnityWebRequest.isDone)))
     {
         #if (ENABLE_PUBNUB_LOGGING)
         //this.PNLog.WriteToLog (string.Format ("CheckElapsedTime: request running timer running {0}",  unityWebRequestWrapper.CurrentRequestType.ToString ()), PNLoggingMethod.LevelInfo);
         #endif
     }
     else
     {
         #if (ENABLE_PUBNUB_LOGGING)
         this.PNLog.WriteToLog(string.Format("CheckElapsedTime: timer {0}", unityWebRequestWrapper.Timer.ToString()), PNLoggingMethod.LevelInfo);
         #endif
     }
 }
Esempio n. 8
0
        public void ProcessTimeout(UnityWebRequestWrapper unityWebRequestWrapper, string key, bool isComplete)
        {
            try {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("ProcessTimeout: {0}", unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelInfo);
                #endif

                if (!isComplete)
                {
                    AbortRequest(key, false);
                }
                unityWebRequestWrapper.CurrentRequestState.ResponseCode = 0;
                unityWebRequestWrapper.CurrentRequestState.URL          = unityWebRequestWrapper.URL;
                FireEvent("Timed out", true, true, unityWebRequestWrapper.CurrentRequestState, unityWebRequestWrapper.CurrentRequestType, key);
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("ProcessTimeout: WWW Error: {0} sec timeout", unityWebRequestWrapper.Timeout.ToString()), PNLoggingMethod.LevelInfo);
                #endif
            } catch (Exception ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("ProcessTimeout: {0} {1}", ex.ToString(), unityWebRequestWrapper.CurrentRequestType.ToString()), PNLoggingMethod.LevelError);
                #endif
            }
        }
Esempio n. 9
0
        public void ProcessResponse(UnityWebRequestWrapper unityWebRequestWrapper, string key)
        {
            try {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("ProcessResponse: Process Request {0}, url: {1}, OPType: {2}", unityWebRequestWrapper.CurrentRequestType.ToString(), unityWebRequestWrapper.URL, unityWebRequestWrapper.CurrentRequestState.OperationType), PNLoggingMethod.LevelInfo);
                #endif

                if (unityWebRequestWrapper.CurrentUnityWebRequest != null)
                {
                    string message = "";
                    bool   isError = false;
                    if (unityWebRequestWrapper.CurrentUnityWebRequest.downloadHandler != null)
                    {
                        message = unityWebRequestWrapper.CurrentUnityWebRequest.downloadHandler.text;
                    }

                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PNLog.WriteToLog(string.Format("ProcessResponse: unityWebRequestWrapper.CurrentUnityWebRequest.isNetworkError {0}\n unityWebRequestWrapper.CurrentUnityWebRequest.isHttpError {1}", unityWebRequestWrapper.CurrentUnityWebRequest.isNetworkError, unityWebRequestWrapper.CurrentUnityWebRequest.isHttpError), PNLoggingMethod.LevelInfo);
                    #endif

#if (NETFX_CORE)
#if (ENABLE_PUBNUB_LOGGING)
                    this.PNLog.WriteToLog(string.Format("ProcessResponse: WWW Sub NETFX_CORE {0}\n Message: {1}\n URL: {2}", unityWebRequestWrapper.CurrentRequestType.ToString(), unityWebRequestWrapper.CurrentUnityWebRequest.error, unityWebRequestWrapper.URL), PNLoggingMethod.LevelInfo);
#endif
                    if (!string.IsNullOrEmpty(unityWebRequestWrapper.CurrentUnityWebRequest.error))
                    {
                        message = string.Format("{0}\"Error\": \"{1}\", \"Description\": {2}{3}", "{", unityWebRequestWrapper.CurrentUnityWebRequest.error, message, "}");
                        isError = true;
                    }
#else
                    if ((!unityWebRequestWrapper.CurrentUnityWebRequest.isNetworkError) &&
                        (!unityWebRequestWrapper.CurrentUnityWebRequest.isHttpError))
                    {
#if (ENABLE_PUBNUB_LOGGING)
                        this.PNLog.WriteToLog(string.Format("ProcessResponse: WWW Sub {0}\n Message: {1}\n URL: {2}", unityWebRequestWrapper.CurrentRequestType.ToString(), unityWebRequestWrapper.CurrentUnityWebRequest.error, unityWebRequestWrapper.URL), PNLoggingMethod.LevelInfo);
#endif
                        isError = false;
                    }
                    else
                    {
#if (ENABLE_PUBNUB_LOGGING)
                        this.PNLog.WriteToLog(string.Format("ProcessResponse: WWW Sub {0}\n Error: {1},\n text: {2}\n URL: {3}", unityWebRequestWrapper.CurrentRequestType.ToString(), unityWebRequestWrapper.CurrentUnityWebRequest.error, message, unityWebRequestWrapper.URL), PNLoggingMethod.LevelInfo);
#endif
                        message = string.Format("{0}\"Error\": \"{1}\", \"Description\": {2}{3}", "{", unityWebRequestWrapper.CurrentUnityWebRequest.error, message, "}");
                        isError = true;
                    }
#endif
                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PNLog.WriteToLog(string.Format("message: {0}", message), PNLoggingMethod.LevelInfo);
                    #endif

                    if (unityWebRequestWrapper.CurrentRequestState != null)
                    {
                        unityWebRequestWrapper.CurrentRequestState.EndRequestTicks = DateTime.UtcNow.Ticks;
                        unityWebRequestWrapper.CurrentRequestState.ResponseCode    = unityWebRequestWrapper.CurrentUnityWebRequest.responseCode;
                        unityWebRequestWrapper.CurrentRequestState.URL             = unityWebRequestWrapper.CurrentUnityWebRequest.url;

                        #if (ENABLE_PUBNUB_LOGGING)
                        this.PNLog.WriteToLog(string.Format("ProcessResponse: WWW Sub request2 {0} \n{1} \nresponseCode: {2}", unityWebRequestWrapper.CurrentRequestState.OperationType, unityWebRequestWrapper.CurrentRequestType, unityWebRequestWrapper.CurrentRequestState.ResponseCode), PNLoggingMethod.LevelInfo);
                        #endif
                    }
                    #if (ENABLE_PUBNUB_LOGGING)
                    else
                    {
                        this.PNLog.WriteToLog(string.Format("ProcessResponse: WWW Sub request null2"), PNLoggingMethod.LevelInfo);
                    }
                    this.PNLog.WriteToLog("BEFORE FireEvent", PNLoggingMethod.LevelInfo);
                    #endif
                    FireEvent(message, isError, false, unityWebRequestWrapper.CurrentRequestState, unityWebRequestWrapper.CurrentRequestType, key);
                }
            } catch (PubNubUserException ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("ProcessResponse: PubNubUserException: Exception={0}", ex.ToString()), PNLoggingMethod.LevelError);
                #endif
                throw;
            } catch (Exception ex) {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PNLog.WriteToLog(string.Format("ProcessResponse: RunWebRequestSub {0}, Exception: {1}", unityWebRequestWrapper.CurrentRequestType.ToString(), ex.ToString()), PNLoggingMethod.LevelError);
                #endif
            }
        }
Esempio n. 10
0
        public string Run(RequestState pubnubRequestState)
        {
            if (!string.IsNullOrEmpty(pubnubRequestState.WebRequestId))
            {
                AbortRequest(pubnubRequestState.WebRequestId, false);
            }

            bool delayStart = false;
            //for heartbeat and presence heartbeat treat reconnect as pause
            PNCurrentRequestType crt;

            switch (pubnubRequestState.OperationType)
            {
            case PNOperationType.PNHeartbeatOperation:
                crt        = PNCurrentRequestType.Heartbeat;
                delayStart = pubnubRequestState.Reconnect;
                break;

            case PNOperationType.PNPresenceHeartbeatOperation:
                crt        = PNCurrentRequestType.PresenceHeartbeat;
                delayStart = pubnubRequestState.Reconnect;
                break;

            case PNOperationType.PNSubscribeOperation:
                crt = PNCurrentRequestType.Subscribe;
                if (pubnubRequestState.Pause > 0)
                {
                    delayStart = true;
                }
                break;

            default:
                crt = PNCurrentRequestType.NonSubscribe;
                break;
            }

            #if (ENABLE_PUBNUB_LOGGING)
            this.PNLog.WriteToLog(string.Format("pubnubRequestState: {0}", pubnubRequestState.OperationType), PNLoggingMethod.LevelInfo);
            #endif

            string webRequestId = "";
            UnityWebRequestWrapper webRequestWrapper = new UnityWebRequestWrapper(crt, pubnubRequestState);

            lock (syncRoot){
                webRequestId = GenerateAndValidateWebRequestId();
                currentWebRequests.Add(webRequestId, webRequestWrapper);
            }

            #if (ENABLE_PUBNUB_LOGGING)
            this.PNLog.WriteToLog(string.Format("Run crt: {0}", crt), PNLoggingMethod.LevelInfo);
            #endif

            if (delayStart)
            {
                webRequestWrapper.PauseTimer    = pubnubRequestState.Pause;
                webRequestWrapper.RunPauseTimer = true;
            }
            else
            {
                StartWebRequests(webRequestWrapper, webRequestId);
            }
            return(webRequestId);
        }