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); }
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; }
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; }
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 ); }
private void HandleTimeoutEvent(Loop loop, TimerWatcher watcher, EventTypes revents) { if (Expires <= DateTime.UtcNow) { if (TimedOut != null) TimedOut (this, EventArgs.Empty); Close (); } }
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 (); }
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; }
public void AddTimeout(Timeout timeout) { TimerWatcher t = new TimerWatcher (timeout.begin, timeout.span, evloop, HandleTimeout); t.UserData = timeout; t.Start (); }
private void HandleTimeout(Loop loop, TimerWatcher timeout, EventTypes revents) { Timeout t = (Timeout) timeout.UserData; AppHost.RunTimeout (t); if (!t.ShouldContinueToRepeat ()) timeout.Stop (); }
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; } }
void HandleWriteTimeout(TimerWatcher watcher, EventTypes revents) { if (writeTimeoutContinuation != null) { writeTimeoutWatcher.Repeat = DateTime.Now - writeTimeoutContinuation.Value; writeTimeoutWatcher.Again (); writeTimeoutContinuation = null; } else { RaiseError (new TimeoutException ()); PauseWriting (); } }
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 (); } }
/// <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(); } }
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; }