Exemplo n.º 1
0
 private void _server_ObjectReceived(Message m, IConnexion client, ITransport transport)
 {
     if (m is ObjectMessage)
     {
         object obj = ((ObjectMessage) m).Object;
         if (obj is PositionChanged)
         {
             if(PositionUpdates != null)
             {
                 PositionUpdates(((PositionChanged) obj).X, ((PositionChanged) obj).Y);
             }
         }
     }
 }
Exemplo n.º 2
0
 public void Add(Exception e, Message m)
 {
     Add(e, new PendingMessage(m, null, null));
 }
Exemplo n.º 3
0
 private void server_SendMessage(Message m, int clientNumber)
 {
     foreach (IConnexion c in Server.Clients)
     {
         if (--clientNumber == 0)
         {
             c.Send(m, MessageDeliveryRequirements.LeastStrict, null);
         }
     }
 }
Exemplo n.º 4
0
        private void FragmentMessage(Message message, ITransportDeliveryCharacteristics tdc, 
            TransportPacket packet, MarshalledResult mr)
        {
            // <item> if the message is the first fragment, then the high-bit is
            //     set on the message-type; the number of fragments is encoded using
            //     the adaptive <see cref="ByteUtils.EncodeLength(int)"/> format.
            //     <pre>[byte:message-type'] [byte:channelId] [uint32:packet-size]
            //         [byte:seqno] [bytes:encoded-#-fragments] [bytes:frag]</pre>
            // </item>
            // <item> for all subsequent fragments; seqno' = seqno | 128;
            //     the number of fragments is encoded using the adaptive
            //     <see cref="ByteUtils.EncodeLength(int)"/> format.
            //     <pre>[byte:message-type'] [byte:channelId] [uint32:packet-size]
            //         [byte:seqno'] [bytes:encoded-fragment-#] [bytes:frag]</pre>

            // Although we use an adaptive scheme for encoding the number,
            // we assume a maximum of 4 bytes for encoding # frags
            // for a total of 4 extra bytes bytes; we determine the frag
            // size from the message size - MaxHeaderSize
            const uint maxFragHeaderSize = 1 /*seqno*/ + 4;
            const uint maxHeaderSize = LWMCFv11.HeaderSize + maxFragHeaderSize;
            uint maxPacketSize = Math.Max(maxHeaderSize, tdc.MaximumPacketSize - maxHeaderSize);
            // round up the number of possible fragments
            uint numFragments = (uint)(packet.Length + maxPacketSize - 1) / maxPacketSize;
            Debug.Assert(numFragments > 1);
            uint seqNo = AllocateOutgoingSeqNo(tdc);
            for (uint fragNo = 0; fragNo < numFragments; fragNo++)
            {
                TransportPacket newPacket = new TransportPacket();
                Stream s = newPacket.AsWriteStream();
                uint fragSize = (uint)Math.Min(maxPacketSize,
                    packet.Length - (fragNo * maxPacketSize));
                if (fragNo == 0)
                {
                    s.WriteByte((byte)seqNo);
                    ByteUtils.EncodeLength(numFragments, s);
                }
                else
                {
                    s.WriteByte((byte)(seqNo | 128));
                    ByteUtils.EncodeLength(fragNo, s);
                }
                newPacket.Prepend(LWMCFv11.EncodeHeader((MessageType)((byte)message.MessageType | 128),
                    message.ChannelId, (uint)(fragSize + s.Length)));
                newPacket.Append(packet, (int)(fragNo * maxPacketSize), (int)fragSize);
                mr.AddPacket(newPacket);
            }
            packet.Dispose();
        }
Exemplo n.º 5
0
 private void s_MessageReceived(Message m, IConnexion client, ITransport transport)
 {
     if (Verbose > 2 && log.IsInfoEnabled)
     {
         log.Info(String.Format("received message: {0} from Client {1} via {2}",
                                 m, client.Identity, transport));
     }
     //repeat whatever we receive to everyone else
     server.Send(m, null, new MessageDeliveryRequirements(transport.Reliability,
         MessageAggregation.Immediate, transport.Ordering));
 }
Exemplo n.º 6
0
 public IMarshalledResult Marshal(Message m, ITransportDeliveryCharacteristics tdc)
 {
     return Marshaller.Marshal(0, m, tdc);
 }
