Ejemplo n.º 1
0
 public RpcContext(Socket Socket, IOCPReaderWriter IO, IOCPMessageQueueRPC MessageQueue, IRpcCall Call)
 {
     _Socket       = Socket;
     _IO           = IO;
     _MessageQueue = MessageQueue;
     _Call         = Call;
 }
Ejemplo n.º 2
0
        public void WriteMessage(Stream stream, uint requestId, IRpcCall call, IRpcResult result)
        {
            // TODO: It may be more efficient to write complete request to a memory buffer first
            // and then write into the stream to avoid blocking other threads while
            // data actually being serialized.
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                lock (this.writeLock)
                {
                    writer.Write(requestId);

                    byte contentFlags = 0;
                    contentFlags |= call != null ? ProtocolObjectFactory.HasCallFlag : (byte)0;
                    contentFlags |= result != null ? ProtocolObjectFactory.HasResultFlag : (byte)0;
                    writer.Write(contentFlags);

                    if (call != null)
                    {
                        ((RpcCall)call).Serialize(writer);
                    }

                    if (result != null)
                    {
                        ((RpcResult)result).Serialize(writer);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void Load(Assembly assembly)
        {
            var types = AssemblyManager.Instance.GetAllTypesByAttribute(assembly, typeof(MessageHandlerAttribute));

            foreach (Type type in types)
            {
                object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }
                MessageHandlerAttribute messageHandlerAttribute = attrs[0] as MessageHandlerAttribute;
                if (messageHandlerAttribute == null)
                {
                    continue;
                }
                IRpcCall iMHandler = Activator.CreateInstance(type) as IRpcCall;
                if (iMHandler == null)
                {
                    Log.Error($"message handle {type.Name} 需要继承 IMHandler");
                    continue;
                }
                Type   messageType = iMHandler.GetRequestType();
                ushort opcode      = IOpCodeType.GetOpcode(messageType);
                if (opcode == 0)
                {
                    Log.Error($"消息opcode为0: {messageType.Name}");
                    continue;
                }

                RegisterHandler(opcode, iMHandler);
            }
        }
Ejemplo n.º 4
0
        private void ExecuteRequest(ServerContext context, IRpcCall call, IRpcResult result)
        {
            IRpcServiceStub service;

            // Check whether the called method is for service or transient object.
            // The service are identified by name and the lifetime is controlled by the
            // server side.
            // The transient objects are registered as result of a method call and
            // identified by object ID. The lifetime of transient object is
            // controlled by the client.
            if (call.ObjectId != 0)
            {
                // Try to find object that corresponds to the marshalled interface.
                service = context.ObjectManager.GetInstance(call.ObjectId);
                if (service == null)
                {
                    result.Status = RpcStatus.UnknownInterface;
                    this.log.ErrorFormat(
                        "Cannot find object '{0}' with '{1}.{2}' interface",
                        call.ObjectId,
                        call.ServiceName,
                        call.MethodName);
                }
            }
            else
            {
                // Try to find service in global scope and then within connection context.
                service = this.objectManager.GetService(call.ServiceName);
                if (service == null)
                {
                    service = context.ObjectManager.GetService(call.ServiceName);
                    if (service == null)
                    {
                        this.log.ErrorFormat(
                            "Cannot find service with interface '{0}.{1}'",
                            call.ServiceName,
                            call.MethodName);
                        result.Status = RpcStatus.UnknownInterface;
                    }
                }
            }

            if (service != null)
            {
                this.log.TraceFormat("Calling method '{0}.{1}'.", call.ServiceName, call.MethodName);
                service.CallMethod(context, call, result);
            }
            else
            {
                Debug.Assert(result.Status != RpcStatus.Succeeded, "Result cannot be a success");
            }
        }
Ejemplo n.º 5
0
        public void CallMethod(IServerContext context, IRpcCall rpcCall, IRpcResult result)
        {
            if (rpcCall.MethodName == "Ping")
            {
                if (rpcCall.CallData == null || rpcCall.CallData.Length != 4)
                {
                    result.Status = RpcStatus.InvalidCallParameter;
                    return;
                }

                var counter = BitConverter.ToInt32(rpcCall.CallData, 0);
                ++counter;
                result.ResultData = BitConverter.GetBytes(counter);
            }
            else
            {
                result.Status = RpcStatus.UnknownMethod;
            }
        }
Ejemplo n.º 6
0
        public void CallMethod(IServerContext context, IRpcCall rpcCall, IRpcResult rpcResult)
        {
            rpcResult.Status = RpcStatus.Succeeded;

            if (rpcCall.MethodName == "Method_V_V")
            {
                this.Impl.Method_V_V(context);
            }
            else if (rpcCall.MethodName == "Method_V_M")
            {
                var input = new StructType();
                input.MergeFrom(new CodedInputStream(rpcCall.CallData));
                this.Impl.Method_V_M(context, input);
                rpcResult.ResultData = new Empty().ToByteArray();
            }
            else if (rpcCall.MethodName == "Method_M_V")
            {
                var result = this.Impl.Method_M_V(context);
                rpcResult.ResultData = result.ToByteArray();
            }
            else if (rpcCall.MethodName == "Method_M_M")
            {
                var input = new StructType();
                input.MergeFrom(new CodedInputStream(rpcCall.CallData));
                var result = this.Impl.Method_M_M(context, input);
                rpcResult.ResultData = result.ToByteArray();
            }
            else if (rpcCall.MethodName == "AsyncMethod_V_V")
            {
                this.Impl.AsyncMethod_V_V(context);
            }
            else if (rpcCall.MethodName == "AsyncMethod_V_M")
            {
                var input = new StructType();
                input.MergeFrom(new CodedInputStream(rpcCall.CallData));
                this.Impl.AsyncMethod_V_M(context, input);
            }
            else
            {
                rpcResult.Status = RpcStatus.UnknownMethod;
            }
        }
Ejemplo n.º 7
0
 public void CallMethod(IServerContext context, IRpcCall rpcCall, IRpcResult rpcResult)
 {
 }
Ejemplo n.º 8
0
 public void CallMethod(IRpcCall rpcCall)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 9
0
 public void RegisterHandler(ushort opcode, IRpcCall handler)
 {
     m_Dict_Handlers[opcode] = handler;
 }