示例#1
0
        public void Handle(Message message)
        {
            if (message.GetPropertyObjectID == null)
            {
                throw new ArgumentNullException("GetPropertyObjectID is null");
            }

            var entry = this.m_ObjectStorage.Find(message.GetPropertyObjectID).FirstOrDefault();

            if (entry == null)
            {
                throw new InvalidOperationException("Get property message received but we don't own the object!");
            }

            var obj = entry.Value;

            var mi = obj.GetType().GetMethod("get_" + message.GetPropertyPropertyName + "__Distributed0", BindingFlagsCombined.All);
            if (mi == null)
            {
                throw new MissingMethodException(
                    obj.GetType().FullName,
                    "get_" + message.GetPropertyPropertyName + "__Distributed0");
            }

            var value = DpmEntrypoint.InvokeDynamic(obj.GetType(), mi, obj, new Type[0], new object[] { });

            var client = this.m_ClientLookup.Lookup(message.Sender.IPEndPoint);
            client.Send(this.m_MessageConstructor.ConstructGetPropertyResultMessage(message.ID, value));
        }
示例#2
0
        public void Send(Stream stream, Message message)
        {
            if (!stream.CanWrite)
            {
                return;
            }

            if (message.ID == null)
            {
                throw new InvalidOperationException("Can not serialize a message without an ID!");
            }

            message.SentFromReceivingThread = NetworkThreadContext.IsSentFromReceivingThread();

            var writer = new BinaryWriter(stream);
            using (var memory = new MemoryStream())
            {
                Serializer.Serialize(memory, message);
                var length = (int)memory.Position;
                memory.Seek(0, SeekOrigin.Begin);
                var reader = new BinaryReader(memory);
                var bytes = reader.ReadBytes(length);
                var checksum = this.CalculateChecksum(length, bytes);

                writer.Write(length);
                writer.Write(checksum);
                writer.Write(bytes);
            }
        }
示例#3
0
 public void Put(Message message)
 {
     lock (this.m_Messages)
     {
         this.m_Messages.Add(message);
     }
 }
示例#4
0
        public void Handle(Message message)
        {
            var entry = this.m_ObjectStorage.Find(message.SetPropertyObjectID).FirstOrDefault();

            if (entry == null)
            {
                throw new InvalidOperationException("Set property message received but we don't own the object!");
            }

            var obj = entry.Value;

            var mi = obj.GetType().GetMethod("set_" + message.SetPropertyPropertyName + "__Distributed0", BindingFlagsCombined.All);
            if (mi == null)
            {
                throw new MissingMethodException(obj.GetType().FullName, "set_" + message.SetPropertyPropertyName + "__Distributed0");
            }

            var value = this.m_ObjectWithTypeSerializer.Deserialize(message.SetPropertyPropertyValue);

            DpmEntrypoint.InvokeDynamic(obj.GetType(), mi, obj, new Type[0], new[] { value });
            this.m_ObjectStorage.UpdateOrPut(new LiveEntry
                {
                    Key = message.SetPropertyObjectID,
                    Value = obj,
                    Owner = this.m_LocalNode.Self
                });

            var client = this.m_ClientLookup.Lookup(message.Sender.IPEndPoint);
            client.Send(this.m_MessageConstructor.ConstructSetPropertyConfirmationMessage(message.ID));
        }
示例#5
0
        /// <summary>
        /// Handle the specified message using the appropriate message handler.  This
        /// is called when a message has been received over the network.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        public void Receive(Message message)
        {
            if (!this.m_HandlerMappings.ContainsKey(message.Type))
            {
                throw new InvalidOperationException("No handler for message type " + message.Type);
            }

            this.m_HandlerMappings[message.Type].Handle(message);
        }
