Skip to content

Commit

Permalink
Added coder factory for servers
Browse files Browse the repository at this point in the history
- Added coder factory for servers fixing the same coder being used by multiple peers (stupid for not finding this)
- Moved to 0.3.1
  • Loading branch information
alandoherty committed Dec 3, 2018
1 parent 3d2a195 commit 483c589
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ProtoSocket/ProtoSocket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard1.3</TargetFramework>
<Authors>Alan Doherty &amp; WIFIPLUG Ltd</Authors>
<Company>Alan Doherty</Company>
<Version>0.3.0</Version>
<Version>0.3.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>A networking library for frame-based, performant asynchronous sockets on .NET Core</Description>
<Copyright>Alan Doherty 2018</Copyright>
Expand Down
15 changes: 15 additions & 0 deletions src/ProtoSocket/ProtocolCoderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ProtoSocket
{
/// <summary>
/// A factory function to create peer coders.
/// </summary>
/// <typeparam name="TFrame">The frame type.</typeparam>
/// <param name="peer">The peer.</param>
/// <returns>The coder.</returns>
public delegate IProtocolCoder<TFrame> ProtocolCoderFactory<TFrame>(ProtocolPeer<TFrame> peer)
where TFrame : class;
}
8 changes: 8 additions & 0 deletions src/ProtoSocket/ProtocolPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,14 @@ public Subscription(ProtocolPeer<TFrame> peer, IObserver<TFrame> observer, Predi
protected ProtocolPeer(IProtocolCoder<TFrame> coder) {
_coder = coder;
}

/// <summary>
/// Creates an uninitialized protocol peer.
/// </summary>
/// <param name="coderFactory">The protocol coder factory.</param>
protected ProtocolPeer(ProtocolCoderFactory<TFrame> coderFactory) {
_coder = coderFactory(this);
}
#endregion
}

Expand Down
14 changes: 7 additions & 7 deletions src/ProtoSocket/ProtocolServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ProtocolServer<TConnection, TFrame> : IDisposable, IProtocolServer
private IConnectionFilter _filter;
private List<TConnection> _connections = new List<TConnection>();
private List<TConnection> _connectionsAnnounced = new List<TConnection>();
private IProtocolCoder<TFrame> _coder;
private ProtocolCoderFactory<TFrame> _coderFactory;
private Uri _endpoint;

private CancellationTokenSource _stopSource;
Expand Down Expand Up @@ -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<TFrame> e) {
Expand Down Expand Up @@ -293,13 +293,13 @@ public void Dispose() {
/// <summary>
/// Creates a new protocol server.
/// </summary>
/// <param name="coder">The protocol coder.</param>
public ProtocolServer(IProtocolCoder<TFrame> coder) {
/// <param name="coderFactory">The protocol coder factory.</param>
public ProtocolServer(ProtocolCoderFactory<TFrame> 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
}
Expand Down

0 comments on commit 483c589

Please sign in to comment.