Пример #1
0
        private static void Main(string[] args)
        {
            Curl.GlobalInit(CurlInitFlag.Default);
            var handles = new List <CurlEasy>();

            using (var multi = new CurlMulti())
            {
                foreach (var url in new[]
                {
                    "https://www.google.com",
                    "https://www.yahoo.com",
                    "http://edition.cnn.com",
                    "https://news.ycombinator.com",
                    "https://pages.github.com"
                })
                {
                    Writers.Add(url, new BinaryWriter(new FileStream($"dl-{HostName(url)}.html", FileMode.Create)));
                    var easy = CreateEasy(url);
                    multi.AddHandle(easy);
                    handles.Add(easy);
                }

                multi.Pipelining = CurlPipelining.Multiplex;

                var stillRunning = 1;
                // call CurlMulti.Perform right away (note ref qualifier)
                multi.Perform(ref stillRunning);

                while (stillRunning != 0)
                {
                    var mc = multi.FdSet();
                    if (mc != CurlMultiCode.Ok)
                    {
                        WriteLine($"curl_multi_fdset() failed, code: {mc}");
                        break;
                    }

                    /* On success the value of maxfd is guaranteed to be >= -1. We call select(maxfd + 1, ...); specially in case of (maxfd == -1) there are no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- to sleep 100ms, which is the minimum suggested value in the curl_multi_fdset() doc. */
                    var rc = 0;

                    if (multi.MaxFd == -1)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        rc = multi.Select(1000); // one second
                    }
                    switch (rc)
                    {
                    case -1:     // select error
                        break;

                    default:
                    {
                        multi.Perform(ref stillRunning);
                        break;
                    }
                    }
                }
            }

            foreach (var w in Writers.Values)
            {
                w.Dispose();
            }

            foreach (var h in handles)
            {
                h.Dispose();
            }

            Curl.GlobalCleanup();

            WriteLine("\nPress <ENTER> to exit...");
            ReadLine();
        }
Пример #2
0
        public static void Main(String[] args)
        {
            try
            {
                Curl.GlobalInit(CurlInitFlag.All);

                CurlWriteCallback wf = OnWriteData;

                var handles = new List <CurlEasy>();
                var urls    = new[]
                {
                    "http://www.codeplex.com",
                    "http://www.yahoo.com",
                    "http://www.cnn.com",
                    "http://www.abc.com",
                    "http://www.bbc.co.uk"
                };

                using (var multi = new CurlMulti())
                {
                    foreach (var url in urls)
                    {
                        var easy = CreateEasy(url, wf);
                        multi.AddHandle(easy);
                        handles.Add(easy);
                    }

                    var runningObjects = 1;
                    // call CurlMulti.Perform right away (note ref qualifier)
                    multi.Perform(ref runningObjects);

                    while (runningObjects != 0)
                    {
                        var fdret = multi.FdSet();
                        //if (multi.MaxFd > -1)
                        {
                            var rc = multi.Select(1000); // one second
                            switch (rc)
                            {
                            case -1:
                                Console.WriteLine("CurlMulti.Select() returned -1");
                                //runningObjects = 0;
                                break;

                            case 0:     // time out
                            default:
                            {
                                multi.Perform(ref runningObjects);
                                break;
                            }
                            }
                        }
                    }
                }

                foreach (var easy in handles)
                {
                    easy.Dispose();
                }
                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
            }
        }