Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Reactors

Aaron Stannard edited this page Jul 3, 2014 · 5 revisions

Reactors are the foundation Helios' socket server capabilities. Reactors are able to bind to a TCP or UDP socket and accept incoming data and connections across a fixed-size number of threads, and every reactor inherits from the IReactor interface (see IReactor.cs)

The value of this is significant - if you have a socket server with 1000 active TCP connections, you don't need one thread for every individual connection. Instead, Helios can performantly manage those connections with a fixed number of threads (typically 2-3 threads) via its EventLoop.

Built-in Reactors

Two reactors are available out of the box with Helios:

  • TcpProxyReactor and
  • UdpProxyReactor.

The difference is solely in the type of socket being bound by the reactor. You can always extend ReactorBase (see ReactorBase.cs) or implement IReactor from scratch if you wish to create your own reactor implementation.

Instantiating a Reactor

While you can technically call new TcpProxyReactor(...) directly, it's often easier to use Helios' built-in ServerBootstrap class (see ServerBootstrap.cs), which allows you to fluently constructor a reactor instance to suit your needs.

Here's an example of how to use ServerBootstrap to create a TCP reactor using mostly default settings (taken from the TcpReactorServer sample program):

var bootstrapper =
	new ServerBootstrap()
		.WorkerThreads(2)
		.SetTransport(TransportType.Tcp)
		.Build();
var reactor = bootstrapper.NewReactor(NodeBuilder.BuildNode().Host(ip).WithPort(Port));

The ServerBootstrapper yields a ServerFactory object, which will create new reactors for you via the ServerFactory.NewReactor method.

This method takes an INode object as an argument, which is used inside Helios to describe the network topology of a single node (address / ip pair) in a network.

You can use NodeBuilder to fluently create INode instances for you, or you can just allocate an INode the older-fashioned way:

var node = new Node(){ Host = IPAddress.Any, Port = 1337 };

Reactor Events

All network I/O inside Helios is event-driven - all reactors use an IEventLoop internally to fire network events back to your handler code.

The events that you need to handle with Helios' reactors are:

  • OnConnection - event is fired when a new connection is established. Most important for TCP connections, but still useful in UDP scenarios too.
  • OnDisconnection - event is fired when a connection is closed. Important for TCP connections, happens virtually never or UDP connections unless there's custom application code to create explicit connnectons.
  • OnReceive - event is fired when data is received by the reactors' IEventLoop and it provides an IConnection object that can be used to respond directly back to the sender for both TCP and UDP connections.
  • OnError - fired when a recoverable error, such as a malformed message, is caught and handled. Critical errors, such as a socket failure, raise the OnDisconnection event instead.
Clone this wiki locally