The overall goal is to implement a fairly generic networking library, and then use it to build a peer-to-peer chat sample. This idea was initially discussed on aspnet/SignalR#1453.
This prototype is meant to assist in the current refactoring of the Protocols/Sockets layer of SignalR and Kestrel.
However, to keep it simply, it only relies on the .NET framework (and the Logging library).
Getting Started
- Compile.
- Run many instances of the
PeerChat
app. It uses UDP broadcasting to enable the instances to find each others. So, it should work when running on different computers. If you run many instances on the same computer, only the first instance will be able to find the next instances. All these next instances will only connect to the first instance (which is fine if you only have two instances). - Type a message and press Enter to send it to the other peers.
- Type Enter without writing anything to gracefully stop the instance.
Architecture
RawCommunication.Net
is a simple Sockets-based library with the following classes:
SocketAwaitable
: Similar to the Kestrel's version, however, it returns a bool for success instead of throwing. And there are extensions method to make calling it simpler. This makes the high-performance SocketAsyncEventArgs fairly elegant to use.Broadcast
andTransport
: Based on Kestrel's SocketTransport.Broadcast
sends and receives UDP broadcast messages.Transport
accepts incoming connections and initiates outgoing connections.Connection
: Based on Kestrel's SocketConnection. Sends and receives bytes through its open socket.
RawCommunication.PeerChat.App
is a console application that implements a simple peer-to-peer chat.
Limitations
This library doesn't depend on the new memory/pipelines libraries, it instead simulates how they work.
So, to keep it simple, it relies on delegates to wire up the classes. And the Chat
class has an awkward implementation for its ReadAsync()
.
I also don't like that it throw new SocketException()
when a connection/listener is closed. Ideally, it should just exit the loop. But that requires some refactoring or duplication of code for exception handling.
And Broadcast.OnBroadcastReceived()
should not be async void
.