Skip to content

Commit 3ec7b8a

Browse files
committed
Using this optimization also for AspNetCore WebSockets Server
1 parent da4b67f commit 3ec7b8a

File tree

7 files changed

+58
-24
lines changed

7 files changed

+58
-24
lines changed

src/net45/Extensions/WampSharp.AspNetCore.WebSockets.Server/AspNetCoreWebSocketTransport.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override void Dispose()
3333

3434
protected override void OpenConnection<TMessage>(WebSocketData original, IWampConnection<TMessage> connection)
3535
{
36-
WebSocketWrapperConnection<TMessage> casted = connection as WebSocketWrapperConnection<TMessage>;
36+
IWampWebSocketWrapperConnection casted = connection as IWampWebSocketWrapperConnection;
3737

3838
Task task = Task.Run(casted.RunAsync);
3939

@@ -49,6 +49,8 @@ protected override IWampConnection<TMessage> CreateBinaryConnection<TMessage>
4949
(WebSocketData connection,
5050
IWampBinaryBinding<TMessage> binding)
5151
{
52+
ConfigureComputeBytes(binding);
53+
5254
return new BinaryWebSocketConnection<TMessage>
5355
(connection.WebSocket,
5456
binding,
@@ -60,13 +62,23 @@ protected override IWampConnection<TMessage> CreateTextConnection<TMessage>
6062
(WebSocketData connection,
6163
IWampTextBinding<TMessage> binding)
6264
{
65+
ConfigureComputeBytes(binding);
66+
6367
return new TextWebSocketConnection<TMessage>
6468
(connection.WebSocket,
6569
binding,
6670
new AspNetCoreCookieProvider(connection.HttpContext),
6771
AuthenticatorFactory);
6872
}
6973

74+
private void ConfigureComputeBytes<TMessage, TRaw>(IWampTransportBinding<TMessage, TRaw> binding)
75+
{
76+
if (binding.ComputeBytes == null)
77+
{
78+
binding.ComputeBytes = true;
79+
}
80+
}
81+
7082
public override void Open()
7183
{
7284
mHandler = this.WebSocketHandler;

src/net45/Extensions/WampSharp.WebSockets/WebSockets/BinaryWebSocketWrapperConnection.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace WampSharp.WebSockets
88
{
9-
public class BinaryWebSocketWrapperConnection<TMessage> : WebSocketWrapperConnection<TMessage>
9+
public class BinaryWebSocketWrapperConnection<TMessage> : WebSocketWrapperConnection<TMessage, byte[]>
1010
{
1111
private readonly IWampBinaryBinding<TMessage> mBinding;
1212

@@ -22,12 +22,6 @@ protected BinaryWebSocketWrapperConnection(IClientWebSocketWrapper clientWebSock
2222
mBinding = binding;
2323
}
2424

25-
protected override ArraySegment<byte> GetMessageInBytes(WampMessage<object> message)
26-
{
27-
byte[] bytes = mBinding.Format(message);
28-
return new ArraySegment<byte>(bytes);
29-
}
30-
3125
protected override WebSocketMessageType WebSocketMessageType => WebSocketMessageType.Binary;
3226
}
3327
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Threading.Tasks;
2+
3+
namespace WampSharp.WebSockets
4+
{
5+
public interface IWampWebSocketWrapperConnection
6+
{
7+
IClientWebSocketWrapper ClientWebSocket { get; }
8+
Task RunAsync();
9+
}
10+
}

src/net45/Extensions/WampSharp.WebSockets/WebSockets/TextWebSocketWrapperConnection.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace WampSharp.WebSockets
99
{
10-
public class TextWebSocketWrapperConnection<TMessage> : WebSocketWrapperConnection<TMessage>
10+
public class TextWebSocketWrapperConnection<TMessage> : WebSocketWrapperConnection<TMessage, string>
1111
{
1212
private readonly IWampTextBinding<TMessage> mBinding;
1313

@@ -23,15 +23,6 @@ protected TextWebSocketWrapperConnection(IClientWebSocketWrapper clientWebSocket
2323
mBinding = binding;
2424
}
2525

26-
protected override ArraySegment<byte> GetMessageInBytes(WampMessage<object> message)
27-
{
28-
string formatted = mBinding.Format(message);
29-
30-
byte[] bytes = Encoding.UTF8.GetBytes(formatted);
31-
32-
return new ArraySegment<byte>(bytes);
33-
}
34-
3526
protected override WebSocketMessageType WebSocketMessageType => WebSocketMessageType.Text;
3627
}
3728
}

src/net45/Extensions/WampSharp.WebSockets/WebSockets/WebSocketWrapperConnection.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace WampSharp.WebSockets
1313
{
1414
// Based on this sample:
1515
// https://code.msdn.microsoft.com/vstudio/The-simple-WebSocket-4524921c
16-
public abstract class WebSocketWrapperConnection<TMessage> : AsyncWebSocketWampConnection<TMessage>
16+
public abstract class WebSocketWrapperConnection<TMessage, TRaw> : AsyncWebSocketWampConnection<TMessage>, IWampWebSocketWrapperConnection
1717
{
18-
private readonly IWampStreamingMessageParser<TMessage> mParser;
18+
private readonly IWampMessageParser<TMessage, TRaw> mParser;
1919
private readonly IWebSocketWrapper mWebSocket;
2020
private CancellationTokenSource mCancellationTokenSource;
2121
private readonly Uri mAddressUri;
2222
private readonly CancellationToken mCancellationToken;
2323

24-
public WebSocketWrapperConnection(IWebSocketWrapper webSocket, IWampStreamingMessageParser<TMessage> parser, ICookieProvider cookieProvider, ICookieAuthenticatorFactory cookieAuthenticatorFactory) :
24+
public WebSocketWrapperConnection(IWebSocketWrapper webSocket, IWampMessageParser<TMessage, TRaw> parser, ICookieProvider cookieProvider, ICookieAuthenticatorFactory cookieAuthenticatorFactory) :
2525
base(cookieProvider, cookieAuthenticatorFactory)
2626
{
2727
mWebSocket = webSocket;
@@ -30,7 +30,7 @@ public WebSocketWrapperConnection(IWebSocketWrapper webSocket, IWampStreamingMes
3030
mCancellationToken = mCancellationTokenSource.Token;
3131
}
3232

33-
protected WebSocketWrapperConnection(IClientWebSocketWrapper clientWebSocket, Uri addressUri, string protocolName, IWampStreamingMessageParser<TMessage> parser) :
33+
protected WebSocketWrapperConnection(IClientWebSocketWrapper clientWebSocket, Uri addressUri, string protocolName, IWampMessageParser<TMessage, TRaw> parser) :
3434
this(clientWebSocket, parser, null, null)
3535
{
3636
clientWebSocket.Options.AddSubProtocol(protocolName);
@@ -44,7 +44,11 @@ protected override Task SendAsync(WampMessage<object> message)
4444
return mWebSocket.SendAsync(messageToSend, WebSocketMessageType, true, mCancellationToken);
4545
}
4646

47-
protected abstract ArraySegment<byte> GetMessageInBytes(WampMessage<object> message);
47+
private ArraySegment<byte> GetMessageInBytes(WampMessage<object> message)
48+
{
49+
byte[] bytes = mParser.GetBytes(message);
50+
return new ArraySegment<byte>(bytes);
51+
}
4852

4953
protected abstract WebSocketMessageType WebSocketMessageType { get; }
5054

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using WampSharp.Core.Message;
2+
using WampSharp.V2.Binding.Messages;
3+
4+
namespace WampSharp.V2.Binding.Parsers
5+
{
6+
public static class WampMessageParserExtensions
7+
{
8+
public static byte[] GetBytes<TMessage, TRaw>(this IWampMessageParser<TMessage, TRaw> parser,
9+
WampMessage<object> message)
10+
{
11+
if (message is RawMessage<TRaw> rawMessage &&
12+
rawMessage.Bytes != null)
13+
{
14+
return rawMessage.Bytes;
15+
}
16+
else
17+
{
18+
TRaw raw = parser.Format(message);
19+
byte[] binary = parser.GetBytes(raw);
20+
return binary;
21+
}
22+
}
23+
}
24+
}

src/net45/WampSharp/WAMP2/V2/Binding/WampTransportBinding.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public override WampMessage<object> GetRawMessage(WampMessage<object> message)
4545

4646
private RawMessage<TRaw> GetFormattedMessage(WampMessage<object> message)
4747
{
48-
4948
if (!(message is RawMessage<TRaw> result))
5049
{
5150
result = new RawMessage<TRaw>(message);

0 commit comments

Comments
 (0)