Exemplo n.º 7
0
        public override void Schedule(Message msg, MessageDeliveryRequirements mdr,
            ChannelDeliveryRequirements cdr)
        {
            MessageAggregation aggr = mdr == null ? cdr.Aggregation : mdr.Aggregation;

            // Place the message, performing any channel-compaction if so specified
            // by cdr.Freshness
            Aggregate(msg, mdr, cdr);

            if (aggr == MessageAggregation.FlushChannel)
            {
                // make sure ALL other messages on this CHANNEL are sent, and then send <c>msg</c>.
                FlushChannelMessages(msg.ChannelId);
            }
            else if (aggr == MessageAggregation.FlushAll)
            {
                // make sure ALL messages are sent, then send <c>msg</c>.
                Flush();
            }
            else if (aggr == MessageAggregation.Immediate)
            {
                // bundle <c>msg</c> first and then cram on whatever other messages are waiting.
                Flush();
            }
        }
Exemplo n.º 8
0
 protected void NotifyMessageSent(Message message, ITransport transport)
 {
     if (MessagesSent == null)
     {
         return;
     }
     SingleItem<Message> list = _singleMessage.Obtain();
     try
     {
         list[0] = message;
         MessagesSent(list, transport);
     }
     finally
     {
         _singleMessage.Return(list);
     }
 }
Exemplo n.º 9
0
 public void Clear()
 {
     Message = null;
     MDR = null;
     CDR = null;
 }
Exemplo n.º 10
0
 public PendingMessage(Message m, MessageDeliveryRequirements mdr, ChannelDeliveryRequirements cdr)
 {
     Message = m;
     MDR = mdr;
     CDR = cdr;
 }
Exemplo n.º 11
0
 public MessageEventArgs(ITransport transport, Message message)
 {
     Transport = transport;
     Message = message;
 }
Exemplo n.º 12
0
        public override IMarshalledResult Marshal(int senderIdentity, Message msg, ITransportDeliveryCharacteristics tdc)
        {
            // This marshaller doesn't use <see cref="senderIdentity"/>.
            if (msg is RawMessage)
            {
                MarshalledResult mr = new MarshalledResult();
                TransportPacket tp = new TransportPacket();

                // NB: SystemMessages use ChannelId to encode the sysmsg descriptor
                RawMessage rm = (RawMessage)msg;
                tp.Prepend(LWMCFv11.EncodeHeader(msg.MessageType, msg.ChannelId, (uint)rm.Bytes.Length));
                tp.Append(rm.Bytes);
                mr.AddPacket(tp);
                return mr;
            }
            return base.Marshal(senderIdentity, msg, tdc);
        }
Exemplo n.º 13
0
 protected override void MarshalContents(Message m, Stream output, ITransportDeliveryCharacteristics tdc)
 {
     // Individual marshalling methods are **NO LONGER RESPONSIBLE** for encoding
     // the payload length
     switch (m.MessageType)
     {
     case MessageType.Binary:
         MarshalBinary(((BinaryMessage)m).Bytes, output);
         break;
     case MessageType.String:
         MarshalString(((StringMessage)m).Text, output);
         break;
     case MessageType.Object:
         MarshalObject(((ObjectMessage)m).Object, output);
         break;
     case MessageType.Tuple1D:
     case MessageType.Tuple2D:
     case MessageType.Tuple3D:
         MarshalTupleMessage((TupleMessage)m, output);
         break;
     default:
         base.MarshalContents(m, output, tdc);
         break;
     }
 }
Exemplo n.º 14
0
 /// <summary>
 /// Marshal the contents of the message <see cref="m"/> onto the stream <see cref="output"/>.
 /// The channelId and message type have already been placed on <see cref="output"/>.
 /// This method is **not responsible** for encoding the message payload length.
 /// </summary>
 /// <param name="m">the message contents to be marshalled</param>
 /// <param name="output">the destination for the marshalled message payload</param>
 /// <param name="tdc">the characteristics of the transport that is to be used for sending</param>
 protected virtual void MarshalContents(Message m, Stream output, ITransportDeliveryCharacteristics tdc)
 {
     // Individual marshalling methods are **NO LONGER RESPONSIBLE** for encoding
     // the payload length
     switch (m.MessageType)
     {
     case MessageType.Session:
         MarshalSessionAction((SessionMessage)m, output);
         break;
     case MessageType.System:
         // channelId is the system message type
         MarshalSystemMessage((SystemMessage)m, output);
         break;
     default:
         throw new MarshallingException(String.Format("ERROR: {0} cannot handle messages of type {1}",
             this.GetType().Name, m.GetType().Name));
     }
 }
