Esempio n. 1
0
 // Note: 239.0.0.0 is the start of the UDP multicast addresses reserved for
 // private use.
 // Note: 49152 is the result I get out of executing;
 //      netsh int ipv4 show dynamicport udp
 // on Windows 10.
 public MessageService(
     MessageRegistrar messageRegistrar,
     string multicastAddress = "239.0.0.0",
     int multicastPort       = 49152)
 {
     //if (messageRegistrar == null)
     //{
     //    throw new ArgumentNullException("MessageRegistrar is null");
     //}
     this.messageRegistrar  = messageRegistrar;
     this.multicastEndpoint = new IPEndPoint(IPAddress.Parse(multicastAddress), multicastPort);
 }
        byte[] Serialize <T>(T message) where T : Message
        {
            var stream = new MemoryStream();

            var writer = new BinaryWriter(stream);

            try
            {
                writer.Write(MessageRegistrar.KeyFromMessageType <T>());
                message.Save(writer);
                writer.Flush();

                if (stream.Length > Constants.MAX_UDP_SIZE)
                {
                    throw new ArgumentException("Message size exceeded maximum length");
                }
            }
            finally
            {
                writer.Dispose();
            }
            return(stream.ToArray());
        }