Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Feb 15, 2024
1 parent caba38b commit 23f3c49
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 92 deletions.
1 change: 1 addition & 0 deletions Websocket.Client.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=BBBB/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Bitmex/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Fakely/@EntryIndexedValue">True</s:Boolean>
Expand Down
33 changes: 27 additions & 6 deletions src/Websocket.Client/IWebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,27 @@ public interface IWebsocketClient : IDisposable

/// <summary>
/// Send message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Message to be sent</param>
void Send(string message);
/// <returns>true if the message was written to the queue</returns>
bool Send(string message);

/// <summary>
/// Send binary message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Binary message to be sent</param>
void Send(byte[] message);
/// <returns>true if the message was written to the queue</returns>
bool Send(byte[] message);

/// <summary>
/// Send binary message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Binary message to be sent</param>
void Send(ArraySegment<byte> message);
/// <returns>true if the message was written to the queue</returns>
bool Send(ArraySegment<byte> message);

/// <summary>
/// Send message to the websocket channel.
Expand All @@ -157,6 +160,24 @@ public interface IWebsocketClient : IDisposable
/// <param name="message">Message to be sent</param>
Task SendInstant(byte[] message);

/// <summary>
/// Send already converted text message to the websocket channel.
/// Use this method to avoid double serialization of the text message.
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Message to be sent</param>
/// <returns>true if the message was written to the queue</returns>
bool SendAsText(byte[] message);

/// <summary>
/// Send already converted text message to the websocket channel.
/// Use this method to avoid double serialization of the text message.
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Message to be sent</param>
/// <returns>true if the message was written to the queue</returns>
bool SendAsText(ArraySegment<byte> message);

/// <summary>
/// Force reconnection.
/// Closes current websocket stream and perform a new connection to the server.
Expand Down
104 changes: 21 additions & 83 deletions src/Websocket.Client/WebsocketClient.Sending.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,41 @@ public partial class WebsocketClient

/// <summary>
/// Send text message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Text message to be sent</param>
public void Send(string message)
/// <returns>true if the message was written to the queue</returns>
public bool Send(string message)
{
Validations.Validations.ValidateInput(message, nameof(message));

_messagesTextToSendQueue.Writer.TryWrite(new RequestTextMessage(message));
return _messagesTextToSendQueue.Writer.TryWrite(new RequestTextMessage(message));
}

/// <summary>
/// Send binary message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Binary message to be sent</param>
public void Send(byte[] message)
/// <returns>true if the message was written to the queue</returns>
public bool Send(byte[] message)
{
Validations.Validations.ValidateInput(message, nameof(message));

_messagesBinaryToSendQueue.Writer.TryWrite(new ArraySegment<byte>(message));
return _messagesBinaryToSendQueue.Writer.TryWrite(new ArraySegment<byte>(message));
}

/// <summary>
/// Send binary message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Binary message to be sent</param>
public void Send(ArraySegment<byte> message)
/// <returns>true if the message was written to the queue</returns>
public bool Send(ArraySegment<byte> message)
{
Validations.Validations.ValidateInput(message, nameof(message));

_messagesBinaryToSendQueue.Writer.TryWrite(message);
}

/// <summary>
/// Send text message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// </summary>
/// <param name="message">Text message to be sent</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
public ValueTask SendAsync(string message, CancellationToken cancellationToken = default)
{
Validations.Validations.ValidateInput(message, nameof(message));

return _messagesTextToSendQueue.Writer.WriteAsync(new RequestTextMessage(message), cancellationToken);
}

/// <summary>
/// Send binary message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// </summary>
/// <param name="message">Binary message to be sent</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
public ValueTask SendAsync(byte[] message, CancellationToken cancellationToken = default)
{
Validations.Validations.ValidateInput(message, nameof(message));

return _messagesBinaryToSendQueue.Writer.WriteAsync(new ArraySegment<byte>(message), cancellationToken);
}

/// <summary>
/// Send binary message to the websocket channel.
/// It inserts the message to the queue and actual sending is done on an other thread
/// </summary>
/// <param name="message">Binary message to be sent</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
public ValueTask SendAsync(ArraySegment<byte> message, CancellationToken cancellationToken = default)
{
Validations.Validations.ValidateInput(message, nameof(message));

return _messagesBinaryToSendQueue.Writer.WriteAsync(message, cancellationToken);
return _messagesBinaryToSendQueue.Writer.TryWrite(message);
}

