From 585c936cf3be7becf9af62b8d93d01de63ab5a95 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:14:57 +0200 Subject: [PATCH] Network: Create SocketListenerFactory. Services can be injected into the factory to pass into SocketListener, instead of referencing IRCd properties. task: #12 --- cmpctircd/IRCd.cs | 6 ++++-- cmpctircd/ISocketListenerFactory.cs | 7 +++++++ cmpctircd/Program.cs | 1 + cmpctircd/SocketListener.cs | 1 + cmpctircd/SocketListenerFactory.cs | 9 +++++++++ 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 cmpctircd/ISocketListenerFactory.cs create mode 100644 cmpctircd/SocketListenerFactory.cs diff --git a/cmpctircd/IRCd.cs b/cmpctircd/IRCd.cs index 41f2bd6..3328a6e 100644 --- a/cmpctircd/IRCd.cs +++ b/cmpctircd/IRCd.cs @@ -9,6 +9,7 @@ using cmpctircd.Modes; public class IRCd { + private readonly ISocketListenerFactory socketListenerFactory; private readonly IList Listeners = new List(); public readonly IList Connectors = new List(); public PacketManager PacketManager { get; } @@ -50,9 +51,10 @@ public class IRCd { public List Clients => ClientLists.SelectMany(clientList => clientList).ToList(); public List Servers => ServerLists.SelectMany(serverList => serverList).ToList(); - public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services) { + public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services, ISocketListenerFactory socketListenerFactory) { this.Log = log; this.Config = config; + this.socketListenerFactory = socketListenerFactory; // Interpret the ConfigData SID = config.SID; @@ -99,7 +101,7 @@ public void Run() { PacketManager.Load(); foreach(var listener in Config.Sockets.OfType()) { - SocketListener sl = new SocketListener(this, listener); + SocketListener sl = socketListenerFactory.CreateSocketListener(this, listener); Log.Info($"==> Listening on: {listener.Host}:{listener.Port} ({listener.Type}) ({(listener.IsTls ? "TLS" : "Plain" )})"); Listeners.Add(sl); diff --git a/cmpctircd/ISocketListenerFactory.cs b/cmpctircd/ISocketListenerFactory.cs new file mode 100644 index 0000000..824150b --- /dev/null +++ b/cmpctircd/ISocketListenerFactory.cs @@ -0,0 +1,7 @@ +using cmpctircd.Configuration; + +namespace cmpctircd { + public interface ISocketListenerFactory { + SocketListener CreateSocketListener(IRCd ircd, SocketElement config); + } +} \ No newline at end of file diff --git a/cmpctircd/Program.cs b/cmpctircd/Program.cs index 5d9c0af..301312e 100644 --- a/cmpctircd/Program.cs +++ b/cmpctircd/Program.cs @@ -28,6 +28,7 @@ static IHostBuilder CreateHostBuilder(string[] args) { services.AddScoped(); services.AddScoped(sp => sp.GetRequiredService().Sender as Client); services.AddScoped(sp => sp.GetRequiredService().Sender as Server); + services.AddTransient(); services.AddHostedService(); }); } diff --git a/cmpctircd/SocketListener.cs b/cmpctircd/SocketListener.cs index 19bdfa6..787ecc0 100644 --- a/cmpctircd/SocketListener.cs +++ b/cmpctircd/SocketListener.cs @@ -12,6 +12,7 @@ namespace cmpctircd { public class SocketListener { + private readonly Log log; protected IRCd _ircd; private Boolean _started = false; private TcpListener _listener = null; diff --git a/cmpctircd/SocketListenerFactory.cs b/cmpctircd/SocketListenerFactory.cs new file mode 100644 index 0000000..a1200a9 --- /dev/null +++ b/cmpctircd/SocketListenerFactory.cs @@ -0,0 +1,9 @@ +using cmpctircd.Configuration; + +namespace cmpctircd { + public class SocketListenerFactory : ISocketListenerFactory { + public SocketListener CreateSocketListener(IRCd ircd, SocketElement config) { + return new SocketListener(ircd, config); + } + } +}