示例#6
0
        public void Handle(Message message)
        {
            var client = this.m_ClientLookup.Lookup(message.Sender.IPEndPoint);

            var results = this.m_Lookup.Find(message.FetchKey).ToArray();

            var serializedResults =
                results.Select(
                    x =>
                    new SerializedEntry
                    {
                        Key = x.Key,
                        Owner = x.Owner,
                        Value = this.m_ObjectWithTypeSerializer.Serialize(x.Value)
                    }).ToArray();

            var confirmationMessage = this.m_MessageConstructor.ConstructFetchResultMessage(
                message.FetchKey,
                serializedResults);

            client.Send(confirmationMessage);
        }
示例#7
0
 /// <summary>
 /// Send a message to the remote host that this client handler is connected to.
 /// </summary>
 /// <param name="message">
 /// The message.
 /// </param>
 public void Send(Message message)
 {
     message.Sender = this.m_LocalNode.Self;
     this.Receive(message);
 }
示例#8
0
        /// <summary>
        /// Send a message to the remote host that this client handler is connected to.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        public void Send(Message message)
        {
            try
            {
                NetworkThreadContext.AssertSendIsValid((IPEndPoint)this.m_Client.Client.RemoteEndPoint);

                lock (this.m_Client)
                {
                    this.m_MessageIo.Send(this.m_Client.GetStream(), message);
                }
            }
            catch (ObjectDisposedException)
            {
                // The client is no longer available.
                // TODO: Remove the client from the lookup list.
            }
        }
示例#9
0
 /// <summary>
 /// Handle the specified message using the appropriate message handler.  This
 /// is called when a message has been received over the network.
 /// </summary>
 /// <param name="message">
 /// The message.
 /// </param>
 public void Receive(Message message)
 {
     this.m_HandlerMappings[message.Type].Handle(message);
 }
示例#10
0
        public void Handle(Message message)
        {
            if (message.InvokeTypeArguments == null)
            {
                message.InvokeTypeArguments = new string[0];
            }

            if (message.InvokeArguments == null)
            {
                message.InvokeArguments = new ObjectWithType[0];
            }

            var entry = this.m_ObjectStorage.Find(message.InvokeObjectID).FirstOrDefault();

            if (entry == null)
            {
                throw new InvalidOperationException("Invoke message received but we don't own the object!");
            }

            var obj = entry.Value;

            var mi = obj.GetType().GetMethod(message.InvokeMethod, BindingFlagsCombined.All);
            if (mi == null)
            {
                throw new MissingMethodException(obj.GetType().FullName, message.InvokeMethod);
            }

            var targs = message.InvokeTypeArguments.Select(Type.GetType).ToArray();
            var args = message.InvokeArguments.Select(x => this.m_ObjectWithTypeSerializer.Deserialize(x)).ToArray();

            var allowed = false;

            if (this.m_LocalNode.Architecture == Architecture.PeerToPeer)
            {
                // In peer-to-peer, all methods are callable.
                allowed = true;
            }
            else if (this.m_LocalNode.Architecture == Architecture.ServerClient)
            {
                if (message.Sender == this.m_LocalNode.Self)
                {
                    // This message is coming from the server.
                    allowed = true;
                }
                else
                {
                    var originalName = mi.Name.Substring(0, mi.Name.Length - "__Distributed0".Length);
                    var originalMethod = obj.GetType().GetMethod(originalName, BindingFlagsCombined.All);

                    // The client is calling this method, ensure they are allowed to call it.
                    allowed = originalMethod.GetCustomAttributes(typeof(ClientCallableAttribute), false).Any();
                }
            }

            if (!allowed)
            {
                // If the sender is not permitted to call this method, we just return (we
                // don't even bother giving them a response since they're either using an
                // out-of-date client or attempting to bypass security).
                throw new InvalidOperationException("Received a call to invoke " + message.InvokeMethod + " on " + obj.GetType());
            }

            var result = DpmEntrypoint.InvokeDynamic(
                obj.GetType(),
                mi,
                obj,
                targs,
                args);

            var client = this.m_ClientLookup.Lookup(message.Sender.IPEndPoint);
            client.Send(this.m_MessageConstructor.ConstructInvokeResultMessage(message.ID, result));
        }