duankui1532 2014-12-17 11:35
浏览 81
已采纳

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

I am developing a personal project in Golang, using libtorrent-go

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 const* rd = alert_cast<save_resume_data_alert>(a);
...

But i really have not idea how to cast it in 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 {}
}
  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2014-12-17 17:17
    关注

    As stated above, libtorrent-go developer answered me, so I am forwarding the answer for posterity reasons.

    Casting C++ structures in Golang using SWIG library is documented in SWIG-Golang documentation.
    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. 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.

    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())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)