Exemplo n.º 15
0
        /// <summary>
        /// Marshal the given message to the provided stream in a form suitable to
        /// be sent out on the provided transport.
        /// </summary>
        /// <param name="senderIdentity">the identity of the sender</param>
        /// <param name="msg">the message being sent, that is to be marshalled</param>
        /// <param name="tdc">the characteristics of the transport that will send the marshalled form</param>
        /// <returns>the marshalled representation</returns>
        public virtual IMarshalledResult Marshal(int senderIdentity, Message msg, ITransportDeliveryCharacteristics tdc)
        {
            // This marshaller doesn't use <see cref="senderIdentity"/>.
            MarshalledResult mr = new MarshalledResult();
            TransportPacket tp = new TransportPacket();

            // We use TransportPacket.Prepend to add the marshalling header in-place
            // after the marshalling.
            Stream output = tp.AsWriteStream();
            Debug.Assert(output.CanSeek);
            MarshalContents(msg, output, tdc);
            output.Flush();
            // System messages don't have a channelId -- we encode their system message
            // type as the channelId instead
            tp.Prepend(LWMCFv11.EncodeHeader(msg.MessageType,
                msg.MessageType == MessageType.System
                    ? (byte)((SystemMessage)msg).Descriptor : msg.ChannelId,
                (uint)output.Position));
            mr.AddPacket(tp);
            return mr;
        }
Exemplo n.º 16
0
 public abstract void Schedule(Message m, MessageDeliveryRequirements mdr,
     ChannelDeliveryRequirements cdr);
Exemplo n.º 17
0
 protected virtual void FastpathSendMessage(ITransport t, Message m)
 {
     IMarshalledResult mr = cnx.Marshal(m, t);
     try
     {
         TransportPacket tp;
         while ((tp = mr.RemovePacket()) != null)
         {
             cnx.SendPacket(t, tp);
         }
         NotifyMessageSent(m, t);
     }
     finally
     {
         mr.Dispose();
     }
 }
Exemplo n.º 18
0
 public void CheckMarshaller(IMarshaller m)
 {
     Message[] messages = new Message[] {
         new BinaryMessage(0, RandomBytes(0)),
         new BinaryMessage(0, RandomBytes(10)),
         new BinaryMessage(0, RandomBytes(100)),
         new ObjectMessage(33, new List<object>()),
         new ObjectMessage(5, new List<int>()),
         new ObjectMessage(2, new Dictionary<byte,object>()),
         new StringMessage(156, "this is a test of faith"),
         new StringMessage(53, "เ, แ, โ, ใ, ไ"),
         new StringMessage(24, "\u005C\uFF5E\u301C"),
         new SystemMessage(SystemMessageType.IdentityRequest),
         new SystemPingMessage(SystemMessageType.PingRequest, 0, 0),
         new SystemIdentityResponseMessage(0),
         new TupleMessage(0, 9834, 0),
         new TupleMessage(0, 9834, 0, 1),
         new TupleMessage(0, 9834, 0, 1, 2),
         new TupleMessage(0, 9834, (byte)255, (sbyte)(127), (ushort)5),
         new TupleMessage(0, 9834, "test", (sbyte)(-128), new DateTime()),
     };
     foreach (Message original in messages)
     {
         bool seen = false;
         IMarshalledResult mr = m.Marshal(0, original, new NullTransport());
         while (mr.HasPackets)
         {
             TransportPacket tp = mr.RemovePacket();
             m.Unmarshal(tp, new NullTransport(),
                 (sender, mea) => {
                     seen = true;
                     AssertAreEquivalent(original, mea.Message);
                 });
             tp.Dispose();
         }
         Assert.IsTrue(seen, "Marshaller has not reported a message available");
         mr.Dispose();
     }
 }
Exemplo n.º 19
0
 public override void Schedule(Message m, MessageDeliveryRequirements mdr,
     ChannelDeliveryRequirements cdr)
 {
     try
     {
         ITransport t = cnx.FindTransport(mdr, cdr);
         FastpathSendMessage(t, m);
     }
     catch (NoMatchingTransport e)
     {
         NotifyError(new ErrorSummary(Severity.Warning, SummaryErrorCode.MessagesCannotBeSent,
             e.Message, new PendingMessage(m, mdr, cdr), e));
     }
 }
Exemplo n.º 20
0
        protected void AssertAreEquivalent(Message original, Message created)
        {
            Assert.AreEqual(original.GetType(), created.GetType());
            Assert.AreEqual(original.ChannelId, created.ChannelId);
            Assert.AreEqual(original.MessageType, created.MessageType);
            switch (original.MessageType)
            {
                case MessageType.Binary:
                    Assert.AreEqual(((BinaryMessage)original).Bytes, ((BinaryMessage)created).Bytes);
                    return;
                case MessageType.Object:
                    Assert.AreEqual(((ObjectMessage)original).Object, ((ObjectMessage)created).Object);
                    return;
                case MessageType.String:
                    Assert.AreEqual(((StringMessage)original).Text, ((StringMessage)created).Text);
                    return;

                case MessageType.System:
                    Assert.AreEqual(original.GetType(), created.GetType());
                    Assert.AreEqual(original.ToString(), created.ToString());
                    return;

                case MessageType.Session:
                    Assert.AreEqual(((SessionMessage)original).Action, ((SessionMessage)created).Action);
                    Assert.AreEqual(((SessionMessage)original).ClientId, ((SessionMessage)created).ClientId);
                    return;

                case MessageType.Tuple1D:
                    Assert.AreEqual(((TupleMessage)original).ClientId, ((TupleMessage)created).ClientId);
                    Assert.AreEqual(((TupleMessage)original).X, ((TupleMessage)created).X);
                    return;
                case MessageType.Tuple2D:
                    Assert.AreEqual(((TupleMessage)original).ClientId, ((TupleMessage)created).ClientId);
                    Assert.AreEqual(((TupleMessage)original).X, ((TupleMessage)created).X);
                    Assert.AreEqual(((TupleMessage)original).Y, ((TupleMessage)created).Y);
                    return;
                case MessageType.Tuple3D:
                    Assert.AreEqual(((TupleMessage)original).ClientId, ((TupleMessage)created).ClientId);
                    Assert.AreEqual(((TupleMessage)original).X, ((TupleMessage)created).X);
                    Assert.AreEqual(((TupleMessage)original).Y, ((TupleMessage)created).Y);
                    Assert.AreEqual(((TupleMessage)original).Z, ((TupleMessage)created).Z);
                    return;

                default:
                    throw new NotImplementedException();
            }
        }
Exemplo n.º 21
0
        /// <summary>Adds the message to a list, waiting to be sent out.</summary>
        /// <param name="newMsg">The message to be aggregated</param>
        /// <param name="mdr">How it should be sent out (potentially null)</param>
        /// <param name="cdr">General delivery instructions for this message's channel.</param>
        private void Aggregate(Message newMsg, MessageDeliveryRequirements mdr, ChannelDeliveryRequirements cdr)
        {
            Debug.Assert(mdr != null || cdr != null);
            if (newMsg.MessageType != MessageType.System && cdr != null
                && cdr.Freshness == Freshness.IncludeLatestOnly)
            {
                pending.RemoveAll(pendingMsg => pendingMsg.Message.ChannelId == newMsg.ChannelId
                    && pendingMsg.Message.MessageType != MessageType.System);
            }

            if (!channelIndices.ContainsKey(newMsg.ChannelId))
            {
                channelIndices[newMsg.ChannelId] = channels.Count;
                channels.Add(newMsg.ChannelId);
            }

            PendingMessage pm = pmPool.Obtain();
            pm.Message = newMsg;
            pm.MDR = mdr;
            pm.CDR = cdr;

            MessageAggregation aggr = mdr == null ? cdr.Aggregation : mdr.Aggregation;
            if (aggr == MessageAggregation.Immediate)
            {
                pending.Insert(0, pm);
                nextChannelIndex = channelIndices[newMsg.ChannelId];
            }
            else
            {
                pending.Add(pm);
            }
        }
Exemplo n.º 22
0
 protected virtual void FastpathSendMessage(ITransport t, Message m)
 {
     IMarshalledResult mr = cnx.Marshal(m, t);
     try
     {
         TransportPacket tp;
         while ((tp = mr.RemovePacket()) != null)
         {
             if (tp.Length > t.MaximumPacketSize)
             {
                     NotifyError(new ErrorSummary(Severity.Warning,
                         SummaryErrorCode.MessagesCannotBeSent,
                         "Unable to send messages", m, null));
                     tp.Dispose();
                     return;
             }
             cnx.SendPacket(t, tp);
         }
         NotifyMessageSent(m, t);
     }
     finally
     {
         mr.Dispose();
     }
 }
Exemplo n.º 23
0
 public void Send(Message msg, MessageDeliveryRequirements mdr, ChannelDeliveryRequirements cdr)
 {
     Scheduler.Schedule(msg, mdr, cdr);
 }
Exemplo n.º 24
0
 public IMarshalledResult Marshal(int senderIdentity, Message message, ITransportDeliveryCharacteristics tdc)
 {
     IMarshalledResult mr = subMarshaller.Marshal(senderIdentity, message, tdc);
     MarshalledResult result = new MarshalledResult();
     while (mr.HasPackets)
     {
         TransportPacket packet = mr.RemovePacket();
         byte[] encoded = Encode(packet.ToArray());
         // FIXME: this is a bit awkward: if encoded is small, then
         // we're likely better off copying these into a contiguous
         // block of memory.  But if big, then we're probably better
         // off using these byte arrays as the backing store.
         TransportPacket newPacket = new TransportPacket(
             DataConverter.Converter.GetBytes(senderIdentity),
             ByteUtils.EncodeLength((uint)encoded.Length),
             encoded);
         result.AddPacket(newPacket);
     }
     mr.Dispose();
     return result;
 }
Exemplo n.º 25
0
 public IMarshalledResult Marshal(int senderIdentity, Message message, ITransportDeliveryCharacteristics tdc)
 {
     IMarshalledResult submr = subMarshaller.Marshal(senderIdentity, message, tdc);
     // Hmm, maybe this would be better as a generator, just in case the
     // sub-marshaller returns an infinite message.
     MarshalledResult mr = new MarshalledResult();
     while (submr.HasPackets)
     {
         TransportPacket packet = submr.RemovePacket();
         // Need to account for LWMCFv1.1 header for non-LWMCFv1.1 marshallers
         uint contentLength = (uint)packet.Length -
             (subMarshallerIsLwmcf11 ? LWMCFv11.HeaderSize : 0);
         // If this packet fits within the normal transport packet length
         // then we don't have to fragment it.
         if (LWMCFv11.HeaderSize + contentLength < tdc.MaximumPacketSize)
         {
             // Message fits within the transport packet length, so sent unmodified
             //     <pre>[byte:message-type] [byte:channelId] [uint32:packet-size]
             //         [bytes:content]</pre>
             if (!subMarshallerIsLwmcf11)
             {
                 // need to prefix the LWMCFv1.1 header
                 packet.Prepend(LWMCFv11.EncodeHeader(message.MessageType,
                     message.ChannelId, (uint)packet.Length));
             }
             mr.AddPacket(packet);
         }
         else
         {
             FragmentMessage(message, tdc, packet, mr);
         }
     }
     submr.Dispose();
     return mr;
 }
Exemplo n.º 26
0
 public CannotSendMessagesError(IConnexion source, Exception ex, Message msg)
     : this(source)
 {
     Add(ex, msg);
 }
Exemplo n.º 27
0
 /// <summary>Send a message to many clients in an efficient manner.</summary>
 /// <param name="message">The message to send</param>
 /// <param name="list">The list of clients; if null then all clients</param>
 /// <param name="mdr">How to send it (can be null)</param>
 public virtual void Send(Message message, ICollection<IConnexion> list, MessageDeliveryRequirements mdr)
 {
     Send(new SingleItem<Message>(message), list, mdr);
 }
Exemplo n.º 28
0
        private void server_MessageReceived(Message m, IConnexion client, ITransport transport)
        {
            switch (m.MessageType)
            {
            case MessageType.Binary:
                if (!ByteUtils.Compare(((BinaryMessage) m).Bytes, StandardObjects.ByteMessage))
                {
                    Console.WriteLine("Invalid byte message: {0}",
                        ByteUtils.DumpBytes(((BinaryMessage) m).Bytes));
                    errorOccurred = true;
                }
                break;
            case MessageType.String:
                if (!StandardObjects.StringMessage.Equals(((StringMessage) m).Text))
                {
                    Console.WriteLine("Invalid strings message: {0}",
                        ((StringMessage) m).Text);
                    errorOccurred = true;
                }
                break;

            case MessageType.Object:
                if (!StandardObjects.Equivalent(StandardObjects.ObjectMessage, ((ObjectMessage) m).Object))
                {
                    Console.WriteLine("Invalid object message: {0}",
                        ((ObjectMessage) m).Object);
                    errorOccurred = true;
                }
                break;
            }

            // Randomly send a message elsewhere 10% of the time;
            // this has multiple senders from outside of the server's listening loop
            if (random.Next(0, 100) < 10)
            {
                return;
            }

            int clientNumber = random.Next(0, Server.Clients.Count - 1);
            server_SendMessage(m, clientNumber);
        }