internal void RaisePeerMessageTransferred(PeerMessageEventArgs e) { if (PeerMessageTransferred == null) { return; } Task.Factory.StartNew(() => { var h = PeerMessageTransferred; if (h == null) { return; } if (!(e.Message is MessageBundle)) { h(e.TorrentManager, e); } else { // Message bundles are only a convience for internal usage! var b = (MessageBundle)e.Message; foreach ( var args in b.Messages.Select( message => new PeerMessageEventArgs(e.TorrentManager, message, e.Direction, e.ID))) { h(args.TorrentManager, args); } } }); }
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); } }