|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Net.WebSockets; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace NetCoreStack.WebSockets.Internal |
| 11 | +{ |
| 12 | + public class ClientWebSocketReceiver |
| 13 | + { |
| 14 | + private readonly IServiceProvider _serviceProvider; |
| 15 | + private readonly WebSocketReceiverContext _context; |
| 16 | + private readonly Action<WebSocketReceiverContext> _closeCallback; |
| 17 | + private readonly Action<string> _handshakeCallback; |
| 18 | + private readonly ILogger<ClientWebSocketReceiver> _logger; |
| 19 | + |
| 20 | + public ClientWebSocketReceiver(IServiceProvider serviceProvider, |
| 21 | + WebSocketReceiverContext context, |
| 22 | + Action<WebSocketReceiverContext> closeCallback, |
| 23 | + Action<string> handshakeCallback = null) |
| 24 | + { |
| 25 | + _serviceProvider = serviceProvider; |
| 26 | + _context = context; |
| 27 | + _closeCallback = closeCallback; |
| 28 | + _handshakeCallback = handshakeCallback; |
| 29 | + _logger = context.LoggerFactory.CreateLogger<ClientWebSocketReceiver>(); |
| 30 | + } |
| 31 | + |
| 32 | + public async Task ReceiveAsync() |
| 33 | + { |
| 34 | + try |
| 35 | + { |
| 36 | + var buffer = new byte[NCSConstants.ChunkSize]; |
| 37 | + var result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 38 | + while (!result.CloseStatus.HasValue) |
| 39 | + { |
| 40 | + if (result.MessageType == WebSocketMessageType.Text) |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + var context = result.ToContext(buffer); |
| 45 | + if (context.Command == WebSocketCommands.Handshake) |
| 46 | + { |
| 47 | + _context.ConnectionId = context.Value?.ToString(); |
| 48 | + _handshakeCallback?.Invoke(_context.ConnectionId); |
| 49 | + } |
| 50 | + |
| 51 | + var invocator = _context.GetInvocator(_serviceProvider); |
| 52 | + invocator?.InvokeAsync(context); |
| 53 | + } |
| 54 | + catch (Exception ex) |
| 55 | + { |
| 56 | + _logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Text); |
| 57 | + } |
| 58 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 59 | + } |
| 60 | + |
| 61 | + if (result.MessageType == WebSocketMessageType.Binary) |
| 62 | + { |
| 63 | + byte[] binaryResult = null; |
| 64 | + using (var ms = new MemoryStream()) |
| 65 | + { |
| 66 | + while (!result.EndOfMessage) |
| 67 | + { |
| 68 | + if (!result.CloseStatus.HasValue) |
| 69 | + { |
| 70 | + await ms.WriteAsync(buffer, 0, result.Count); |
| 71 | + } |
| 72 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 73 | + } |
| 74 | + if (result.EndOfMessage) |
| 75 | + { |
| 76 | + if (!result.CloseStatus.HasValue) |
| 77 | + { |
| 78 | + await ms.WriteAsync(buffer, 0, result.Count); |
| 79 | + } |
| 80 | + } |
| 81 | + binaryResult = ms.ToArray(); |
| 82 | + } |
| 83 | + try |
| 84 | + { |
| 85 | + var context = await result.ToBinaryContextAsync(_context.Compressor, binaryResult); |
| 86 | + var invocator = _context.GetInvocator(_serviceProvider); |
| 87 | + invocator?.InvokeAsync(context); |
| 88 | + } |
| 89 | + catch (Exception ex) |
| 90 | + { |
| 91 | + _logger.LogWarning(ex, "{0} Invocator error occurred for message type: {1}", NCSConstants.WarningSymbol, WebSocketMessageType.Binary); |
| 92 | + } |
| 93 | + result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + await _context.WebSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); |
| 98 | + _closeCallback?.Invoke(_context); |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + if (ex is TaskCanceledException) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + var dictionary = new Dictionary<string, string>(); |
| 108 | + dictionary.Add(nameof(_context.ConnectionId), _context.ConnectionId); |
| 109 | + |
| 110 | + if (_context.InvocatorContext != null) |
| 111 | + { |
| 112 | + dictionary.Add(nameof(_context.InvocatorContext.ConnectorName), _context.InvocatorContext.ConnectorName); |
| 113 | + dictionary.Add(nameof(_context.InvocatorContext.Uri), Convert.ToString(_context.InvocatorContext.Uri)); |
| 114 | + } |
| 115 | + |
| 116 | + _logger.LogWarning(ex, "{0} receive exception: {1}", NCSConstants.WarningSymbol, JsonConvert.SerializeObject(dictionary)); |
| 117 | + } |
| 118 | + finally |
| 119 | + { |
| 120 | + _closeCallback?.Invoke(_context); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments