示例#1
0
        public override void Close()
        {
            base.Close();
            if (Handle != IntPtr.Zero)
            {
                PauseReading();
                PauseWriting();

                readWatcher.Dispose();
                writeWatcher.Dispose();

                if (readTimeoutWatcher != null)
                {
                    readTimeoutWatcher.Dispose();
                }
                if (writeTimeoutWatcher != null)
                {
                    writeTimeoutWatcher.Dispose();
                }

                readWatcher         = null;
                writeWatcher        = null;
                readTimeoutWatcher  = null;
                writeTimeoutWatcher = null;

                Handle = IntPtr.Zero;
            }
        }
示例#2
0
        protected override void Dispose(bool disposing)
        {
            if (Handle != IntPtr.Zero)
            {
                PauseReading();
                PauseWriting();

                readWatcher.Dispose();
                writeWatcher.Dispose();

                if (readTimeoutWatcher != null)
                {
                    readTimeoutWatcher.Dispose();
                }
                if (writeTimeoutWatcher != null)
                {
                    writeTimeoutWatcher.Dispose();
                }

                readWatcher         = null;
                writeWatcher        = null;
                readTimeoutWatcher  = null;
                writeTimeoutWatcher = null;

                Handle = IntPtr.Zero;
            }
            base.Dispose(disposing);
        }
示例#3
0
        public void AddTimeout(Timeout timeout)
        {
            TimerWatcher t = new TimerWatcher(timeout.begin, timeout.span, evloop, HandleTimeout);

            t.UserData = timeout;
            t.Start();
        }
示例#4
0
        public virtual void Close()
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            DisableReading();
            DisableWriting();

            read_watcher.Dispose();
            write_watcher.Dispose();
            timeout_watcher.Dispose();

            read_watcher    = null;
            write_watcher   = null;
            timeout_watcher = null;
            handle          = IntPtr.Zero;

            foreach (IWriteOperation op in write_ops)
            {
                op.Dispose();
            }
            write_ops.Clear();

            if (Closed != null)
            {
                Closed(this, EventArgs.Empty);
            }
            Closed        = null;
            read_callback = null;

            GC.SuppressFinalize(this);
        }
示例#5
0
        private void HandleTimeout(Loop loop, TimerWatcher timeout, EventTypes revents)
        {
            Timeout t = (Timeout)timeout.UserData;

            AppHost.RunTimeout(t);
            if (!t.ShouldContinueToRepeat())
            {
                timeout.Stop();
            }
        }
示例#6
0
        public int Main(string[] args)
        {
            TimerWatcher tw = new TimerWatcher(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), (LibEvLoop)IOLoop.Instance.EventLoop,
                                               (l, w, et) => Console.WriteLine("{0}: Beep", DateTime.Now));

            tw.Start();


            return(0);
        }
示例#7
0
 private void HandleTimeoutEvent(Loop loop, TimerWatcher watcher, EventTypes revents)
 {
     if (Expires <= DateTime.UtcNow)
     {
         if (TimedOut != null)
         {
             TimedOut(this, EventArgs.Empty);
         }
         Close();
     }
 }
示例#8
0
 void HandleWriteTimeout(Loop loop, TimerWatcher watcher, EventTypes revents)
 {
     if (writeTimeoutContinuation != null)
     {
         writeTimeoutWatcher.Repeat = DateTime.Now - writeTimeoutContinuation.Value;
         writeTimeoutWatcher.Again();
         writeTimeoutContinuation = null;
     }
     else
     {
         RaiseError(new TimeoutException());
         PauseWriting();
     }
 }
示例#9
0
        public void SetHandle(IntPtr handle)
        {
            if (this.handle != IntPtr.Zero && this.handle != handle)
            {
                Close();
            }

            this.handle     = handle;
            read_watcher    = new IOWatcher(handle, EventTypes.Read, ioloop.EventLoop, HandleIOReadEvent);
            write_watcher   = new IOWatcher(handle, EventTypes.Write, ioloop.EventLoop, HandleIOWriteEvent);
            timeout_watcher = new TimerWatcher(TimeOut, TimeOut, ioloop.EventLoop, HandleTimeoutEvent);

            timeout_watcher.Start();
        }
示例#10
0
 void HandleReadTimeout(TimerWatcher watcher, EventTypes revents)
 {
     if (readTimeoutContinuation != null)
     {
         readTimeoutWatcher.Repeat = DateTime.Now - readTimeoutContinuation.Value;
         readTimeoutWatcher.Again();
         readTimeoutContinuation = null;
     }
     else
     {
         RaiseError(new TimeoutException());
         PauseReading();
     }
 }
