diff --git a/examples/multi_and_wait.go b/examples/multi_and_wait.go new file mode 100644 index 0000000..5b50862 --- /dev/null +++ b/examples/multi_and_wait.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "github.com/msva/go-curl" + "time" +) + +func main() { + ch1 := curl.EasyInit() + ch2 := curl.EasyInit() + + ch1.Setopt(curl.OPT_URL, "http://www.google.com") + ch1.Setopt(curl.OPT_HEADER, 1) + ch1.Setopt(curl.OPT_VERBOSE, true) + ch2.Setopt(curl.OPT_URL, "http://www.microsoft.com") + ch2.Setopt(curl.OPT_HEADER, 1) + ch2.Setopt(curl.OPT_VERBOSE, true) + + mh := curl.MultiInit() + + mh.AddHandle(ch1) + mh.AddHandle(ch2) + + var repeats int + + for { + stillRunning, err := mh.Perform() + if err != nil { + fmt.Printf("Error perform: %s\n", err) + break + } else if stillRunning > 0 { + fmt.Printf("Still running: %d\n", stillRunning) + } else { + break + } + + timeoutMs := 1000 + curlTimeout, err := mh.Timeout() + if err != nil { + fmt.Printf("Error multi_timeout: %s\n", err) + } + if curlTimeout >= 0 { + timeoutMs = curlTimeout + } + + numFds, err := mh.Wait(timeoutMs) + if err != nil { + fmt.Printf("Error wait: %s\n", err) + break + } + + // 'numFds' being zero means either a timeout or no file descriptors to + // wait for. Try timeout on first occurrence, then assume no file + // descriptors, which means wait for 100 milliseconds to prevent spinning + // in Perform + Wait. + if numFds == 0 { + repeats++ // count number of repeated zero numFds + if repeats > 1 { + time.Sleep(100 * time.Millisecond) + } + } else { + repeats = 0 + } + } +} diff --git a/multi.go b/multi.go index 692a9fc..fc71885 100644 --- a/multi.go +++ b/multi.go @@ -96,6 +96,14 @@ func (mcurl *CURLM) Perform() (int, error) { return int(running_handles), err } +// curl_multi_wait - polls on all easy handles in a multi handle +func (mcurl *CURLM) Wait(timeoutMs int) (int, error) { + p := mcurl.handle + handles_with_events := C.int(0) + err := newCurlMultiError(C.curl_multi_wait(p, nil, 0, C.int(timeoutMs), &handles_with_events)) + return int(handles_with_events), err +} + // curl_multi_add_handle - add an easy handle to a multi session func (mcurl *CURLM) AddHandle(easy *CURL) error { mp := mcurl.handle