This project is read-only.


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.


  • 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.





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.


Tells if the service is active or not.


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.


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.


Returns the current default Filter chain for the service.


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


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


Returns the session configuration.


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


Returns the IoServiceStatistics object for this service.


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.


Writes the given message to all the managed sessions.


Releases all the resources the service has allocated.


Occurs when this service is activated.


Occurs when this service is idle.


Occurs when this service is deactivated.


Occurs when a new session is created.


Occurs when a new session is being destroyed.


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


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


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


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


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


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.


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:



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();


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



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


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;


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:


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