Ejemplo n.º 1
0
        private static void StaticCallback(IntPtr loop, IntPtr watcher, EventTypes revents)
        {
            UnmanagedTimerWatcher iow = (UnmanagedTimerWatcher)Marshal.PtrToStructure(watcher, typeof(UnmanagedTimerWatcher));

            GCHandle     gchandle = GCHandle.FromIntPtr(iow.data);
            TimerWatcher w        = (TimerWatcher)gchandle.Target;

            w.callback(w.Loop, w, revents);
        }
Ejemplo n.º 2
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;

        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
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 );

        }
Ejemplo n.º 5
0
 private void HandleTimeoutEvent(Loop loop, TimerWatcher watcher, EventTypes revents)
 {
     if (Expires <= DateTime.UtcNow) {
         if (TimedOut != null)
             TimedOut (this, EventArgs.Empty);
         Close ();
     }
 }
Ejemplo n.º 6
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 ();
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
 public void AddTimeout(Timeout timeout)
 {
     TimerWatcher t = new TimerWatcher (timeout.begin, timeout.span, evloop, HandleTimeout);
     t.UserData = timeout;
     t.Start ();
 }
Ejemplo n.º 9
0
        private void HandleTimeout(Loop loop, TimerWatcher timeout, EventTypes revents)
        {
            Timeout t = (Timeout) timeout.UserData;

            AppHost.RunTimeout (t);
            if (!t.ShouldContinueToRepeat ())
               timeout.Stop ();
        }
Ejemplo n.º 10
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;
            }
        }
Ejemplo n.º 11
0
 void HandleWriteTimeout(TimerWatcher watcher, EventTypes revents)
 {
     if (writeTimeoutContinuation != null) {
         writeTimeoutWatcher.Repeat = DateTime.Now - writeTimeoutContinuation.Value;
         writeTimeoutWatcher.Again ();
         writeTimeoutContinuation = null;
     } else {
         RaiseError (new TimeoutException ());
         PauseWriting ();
     }
 }
Ejemplo n.º 12
0
 void HandleReadTimeout(Loop loop, TimerWatcher watcher, EventTypes revents)
 {
     if (readTimeoutContinuation != null) {
         readTimeoutWatcher.Repeat = DateTime.Now - readTimeoutContinuation.Value;
         readTimeoutWatcher.Again ();
         readTimeoutContinuation = null;
     } else {
         RaiseError (new TimeoutException ());
         PauseReading ();
     }
 }
Ejemplo n.º 13
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();
            }
        }
Ejemplo n.º 14
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;
        }