Skip to content

Commit 49ace85

Browse files
gedemgedem
authored andcommitted
Compression fix, GZip check default
1 parent bad6931 commit 49ace85

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

src/NetCoreStack.WebSockets/ConnectionManager.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ConnectionManager(IStreamCompressor compressor,
3939
Connections = new ConcurrentDictionary<string, WebSocketTransport>();
4040
}
4141

42-
private async Task<byte[]> PrepareBytesAsync(byte[] input, IDictionary<string, object> properties, bool compression = true)
42+
private async Task<byte[]> PrepareBytesAsync(byte[] input, IDictionary<string, object> properties)
4343
{
4444
if (input == null)
4545
{
@@ -49,19 +49,23 @@ private async Task<byte[]> PrepareBytesAsync(byte[] input, IDictionary<string, o
4949
if (properties == null)
5050
properties = new Dictionary<string, object>();
5151

52-
object compressedKey = null;
53-
if (properties.TryGetValue(CompressedKey, out compressedKey))
54-
properties[CompressedKey] = compression;
52+
bool compressed = GZipHelper.IsGZipHeader(input);
53+
54+
object key = null;
55+
if (properties.TryGetValue(CompressedKey, out key))
56+
properties[CompressedKey] = compressed;
5557
else
56-
properties.Add(CompressedKey, compression);
58+
properties.Add(CompressedKey, compressed);
59+
60+
string props = JsonConvert.SerializeObject(properties);
61+
byte[] header = Encoding.UTF8.GetBytes($"{props}{Splitter}");
5762

58-
var props = JsonConvert.SerializeObject(properties);
59-
var propsBytes = Encoding.UTF8.GetBytes($"{props}{Splitter}");
63+
if (compressed)
64+
header = await _compressor.CompressAsync(header);
6065

61-
var bytesCount = input.Length;
62-
input = propsBytes.Concat(input).ToArray();
66+
input = header.Concat(input).ToArray();
6367

64-
if (compression)
68+
if (!compressed)
6569
return await _compressor.CompressAsync(input);
6670

6771
return input;
@@ -156,14 +160,14 @@ public async Task BroadcastAsync(WebSocketMessageContext context)
156160
}
157161
}
158162

159-
public async Task BroadcastBinaryAsync(byte[] inputs, IDictionary<string, object> properties, bool compression = true)
163+
public async Task BroadcastBinaryAsync(byte[] inputs, IDictionary<string, object> properties)
160164
{
161165
if (!Connections.Any())
162166
{
163167
return;
164168
}
165169

166-
var bytes = await PrepareBytesAsync(inputs, properties, compression);
170+
var bytes = await PrepareBytesAsync(inputs, properties);
167171

168172
using (var ms = new MemoryStream(bytes))
169173
{
@@ -215,7 +219,7 @@ public async Task SendAsync(string connectionId, WebSocketMessageContext context
215219
await SendAsync(transport, descriptor);
216220
}
217221

218-
public async Task SendBinaryAsync(string connectionId, byte[] input, IDictionary<string, object> properties, bool compression = true)
222+
public async Task SendBinaryAsync(string connectionId, byte[] input, IDictionary<string, object> properties)
219223
{
220224
if (!Connections.Any())
221225
{
@@ -228,7 +232,7 @@ public async Task SendBinaryAsync(string connectionId, byte[] input, IDictionary
228232
throw new ArgumentOutOfRangeException(nameof(transport));
229233
}
230234

231-
byte[] bytes = await PrepareBytesAsync(input, properties, compression);
235+
byte[] bytes = await PrepareBytesAsync(input, properties);
232236

233237
using (var ms = new MemoryStream(bytes))
234238
{

src/NetCoreStack.WebSockets/Interfaces/IConnectionManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ public interface IConnectionManager
2424
/// </summary>
2525
/// <param name="inputs"></param>
2626
/// <param name="properties"></param>
27-
/// <param name="compression"></param>
2827
/// <returns></returns>
29-
Task BroadcastBinaryAsync(byte[] input, IDictionary<string, object> properties, bool compression = true);
28+
Task BroadcastBinaryAsync(byte[] input, IDictionary<string, object> properties);
3029

3130
/// <summary>
3231
/// Send text message to specified connection
@@ -44,7 +43,7 @@ public interface IConnectionManager
4443
/// <param name="properties">Additional transport data</param>
4544
/// <param name="compression">Compression status of the data, default value is true</param>
4645
/// <returns></returns>
47-
Task SendBinaryAsync(string connectionId, byte[] input, IDictionary<string, object> properties, bool compression = true);
46+
Task SendBinaryAsync(string connectionId, byte[] input, IDictionary<string, object> properties);
4847

4948
/// <summary>
5049
/// Close the specified connection

test/ServerTestApp/Controllers/DiscoveryController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ public async Task<IActionResult> SendCompressedBinaryAsync([FromBody]Context mod
107107
try
108108
{
109109
var routeValueDictionary = new RouteValueDictionary(new { Key = key });
110-
var bytes = _distrubutedCache.Get(key);
111-
var compressor = HttpContext.RequestServices.GetService<ICompressor>();
112-
var compressedBytes = await compressor.CompressAsync(bytes);
113-
await _connectionManager.BroadcastBinaryAsync(compressedBytes, routeValueDictionary, false);
110+
111+
// Get compressed bytes from redis
112+
var compressedBytes = _distrubutedCache.Get(key);
113+
await _connectionManager.BroadcastBinaryAsync(compressedBytes, routeValueDictionary);
114114
}
115115
catch (Exception ex)
116116
{

0 commit comments

Comments
 (0)