Beispiel #1
0
        /// <summary>
        /// Invoked when a message is received from a connected peer.
        /// </summary>
        /// <param name="peer">Peer that sent the message.</param>
        /// <param name="data">Received message data.</param>
        public void MessageReceived(PeerConnection peer, byte[] data)
        {
            if (data.Length == 0)
            {
                return;
            }

            var reader = new BigEndianBinaryReader(new MemoryStream(data));

            byte messageId = reader.ReadByte();

            if (messageHandlerRegistrations.TryGetValue(Tuple.Create(peer, messageId), out IModule module))
            {
                var customValues           = peer.GetCustomValues(module);
                var messageReceivedContext = new MessageReceivedContext(
                    peer,
                    this,
                    messageId,
                    data.Length - 1,
                    reader,
                    customValues,
                    rMessageId => RegisterModuleForMessageId(peer, module, rMessageId));

                module.OnMessageReceived(messageReceivedContext);
                Log.LogTrace($"Message of type {messageId} handled by module {module.GetType().Name}");
            }
            else
            {
                // Unknown message type
                Log.LogWarning($"Received unknown message type {messageId} from {peer.Address}");
                peer.Disconnect();
            }
        }
Beispiel #2
0
        private void PeerConnected(PeerConnection peer)
        {
            Log.LogInformation($"Connected to peer at {peer.Address}");

            lock (peersLock)
                peers.Add(peer);

            foreach (var module in modules.Modules)
            {
                var context = new PeerContext(
                    peer,
                    peer.GetCustomValues(module),
                    this,
                    messageId => RegisterModuleForMessageId(peer, module, messageId));
                module.OnPeerConnected(context);
            }

            peer.ReceiveData();
        }