Nito.Async is a collection of classes to assist asynchronous programming in .NET, particularly the development and use of classes adhering to the event-based asynchronous pattern.
Note: If you're looking for Nito.AsyncEx (a library for working with the async
/await
and Tasks), that is a separate project.
Nito.Async
includes:
ActionThread
- A child thread with an event-driven main loop sufficient to own and synchronize event-based asynchronous components.Timer
- A general-purpose timer using the asynchronous event-based model.GenericSynchronizingObject
- An implementation ofISynchronizeInvoke
that usesSynchronizationContext
to synchronize.CallbackContext
- A context manager for asynchronous callbacks.AsyncResultEventArgs<T>
- AnAsyncCompletedEventArgs
with a single result value of typeT
.ActionDispatcher
- A queue of actions, providing a main loop with aSynchronizationContext
for child threads.Sync.Synchronize
- Wrappers for various delegate types that provide synchronization usingAsyncOperation
. These allow fairly easy-to-write translations from theIAsyncResult
pattern to the event-based asynchronous pattern.
The current Nito.Async.Sockets
API has been frozen. A new (v2) API will be developed that provides better separation between protocol components (e.g., type of message framing, keepalive system, etc., will all be orthogonal). The new API may be based on the Rx framework, but is more likely to be tied into the .NET 4.0 Task Parallel Library.
IAsyncTcpConnection
,ClientTcpSocket
,ServerTcpSocket
, andServerChildTcpSocket
- Sockets using the event-based asynchronous pattern.SocketPacketProtocol
- A class that provides a packet protocol for socket connections, including keepalive packets.ISimpleAsyncTcpConnection
,SimpleClientTcpSocket
,SimpleServerTcpSocket
, andSimpleServerChildTcpSocket
- Sockets using the event-based asynchronous pattern with a packet protocol.
One major problem faced by newcomers to TCP/IP is thread synchronization. It can be difficult to learn networking concepts and multithreading at the same time.
The ClientTcpSocket
and ServerTcpSocket
classes provide simple thread synchronization by exposing an event-based API for TCP/IP sockets.
There are two other common problems that are discovered when designing a TCP/IP protocol:
- Non-packetized data. Most people read about how TCP/IP uses packets, and they believe that message boundaries are preserved. Local-machine testing confirms this misunderstanding. However, TCP/IP sockets deal with a stream of bytes, not messages.
- Detection of dropped connections. Again, people read about TCP/IP's keepalive packets, but don't realize that they are pretty much useless.
The SimpleClientTcpSocket
and SimpleServerTcpSocket
solve these problems by defining a protocol with message boundaries and automatic keepalive messages.
For more information on TCP/IP socket handling in .NET, see the TCP/IP .NET Sockets FAQ.