public void OnDSUpdated(object result, CallbackEntry cbEntry, OpCode operationCode)
        {
            if (cbEntry != null)
            {
                AsyncCallbackInfo info = cbEntry.WriteBehindOperationCompletedCallback as AsyncCallbackInfo;
                if (info != null && (short)info.Callback != -1)
                {
                    Hashtable resTbl = result as Hashtable;
                    Hashtable newRes = null;
                    if (resTbl != null)
                    {
                        newRes = new Hashtable();
                        IDictionaryEnumerator ide = resTbl.GetEnumerator();
                        while (ide.MoveNext())
                        {
                            object val = ide.Value;
                            if (val != null && val is string)
                            {
                                newRes[ide.Key] = (DataSourceOpResult)Convert.ToInt32(val);
                            }
                            else
                            {
                                newRes[ide.Key] = ide.Value;
                            }
                        }
                    }

                    _parent.OnDataSourceUpdated((short)info.Callback, newRes as Hashtable, operationCode, true);
                }
            }
        }
        public void Process()
        {
            object[] package = null;
            package = (object[])SerializationUtil.CompactDeserialize(_result, _cacheContext);

            string            key      = (string)package[0];
            AsyncCallbackInfo cbInfo   = (AsyncCallbackInfo)package[1];
            object            opResult = package[2];

            ClientManager clientManager = null;

            lock (ConnectionManager.ConnectionTable)
                clientManager = (ClientManager)ConnectionManager.ConnectionTable[cbInfo.Client];

            if (clientManager != null)
            {
                Alachisoft.NCache.Common.Protobuf.Response response = new Alachisoft.NCache.Common.Protobuf.Response();
                response.requestId = cbInfo.RequestID;
                response.asyncOpCompletedCallback = Alachisoft.NCache.SocketServer.Util.EventHelper.GetAsyncOpCompletedResponse(clientManager, cbInfo, opResult, _opCode, key);
                response.responseType             = Alachisoft.NCache.Common.Protobuf.Response.Type.ASYNC_OP_COMPLETED_CALLBACK;

                IList serializedResponse = Alachisoft.NCache.Common.Util.ResponseHelper.SerializeResponse(response);

                ConnectionManager.AssureSend(clientManager, serializedResponse, Common.Enum.Priority.Low);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 更新异步回调
        /// </summary>
        private void UpdateAsyncCallback()
        {
            for (int i = 0; i < m_asyncCallbackInfos.Count; ++i)
            {
                AsyncCallbackInfo info = m_asyncCallbackInfos[i];
                info.OnLoaded(info.Group, info.Data);
            }

            m_asyncCallbackInfos.Clear();
        }
Beispiel #4
0
        private object PackageResult(string key, short callbackId, object result)
        {
            object[] package = new object[3];
            package[0] = key;

            AsyncCallbackInfo cbEntry = new AsyncCallbackInfo(-1, null, callbackId);

            package[1] = cbEntry;
            package[2] = result;

            return(package);
        }
Beispiel #5
0
        /// <summary>
        /// 增加异步回调
        /// </summary>
        /// <param name="onLoaded">回调</param>
        /// <param name="group">加载组</param>
        /// <param name="data">资源对象</param>
        public void AddAsyncCallback(LoadMgr.GroupLoadedCallback onLoaded, LoaderGroup group, object data)
        {
            if (onLoaded == null)
            {
                return;
            }

            AsyncCallbackInfo info = new AsyncCallbackInfo
            {
                OnLoaded = onLoaded,
                Group    = group,
                Data     = data
            };

            m_asyncCallbackInfos.Add(info);
        }
Beispiel #6
0
        /// <summary>
        /// Called when an async operation is completed
        /// </summary>
        /// <param name="key">key being used for async operation</param>
        /// <param name="callbackEntry">callback entry being used for async operation</param>
        private void AsyncOperationCompleted(object opCode, object result, EventContext eventContext)
        {
            if (result is object[])
            {
                if (_client != null)
                {
                    AsyncCallbackInfo cbInfo = ((object[])result)[1] as AsyncCallbackInfo;
                    if (cbInfo != null && cbInfo.Client != _client.ClientID)
                    {
                        return;
                    }
                    //client older then 4.1 sp2 private patch 4 does not support bulk Events
                    if (_client.ClientVersion >= 4124)
                    {
                        object[] package = null;
                        package = (object[])SerializationUtil.CompactDeserialize(result, _cacheId);

                        string            key           = (string)package[0];
                        AsyncCallbackInfo cbInformation = (AsyncCallbackInfo)package[1];
                        object            opResult      = package[2];

                        Alachisoft.NCache.Common.Protobuf.AsyncOperationCompletedCallbackResponse asyncOperationCompleted = EventHelper.GetAsyncOpCompletedResponse(_client, cbInformation, opResult, opCode, key);

                        Alachisoft.NCache.Common.Protobuf.BulkEventItemResponse eventitem = new Common.Protobuf.BulkEventItemResponse();
                        eventitem.eventType = Common.Protobuf.BulkEventItemResponse.EventType.ASYNC_OP_COMPLETED_EVENT;
                        eventitem.asyncOperationCompletedCallback = asyncOperationCompleted;

                        //_client.EnqueueEvent(eventitem);
                        //To avoid NullReference problem if both evnt and NCache.Dispose are called simultaenously
                        ClientManager client = _client;
                        if (client != null)
                        {
                            client.ConnectionManager.EnqueueEvent(eventitem, _client.SlaveId);
                        }
                    }
                    else
                    {
                        lock (ConnectionManager.CallbackQueue)
                        {
                            ConnectionManager.CallbackQueue.Enqueue(new AsyncOpCompletedCallback(/*notification.CallerID,*/ opCode, result, /*notification.ClientSocket,*/ _cacheId));
                            Monitor.Pulse(ConnectionManager.CallbackQueue);
                        }
                    }
                }
            }
        }
Beispiel #7
0
        internal static AsyncOperationCompletedCallbackResponse GetAsyncOpCompletedResponse(ClientManager clientManager, AsyncCallbackInfo cbInfo, object opResult, object opCode, string key)
        {
            Alachisoft.NCache.Common.Protobuf.AsyncOperationCompletedCallbackResponse asyncOperationCompleted = new Alachisoft.NCache.Common.Protobuf.AsyncOperationCompletedCallbackResponse();

            switch ((AsyncOpCode)opCode)
            {
            case AsyncOpCode.Add:
            case AsyncOpCode.Remove:
            case AsyncOpCode.Update:
                asyncOperationCompleted.key = key;
                break;
            }

            asyncOperationCompleted.requestId = cbInfo.RequestID;

            if (opResult is System.Exception)
            {
                Alachisoft.NCache.Common.Protobuf.Exception exc = new Alachisoft.NCache.Common.Protobuf.Exception();
                exc.message   = ((System.Exception)opResult).Message;
                exc.exception = ((System.Exception)opResult).ToString();
                exc.type      = Alachisoft.NCache.Common.Protobuf.Exception.Type.GENERALFAILURE;

                asyncOperationCompleted.exc     = exc;
                asyncOperationCompleted.success = false;
            }
            else
            {
                asyncOperationCompleted.success = true;
            }

            return(asyncOperationCompleted);
        }
Beispiel #8
0
        public virtual void OnAsyncOperationCompleted(object opCode, object result, bool notifyAsync)
        {
            try
            {
                BitSet   flag    = new BitSet();
                object[] package = null;

                package = (object[])_parent.SafeDeserialize(result, _parent._serializationContext, flag);

                string            key    = (string)package[0];
                AsyncCallbackInfo cbInfo = (AsyncCallbackInfo)package[1];
                object            res    = package[2];

                AsyncOpCode code      = (AsyncOpCode)opCode;
                int         processid = System.Diagnostics.Process.GetCurrentProcess().Id;

                switch (code)
                {
                case AsyncOpCode.Add:
                    try
                    {
                        if (cbInfo != null)
                        {
                            AsyncItemAddedCallback cb =
                                (AsyncItemAddedCallback)_parent._asyncCallbackIDsMap.GetResource(
                                    cbInfo.Callback);
                            if (cb != null)
                            {
                                _parent._asyncCallbackIDsMap.RemoveResource(cbInfo.Callback);
                                _parent._asyncCallbacksMap.RemoveResource(
                                    "aiacb-" + processid + "-" + cb.Method.Name);

                                if (notifyAsync)
                                {
#if !NETCORE
                                    cb.BeginInvoke(key, res, null, null);
#elif NETCORE
                                    TaskFactory factory = new TaskFactory();
                                    Task        task    = factory.StartNew(() => cb(key, res));
#endif
                                }
                                else
                                {
                                    cb(key, res);
                                }

                                if (_parent._perfStatsCollector != null)
                                {
                                    _parent._perfStatsCollector.IncrementEventsProcessedPerSec();
                                }
                            }
                        }
                    }
                    catch
                    {
                    }

                    break;

                case AsyncOpCode.Update:
                    try
                    {
                        if (cbInfo != null)
                        {
                            AsyncItemUpdatedCallback cb =
                                (AsyncItemUpdatedCallback)_parent._asyncCallbackIDsMap.GetResource(
                                    cbInfo.Callback);
                            if (cb != null)
                            {
                                if (notifyAsync)
                                {
#if !NETCORE
                                    cb.BeginInvoke(key, res, null, null);
#elif NETCORE
                                    TaskFactory factory = new TaskFactory();
                                    Task        task    = factory.StartNew(() => cb(key, res));
#endif
                                }
                                else
                                {
                                    cb(key, res);
                                }

                                _parent._asyncCallbackIDsMap.RemoveResource(cbInfo.Callback);
                                _parent._asyncCallbacksMap.RemoveResource(
                                    "aiucb-" + processid + "-" + cb.Method.Name);
                            }
                        }
                    }
                    catch
                    {
                    }

                    break;

                case AsyncOpCode.Remove:
                    try
                    {
                        if (cbInfo != null)
                        {
                            AsyncItemRemovedCallback cb =
                                (AsyncItemRemovedCallback)_parent._asyncCallbackIDsMap.GetResource(
                                    cbInfo.Callback);
                            if (cb != null)
                            {
                                if (notifyAsync)
                                {
#if !NETCORE
                                    cb.BeginInvoke(key, res, null, null);
#elif NETCORE
                                    TaskFactory factory = new TaskFactory();
                                    Task        task    = factory.StartNew(() => cb(key, res));
#endif
                                }
                                else
                                {
                                    cb(key, res);
                                }

                                _parent._asyncCallbackIDsMap.RemoveResource(cbInfo.Callback);
                                _parent._asyncCallbacksMap.RemoveResource(
                                    "aircb-" + processid + "-" + cb.Method.Name);
                            }
                        }
                    }
                    catch
                    {
                    }

                    break;

                case AsyncOpCode.Clear:
                    try
                    {
                        if (cbInfo != null)
                        {
                            AsyncCacheClearedCallback cb =
                                (AsyncCacheClearedCallback)_parent._asyncCallbackIDsMap.GetResource(
                                    cbInfo.Callback);
                            if (cb != null)
                            {
                                if (notifyAsync)
                                {
#if !NETCORE
                                    cb.BeginInvoke(res, null, null);
#elif NETCORE
                                    TaskFactory factory = new TaskFactory();
                                    Task        task    = factory.StartNew(() => cb(res));
#endif
                                }
                                else
                                {
                                    cb(res);
                                }

                                _parent._asyncCallbackIDsMap.RemoveResource(cbInfo.Callback);
                                _parent._asyncCallbacksMap.RemoveResource(
                                    "acccb-" + processid + "-" + cb.Method.Name);
                            }
                        }
                    }
                    catch
                    {
                    }

                    break;
                }
            }

            catch
            {
            }
        }