Example #1
0
        public static GlobalServiceId Read(Message data)
        {
            var contexId = data.RemotingManager.ContextManager.ReadContextId(data);
            var id = (ushort)data.Serializer.ReadI16();

            return new GlobalServiceId(contexId, id);
        }
Example #2
0
        private static object ReadDataObject(Serializer serializer, Type type, Message message)
        {
            if(!typeof(IObservableDataObject).IsAssignableFrom(type))
                return Activator.CreateInstance(type);

            IDataObjectFactory dataObjectFactory;
            int dataObjectId;

            var referenceRemoteId = serializer.ReadI32();
            var isPolymorphic = serializer.ReadBool();
            if (isPolymorphic)
            {
                var contextId = serializer.ReadI16();
                dataObjectId = serializer.ReadI16();
                dataObjectFactory = message.RemotingManager.ContextManager.GetDataObjectFactory(contextId);
            }
            else
            {
                dataObjectId = type.GetCustomAttribute<DataObjectAttribute>().Id;
                var contextId = message.RemotingManager.ContextManager.GetContextId(type.Assembly);
                dataObjectFactory = message.RemotingManager.ContextManager.GetDataObjectFactory(contextId);
            }

            var dataObject = dataObjectFactory.CreateDataObjectReference((ushort) dataObjectId, message.Peer, referenceRemoteId, message.RemotingManager.ReferenceManager);
            dataObject.Read(serializer, null, message);

            return dataObject;
        }
Example #3
0
 public void Process(Message data)
 {
     switch (data.Header.CallType)
     {
         case MessageType.Call:
         case MessageType.Reply:
         case MessageType.EventSubscribe:
         case MessageType.EventUnsubscribe:
             ServiceManager.Process(data);
             return;
         case MessageType.DelegateReply:
         case MessageType.DelegateCall:
             DelegateManager.Process(data);
             return;
         case MessageType.ReferenceChanges:
         case MessageType.ReferenceSubscribe:
         case MessageType.ReferenceUnsubscribe:
             ReferenceManager.Process(data);
             return;
         case MessageType.ContextRequest:
         case MessageType.ContextResponse:
             ContextManager.Process(data);
             return;
         default:
             Debug.Assert(false);
             return;
     }
 }
Example #4
0
        public Task<Message> DispatchCall(int callId, Message data)
        {
            var tcs = new TaskCompletionSource<Message>();
            pendingCalls.Add(callId, tcs);

            data.Peer.Dispatch(data);

            return tcs.Task;
        }
Example #5
0
        public override void Dispatch(Message data)
        {
            var bytes = data.Serializer.Buffer.ReadAllBytes();

            var packet = new Packet(0);
            packet.Write(new List<byte>(bytes));
            packet.Flags = ConvertFlags(data.Flags);

            Session.Peer.QueuePacket(packet, 0);
        }
Example #6
0
        internal void SetResult(int callId, Message data)
        {
            if (!pendingCalls.ContainsKey(callId))
            {
                Log.Error("Received unexpected reply.");
                return;
            }

            pendingCalls[callId].SetResult(data);
        }
Example #7
0
        internal void ProcessEventSubscribe(Message data)
        {
            var eventId = data.Serializer.ReadI32();
            var remoteDelegateId = data.Serializer.ReadI32();

            var subscribe = processors[eventId].Subscribe;
            if (subscribe == null)
                throw new Exception();

            subscribe(data.Peer, remoteDelegateId);
        }
Example #8
0
        public void Process(byte[] data, RemotingPeer peer)
        {
            var serializer = new BinarySerializer();
            serializer.Buffer.Write(data, 0, data.Length);
            serializer.Buffer.Position = 0;

            var message = new Message(peer, this, serializer);
            message.Header.Read();

            Process(message);
        }
Example #9
0
 public void ProcessCall(Message.Call call)
 {
     var processCall = processors[call.MethodId].Call;
     if (processCall != null)
     {
         processCall(call);
     }
     else
     {
         var response = new Message(call.Data, MessageType.Exception);
         RemotingException x = new RemotingException(RemotingException.ExceptionType.UnknownMethod, "Invalid method id: '" + call.MethodId + "'");
         response.Serializer.WriteI32(call.Id);
         x.Write(response.Serializer);
         response.Dispatch();
     }
 }
Example #10
0
 public void Process(Message data)
 {
     switch (data.Header.CallType)
     {
         case MessageType.ReferenceChanges:
             ProcessChanges(data);
             return;
         case MessageType.ReferenceSubscribe:
             ProcessSubscribe(data);
             return;
         case MessageType.ReferenceUnsubscribe:
             ProcessUnsubscribe(data);
             return;
         default:
             Debug.Assert(false);
             return;
     }
 }
Example #11
0
        public unsafe void DispatchChanges()
        {
            foreach (var reference in referencesChanged)
            {
                var bitFieldsCount = reference.DataObject.BaseDataObjectCount + 1;
                var bitFields = stackalloc BitField[bitFieldsCount];

                reference.DataObject.GetResetChanges(bitFields);

                foreach (var peer in reference.Subscribers)
                {
                    var peerData = new Message(peer, remotingManager, reference.LocalId, 0, MessageType.ReferenceChanges);
                    // TODO: Optimize this. Dont't serialize again for each peer.
                    reference.DataObject.Write(peerData.Serializer, bitFields, bitFieldsCount, peerData);
                    peerData.Dispatch();
                }
            }
        }
Example #12
0
 public abstract void Invoke(Message.DelegateCall call);
