Example #1
0
        //同步方法
        public RpcResult <TaskVoidRes> ScheduleTransfor(ScheduleTaskReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(2001, 105);

            message.FriendlyServiceName = "TaskProducerService.ScheduleTransfor";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <TaskVoidRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new TaskVoidRes();
            }
            else
            {
                result.Data = TaskVoidRes.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #2
0
        static void Main(string[] args)
        {
            new ClientProxyBuilder().UseServer("127.0.0.1:6201").BuildDefault();

            using (var caller = DotBPE.Rpc.Environment.ServiceProvider.GetService <ICallInvoker <AmpMessage> >())
            {
                Console.WriteLine("ready to send message");
                ushort     serviceId = 10000;
                AmpMessage req       = AmpMessage.CreateRequestMessage(serviceId, 0);
                req.Data = Encoding.UTF8.GetBytes("dotbpe");

                Console.WriteLine("send name:dotbpe to sever-->");

                try
                {
                    var res = caller.BlockingCall(req);

                    if (res.Code == 0)
                    {
                        string hello = Encoding.UTF8.GetString(res.Data);
                        Console.WriteLine("server repsonse:<-----{0}", hello);
                    }
                    else
                    {
                        Console.WriteLine("server error,code={0}", res.Code);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("error occ {0}", ex.Message);
                }
            }
            Console.WriteLine("channel is closed!");
        }
        //同步方法
        public RpcResult <HelloRes> SayHelloAgain(HelloReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10006, 2);

            message.FriendlyServiceName = "Greeter.SayHelloAgain";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <HelloRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = HelloRes.Parser.ParseFrom(response.Data);
            }
            return(result);
        }
        //同步方法
        public RpcResult <QPaperStaRsp> GetAPaperSta(GetQPaperStaReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10000, 24);

            message.FriendlyServiceName = "SurveyGateService.GetAPaperSta";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <QPaperStaRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = QPaperStaRsp.Parser.ParseFrom(response.Data);
            }
            return(result);
        }
Example #5
0
        public async Task <RpcResult <TaskVoidRes> > ScheduleSecondTransforAsync(ScheduleTaskReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(2001, 106);

            message.FriendlyServiceName = "TaskProducerService.ScheduleSecondTransfor";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <TaskVoidRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new TaskVoidRes();
            }
            else
            {
                result.Data = TaskVoidRes.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
        //同步方法
        public RpcResult <RegisterRsp> Register(RegisterReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10001, 31);

            message.FriendlyServiceName = "UserGateService.Register";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <RegisterRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = RegisterRsp.Parser.ParseFrom(response.Data);
            }
            return(result);
        }
Example #7
0
        public async Task <RpcResult <AddRes> > AddAsync(AddReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10005, 1);

            message.FriendlyServiceName = "Math.Add";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <AddRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new AddRes();
            }
            else
            {
                result.Data = AddRes.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #8
0
        //同步方法
        public RpcResult <LoginRsp> Login(LoginReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20003, 2);

            message.FriendlyServiceName = "UserService.Login";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <LoginRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new LoginRsp();
            }
            else
            {
                result.Data = LoginRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
        //同步方法
        public RpcResult <VoidRsp> AddAPaperCount(AddAPaperReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20001, 5);

            message.FriendlyServiceName = "QPaperService.AddAPaperCount";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <VoidRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new VoidRsp();
            }
            else
            {
                result.Data = VoidRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #10
0
        public async Task <RpcResult <RegisterRsp> > RegisterAsync(RegisterReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20003, 1);

            message.FriendlyServiceName = "UserService.Register";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <RegisterRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new RegisterRsp();
            }
            else
            {
                result.Data = RegisterRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #11
0
        public async Task <RpcResult <VoidRes> > EnqueueAsync(EnqueueReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(1000, 1);

            message.FriendlyServiceName = "TaskProducerService.Enqueue";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <VoidRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = VoidRes.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #12
0
        //同步方法
        public RpcResult <VoidRes> Schedule(ScheduleReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(1000, 2);

            message.FriendlyServiceName = "TaskProducerService.Schedule";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <VoidRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = VoidRes.Parser.ParseFrom(response.Data);
            }
            return(result);
        }
Example #13
0
        public async Task SendAsyncWithoutErrorTest()
        {
            var router = new Mock <IServiceRouter>();

            router.Setup(x => x.FindRouterPoint(It.IsAny <string>())).Returns(
                Task.FromResult <IRouterPoint>(new RouterPoint
            {
                RoutePointType = RoutePointType.Remote
            }));

            var transport = new Mock <ITransport <AmpMessage> >();

            transport.Setup(x => x.SendAsync(It.IsAny <AmpMessage>())).Returns(Task.CompletedTask);

            var factory = new Mock <ITransportFactory <AmpMessage> >();

            factory.Setup(x => x.CreateTransport(It.IsAny <EndPoint>())).Returns(
                Task.FromResult(transport.Object));

            var handler = new Mock <IClientMessageHandler <AmpMessage> >();
            var logger  = NullLogger <DefaultRpcClient> .Instance;


            var client = new DefaultRpcClient(router.Object, factory.Object, handler.Object, logger);

            await client.SendAsync(AmpMessage.CreateRequestMessage(100, 1));
        }
Example #14
0
        public async Task <RpcResult <APaperStaDetailRsp> > GetAPaperStaAsync(GetAPaperStaDetailReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20002, 4);

            message.FriendlyServiceName = "APaperService.GetAPaperSta";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <APaperStaDetailRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new APaperStaDetailRsp();
            }
            else
            {
                result.Data = APaperStaDetailRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
        //同步方法
        public RpcResult <QPaperListRsp> QueryQPaperList(QueryQPaperReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20001, 2);

            message.FriendlyServiceName = "QPaperInnerService.QueryQPaperList";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <QPaperListRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = QPaperListRsp.Parser.ParseFrom(response.Data);
            }
            return(result);
        }
        public async Task <RpcResult <APaperListRsp> > QueryAPaperListAsync(QueryAPaperReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10000, 22);

            message.FriendlyServiceName = "SurveyGateService.QueryAPaperList";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <APaperListRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = APaperListRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
        public async Task <RpcResult <SaveQPaperRsp> > SaveQPaperAsync(SaveQPaperReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20001, 1);

            message.FriendlyServiceName = "QPaperInnerService.SaveQPaper";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <SaveQPaperRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = SaveQPaperRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
        public async Task <RpcResult <GetUserRsp> > CheckLoginAsync(CheckLoginReq req, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10001, 34);

            message.FriendlyServiceName = "UserGateService.CheckLogin";
            message.Data = req.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <GetUserRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }
            else if (response.Data == null)
            {
                result.Code = ErrorCodes.CODE_INTERNAL_ERROR;
            }
            else
            {
                result.Data = GetUserRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #19
0
        public async Task BaseServiceProcessCallTest()
        {
            ISerializer serializer = new JsonSerializer();
            var         fooService = new FooService();

            Assert.Equal("100.0", fooService.Id);


            var req = new FooReq {
                FooWord = "hello Tomato"
            };

            var context1 = new MockContext();

            var reqMessage = AmpMessage.CreateRequestMessage(100, 2);

            reqMessage.Version   = 1;
            reqMessage.CodecType = CodecType.JSON;
            reqMessage.Sequence  = 1;
            reqMessage.Data      = serializer.Serialize(req);

            await fooService.ReceiveAsync(context1, reqMessage);

            Assert.NotNull(context1.ResponseMessage);
            Assert.Equal(0, context1.ResponseMessage.Code);

            Assert.NotNull(context1.ResponseMessage.Data);

            var res = serializer.Deserialize <FooRes>(context1.ResponseMessage.Data);

            Assert.NotNull(res);

            Assert.Equal(req.FooWord, res.RetFooWord);
        }
Example #20
0
        /// <summary>
        /// Async Call
        /// </summary>
        /// <param name="request"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        private async Task <AmpMessage> AsyncCallInner(AmpMessage request, int timeout = 3000)
        {
            DotBPEDiagnosticListenerExtensions.Listener.ClientSendRequest(request);
            AutoSetSequence(request);
            this._logger.LogDebug("new request id={0},type={1}", request.Id, request.InvokeMessageType);

            if (request.InvokeMessageType == InvokeMessageType.InvokeWithoutResponse)
            {
                await SendAsync(request);

                var rsp = AmpMessage.CreateRequestMessage(request.ServiceId, request.MessageId);
                DotBPEDiagnosticListenerExtensions.Listener.ClientReceiveResponse(request, rsp);
                return(rsp);
            }

            var cts = new CancellationTokenSource(timeout);

            //timeout callback
            using (cts.Token.Register(() => TimeOutCallBack(request.Id), false))
            {
                //register callback
                var callbackTask = RegisterResultCallbackAsync(request.Id);

                //async call
                await SendAsync(request);

                //get return message
                var rsp = await callbackTask;

                DotBPEDiagnosticListenerExtensions.Listener.ClientReceiveResponse(request, rsp);
                return(rsp);
            }
        }
Example #21
0
        public async Task <RpcResult <TResult> > AsyncRequest <T, TResult>(string callName, string groupName, int serviceId, ushort messageId,
                                                                           T req, int timeout = 3000)
        {
            RpcResult <TResult> result = new RpcResult <TResult>();
            var reqMessage             = AmpMessage.CreateRequestMessage(serviceId, messageId);

            reqMessage.FriendlyServiceName = callName;
            reqMessage.ServiceGroupName    = groupName;
            reqMessage.CodecType           = (CodecType)Enum.ToObject(typeof(CodecType), this._serializer.CodecType);
            reqMessage.Data = this._serializer.Serialize(req);
            var rsp = await AsyncCallInner(reqMessage, timeout);

            if (rsp != null)
            {
                result.Code = rsp.Code;
                if (rsp.Data != null)
                {
                    result.Data = this._serializer.Deserialize <TResult>(rsp.Data);
                }
            }
            else
            {
                this._logger.LogError("Call {0} , return null", callName);
                result.Code = RpcErrorCodes.CODE_INTERNAL_ERROR;
            }
            return(result);
        }
Example #22
0
        //同步方法
        public RpcResult <AddRes> Add(AddReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10005, 1);

            message.FriendlyServiceName = "Math.Add";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <AddRes>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new AddRes();
            }
            else
            {
                result.Data = AddRes.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #23
0
        //同步方法
        public RpcResult <APaperStaDetailRsp> GetAPaperSta(GetAPaperStaDetailReq req)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(20002, 4);

            message.FriendlyServiceName = "APaperService.GetAPaperSta";


            message.Data = req.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            var result = new RpcResult <APaperStaDetailRsp>();

            if (response.Code != 0)
            {
                result.Code = response.Code;
            }

            if (response.Data == null)
            {
                result.Data = new APaperStaDetailRsp();
            }
            else
            {
                result.Data = APaperStaDetailRsp.Parser.ParseFrom(response.Data);
            }

            return(result);
        }
