func main() { var ( rset, wset, eset syscall.FdSet still_running, curl_timeout int = 0, 0 err error ) ch1 := curl.EasyInit() ch2 := curl.EasyInit() ch1.Setopt(curl.OPT_URL, "http://www.163.com") ch1.Setopt(curl.OPT_HEADER, 0) ch1.Setopt(curl.OPT_VERBOSE, true) ch2.Setopt(curl.OPT_URL, "http://www.baidu.com") ch2.Setopt(curl.OPT_HEADER, 0) ch2.Setopt(curl.OPT_VERBOSE, true) mh := curl.MultiInit() mh.AddHandle(ch1) mh.AddHandle(ch2) for { FD_ZERO(&rset) FD_ZERO(&wset) FD_ZERO(&eset) timeout := syscall.Timeval{Sec: 1, Usec: 0} curl_timeout, err = mh.Timeout() if err != nil { fmt.Printf("Error multi_timeout: %s\n", err) } if curl_timeout >= 0 { timeout.Sec = int64(curl_timeout / 1000) if timeout.Sec > 1 { timeout.Sec = 1 } else { timeout.Usec = int64((curl_timeout % 1000)) * 1000 } } max_fd, err := mh.Fdset(&rset, &wset, &eset) if err != nil { fmt.Printf("Error FDSET: %s\n", err) } _, err = syscall.Select(int(max_fd+1), &rset, &wset, &eset, &timeout) if err != nil { fmt.Printf("Error select: %s\n", err) } else { still_running, err = mh.Perform() if still_running > 0 { fmt.Printf("Still running: %d\n", still_running) } else { break } } } }
// timeToTimeval - Convert time.Time to syscall.Timeval // // Note: This does not use syscall.NsecToTimespec because // that does not work properly for times before 1970, // see https://github.com/golang/go/issues/12777 func timeToTimeval(t *time.Time) syscall.Timeval { var tv syscall.Timeval tv.Usec = int32(t.Nanosecond() / 1000) tv.Sec = t.Unix() return tv }