Example #13
0
        private static void WriteDataObject(Serializer serializer, Type type, IDataObject dataObject, Message message)
        {
            if (!(dataObject is IObservableDataObject))
            {
                dataObject.Write(serializer);
                return;
            }

            var observable = (IObservableDataObject)dataObject;
            if(observable.IsReference)
                message.RemotingManager.ReferenceManager.Publish(observable);

            int referenceLocalId;
            if (!message.RemotingManager.ReferenceManager.TryGetLocalId(observable, out referenceLocalId))
                referenceLocalId = 0;

            serializer.WriteI32(referenceLocalId);

            ushort remoteContextId;
            ushort dataObjectId;
            var polymorphicType = message.RemotingManager.ContextManager.GetPeerPolymorphicType(message.Peer, dataObject.GetType(), type, out remoteContextId, out dataObjectId);
            var isPolymorphic = polymorphicType != type;
            serializer.WriteBool(isPolymorphic);
            if (isPolymorphic)
            {
                serializer.WriteI16((short)remoteContextId);
                serializer.WriteI16((short)dataObjectId);
            }

            dataObject.Write(serializer, polymorphicType);
        }
Example #14
0
 internal void ProcessReply(Message.DelegateReply reply)
 {
     callProcessor.SetResult(reply.Id, reply.Data);
 }
Example #15
0
        private void ProcessContextResponse(Message data)
        {
            var localId = data.Header.LocalId;
            var remoteId = (ushort) data.Header.RemoteId;

            ContextInfo context;
            if(!localIdToContext.TryGetValue(localId, out context))
                throw new Exception("No local context available.");

            if(context.RemoteIds == null)
                context.RemoteIds = new Dictionary<RemotingPeer, ushort>();

            context.RemoteIds.Add(data.Peer, remoteId);
        }
Example #16
0
 internal IContextId ReadContextId(Message data)
 {
     return Loader.ReadContextId(data);
 }
Example #17
0
 internal void ProcessEventUnsubscribe(Message data)
 {
     throw new System.NotImplementedException();
 }
Example #18
0
 public void Process(Message data)
 {
     switch (data.Header.CallType)
     {
         case MessageType.ContextRequest:
             ProcessContextRequest(data);
             return;
         case MessageType.ContextResponse:
             ProcessContextResponse(data);
             return;
         default:
             throw new NotImplementedException();
     }
 }
Example #19
0
 public void RequestContext(RemotingPeer peer, Assembly assembly)
 {
     var context = GetCreateContext(assembly);
     var request = new Message(peer, remotingManager, context.LocalId, 0, MessageType.ContextRequest);
     context.ContextId.Write(request);
     request.Dispatch();
 }
Example #20
0
 public void Write(Message data)
 {
     ContextId.Write(data);
     data.Serializer.WriteI16((short)Id);
 }
Example #21
0
        private void ProcessContextRequest(Message data)
        {
            var remoteId = data.Header.RemoteId;
            var contextId = Loader.ReadContextId(data);

            ContextInfo context;
            if (!contextIdToContext.TryGetValue(contextId, out context))
            {
                //TODO: check data.Peer permissions
                var task = Loader.LoadContext(contextId);
                task.ContinueWith(t =>
                {
                    if(t.Result == null)
                        throw new Exception("Context could not be loaded.");

                    var cxt = GetCreateContext(t.Result);
                    var res = new Message(data.Peer, remotingManager, cxt.LocalId, remoteId, MessageType.ContextResponse);
                    res.Dispatch();
                });
                return;
            }

            var response = new Message(data.Peer, remotingManager, context.LocalId, remoteId, MessageType.ContextResponse);
            response.Dispatch();
        }
Example #22
0
 public void Read(Message data)
 {
     Name = data.Serializer.ReadString();
     MajorVersion = data.Serializer.ReadI32();
 }
Example #23
0
        public void Subscribe(IObservableDataObject dataObject, RemotingPeer peer, int remoteId)
        {
            Reference reference;
            if (!dataObjectToReference.TryGetValue(dataObject, out reference))
                reference = CreateReference(dataObject);

            reference.Subscription = new Subscription(peer, remoteId);

            subscriptionToReference.Add(reference.Subscription, reference);

            var data = new Message(peer, remotingManager, reference.LocalId, remoteId, MessageType.ReferenceSubscribe);
            data.Dispatch();
        }
Example #24
0
 public Task<Message> DispatchCall(Message.DelegateCall call)
 {
     return callProcessor.DispatchCall(call.Id, call.Data);
 }
Example #25
0
 public void Write(Message data)
 {
     data.Serializer.WriteString(Name);
     data.Serializer.WriteI32(MajorVersion);
 }
Example #26
0
        private void ProcessChanges(Message data)
        {
            var subscription = new Subscription(data.Peer, data.Header.RemoteId);
            Reference reference;
            if (!subscriptionToReference.TryGetValue(subscription, out reference))
                throw new Exception("Reference not found.");

            reference.DataObject.Read(data.Serializer, null, data);
        }
Example #27
0
 IContextId IContextLoader.ReadContextId(Message data)
 {
     var moduleId = new ModuleId();
     moduleId.Read(data);
     return moduleId;
 }
Example #28
0
        private void ProcessSubscribe(Message data)
        {
            var localId = data.Header.LocalId;

            Reference reference;
            if (!localIdToReference.TryGetValue(localId, out reference))
                throw new Exception("No reference to subscribe to.");

            if (reference.Subscribers == null)
                reference.Subscribers = new HashSet<RemotingPeer>();

            reference.Subscribers.Add(data.Peer);
            //TODO send complete serialization
        }
Example #29
0
        private void ProcessUnsubscribe(Message data)
        {
            var localId = data.Header.LocalId;

            Reference reference;
            if (!localIdToReference.TryGetValue(localId, out reference))
                throw new Exception("No reference to subscribe to.");

            reference.Subscribers.Remove(data.Peer);
        }
Example #30
0
 public abstract void Dispatch(Message data);