Example #24
0
//同步方法
        public HelloResponse Hello(HelloRequest request)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(100, 1);

            message.Data = request.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response != null && response.Data != null)
            {
                return(HelloResponse.Parser.ParseFrom(response.Data));
            }
            throw new RpcException("请求出错,请检查!");
        }
Example #25
0
        public async Task <HelloResponse> HelloAsync(HelloRequest request, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(100, 1);

            message.Data = request.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response != null && response.Data != null)
            {
                return(HelloResponse.Parser.ParseFrom(response.Data));
            }
            throw new RpcException("请求出错,请检查!");
        }
        private async Task Excute(int serviceId, int messageId, string data, PerformContext context)
        {
            try
            {
                CommonTaskReq req = new CommonTaskReq();
                req.JobId     = context.BackgroundJob.Id;
                req.ServiceId = serviceId;
                req.MessageId = messageId;
                req.Data      = data;

                var reqMsg = AmpMessage.CreateRequestMessage(HangfireConstant.CONSUMER_SERVER_ID, HangfireConstant.CONSUMER_DEFAULT_MESSAGE_ID);
                reqMsg.Data = req.ToByteArray();

                context.WriteLine("Start Excute Task With serviceId={0},messageId={1},data ={2}", serviceId, messageId, data);

                var resMsg = await _invoker.AsyncCall(reqMsg, 30000); //30秒超时

                if (resMsg != null)
                {
                    if (resMsg.Code == 0)
                    {
                        context.WriteLine("Excute Success!");
                    }
                    else
                    {
                        context.SetTextColor(ConsoleTextColor.Red);
                        string errorMsg = "";
                        if (resMsg.Data != null)
                        {
                            var resData = TaskVoidRes.Parser.ParseFrom(resMsg.Data);
                            errorMsg = resData.ReturnMessage;
                        }

                        context.WriteLine("Excute failed, Code = {0},ErrorMessage={1}", resMsg.Code, errorMsg);
                        context.ResetTextColor();
                    }
                }
                else
                {
                    context.SetTextColor(ConsoleTextColor.Red);
                    context.WriteLine("Excute Error, Response is null");
                    context.ResetTextColor();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "调用任务消费服务出错:{0}", ex.Message);
            }
        }
Example #27
0
//同步方法
        public BenchmarkMessage Echo(BenchmarkMessage request)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(50000, 1);

            message.Data = request.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            if (response.Data == null)
            {
                return(new BenchmarkMessage());
            }
            return(BenchmarkMessage.Parser.ParseFrom(response.Data));
        }
Example #28
0
//同步方法
        public HelloRes SayHelloAgain(HelloReq request)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10006, 2);

            message.Data = request.ToByteArray();
            var response = base.CallInvoker.BlockingCall(message);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            if (response.Data == null)
            {
                return(new HelloRes());
            }
            return(HelloRes.Parser.ParseFrom(response.Data));
        }
Example #29
0
        public async Task <HelloRes> SayHelloAgainAsync(HelloReq request, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(10006, 2);

            message.Data = request.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            if (response.Data == null)
            {
                return(new HelloRes());
            }
            return(HelloRes.Parser.ParseFrom(response.Data));
        }
Example #30
0
        public async Task <BenchmarkMessage> EchoAsync(BenchmarkMessage request, int timeOut = 3000)
        {
            AmpMessage message = AmpMessage.CreateRequestMessage(50000, 1);

            message.Data = request.ToByteArray();
            var response = await base.CallInvoker.AsyncCall(message, timeOut);

            if (response == null)
            {
                throw new RpcException("error,response is null !");
            }
            if (response.Data == null)
            {
                return(new BenchmarkMessage());
            }
            return(BenchmarkMessage.Parser.ParseFrom(response.Data));
        }