Esempio n. 1
0
        internal void RaisePeerMessageTransferred(PeerMessageEventArgs e)
        {
            if (PeerMessageTransferred == null)
            {
                return;
            }

            ThreadPool.QueueUserWorkItem(state =>
            {
                var handler = PeerMessageTransferred;
                if (handler == null)
                {
                    return;
                }

                var messageBundle = e.Message as MessageBundle;
                if (messageBundle == null)
                {
                    handler(e.TorrentManager, e);
                }
                else
                {
                    // Message bundles are only a convience for internal usage!
                    var peerMessageEventArgs = messageBundle.Messages
                                               .Select(m => new PeerMessageEventArgs(e.TorrentManager, m, e.Direction, e.ID));

                    foreach (var args in peerMessageEventArgs)
                    {
                        handler(args.TorrentManager, args);
                    }
                }
            });
        }
Esempio n. 2
0
        private void MessageReceived(bool successful, PeerMessage message, object state)
        {
            var id = (PeerId)state;

            if (!successful)
            {
                id.ConnectionManager.CleanupSocket(id, "Could not receive a message");
                return;
            }

            try
            {
                var e = new PeerMessageEventArgs(id.TorrentManager, message, Direction.Incoming, id);
                id.ConnectionManager.RaisePeerMessageTransferred(e);

                message.Handle(id);

                id.LastMessageReceived = DateTime.Now;
                PeerIO.EnqueueReceiveMessage(id.Connection, id.Decryptor, id.TorrentManager.DownloadLimiter, id.Monitor,
                                             id.TorrentManager, MessageReceivedCallback, id);
            }
            catch (TorrentException ex)
            {
                id.ConnectionManager.CleanupSocket(id, ex.Message);
            }
        }