Ejemplo n.º 1
0
        /// <summary>
        /// Handles a received netgram
        /// </summary>
        /// <param name="message">The netgram received message</param>
        private void _handleReceivedNetgram(NetgramReceivedMessage message)
        {
            var buffer = message.Segment.Buffer;
            var offset = message.Segment.Offset;
            var end    = message.Segment.End;

            NetgramHeader header = new NetgramHeader();

            offset = header.Deserialize(buffer, offset, end);
            InboundAppgram appgram = null;

            lock (_lock)
            {
                if (header.IsNetworkMessage)
                {
                    _processNetworkMessage(message, header, buffer, offset, end);
                }
                else
                {
                    appgram = _createInboundAppgram(message, header, buffer, offset, end);
                }
            }

            if (appgram != null)
            {
                _appgramObservers.Next(appgram);
            }
        }
Ejemplo n.º 2
0
 public NetgramContent(Address destination, NetgramHeader header, IContent content)
 {
     this.Destination    = destination;
     this.Header         = header;
     this.NetworkMessage = null;
     this.Content        = content;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates an inbound appgram
        /// </summary>
        /// <param name="netgram">The received netgram</param>
        /// <param name="header">The header of the netgram</param>
        /// <param name="buffer">The buffer containing the appgram</param>
        /// <param name="offset">The offset of the appgram within the buffer</param>
        /// <param name="end">The end of the content in the buffer</param>
        private InboundAppgram _createInboundAppgram(NetgramReceivedMessage netgram, NetgramHeader header, byte[] buffer, int offset, int end)
        {
            InboundAppgram appgram = new InboundAppgram();

            appgram.Source         = _getSource(netgram, header);
            appgram.ExpectingReply = header.ExpectingReply;
            appgram.Priority       = header.Priority;
            appgram.Segment        = new BufferSegment(buffer, offset, end);
            return(appgram);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Retrieves the qualified source address of a netgram
 /// </summary>
 /// <param name="netgram">The received netgram</param>
 /// <param name="header">The netgram header</param>
 /// <returns>The qualified source address</returns>
 private Address _getSource(NetgramReceivedMessage netgram, NetgramHeader header)
 {
     if (header.Source != null)
     {
         return(header.Source);
     }
     else
     {
         Route route = _table.GetRouteByPortId(netgram.PortId);
         return(new Address(route.Network, netgram.Source));
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Sends a network message
        /// </summary>
        /// <param name="destination">The destination address</param>
        /// <param name="expectingReply">True if a reply is expected to this network message, false otherwise</param>
        /// <param name="priority">The netgram priority to send the network message with</param>
        /// <param name="message">The message to send</param>
        private void _sendNetworkMessage(Address destination, bool expectingReply, NetgramPriority priority, INetworkMessage message)
        {
            NetgramHeader header = new NetgramHeader();

            header.IsNetworkMessage = true;
            header.Destination      = destination;
            header.HopCount         = 64;
            header.ExpectingReply   = expectingReply;
            header.Priority         = priority;
            header.MessageType      = (byte)message.Type;
            var content = new NetgramContent(destination, header, message);

            _send(content);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sends an appgram
        /// </summary>
        /// <param name="appgram">The appgram to send</param>
        public void SendAppgram(OutboundAppgram appgram)
        {
            Contract.Requires(appgram != null);
            Contract.Requires(appgram.Destination != null);
            Contract.Requires(appgram.Content != null);

            var header = new NetgramHeader();

            header.Destination      = null;
            header.Source           = null;
            header.HopCount         = 64;
            header.IsNetworkMessage = false;
            header.ExpectingReply   = appgram.ExpectingReply;
            header.Priority         = appgram.Priority;
            var content = new NetgramContent(appgram.Destination, header, appgram.Content);

            lock (_lock)
            {
                _send(content);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Processes a received network message
        /// </summary>
        /// <param name="netgram">The netgram received message</param>
        /// <param name="header">The header of the netgram</param>
        /// <param name="buffer">The buffer containing the appgram</param>
        /// <param name="offset">The offset of the appgram within the buffer</param>
        /// <param name="end">The end of the content in the buffer</param>
        private void _processNetworkMessage(NetgramReceivedMessage netgram, NetgramHeader header, byte[] buffer, int offset, int end)
        {
            try
            {
                INetworkMessage message = _createNetworkMessage(header.VendorId, header.MessageType);
                offset = message.Deserialize(buffer, offset, end);

                switch (message.Type)
                {
                case MessageType.WhoIsRouterToNetwork:
                    _processWhoIsRouterToNetworkMessage(netgram, header, (WhoIsRouterToNetworkMessage)message);
                    break;

                case MessageType.IAmRouterToNetwork:
                    _processIAmRouterToNetworkMessage(netgram, header, (IAmRouterToNetworkMessage)message);
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Ejemplo n.º 8
0
 public NetgramContent(Address destination, NetgramHeader header, INetworkMessage networkMessage)
 {
     this.Destination    = destination;
     this.Header         = header;
     this.NetworkMessage = networkMessage;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Processes a received who is router to network message
 /// </summary>
 /// <param name="netgram">The netgram received message</param>
 /// <param name="header">The netgram header</param>
 /// <param name="message">The who is router to network message</param>
 private void _processWhoIsRouterToNetworkMessage(NetgramReceivedMessage netgram, NetgramHeader header, WhoIsRouterToNetworkMessage message)
 {
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Processes a received i am router to network message
        /// </summary>
        /// <param name="netgram">The received netgram</param>
        /// <param name="header">The netgram header</param>
        /// <param name="message">The i am router to network message</param>
        private void _processIAmRouterToNetworkMessage(NetgramReceivedMessage netgram, NetgramHeader header, IAmRouterToNetworkMessage message)
        {
            // we only use routes from directly connected devices
            if (header.Destination == null && message.Networks != null)
            {
                foreach (var network in message.Networks)
                {
                    var route = _table.AddRemoteRoute(network, netgram.PortId, netgram.Source);

                    for (var node = _netgramQueue.First; node != null;)
                    {
                        if (node.Value.Destination.Network == network)
                        {
                            var temp = node.Next;
                            _sendWithRoute(route, node.Value);
                            _netgramQueue.Remove(node);
                            node = temp;
                        }
                        else
                        {
                            node = node.Next;
                        }
                    }

                    for (var node = _networkSearchTimers.First; node != null;)
                    {
                        if (node.Value.Network == network)
                        {
                            var temp = node.Next;
                            node.Value.Dispose();
                            _networkSearchTimers.Remove(node);
                            node = temp;
                        }
                        else
                        {
                            node = node.Next;
                        }
                    }
                }
            }
        }