From 483c5895ae8101e080c5b037f4a896de7d3e5154 Mon Sep 17 00:00:00 2001 From: Alan Doherty Date: Mon, 3 Dec 2018 23:17:39 +0000 Subject: [PATCH] Added coder factory for servers - Added coder factory for servers fixing the same coder being used by multiple peers (stupid for not finding this) - Moved to 0.3.1 --- src/ProtoSocket/ProtoSocket.csproj | 2 +- src/ProtoSocket/ProtocolCoderFactory.cs | 15 +++++++++++++++ src/ProtoSocket/ProtocolPeer.cs | 8 ++++++++ src/ProtoSocket/ProtocolServer.cs | 14 +++++++------- 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/ProtoSocket/ProtocolCoderFactory.cs diff --git a/src/ProtoSocket/ProtoSocket.csproj b/src/ProtoSocket/ProtoSocket.csproj index 2a08658..ab64175 100644 --- a/src/ProtoSocket/ProtoSocket.csproj +++ b/src/ProtoSocket/ProtoSocket.csproj @@ -4,7 +4,7 @@ netstandard1.3 Alan Doherty & WIFIPLUG Ltd Alan Doherty - 0.3.0 + 0.3.1 true A networking library for frame-based, performant asynchronous sockets on .NET Core Alan Doherty 2018 diff --git a/src/ProtoSocket/ProtocolCoderFactory.cs b/src/ProtoSocket/ProtocolCoderFactory.cs new file mode 100644 index 0000000..3230fea --- /dev/null +++ b/src/ProtoSocket/ProtocolCoderFactory.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProtoSocket +{ + /// + /// A factory function to create peer coders. + /// + /// The frame type. + /// The peer. + /// The coder. + public delegate IProtocolCoder ProtocolCoderFactory(ProtocolPeer peer) + where TFrame : class; +} diff --git a/src/ProtoSocket/ProtocolPeer.cs b/src/ProtoSocket/ProtocolPeer.cs index 7d0e186..992950e 100644 --- a/src/ProtoSocket/ProtocolPeer.cs +++ b/src/ProtoSocket/ProtocolPeer.cs @@ -1130,6 +1130,14 @@ public Subscription(ProtocolPeer peer, IObserver observer, Predi protected ProtocolPeer(IProtocolCoder coder) { _coder = coder; } + + /// + /// Creates an uninitialized protocol peer. + /// + /// The protocol coder factory. + protected ProtocolPeer(ProtocolCoderFactory coderFactory) { + _coder = coderFactory(this); + } #endregion } diff --git a/src/ProtoSocket/ProtocolServer.cs b/src/ProtoSocket/ProtocolServer.cs index 911c21f..a66baa5 100644 --- a/src/ProtoSocket/ProtocolServer.cs +++ b/src/ProtoSocket/ProtocolServer.cs @@ -26,7 +26,7 @@ public class ProtocolServer : IDisposable, IProtocolServer private IConnectionFilter _filter; private List _connections = new List(); private List _connectionsAnnounced = new List(); - private IProtocolCoder _coder; + private ProtocolCoderFactory _coderFactory; private Uri _endpoint; private CancellationTokenSource _stopSource; @@ -170,7 +170,7 @@ private async void AcceptNext(TcpClient client) { } // create connection - TConnection connection = (TConnection)Activator.CreateInstance(typeof(TConnection), this, _coder); + TConnection connection = (TConnection)Activator.CreateInstance(typeof(TConnection), this, _coderFactory); // add events connection.Connected += delegate (object o, PeerConnectedEventArgs e) { @@ -293,13 +293,13 @@ public void Dispose() { /// /// Creates a new protocol server. /// - /// The protocol coder. - public ProtocolServer(IProtocolCoder coder) { + /// The protocol coder factory. + public ProtocolServer(ProtocolCoderFactory coderFactory) { // check coder isn't null - if (coder == null) - throw new ArgumentNullException(nameof(coder), "The coder cannot be null"); + if (coderFactory == null) + throw new ArgumentNullException(nameof(coderFactory), "The coder factory cannot be null"); - _coder = coder; + _coderFactory = coderFactory; } #endregion }