Skip to content

Commit

Permalink
step 1 factor out use of ssl config helper
Browse files Browse the repository at this point in the history
  • Loading branch information
aallbrig committed Jan 8, 2024
1 parent 6733eb4 commit 95e4b3b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

namespace Mirror.SimpleWeb
{
internal sealed class Connection : IDisposable
public interface IConnection
{
public TcpClient Client { get; }
public Stream Stream { get; set; }
}
internal sealed class Connection : IConnection, IDisposable
{
readonly object disposedLock = new object();

public const int IdNotSet = -1;
public TcpClient client;
public TcpClient Client => client;
public int connId = IdNotSet;

/// <summary>
Expand All @@ -28,6 +34,12 @@ internal sealed class Connection : IDisposable
public string remoteAddress;

public Stream stream;
public Stream Stream
{
get => stream;
set => stream = value;
}

public Thread receiveThread;
public Thread sendThread;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Mirror.SimpleWeb
{
public interface ICreateStream
{
bool TryCreateStream(IConnection conn);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SslConfig(bool enabled, string certPath, string certPassword, SslProtocol
this.sslProtocols = sslProtocols;
}
}
internal class ServerSslHelper
internal class ServerSslHelper: ICreateStream
{
readonly SslConfig config;
readonly X509Certificate2 certificate;
Expand All @@ -37,14 +37,14 @@ public ServerSslHelper(SslConfig sslConfig)
}
}

internal bool TryCreateStream(Connection conn)
public bool TryCreateStream(IConnection conn)
{
NetworkStream stream = conn.client.GetStream();
NetworkStream stream = conn.Client.GetStream();
if (config.enabled)
{
try
{
conn.stream = CreateStream(stream);
conn.Stream = CreateStream(stream);
return true;
}
catch (Exception e)
Expand All @@ -55,7 +55,7 @@ internal bool TryCreateStream(Connection conn)
}
else
{
conn.stream = stream;
conn.Stream = stream;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SimpleWebServer(int maxMessagesPerTick, TcpConfig tcpConfig, int maxMessa
// use max because bufferpool is used for both messages and handshake
int max = Math.Max(maxMessageSize, handshakeMaxSize);
bufferPool = new BufferPool(5, 20, max);
server = new WebSocketServer(tcpConfig, maxMessageSize, handshakeMaxSize, sslConfig, bufferPool);
server = new WebSocketServer(tcpConfig, maxMessageSize, handshakeMaxSize, bufferPool, new ServerSslHelper(sslConfig));
}

public void Start(ushort port)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public class WebSocketServer
Thread acceptThread;
bool serverStopped;
readonly ServerHandshake handShake;
readonly ServerSslHelper sslHelper;
readonly ICreateStream streamCreator;
readonly BufferPool bufferPool;
readonly ConcurrentDictionary<int, Connection> connections = new ConcurrentDictionary<int, Connection>();

int _idCounter = 0;

public WebSocketServer(TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig, BufferPool bufferPool)
public WebSocketServer(TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, BufferPool bufferPool, ICreateStream streamCreator)
{
this.tcpConfig = tcpConfig;
this.maxMessageSize = maxMessageSize;
sslHelper = new ServerSslHelper(sslConfig);
this.streamCreator = streamCreator;
this.bufferPool = bufferPool;
handShake = new ServerHandshake(this.bufferPool, handshakeMaxSize);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ void HandshakeAndReceiveLoop(Connection conn)
{
try
{
bool success = sslHelper.TryCreateStream(conn);
bool success = streamCreator.TryCreateStream(conn);
if (!success)
{
Log.Warn($"[SWT-WebSocketServer]: Failed to create SSL Stream {conn}");
Expand Down
Loading

0 comments on commit 95e4b3b

Please sign in to comment.