简体   繁体   English

使用Golang绑定在libtorrent中强制转换“警报”类型

[英]Casting an “alert” type in libtorrent, using Golang bindings

I am developing a personal project in Golang, using libtorrent-go 我正在使用libtorrent-go在Golang中开发个人项目

When I do receive an alert of type "save_resume_data_alert" , I pick it up and have to CAST it as written in libtorrent documentation 当我收到类型为"save_resume_data_alert"的警报时,我将其拾起并必须按照libtorrent文档中的说明进行投射

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

But i really have not idea how to cast it in golang! 但是我真的不知道如何在golang中使用它! Current code: 当前代码:

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 {}
}

As stated above, libtorrent-go developer answered me, so I am forwarding the answer for posterity reasons. 如上所述, libtorrent-go开发人员回答了我,因此出于后代原因,我转发了答案。

Casting C++ structures in Golang using SWIG library is documented in SWIG-Golang documentation . SWIG-Golang文档中介绍了使用SWIG库在Golang中强制转换C ++结构。
In particular in this statement: 特别是在此声明中:

Given a value of the interface type, Go code can retrieve the pointer to the C++ type by calling the Swigcptr method. 给定接口类型的值,Go代码可以通过调用Swigcptr方法来检索指向C ++类型的指针。 This will return a value of type SwigcptrClassName, which is just a name for uintptr. 这将返回SwigcptrClassName类型的值,这只是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. Go类型转换可用于将该值转换为其他C ++类型,但是请注意,此转换将不进行类型检查,并且实质上等效于reinterpret_cast。 This should only be used for very special cases, such as where C++ would use a dynamic_cast. 这仅应用于非常特殊的情况,例如C ++将使用dynamic_cast的情况。

In that particular piece of code I posted above, the following was necessary to make it work: 在我上面发布的那段特定代码中,需要以下代码才能使其正常工作:

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM