public TValue AddOrUpdate(TKey key, Func <TKey, TValue> addValueFactory, Func <TKey, TValue, TValue> updateValueFactory)
        {
            if (addValueFactory == null)
            {
                throw new ArgumentNullException("addValueFactory");
            }
            if (updateValueFactory == null)
            {
                throw new ArgumentNullException("updateValueFactory");
            }
            var needle = PrivateGetNeedle(key);
            Func <WeakNeedle <TKey>, TValue, TValue> factory = (pairKey, foundValue) =>
            {
                TKey foundKey;
                if (PrivateTryGetValue(pairKey, out foundKey))
                {
                    return(updateValueFactory(foundKey, foundValue));
                }
                return(addValueFactory(key));
            };
            Func <WeakNeedle <TKey>, TValue> valueFactory = input => addValueFactory(key);
            bool added;
            var  result = _wrapped.AddOrUpdate
                          (
                needle,
                valueFactory,
                factory,
                out added
                          );

            if (!added)
            {
                _reservoir.DonateNeedle(needle);
            }
            return(result);
        }
Example #2
0
        internal static SafeDictionary <PubnubChannelCallbackKey, object> CreateChannelCallbacks <T>(string[] channels, ResponseType responseType,
                                                                                                     Action <T> userCallback, Action <T> connectCallback, Action <PubnubClientError> errorCallback)
        {
            SafeDictionary <PubnubChannelCallbackKey, object> channelCallbacks = new SafeDictionary <PubnubChannelCallbackKey, object> ();

            foreach (string channel in channels)
            {
                PubnubChannelCallbackKey callbackKey = new PubnubChannelCallbackKey();
                callbackKey.Channel = channel;
                callbackKey.Type    = responseType;
                PubnubChannelCallback <T> pubnubChannelCallbacks = new PubnubChannelCallback <T> ();
                pubnubChannelCallbacks.Callback        = userCallback;
                pubnubChannelCallbacks.ConnectCallback = connectCallback;
                pubnubChannelCallbacks.ErrorCallback   = errorCallback;
                channelCallbacks.AddOrUpdate(callbackKey, pubnubChannelCallbacks, (key, oldValue) => pubnubChannelCallbacks);
            }
            return(channelCallbacks);
        }
Example #3
0
 /// <summary>
 /// GetRequest
 /// </summary>
 /// <param name="encoding">编码</param>
 /// <returns></returns>
 public static ISafeDictionary<string, string> GetRequest(Encoding encoding) {
     ISafeDictionary<string, string> list = new SafeDictionary<string, string>();
     string data = GetRequestInputStream(encoding);
     string[] arr = data.Split('&');
     foreach (string info in arr) {
         if (info.IndexOf("=") != -1) {
             string[] arr2 = info.Split('=');
             list.AddOrUpdate(arr2[0], arr2[1]);
         } else {
             list.AddOrUpdate("", info);
         }
     }
     return list;
 }
        public void TestResponseCallbackWebExceptionHandlerCommon <T>(string message, string[] channels,
                                                                      bool resumeOnReconnect, ResponseType responseType, CurrentRequestType crt, Action <T> userCallback,
                                                                      Action <T> connectCallback, Action <PubnubClientError> errorCallback,
                                                                      bool isTimeout, bool isError, long timetoken, bool ssl, PubnubErrorFilter.Level errorLevel
                                                                      )
        {
            ExceptionMessage = message;
            ExceptionChannel = string.Join(",", channels);

            if (isTimeout)
            {
                ExceptionMessage = "Operation Timeout";
                IsTimeout        = true;
            }
            else
            {
                IsTimeout = false;
            }

            if (isError)
            {
                IsError = true;
            }
            else
            {
                IsError = false;
            }

            RequestState <T> requestState = BuildRequests.BuildRequestState <T> (channels, responseType,
                                                                                 resumeOnReconnect, userCallback, connectCallback, errorCallback, 0, isTimeout, timetoken, typeof(T));

            CustomEventArgs <T> cea = new CustomEventArgs <T> ();

            cea.PubnubRequestState = requestState;
            cea.Message            = message;
            cea.IsError            = isError;
            cea.IsTimeout          = isTimeout;
            cea.CurrRequestType    = crt;

            CRequestType = responseType;
            if (responseType == ResponseType.Presence || responseType == ResponseType.Subscribe)
            {
                ExceptionHandlers.MultiplexException += HandleMultiplexException <T>;
                resultPart1 = false;
            }

            WebException webEx = new WebException("Test web exception");

            SafeDictionary <PubnubChannelCallbackKey, object> channelCallbacks = new SafeDictionary <PubnubChannelCallbackKey, object> ();

            PubnubChannelCallbackKey callbackKey = new PubnubChannelCallbackKey();

            callbackKey.Channel = ExceptionChannel;
            callbackKey.Type    = responseType;
            PubnubChannelCallback <T> pubnubChannelCallbacks = new PubnubChannelCallback <T>();

            pubnubChannelCallbacks.Callback        = userCallback;
            pubnubChannelCallbacks.ConnectCallback = connectCallback;
            pubnubChannelCallbacks.ErrorCallback   = errorCallback;
            channelCallbacks.AddOrUpdate(callbackKey, pubnubChannelCallbacks, (key, oldValue) => pubnubChannelCallbacks);

            ExceptionHandlers.ResponseCallbackWebExceptionHandler <T> (cea, requestState, webEx, ExceptionChannel, channelCallbacks,
                                                                       errorLevel);

            /*if (responseType == ResponseType.Presence || responseType == ResponseType.Subscribe) {
             *  DateTime dt = DateTime.Now;
             *  while (dt.AddSeconds(2) > DateTime.Now) {
             *      UnityEngine.Debug.Log ("waiting");
             *  }
             * }*/
        }