使用 Golang 绑定(bind)在 libtorrent 中转换 "alert"类型

标签 casting go bittorrent libtorrent

我正在使用 Golang 开发一个个人项目,使用 libtorrent-go

当我收到类型为 "save_resume_data_alert" 的警报时,我将其拾取并必须按照 libtorrent documentation 中的说明进行 CAST

...
        save_resume_data_alert const* rd = alert_cast<save_resume_data_alert>(a);
...

但我真的不知道如何在 golang 中转换它!当前代码:

package main

import (
    lt "github.com/steeve/libtorrent-go"

    "log"
    "time"
)

func main() {

    randomTorrent := lt.NewAdd_torrent_params()
    randomTorrent.SetUrl("PUT A MAGNET LINK HERE")
    randomTorrent.SetSave_path(".")

    ec := lt.NewError_code()
    torrentSession := lt.NewSession()
    torrentSession.Set_alert_mask(status_notification + storage_notification)
    torrentSession.Listen_on(lt.NewStd_pair_int_int(6900, 6999), ec)
    if ec.Value() != 0 {
        log.Println(ec.Message())
    }

    torrentHandle := torrentSession.Add_torrent(randomTorrent, ec)
    if ec.Value() != 0 {
        log.Println(ec.Message())
    }

    go func() {
        for {
            if torrentSession.Wait_for_alert(lt.Seconds(10)).Swigcptr() == 0 {
                log.Println("Alert timeout occurred!")
            }

            alert := torrentSession.Pop_alert()
            switch alert.What() {
            default:
                log.Printf("Alert: %#v", alert.What())
            case "metadata_received_alert":
                log.Println("Received Metadata!! finally!")
                torrentHandle.Save_resume_data()
            case "save_resume_data_alert":
                log.Println("Wrote Metadata!")
                // need to actually write the resume_data :( can't find how
            case "save_resume_data_failed_alert":
                log.Println("Failed Metadata!")
            }
        }
    }()

    select {}
}

最佳答案

如上所述,libtorrent-go 开发人员回答了我,因此我转发答案以供后代使用。

使用 SWIG 库在 Golang 中转换 C++ 结构记录在 SWIG-Golang documentation 中.
特别是在这个声明中:

Given a value of the interface type, Go code can retrieve the pointer to the C++ type by calling the Swigcptr method. This will return a value of type SwigcptrClassName, which is just a name for uintptr. A Go type conversion can be used to convert this value to a different C++ type, but note that this conversion will not be type checked and is essentially equivalent to reinterpret_cast. This should only be used for very special cases, such as where C++ would use a dynamic_cast.

在我上面发布的那段特定代码中,以下是使其工作所必需的:

case "save_resume_data_alert":
  log.Println("Wrote Metadata!")
  // need to actually write the resume_data :( can't find how
  SaveRDAlert := lt.SwigcptrSave_resume_data_alert(alert.Swigcptr())
  log.Printf("Resume Data: %#v", SaveRDAlert.GetResume_data())

关于使用 Golang 绑定(bind)在 libtorrent 中转换 "alert"类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524688/

相关文章:

java - 您可以使用 libtorrent 库进行基本的主线 dht 查询,例如 find_node 和 ping

如果我包装我的对象,Golang 转换为自定义类型会失败

c# - 如何让 Monotorrents DHT 工作?

分配给变量时,Golang 无法检测隐式类型的 const

amazon-web-services - DynamoDB PutItem 上的条件表达式

go - 从 goroutine 更新 Prometheus 计数器的正确方法是什么?

go - 如何在 cobra cli 中将命令状态传递给 Postrun

python - 比特流 udp 跟踪器响应

c++ - 什么时候转换为 __m256 而不是加载是正确的?

java - 无法从 Object 转换为 List,但它已经是一个 List?