【p2p、分布式,区块链笔记 Torrent】WebTorrent 的lt_donthave插件

扩展实现

  • https://github.com/webtorrent/lt_donthave/blob/master/index.js
/*! lt_donthave. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */

// 导入所需模块
import arrayRemove from 'unordered-array-remove' // 用于从数组中删除元素的函数
import {
   
    EventEmitter } from 'events' // 导入事件发射器类
import debugFactory from 'debug' // 导入调试工具

// 创建一个调试实例,用于记录调试信息
const debug = debugFactory('lt_donthave')

// 导出一个函数,该函数返回 ltDontHave 类
export default () => {
   
   
  // 定义 ltDontHave 类,继承自 EventEmitter
  class ltDontHave extends EventEmitter {
   
   
    constructor (wire) {
   
   
      super() // 调用父类构造函数

      // 初始化属性
      this._peerSupports = false // 标记对等体是否支持 'lt_donthave'
      this._wire = wire // 保存 wire 对象,表示与对等体的连接
    }

    // 当接收到扩展握手时调用,表示对等体支持 'lt_donthave'
    onExtendedHandshake () {
   
   
      this._peerSupports = true
    }

    // 处理接收到的消息
    onMessage (buf) {
   
   
      let index // 存储块索引
      try {
   
   
        // 创建 DataView 从接收到的缓冲区中读取数据
        const view = new DataView(buf.buffer)
        index = view.getUint32(0) // 获取消息中的块索引
      } catch (err) {
   
   
        // 如果消息无效,直接丢弃
        return
      }

      // 如果对等体没有该块,直接返回
      if (!this._wire.peerPieces.get(index)) return
      debug('got donthave %d', index) // 记录调试信息
      this._wire.peerPieces.set(index, false) // 标记该块为不拥有

      this.emit('donthave', index) // 触发 'donthave' 事件
      this._failRequests(index) // 处理失败的请求
    }

    // 向远程对等体发送不拥有的块索引
    donthave (index) {
   
   
      if (!this._peerSupports) return // 如果对等体不支持,直接返回

      debug('donthave %d', index) // 记录调试信息
      const buf = new Uint8Array(4) // 创建一个 4 字节的缓冲区
      const view = new DataView(buf.buffer) // 创建 DataView
      view.setUint32(0, index) // 将块索引写入缓冲区
      console.log(">>>>>>>>>>>>>>>>", index, ">>>>>>>>>>>>>>", buf) // 打印调试信息
      this._wire.extended('lt_donthave', buf) // 发送 'lt_donthave' 消息
    }

    // 处理失败的请求
    _failRequests (index) {
   
   
      const requests = this._wire.requests // 获取当前请求列表
      for (let i = 0; i < requests.length; i++) {
   
   
        const req = requests[i] // 获取当前请求
        if (req.piece === index) {
   
    // 如果请求的块索引与指定索引匹配
          arrayRemove(requests, i) // 从请求列表中删除该请求
          i -= 1 // 更新索引以检查新值
          this._wire._callback(req, new Error('peer sent donthave'), null) // 调用回调函数,报告请求失败
        }
      }
    }
  }

  // 设置扩展名称
  ltDontHave.prototype.name = 'lt_donthave'

  return ltDontHave // 返回定义的类
}

测试代码

import fixtures from 'webtorrent-fixtures'
import Protocol from 'bittorrent-protocol'
import test from 'tape'
import ltDontHave from './index.js'

const {
   
    leaves } = fixtures

const id1 = Buffer.from('01234567890123456789')
const id2 = Buffer.from('12345678901234567890')
  
  
const wire1 = new
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值