Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SocketIO #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ElectronSharp.API/BridgeConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private static void EnsureSocketTaskIsCreated()
{
var socket = new SocketIO($"http://localhost:{BridgeSettings.SocketPort}", new SocketIOOptions()
{
EIO = 4,
EIO = EngineIO.V4,
Reconnection = true,
ReconnectionAttempts = int.MaxValue,
ReconnectionDelay = 500,
Expand Down
8 changes: 8 additions & 0 deletions ElectronSharp.API/SocketIO/EngineIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SocketIOClient
{
public enum EngineIO
{
V3 = 3,
V4 = 4
}
}
9 changes: 9 additions & 0 deletions ElectronSharp.API/SocketIO/Exceptions/ConnectionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SocketIOClient
{
public class ConnectionException : Exception
{
public ConnectionException(string message, Exception innerException) : base(message, innerException) { }
}
}
11 changes: 11 additions & 0 deletions ElectronSharp.API/SocketIO/Exceptions/TransportException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SocketIOClient.Transport
{
public class TransportException : Exception
{
public TransportException() : base() { }
public TransportException(string message) : base(message) { }
public TransportException(string message, Exception innerException) : base(message, innerException) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ internal static class CancellationTokenSourceExtensions
{
public static void TryDispose(this CancellationTokenSource cts)
{
cts?.Dispose();
try
{
cts?.Dispose();
}
catch
{
//Ignore
}
}

public static void TryCancel(this CancellationTokenSource cts)
{
cts?.Cancel();
if (cts != null && !cts.IsCancellationRequested)
{
cts.TryCancel();
}
}
}
}
21 changes: 21 additions & 0 deletions ElectronSharp.API/SocketIO/Extensions/EventHandlerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace SocketIOClient.Extensions
{
Expand All @@ -13,5 +14,25 @@ public static void TryInvoke(this EventHandler handler, object sender, EventArgs
{
handler?.Invoke(sender, args);
}

public static void TryInvoke<T>(this Action<T> action, T arg1)
{
action?.Invoke(arg1);
}

public static async Task TryInvokeAsync<T>(this Func<T, Task> func, T arg1)
{
if (func is null)
{
return;
}

var task = func(arg1);

if(task is not null)
{
await task;
}
}
}
}
4 changes: 2 additions & 2 deletions ElectronSharp.API/SocketIO/Messages/BinaryMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace SocketIOClient.Messages
{
public class BinaryMessage : IMessage
public class BinaryMessage : IJsonMessage
{
public MessageType Type => MessageType.BinaryMessage;

Expand All @@ -21,7 +21,7 @@ public class BinaryMessage : IMessage

public int BinaryCount { get; set; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions ElectronSharp.API/SocketIO/Messages/ClientAckMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SocketIOClient.Messages
/// <summary>
/// The server calls the client's callback
/// </summary>
public class ClientAckMessage : IMessage
public class ClientAckMessage : IJsonMessage
{
public MessageType Type => MessageType.AckMessage;

Expand All @@ -29,7 +29,7 @@ public class ClientAckMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions ElectronSharp.API/SocketIO/Messages/ClientBinaryAckMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SocketIOClient.Messages
/// <summary>
/// The server calls the client's callback with binary
/// </summary>
public class ClientBinaryAckMessage : IMessage
public class ClientBinaryAckMessage : IJsonMessage
{
public MessageType Type => MessageType.BinaryAckMessage;

Expand All @@ -25,7 +25,7 @@ public class ClientBinaryAckMessage : IMessage

public int BinaryCount { get; set; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
37 changes: 26 additions & 11 deletions ElectronSharp.API/SocketIO/Messages/ConnectedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.ObjectPool;

namespace SocketIOClient.Messages
{
Expand All @@ -20,7 +21,7 @@ public class ConnectedMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand All @@ -29,7 +30,7 @@ public class ConnectedMessage : IMessage

public void Read(string msg)
{
if (Eio == 3)
if (EIO == EngineIO.V3)
{
Eio3Read(msg);
}
Expand All @@ -41,14 +42,14 @@ public void Read(string msg)

public string Write()
{
if (Eio == 3)
if (EIO == EngineIO.V3)
{
return Eio3Write();
}
return Eio4Write();
}

public void Eio4Read(string msg)
private void Eio4Read(string msg)
{
int index = msg.IndexOf('{');
if (index > 0)
Expand All @@ -63,18 +64,26 @@ public void Eio4Read(string msg)
Sid = JsonDocument.Parse(msg).RootElement.GetProperty("sid").GetString();
}

public string Eio4Write()
private static ObjectPool<StringBuilder> _sbPool = new DefaultObjectPool<StringBuilder>(new StringBuilderPooledObjectPolicy());

private string Eio4Write()
{
var builder = new StringBuilder("40");
var builder = _sbPool.Get();
builder.Append("40");

if (!string.IsNullOrEmpty(Namespace))
{
builder.Append(Namespace).Append(',');
}

builder.Append(AuthJsonStr);
return builder.ToString();

var final = builder.ToString();
_sbPool.Return(builder);
return final;
}

public void Eio3Read(string msg)
private void Eio3Read(string msg)
{
if (msg.Length >= 2)
{
Expand All @@ -96,14 +105,18 @@ public void Eio3Read(string msg)
}
}

public string Eio3Write()
private string Eio3Write()
{
if (string.IsNullOrEmpty(Namespace))
{
return string.Empty;
}
var builder = new StringBuilder("40");

var builder = _sbPool.Get();
builder.Append("40");

builder.Append(Namespace);

if (Query != null)
{
int i = -1;
Expand All @@ -122,7 +135,9 @@ public string Eio3Write()
}
}
builder.Append(',');
return builder.ToString();
var final = builder.ToString();
_sbPool.Return(builder);
return final;
}
}
}
2 changes: 1 addition & 1 deletion ElectronSharp.API/SocketIO/Messages/DisconnectedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DisconnectedMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions ElectronSharp.API/SocketIO/Messages/ErrorMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class ErrorMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

public void Read(string msg)
{
if (Eio == 3)
if (EIO == EngineIO.V3)
{
Message = msg.Trim('"');
}
Expand Down
4 changes: 2 additions & 2 deletions ElectronSharp.API/SocketIO/Messages/EventMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace SocketIOClient.Messages
{
public class EventMessage : IMessage
public class EventMessage : IJsonMessage
{
public MessageType Type => MessageType.EventMessage;

Expand All @@ -25,7 +25,7 @@ public class EventMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
10 changes: 10 additions & 0 deletions ElectronSharp.API/SocketIO/Messages/IJsonMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Text.Json;

namespace SocketIOClient.Messages
{
public interface IJsonMessage : IMessage
{
List<JsonElement> JsonElements { get; }
}
}
2 changes: 1 addition & 1 deletion ElectronSharp.API/SocketIO/Messages/IMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IMessage

int BinaryCount { get; }

int Eio { get; set; }
EngineIO EIO { get; set; }

TransportProtocol Protocol { get; set; }

Expand Down
8 changes: 4 additions & 4 deletions ElectronSharp.API/SocketIO/Messages/MessageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static IMessage CreateMessage(MessageType type)

private static readonly Dictionary<string, MessageType> _messageTypes = Enum.GetValues<MessageType>().ToDictionary(v => ((int)v).ToString(), v => v);

public static IMessage CreateMessage(int eio, string msg)
public static IMessage CreateMessage(EngineIO eio, string msg)
{
foreach (var (prefix,item) in _messageTypes)
{
Expand All @@ -46,7 +46,7 @@ public static IMessage CreateMessage(int eio, string msg)
IMessage result = CreateMessage(item);
if (result != null)
{
result.Eio = eio;
result.EIO = eio;
result.Read(msg.Substring(prefix.Length));
return result;
}
Expand All @@ -60,12 +60,12 @@ public static OpenedMessage CreateOpenedMessage(string msg)
var openedMessage = new OpenedMessage();
if (msg[0] == '0')
{
openedMessage.Eio = 4;
openedMessage.EIO = EngineIO.V4;
openedMessage.Read(msg.Substring(1));
}
else
{
openedMessage.Eio = 3;
openedMessage.EIO = EngineIO.V3;
int index = msg.IndexOf(':');
openedMessage.Read(msg.Substring(index + 2));
}
Expand Down
2 changes: 1 addition & 1 deletion ElectronSharp.API/SocketIO/Messages/OpenedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class OpenedMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion ElectronSharp.API/SocketIO/Messages/PingMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PingMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion ElectronSharp.API/SocketIO/Messages/PongMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PongMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion ElectronSharp.API/SocketIO/Messages/ServerAckMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ServerAckMessage : IMessage

public int BinaryCount { get; }

public int Eio { get; set; }
public EngineIO EIO { get; set; }

public TransportProtocol Protocol { get; set; }

Expand Down
Loading