public void Init(uint serviceKey, NetContractDescription description, IMessageFactory messageFactory, INetPeer channel, ActorKey?actor = null)
 {
     Actor           = actor;
     _description    = description;
     _messageFactory = messageFactory;
     Channel         = channel;
 }
        public OperationDispatcher(IMessageFactory msgfactory, IEnumerable <NetProxy> importedProxies)
        {
            _messageFactory = msgfactory;

            foreach (var proxy in importedProxies)
            {
                Type type = proxy.GetType();
                if (type.ContainsAttribute <NetProxyAttribute>())
                {
                    var proxyAttr    = type.GetAttribute <NetProxyAttribute>();
                    var contractAttr = proxyAttr.ContractType.GetAttribute <NetContractAttribute>();

                    var operations = new List <NetOperationDescription>();
                    foreach (var method in type.GetMethods().Where(x => x.ContainsAttribute <NetOperationDispatcherAttribute>()))
                    {
                        var     opAttr  = method.GetAttribute <NetOperationDispatcherAttribute>();
                        Message request = (Message)Activator.CreateInstance(opAttr.RequestMessage);
                        uint?   replyId = null;
                        if (opAttr.ReplyMessage != null)
                        {
                            replyId = ((Message)Activator.CreateInstance(opAttr.ReplyMessage)).Id;
                        }
                        var opDesc = new NetOperationDescription(
                            method.Name, request.Id, replyId, (Func <IMessageFactory, object, Message, Message>)Delegate.CreateDelegate(
                                typeof(Func <IMessageFactory, object, Message, Message>), method));
                        operations.Add(opDesc);
                    }

                    var desc = new NetContractDescription(proxyAttr.ContractTypeId,
                                                          proxyAttr.ContractType, type, contractAttr, operations);
                    RegisterService(desc);
                }
            }
        }
        public TNetContract GetProxy <TNetContract>(uint?localActorId = null) where TNetContract : class
        {
            NetContractDescription contractDesc = Dispatcher.GetContract <TNetContract>();
            var      proxyKey = new ActorProxyKey(new ActorKey(0, localActorId ?? 0), contractDesc.TypeId);
            NetProxy proxy;

            if (!_proxyCache.TryGetValue(proxyKey, out proxy))
            {
                proxy = Dispatcher.CreateProxy(contractDesc.TypeId, MsgFactory, this, localActorId.HasValue ? proxyKey.ActorKey : (ActorKey?)null);
                _proxyCache.Add(proxyKey, proxy);
            }

            return((TNetContract)(object)proxy);
        }
        public void RegisterService(NetContractDescription desc)
        {
            _descByTypeId.Add(desc.TypeId, desc);
            _contractIdByContractType.Add(desc.ContractType, desc.TypeId);

            foreach (var operation in desc.Operations)
            {
                _descByMessageId.Add(operation.RequestMessageId, desc);
                if (operation.ReplyMessageId.HasValue)
                {
                    _descByMessageId.Add(operation.ReplyMessageId.Value, desc);
                }
                _dispatcherByMsgId.Add(operation.RequestMessageId, operation.Dispatch);
            }

            Log.Info("Registered Entity<contractType:{0}, typeId:{1}>", desc.ContractType, desc.TypeId);
        }
        public void OnNetData(BinaryReader reader)
        {
            Message msg = MsgFactory.Deserialize(reader);


            var opHeader = msg.GetHeader <OperationHeader>();

            if (opHeader != null && opHeader.Type == OperationType.Reply)
            {
                Future <Message> future;
                if (_pendingOperation.TryGetValue(opHeader.RequestId, out future))
                {
                    if (msg is InvalidOperation)
                    {
                        var inv = msg as InvalidOperation;
                        future.SetError(inv.ErrorCode, inv.DebugDescription);
                    }
                    else
                    {
                        future.SetResult(msg);
                    }
                    _pendingOperation.Remove(opHeader.RequestId);
                }
            }
            else
            {
                NetContractDescription contractDesc = Dispatcher.GetContractForMessage(msg.Id);

                object handler;
                if (_handlersByNetContractId.TryGetValue(contractDesc.TypeId, out handler))
                {
                    Dispatcher.DispatchOneWay(handler, msg);
                }
            }

            MsgFactory.Free(msg);
        }