/README.md

http://github.com/Rduerden/Node.cs · Markdown · 108 lines · 74 code · 34 blank · 0 comment · 0 complexity · dec27740cc11199cd730ee7bca8c4a2b MD5 · raw file

  1. Node.cs
  2. =======
  3. ### Evented I/O for C# .net ###
  4. Node.cs takes it's inspriation from node.js, providing an extremely simple mechanism for building high performance single threaded applications. Node.cs is built on the event loop and http stack from Manos De Mono (see https://github.com/jacksonh/manos). Node.cs will also allow for interaction with threads, where interaction with the loop for asynchronous operation is not possible.
  5. public class Webserver : INodeProgram
  6. {
  7. public int Main( string[] args )
  8. {
  9. new HttpServer( ( IHttpTransaction t ) =>
  10. {
  11. Console.WriteLine( "got connection {0}", t.Request.Path );
  12. t.Response.Write( "<H1>Hello World!</H1>" );
  13. t.Response.End();
  14. }, IOLoop.Instance ).Listen( "10.0.2.15", 8080 );
  15. Console.WriteLine( "listening on 8080" );
  16. return 0;
  17. }
  18. }
  19. to run this you would simply call node against your compiled assembly -
  20. % node example.webserver.dll
  21. listening on 8080
  22. ### Timers ###
  23. Node.cs defines a simple mechanism to have an action take place after some
  24. specified time in the future
  25. var timers = Timers.Instance;
  26. int handle = timers.ScheduleTimer( TimeSpan.FromSeconds( 5 ),
  27. Console.WriteLine( "hello in the future" );
  28. Scheduling a timer returns a handle to that timer that can be used to cancel it
  29. before it fires
  30. timers.CancelTimer( handle );
  31. ### Task Completion ###
  32. A common issue with frameworks such as this are the deep, deep nests that get
  33. created as a result of chaining multiple indipendant async calls within calls.
  34. Another common issue is the ability to know when a loop of async calls have completed so that further action can be taken.
  35. Node.cs defines a class Complete that allows you to indicate when an async
  36. method is executing and when that method has completed.
  37. Complete c = new Complete();
  38. Timers t = Timers.Instance;
  39. var ts = TimeSpan.FromSeconds( 5 );
  40. int ii = 0;
  41. c.AsyncWork( () => t.ScheduleTimer( ts, h => {ii++; c.AsyncWorkComplete();} ));
  42. c.AsyncWork( () => t.ScheduleTimer( ts, h => {ii++;c.AsyncWorkComplete();} ));
  43. c.OnComplete( () => Console.WriteLine( "Completed " + ii));
  44. A timeout can also be specified to call you back if the async actions have not
  45. completed in the specified time :
  46. c.OnComplete( (rec ) => Console.WriteLine( "Completed " + ii + res ),TimeSpan.FromSeconds( 2 ));
  47. ### Threading ###
  48. Even though Node.cs is aimed at single threaded use cases, there might be times
  49. where you'll want to kick off another thread for async functionality that is
  50. not yet provided by the framework.
  51. Node.cs defines a class Boundary that aims to make interacting with your main
  52. node app from other threads simple.
  53. Thread.CurrentThread.Name = "LoopThread";
  54. var boundary = Boundary.Instance;
  55. Thread t = new Thread( () =>
  56. {
  57. Console.WriteLine( "{0} thread running", Thread.CurrentThread.Name );
  58. boundary.ExecuteOnTargetLoop( () =>
  59. {
  60. Console.WriteLine( "Boundary call executed on {0}",
  61. Thread.CurrentThread.Name );
  62. });
  63. });
  64. t.Name = "BH Thread";
  65. t.Start();
  66. % node example.multithreaded.dll
  67. Starting program
  68. BH Thread thread running
  69. Boundary call executed on LoopThread
  70. ### UDP ###
  71. UDP Message Receiving is supported, sending support will be added shortly.
  72. UDPReceiver rec = new UDPReceiver( loop );
  73. rec.Listen( "10.0.2.15", 6656 );
  74. rec.OnRead( ( u, b, c, rep ) => Console.WriteLine( rep + " " + Encoding.ASCII.GetString( b, 0, c )));