IoService

A Mina.NET IoService is the base class supporting all the IO services, either from the server side or the client side. It will handle all the interaction with your application, and with the remote peer, send and receive messages, manage sessions, conections, etc.

IoService is an interface that is implemented by the two most important interfaces in Mina.NET: IoAcceptor for the server side, and IoConnector for the client side.
In order to build a server, you need to select an implementation of the IoAcceptor interface. For client applications, you need to implement an implementation of the IoConnector interface.

IoService Introduction

IoService provides basic I/O Service and manages I/O Sessions within Mina.NET. It's one of the most crucial part of Mina.NET architecture. The implementing classes of IoService and child interface, are where most of the low level I/O operations are handled.

Responsabilities

  • sessions management: Creates and deletes sessions, detect idleness
  • filter chain management: Handles the filtr chain, allowing the user to change the chain on the fly
  • handler invocation: Calls the handler when some new message is received, etc
  • statistics management: Updates the number of messages sent, bytes sent, and many others
  • lifecycle events: Service activated/idle/deactivated, session events
  • communication management: Handles the transmission of data, in both side

Interface Details

IoService is the base interface for all the IoConnectors and IoAcceptors that provides I/O services and manages I/O sessions. The interface has all the functions need to perform I/O related operations.

Properties

Methods

Events

ActivationTime

Returns the time when this service was activated. It returns the last time when this service was activated if the service is not anymore active.

Active

Tells if the service is active or not.

Disposed

Tells if the service has been disposed. A service will be considered as disposed only when all the resources it has allocated have been released.

FilterChainBuilder

Gets or sets the Filter chain builder. This is useful if one wants to add some new filter that will be injected when the sessions will be created.

FilterChain

Returns the current default Filter chain for the service.

Handler

Gets or sets the IoHandler that will be responsible for handling all the events for the service.

ManagedSessions

Returns the map of all sessions which are currently managed by this service.

SessionConfig

Returns the session configuration.

SessionDataStructureFactory

Gets or sets the IoSessionDataStructureFactory that provides related data structures for a new session created by this service.

Statistics

Returns the IoServiceStatistics object for this service.

TransportMetadata

Returns the Transport meta-data the IoAcceptor or IoConnector is running. The typical details include provider name (mina, async), connection type (connectionless/connection oriented) etc.

Broadcast(Object)

Writes the given message to all the managed sessions.

Dispose()

Releases all the resources the service has allocated.

Activated

Occurs when this service is activated.

Idle

Occurs when this service is idle.

Deactivated

Occurs when this service is deactivated.

SessionCreated

Occurs when a new session is created.

SessionDestroyed

Occurs when a new session is being destroyed.

SessionOpened

Occurs when a session is opened. Only available when the Handler has NOT been set, otherwise use IoHandler.SessionOpened(IoSession) instead.

SessionClosed

Occurs when a session is closed. Only available when the Handler has NOT been set, otherwise use IoHandler.SessionClosed(IoSession) instead.

SessionIdle

Occurs when a session is idle. Only available when the Handler has NOT been set, otherwise use IoHandler.SessionIdle(IoSession, IdleStatus) instead.

ExceptionCaught

Occurs when any exception is thrown. Only available when the Handler has NOT been set, otherwise use IoHandler.ExceptionCaught(IoSession, Exception) instead.

MessageReceived

Occurs when a message is received. Only available when the Handler has NOT been set, otherwise use IoHandler.MessageReceived(IoSession, Object) instead.

MessageSent

Occurs when a message is sent. Only available when the Handler has NOT been set, otherwise use IoHandler.MessageSent(IoSession, Object) instead.

NOTE, the 6 events above, SessionOpened, SessionClosed, SessionIdle, ExceptionCaught, MessageReceived and MessageSent, will never be fired if the Handler property is set, which means you can not use these events and IoHandler classes at the same time.

IoAcceptor

Basically, this interface is named because of the Accept() method, responsible for the creation of new connections between a client and the server. The server accepts incoming connection requests.

As we may deal with more than one kind of transport (TCP/UDP/...), we have more than one implementation for this interface:
  • AsyncSocketAcceptor: the non-blocking TCP transport IoAcceptor
  • AsyncDatagramAcceptor: the non-blocking UDP transport IoAcceptor
  • LoopbackAcceptor: the Loopback (in-application pipe) IoAcceptor

Here is the class diagram for the IoAcceptor interfaces and classes:

IoAcceptor.png

Creation

You first have to select the type of IoAcceptor you want to instanciate. Let's see with an example how it works:

public TcpServer()
{
  IoAcceptor acceptor = new AsyncSocketAcceptor();

  acceptor.Bind(new IPEndPoint(IPAddress.Any, port));

  Console.WriteLine("Listening on " + acceptor.LocalEndPoint);
}

That's it! You have created a TCP server. If you want to start an UDP server, simply replace the first line of code:

...
IoAcceptor acceptor = new AsyncDatagramAcceptor();
...

Unbind

The service can be stopped by calling the Unbind() method.

acceptor.Unbind();

Disposal

The service can also be stopped by calling the Dispose() method.

acceptor.Dispose();

Managing the Filters chain

If you want to manage the filters chain, you may call the FilterChain property. Here is an example:

acceptor.FilterChain.AddLast("logger", new LoggingFilter());

You can also create the chain before and set it into the service:

DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();
chain .AddLast("logger", new LoggingFilter());

acceptor.FilterChainBuilder = chain;

IoConnector

As we have to use an IoAcceptor for servers, you have to implement the IoConnector for clients. Again, we have many implementation classes:
  • AsyncSocketConnector: the non-blocking TCP transport IoConnector
  • AsyncDatagramConnector: the non-blocking UDP transport IoConnector
  • SerialConnector: a IoConnectorfor a serial transport
  • LoopbackConnector: the Loopback (in-application pipe) IoConnector

Here is the class diagram for the IoConnector interfaces and classes:

IoConnector.png

Last edited Apr 15, 2014 at 2:33 AM by longshine, version 5