/// <summary>
Expand Down Expand Up @@ -126,62 +90,36 @@ public Task SendInstant(byte[] message)
/// <summary>
/// Send already converted text message to the websocket channel.
/// Use this method to avoid double serialization of the text message.
/// It inserts the message to the queue and actual sending is done on an other thread
/// </summary>
/// <param name="message">Message to be sent</param>
public void SendTextAsBinary(byte[] message)
{
Validations.Validations.ValidateInput(message, nameof(message));

_messagesTextToSendQueue.Writer.TryWrite(new RequestBinaryMessage(message));
}

/// <summary>
/// Send already converted text message to the websocket channel.
/// Use this method to avoid double serialization of the text message.
/// It inserts the message to the queue and actual sending is done on an other thread
/// </summary>
/// <param name="message">Message to be sent</param>
public void SendTextAsBinary(ArraySegment<byte> message)
{
Validations.Validations.ValidateInput(message, nameof(message));

_messagesTextToSendQueue.Writer.TryWrite(new RequestBinarySegmentMessage(message));
}

/// <summary>
/// Send already converted text message to the websocket channel.
/// Use this method to avoid double serialization of the text message.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Message to be sent</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
public ValueTask SendTextAsBinaryAsync(byte[] message, CancellationToken cancellationToken = default)
/// <returns>true if the message was written to the queue</returns>
public bool SendAsText(byte[] message)
{
Validations.Validations.ValidateInput(message, nameof(message));

return _messagesTextToSendQueue.Writer.WriteAsync(new RequestBinaryMessage(message), cancellationToken);
return _messagesTextToSendQueue.Writer.TryWrite(new RequestBinaryMessage(message));
}

/// <summary>
/// Send already converted text message to the websocket channel.
/// Use this method to avoid double serialization of the text message.
/// It inserts the message to the queue and actual sending is done on an other thread
/// It inserts the message to the queue and actual sending is done on another thread
/// </summary>
/// <param name="message">Message to be sent</param>
/// <param name="cancellationToken">The cancellationToken enables graceful cancellation of asynchronous operations</param>
public ValueTask SendTextAsBinaryAsync(ArraySegment<byte> message, CancellationToken cancellationToken = default)
/// <returns>true if the message was written to the queue</returns>
public bool SendAsText(ArraySegment<byte> message)
{
Validations.Validations.ValidateInput(message, nameof(message));

return _messagesTextToSendQueue.Writer.WriteAsync(new RequestBinarySegmentMessage(message), cancellationToken);
return _messagesTextToSendQueue.Writer.TryWrite(new RequestBinarySegmentMessage(message));
}

/// <summary>
/// Stream/publish fake message (via 'MessageReceived' observable).
/// Use for testing purposes to simulate a server message.
/// </summary>
/// <param name="message">Message to be stream</param>
/// <param name="message">Message to be streamed</param>
public void StreamFakeMessage(ResponseMessage message)
{
Validations.Validations.ValidateInput(message, nameof(message));
Expand Down
5 changes: 2 additions & 3 deletions src/Websocket.Client/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ private async Task Listen(WebSocket client, CancellationToken token)
do
{
ValueWebSocketReceiveResult result;
var ms = _memoryStreamManager.GetStream() as RecyclableMemoryStream;
var ms = (RecyclableMemoryStream)_memoryStreamManager.GetStream();

while (true)
{
result = await client.ReceiveAsync(buffer, token);
ms!.Write(buffer[..result.Count].Span);
ms.Write(buffer[..result.Count].Span);

if (result.EndOfMessage)
break;
Expand All @@ -477,7 +477,6 @@ private async Task Listen(WebSocket client, CancellationToken token)
if (result.MessageType == WebSocketMessageType.Text && IsTextMessageConversionEnabled)
{
var data = GetEncoding().GetString(ms.ToArray());

message = ResponseMessage.TextMessage(data);
}
else if (result.MessageType == WebSocketMessageType.Close)
Expand Down

0 comments on commit 23f3c49

Please sign in to comment.