Esempio n. 1
0
        private async Task writeResponse(RpcResponse response, RpcFilterContext context)
        {
            var responseBuffer = _codex.EncodeServerResponse(response);

            var bufferToSend = context.RpcContext.HandlerContext.Allocator.Buffer(responseBuffer.Length);

            bufferToSend.WriteBytes(responseBuffer);

            await context.RpcContext.HandlerContext.WriteAndFlushAsync(bufferToSend);
        }
Esempio n. 2
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}");
            }
        }