Example #1
0
    public static void Free(IPool _object)
    {
        if (_object == null)
        {
            return;
        }

        ObjectPool objectPool;
        Type       type = _object.GetType();

        if (!poolDict.TryGetValue(type, out objectPool))
        {
            objectPool             = new ObjectPool();
            objectPool.capacity    = defaultCapacity;
            objectPool.objectQueue = new Queue <IPool>();
            poolDict.Add(type, objectPool);
        }

        if (objectPool.objectQueue.Count < objectPool.capacity && !_object.GetInPool())
        {
            objectPool.objectQueue.Enqueue(_object);
            _object.Recycle();
            _object.SetInPool(true);
        }
        else
        {
            _object.OnDestroy();
        }
    }
Example #2
0
        public Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var filterContext = _pool.Rent();

            filterContext.Init(context);

            try
            {
                return(_dispatcher.DispatchAsync(context.Request.Path.ToString().ToLower(), filterContext));
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
            finally
            {
                filterContext.Reset();

                _pool.Recycle(filterContext);
            }

            return(Task.CompletedTask);

            //if (next != null)
            //    await next(context);
        }
Example #3
0
        public async Task ProcessRequest(RpcContext context)
        {
            var filterContext = _pool.Rent();

            filterContext.Init(context);

            try
            {
                await _dispatcher.DispatchAsync(context.Request.Path, filterContext);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
            finally
            {
                _pool.Recycle(filterContext);
            }

            //if (next != null)
            //    await next(context);
        }
Example #4
0
    public void Recycle(Object target)
    {
        //回收
        ActiveInfo info;

        if (mActivePools.TryGetValue(target, out info))
        {
            DecActivePoolInfo(target);
            if (mPools.ContainsKey(info.id))
            {
                IPool pool = mPools[info.id];
                pool.Recycle(target);
            }
            else
            {
                Object.Destroy(target);
            }
        }
        else
        {
            Object.Destroy(target);
        }
    }
Example #5
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            if (message is IByteBuffer buffer)
            {
                var id = 0l;

                /*
                 * skip length field
                 */

                buffer.SkipBytes(4);

                var header = buffer.ReadByte();

                /*
                 * process heart beat
                 */
                if (header == HEARTBEAT_HEADER)
                {
                    if (ensureRegisterd(context))
                    {
                        _checker.UpdateTimeout(context.Channel.Id.AsLongText());
                    }

                    //else ignore
                }
                else if (header == REQUEST)
                {
                    var rpcContext = _pool.Rent();

                    try
                    {
                        /*
                         *  resolve request
                         */
                        var request = new RpcRequest();

                        request.RequestId = buffer.ReadLongLE();

                        id = request.RequestId;

                        var length = buffer.ReadIntLE();

                        var path = buffer.ReadString(length, Encoding.UTF8);

                        parseQuery(request, path);

                        length = buffer.ReadIntLE();

                        request.Body = new byte[length];

                        buffer.ReadBytes(request.Body);

                        /*
                         * login check
                         */
                        if (_validator != null)// configed require  identity validate
                        {
                            try
                            {
                                if (!_checker.IsRegistered(context.Channel.Id.AsLongText()))
                                {
                                    var resposne = _validator.Validate(request.Query["id"], request.Query["password"]) ? RpcResponse.CreateResponse(200, request.RequestId)
                                                                                                                       : RpcResponse.CreateLoginFialedResponse(request.RequestId);

                                    var sendBuffer = _codex.EncodeServerResponse(resposne);

                                    var sendBuffer1 = context.Allocator.Buffer(sendBuffer.Length);

                                    sendBuffer1.WriteBytes(sendBuffer);

                                    context.WriteAndFlushAsync(sendBuffer1);
                                }
                            }
                            catch (Exception ex)
                            {
                                var sendBuffer = _codex.EncodeServerResponse(RpcResponse.CreateErrorResponse(request.RequestId));

                                var sendBuffer1 = context.Allocator.Buffer(sendBuffer.Length);

                                sendBuffer1.WriteBytes(sendBuffer);

                                context.WriteAndFlushAsync(sendBuffer1);

                                return;
                            }
                        }

                        /*
                         *  process request
                         */

                        rpcContext.Init(request, context);

                        _middleWare.ProcessRequest(rpcContext);
                    }
                    catch (Exception ex)
                    {
                        _logger?.Error(ex);

                        context.WriteAndFlushAsync(RpcResponse.CreateErrorResponse(10000));
                    }
                    finally
                    {
                        _pool.Recycle(rpcContext);
                    }
                }
            }
            else
            {
                _logger?.Warn($"unexcepted input {message}");
            }
        }