示例#1
0
            public IPromise <string> Call(RpcRoute route, string argument)
            {
                var message = new RpcMessage
                {
                    Type           = RpcMessageType.ActionRequest,
                    Id             = Id(),
                    ControllerName = route.ControllerName,
                    ActionName     = route.ActionName,
                    Data           = argument,
                    ReturnTag      = new Tag(Aos.Node.NodeId)
                };

                var promise = new Promise <string>();

                _waitingForResult.Add(message.Id, promise);
                _sender.DispatchMessage(route.TargetTag, message.ToString());
                return(promise);
            }
示例#2
0
            void ExecuteAction(RpcMessage message)
            {
                var impl      = _implementationsByName[message.ControllerName];
                var operation = impl.Actions[message.ActionName];

                operation
                .Do(message.Data)
                .Then(result =>
                {
                    var data          = (result as IStringifiable)?.Stringify() ?? result.ToString();
                    var returnMessage = new RpcMessage()
                    {
                        Id   = message.Id,
                        Type = RpcMessageType.ReturnedResult,
                        Data = data
                    };
                    _sender.DispatchMessage(message.ReturnTag, returnMessage.ToString());
                });
            }
示例#3
0
        public object DecodeFrame(IByteBuffer frame)
        {
            byte b0 = frame.ReadByte();
            byte b1 = frame.ReadByte();

            if (ProtocolConstants.MAGIC_CODE_BYTES[0] != b0 ||
                ProtocolConstants.MAGIC_CODE_BYTES[1] != b1)
            {
                throw new ArgumentException("Unknown magic code: " + b0 + ", " + b1);
            }

            byte version = frame.ReadByte();
            // TODO  check version compatible here

            int   fullLength     = frame.ReadInt();
            short headLength     = frame.ReadShort();
            byte  messageType    = frame.ReadByte();
            byte  codecType      = frame.ReadByte();
            byte  compressorType = frame.ReadByte();
            int   requestId      = frame.ReadInt();

            var rpcMessage = new RpcMessage
            {
                Id          = requestId,
                Codec       = codecType,
                Compressor  = compressorType,
                MessageType = messageType
            };

            // direct read head with zero-copy
            int headMapLength = headLength - ProtocolConstants.V1_HEAD_LENGTH;

            if (headMapLength > 0)
            {
                var map = HeadMapSerializer.getInstance().Decode(frame, headMapLength);
                rpcMessage.HeadMap.PutAll(map);
            }

            // read body
            if (messageType == ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST)
            {
                rpcMessage.Body = HeartbeatMessage.PING;
            }
            else if (messageType == ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE)
            {
                rpcMessage.Body = HeartbeatMessage.PONG;
            }
            else
            {
                int bodyLength = fullLength - headLength;
                if (bodyLength > 0)
                {
                    byte[] bs = new byte[bodyLength];
                    frame.ReadBytes(bs);
                    ICompressor compressor = CompressorFactory.GetCompressor(compressorType);
                    bs = compressor.Decompress(bs);
                    ISerializer serializer = EnhancedServiceLoader.Load <ISerializer>(((SerializerType)rpcMessage.Codec).ToString());
                    rpcMessage.Body = serializer.Deserialize <AbstractMessage>(bs);
                }
            }

            if (Logger().IsEnabled(LogLevel.Debug))
            {
                Logger().LogDebug(rpcMessage.ToString());
            }

            return(rpcMessage);
        }