Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class SimpleWebServer

public bool Active { get; private set; }

public SimpleWebServer(int maxMessagesPerTick, TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig)
public SimpleWebServer(int maxMessagesPerTick, TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig, int sendQueueMaxMessageCount)
{
this.maxMessagesPerTick = maxMessagesPerTick;
// 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, sslConfig, bufferPool, sendQueueMaxMessageCount);
}

public void Start(ushort port)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class WebSocketServer

readonly TcpConfig tcpConfig;
readonly int maxMessageSize;
readonly int sendQueueMaxMessageCount;

TcpListener listener;
Thread acceptThread;
Expand All @@ -23,10 +24,11 @@ public class WebSocketServer

int _idCounter = 0;

public WebSocketServer(TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig, BufferPool bufferPool)
public WebSocketServer(TcpConfig tcpConfig, int maxMessageSize, int handshakeMaxSize, SslConfig sslConfig, BufferPool bufferPool, int sendQueueMaxMessageCount)
{
this.tcpConfig = tcpConfig;
this.maxMessageSize = maxMessageSize;
this.sendQueueMaxMessageCount = sendQueueMaxMessageCount;
sslHelper = new ServerSslHelper(sslConfig);
this.bufferPool = bufferPool;
handShake = new ServerHandshake(this.bufferPool, handshakeMaxSize);
Expand Down Expand Up @@ -187,6 +189,13 @@ public void Send(int id, ArrayBuffer buffer)
{
if (connections.TryGetValue(id, out Connection conn))
{
if (sendQueueMaxMessageCount > 0 && conn.sendQueue.Count + 1 >= sendQueueMaxMessageCount)
{
Log.Warn("[SWT-WebSocketServer]: Send: cannot send message to {0} because its send queue has reached max count. Disconnecting connection", id);
conn.Dispose();
return;
}

conn.sendQueue.Enqueue(buffer);
conn.sendPending.Set();
}
Expand Down
6 changes: 5 additions & 1 deletion Assets/Mirror/Transports/SimpleWeb/SimpleWebTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public ushort Port
"If WaitBeforeSend is true then BatchSend Will also be set to true")]
public bool waitBeforeSend = true;

[Tooltip("Disconnects clients if their sendQueue Count reaches this value. Disabled if <= 0.\n" +
"Prevents hundreds of buffers being allocated and pooled forever if the client stops processing incoming messages without disconnecting")]
public int sendQueueMaxMessageCount = 0;

[Header("Client settings")]

[Tooltip("Sets connect scheme to wss. Useful when client needs to connect using wss when TLS is outside of transport.\nNOTE: if sslEnabled is true clientUseWss is also true")]
Expand Down Expand Up @@ -293,7 +297,7 @@ public override void ServerStart()
Log.Warn("[SWT-ServerStart]: Server Already Started");

SslConfig config = SslConfigLoader.Load(sslEnabled, sslCertJson, sslProtocols);
server = new SimpleWebServer(serverMaxMsgsPerTick, TcpConfig, maxMessageSize, maxHandshakeSize, config);
server = new SimpleWebServer(serverMaxMsgsPerTick, TcpConfig, maxMessageSize, maxHandshakeSize, config, sendQueueMaxMessageCount);

server.onConnect += OnServerConnectedWithAddress.Invoke;
server.onDisconnect += OnServerDisconnected.Invoke;
Expand Down