Skip to content

Commit afecdbb

Browse files
committed
stream, performance improvements, tasks
1 parent 33c4f70 commit afecdbb

File tree

12 files changed

+194
-253
lines changed

12 files changed

+194
-253
lines changed
Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Microsoft.Extensions.Logging;
22
using NetCoreStack.WebSockets.Internal;
3-
using Newtonsoft.Json;
43
using System;
5-
using System.Collections.Generic;
64
using System.IO;
75
using System.Net.WebSockets;
86
using System.Threading;
@@ -32,95 +30,78 @@ public ClientWebSocketReceiver(IServiceProvider serviceProvider,
3230

3331
public async Task ReceiveAsync()
3432
{
35-
try
33+
var buffer = new byte[NCSConstants.ChunkSize];
34+
var result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
35+
while (!result.CloseStatus.HasValue)
3636
{
37-
var buffer = new byte[NCSConstants.ChunkSize];
38-
var result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
39-
while (!result.CloseStatus.HasValue)
37+
if (result.MessageType == WebSocketMessageType.Text)
4038
{
41-
if (result.MessageType == WebSocketMessageType.Text)
39+
byte[] inputs = null;
40+
using (var ms = new MemoryStream())
4241
{
43-
try
42+
while (!result.EndOfMessage)
4443
{
45-
var context = result.ToContext(buffer);
46-
if (context.Command == WebSocketCommands.Handshake)
47-
{
48-
_context.ConnectionId = context.Value?.ToString();
49-
_handshakeCallback?.Invoke(_context.ConnectionId);
50-
}
44+
await ms.WriteAsync(buffer, 0, result.Count);
45+
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
46+
}
5147

52-
var invocator = _context.GetInvocator(_serviceProvider);
53-
if (invocator != null)
54-
{
55-
await invocator.InvokeAsync(context);
56-
}
48+
await ms.WriteAsync(buffer, 0, result.Count);
49+
inputs = ms.ToArray();
50+
}
51+
try
52+
{
53+
var context = result.ToContext(inputs);
54+
if (context.Command == WebSocketCommands.Handshake)
55+
{
56+
_context.ConnectionId = context.Value?.ToString();
57+
_handshakeCallback?.Invoke(_context.ConnectionId);
5758
}
58-
catch (Exception ex)
59+
var invocator = _context.GetInvocator(_serviceProvider);
60+
if (invocator != null)
5961
{
60-
_logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Text);
62+
await invocator.InvokeAsync(context);
6163
}
62-
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
6364
}
65+
catch (Exception ex)
66+
{
67+
_logger.LogWarning(ex, "{0} An error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Text);
68+
}
69+
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
70+
}
6471

65-
if (result.MessageType == WebSocketMessageType.Binary)
72+
if (result.MessageType == WebSocketMessageType.Binary)
73+
{
74+
byte[] binaryResult = null;
75+
using (var ms = new MemoryStream())
6676
{
67-
byte[] binaryResult = null;
68-
using (var ms = new MemoryStream())
69-
{
70-
while (!result.EndOfMessage)
71-
{
72-
if (!result.CloseStatus.HasValue)
73-
{
74-
await ms.WriteAsync(buffer, 0, result.Count);
75-
}
76-
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
77-
}
78-
if (result.EndOfMessage)
79-
{
80-
if (!result.CloseStatus.HasValue)
81-
{
82-
await ms.WriteAsync(buffer, 0, result.Count);
83-
}
84-
}
85-
binaryResult = ms.ToArray();
86-
}
87-
try
77+
while (!result.EndOfMessage)
8878
{
89-
var context = await result.ToBinaryContextAsync(_context.Compressor, binaryResult);
90-
var invocator = _context.GetInvocator(_serviceProvider);
91-
if (invocator != null)
92-
{
93-
await invocator.InvokeAsync(context);
94-
}
79+
await ms.WriteAsync(buffer, 0, result.Count);
80+
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
9581
}
96-
catch (Exception ex)
82+
83+
await ms.WriteAsync(buffer, 0, result.Count);
84+
binaryResult = ms.ToArray();
85+
}
86+
try
87+
{
88+
var context = await result.ToBinaryContextAsync(_context.Compressor, binaryResult);
89+
var invocator = _context.GetInvocator(_serviceProvider);
90+
if (invocator != null)
9791
{
98-
_logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Binary);
92+
await invocator.InvokeAsync(context);
9993
}
100-
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
10194
}
95+
catch (Exception ex)
96+
{
97+
_logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Binary);
98+
}
99+
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
102100
}
103-
104-
await _context.WebSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
105-
_closeCallback?.Invoke(_context);
106101
}
107-
catch (Exception ex)
108-
{
109-
var dictionary = new Dictionary<string, string>();
110-
dictionary.Add(nameof(_context.ConnectionId), _context.ConnectionId);
111102

112-
if (_context.InvocatorContext != null)
113-
{
114-
dictionary.Add(nameof(_context.InvocatorContext.ConnectorName), _context.InvocatorContext.ConnectorName);
115-
dictionary.Add(nameof(_context.InvocatorContext.Uri), Convert.ToString(_context.InvocatorContext.Uri));
116-
}
117-
118-
_logger.LogWarning(ex, "{0} receive exception: {1}", NCSConstants.WarningSymbol, JsonConvert.SerializeObject(dictionary));
119-
}
120-
finally
121-
{
122-
_closeCallback?.Invoke(_context);
123-
}
103+
await _context.WebSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
104+
_closeCallback?.Invoke(_context);
124105
}
125106
}
126107
}

0 commit comments

Comments
 (0)