Skip to content

Commit

Permalink
Network: Create SocketListenerFactory.
Browse files Browse the repository at this point in the history
Services can be injected into the factory to pass into SocketListener, instead of referencing IRCd properties.

task: #12
  • Loading branch information
lewishazell committed May 23, 2021
1 parent 9486afb commit 585c936
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cmpctircd/IRCd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using cmpctircd.Modes;

public class IRCd {
private readonly ISocketListenerFactory socketListenerFactory;
private readonly IList<SocketListener> Listeners = new List<SocketListener>();
public readonly IList<SocketConnector> Connectors = new List<SocketConnector>();
public PacketManager PacketManager { get; }
Expand Down Expand Up @@ -50,9 +51,10 @@ public class IRCd {

public List<Client> Clients => ClientLists.SelectMany(clientList => clientList).ToList();
public List<Server> 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;
Expand Down Expand Up @@ -99,7 +101,7 @@ public void Run() {
PacketManager.Load();

foreach(var listener in Config.Sockets.OfType<SocketElement>()) {
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);
Expand Down
7 changes: 7 additions & 0 deletions cmpctircd/ISocketListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using cmpctircd.Configuration;

namespace cmpctircd {
public interface ISocketListenerFactory {
SocketListener CreateSocketListener(IRCd ircd, SocketElement config);
}
}
1 change: 1 addition & 0 deletions cmpctircd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static IHostBuilder CreateHostBuilder(string[] args) {
services.AddScoped<IrcContext>();
services.AddScoped(sp => sp.GetRequiredService<IrcContext>().Sender as Client);
services.AddScoped(sp => sp.GetRequiredService<IrcContext>().Sender as Server);
services.AddTransient<ISocketListenerFactory, SocketListenerFactory>();
services.AddHostedService<IrcApplicationLifecycle>();
});
}
Expand Down
1 change: 1 addition & 0 deletions cmpctircd/SocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace cmpctircd {
public class SocketListener {
private readonly Log log;
protected IRCd _ircd;
private Boolean _started = false;
private TcpListener _listener = null;
Expand Down
9 changes: 9 additions & 0 deletions cmpctircd/SocketListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 585c936

Please sign in to comment.