示例#11
0
        public int ScheduleRepeatingTimer(TimeSpan interval, Action <int> onTimer)
        {
            if (freeList.Count == 0)
            {
                growWatchers(watchers.Length * 2);
            }

            int ret = freeList.Dequeue();

            TimerWatcher tw = new TimerWatcher(interval, interval, (LibEvLoop)loop.EventLoop, (l, w, et) => onTimer(ret));

            watchers[ret] = tw;
            tw.Start();
            return(ret);
        }
示例#12
0
        /// <summary>
        /// true if all is well, false if timed out
        /// </summary>
        public void OnComplete(Action <bool> whenComplete, TimeSpan timeOut)
        {
            if (WorkPending == 0)
            {
                whenComplete(true);
            }
            else
            {
                timedWhenComplete = whenComplete;

                watcher = new TimerWatcher(timeOut, TimeSpan.MaxValue, (LibEvLoop)loop.EventLoop, (l, w, et) =>
                {
                    w.Stop();
                    timedWhenComplete = null;
                    whenComplete(false);
                });
                watcher.Start();
            }
        }
示例#13
0
        public int ScheduleTimer(TimeSpan after, Action <int> onTimer)
        {
            if (freeList.Count == 0)
            {
                growWatchers(watchers.Length * 2);
            }

            int ret = freeList.Dequeue();

            TimerWatcher tw = new TimerWatcher(after, TimeSpan.MaxValue, (LibEvLoop)loop.EventLoop, (l, w, et) =>
            {
                w.Stop();
                watchers[ret] = null;
                freeList.Enqueue(ret);
                onTimer(ret);
            });

            watchers[ret] = tw;
            tw.Start();
            return(ret);
        }
示例#14
0
        private void growWatchers(int newSize)
        {
            int currentSize;

            if (watchers == null)
            {
                watchers    = new TimerWatcher[newSize];
                currentSize = 0;
            }
            else
            {
                currentSize = watchers.Length;
                var w = new TimerWatcher[newSize];
                Array.Copy(watchers, w, currentSize - 1);
                watchers = w;
            }

            for (int i = currentSize; i < newSize; i++)
            {
                freeList.Enqueue(i);
            }
        }
示例#15
0
        static void Main()
        {
            var endpoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 8081);

            uv_init();

            var watch = new PrepareWatcher(() => {
                //Console.WriteLine("Prepare Watcher Called");
            });

            watch.Start();
            var server = new TcpServer((socket) => {
                clientcount++;
                socket.Stream.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1);
                if (clientcount > 5)
                {
                    socket.Close();
                }
                Console.WriteLine("Client Connected");
                socket.Stream.OnRead += (data) => {
                    Console.WriteLine("Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    socket.Stream.Write(data, data.Length);
                };
                //socket.OnClose += () => {
                //	Console.WriteLine("Client Disconnected");
                //};
            });

            server.Listen(endpoint);
            var client = new TcpSocket();

            client.Connect(endpoint, () => {
                client.Stream.OnRead += (data) => {
                    Console.WriteLine("Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    watch.Stop();
                    watch.Dispose();
                    client.Close();
                };
                byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n");
                client.Stream.Write(message, message.Length);
            });
            var pipeserver = new PipeServer((socket) => {
                clientcount++;
                socket.Stream.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1);
                if (clientcount > 5)
                {
                    socket.Close();
                }
                Console.WriteLine("Pipe Client Connected");
                socket.Stream.OnRead += (data) => {
                    Console.WriteLine("Pipe Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    socket.Stream.Write(data, data.Length);
                };
                //socket.OnClose += () => {
                //	Console.WriteLine("Client Disconnected");
                //};
            });

            pipeserver.Listen("libuv-csharp");
            var pipeclient = new PipeSocket();

            pipeclient.Connect("libuv-csharp", () => {
                pipeclient.Stream.OnRead += (data) => {
                    Console.WriteLine("Pipe Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    watch.Stop();
                    watch.Dispose();
                    pipeclient.Close();
                };
                byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n");
                pipeclient.Stream.Write(message, message.Length);
            });
            var watch2 = new PrepareWatcher(() => {
                //Console.WriteLine("Prepare Watcher 2 Called");
            });

            watch2.Start();
            var check = new CheckWatcher(() => {
                //Console.WriteLine("Check Watcher Called");
            });

            check.Start();
            var idle = new IdleWatcher(() => {
                //Console.WriteLine("Idle Watcher Called");
            });

            idle.Start();
            var after = new TimerWatcher(new TimeSpan(0, 0, 5), new TimeSpan(1, 0, 0), () => {
                //Console.WriteLine("After 5 Seconds");
            });

            after.Start();
            var every = new TimerWatcher(new TimeSpan(0, 0, 5), () => {
                //Console.WriteLine("Every 5 Seconds");
                //	after.Stop();
            });

            every.Start();
            var cp = new ChildProcess("ls");

            cp.Spawn();
            uv_run();
        }