diff --git a/CryptoExchange.Net/BaseSocket.cs b/CryptoExchange.Net/BaseSocket.cs new file mode 100644 index 0000000..615d185 --- /dev/null +++ b/CryptoExchange.Net/BaseSocket.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Security.Authentication; +using System.Threading.Tasks; +using CryptoExchange.Net.Interfaces; +using SuperSocket.ClientEngine.Proxy; +using WebSocket4Net; + +namespace CryptoExchange.Net +{ + public class BaseSocket: IWebsocket + { + protected WebSocket socket; + + protected readonly List> errorhandlers = new List>(); + protected readonly List openhandlers = new List(); + protected readonly List closehandlers = new List(); + protected readonly List> messagehandlers = new List>(); + + public bool IsClosed => socket.State == WebSocketState.Closed; + public bool IsOpen => socket.State == WebSocketState.Open; + + public BaseSocket(string url):this(url, new Dictionary(), new Dictionary()) + { + } + + public BaseSocket(string url, IDictionary cookies, IDictionary headers) + { + socket = new WebSocket(url, cookies: cookies.ToList(), customHeaderItems: headers.ToList()); + socket.Opened += (o, s) => Handle(openhandlers); + socket.Closed += (o, s) => Handle(closehandlers); + socket.Error += (o, s) => Handle(errorhandlers, s.Exception); + socket.MessageReceived += (o, s) => Handle(messagehandlers, s.Message); + } + + public event Action OnClose + { + add => closehandlers.Add(value); + remove => closehandlers.Remove(value); + } + public event Action OnMessage + { + add => messagehandlers.Add(value); + remove => messagehandlers.Remove(value); + } + public event Action OnError + { + add => errorhandlers.Add(value); + remove => errorhandlers.Remove(value); + } + public event Action OnOpen + { + add => openhandlers.Add(value); + remove => closehandlers.Remove(value); + } + + protected static void Handle(List handlers) + { + foreach (var handle in new List(handlers)) + handle(); + } + + protected void Handle(List> handlers, T data) + { + foreach (var handle in new List>(handlers)) + handle(data); + } + + public void Close() + { + socket.Close(); + } + + public void Send(string data) + { + socket.Send(data); + } + + public async Task Connect() + { + return await socket.OpenAsync().ConfigureAwait(false); + } + + public void SetEnabledSslProtocols(SslProtocols protocols) + { + socket.Security.EnabledSslProtocols = protocols; + } + + public void SetProxy(string host, int port) + { + IPAddress address; + socket.Proxy = IPAddress.TryParse(host, out address) + ? new HttpConnectProxy(new IPEndPoint(address, port)) + : new HttpConnectProxy(new DnsEndPoint(host, port)); + } + } +} diff --git a/CryptoExchange.Net/CryptoExchange.Net.csproj b/CryptoExchange.Net/CryptoExchange.Net.csproj index 0d73483..aae0d08 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.csproj +++ b/CryptoExchange.Net/CryptoExchange.Net.csproj @@ -7,7 +7,7 @@ CryptoExchange.Net JKorf - 0.0.6 + 0.0.7 false https://github.com/JKorf/CryptoExchange.Net https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE @@ -17,6 +17,7 @@ + diff --git a/CryptoExchange.Net/Interfaces/IWebsocket.cs b/CryptoExchange.Net/Interfaces/IWebsocket.cs new file mode 100644 index 0000000..a81d90d --- /dev/null +++ b/CryptoExchange.Net/Interfaces/IWebsocket.cs @@ -0,0 +1,24 @@ +using System; +using System.Security.Authentication; +using System.Threading.Tasks; + +namespace CryptoExchange.Net.Interfaces +{ + public interface IWebsocket + { + void SetEnabledSslProtocols(SslProtocols protocols); + + event Action OnClose; + event Action OnMessage; + event Action OnError; + event Action OnOpen; + + bool IsClosed { get; } + bool IsOpen { get; } + + Task Connect(); + void Send(string data); + void Close(); + void SetProxy(string host, int port); + } +} diff --git a/CryptoExchange.Net/Interfaces/IWebsocketFactory.cs b/CryptoExchange.Net/Interfaces/IWebsocketFactory.cs new file mode 100644 index 0000000..2858af7 --- /dev/null +++ b/CryptoExchange.Net/Interfaces/IWebsocketFactory.cs @@ -0,0 +1,7 @@ +namespace CryptoExchange.Net.Interfaces +{ + public interface IWebsocketFactory + { + IWebsocket CreateWebsocket(string url); + } +} diff --git a/CryptoExchange.Net/WebsocketFactory.cs b/CryptoExchange.Net/WebsocketFactory.cs new file mode 100644 index 0000000..0ac3af1 --- /dev/null +++ b/CryptoExchange.Net/WebsocketFactory.cs @@ -0,0 +1,12 @@ +using CryptoExchange.Net.Interfaces; + +namespace CryptoExchange.Net +{ + public class WebsocketFactory : IWebsocketFactory + { + public IWebsocket CreateWebsocket(string url) + { + return new BaseSocket(url); + } + } +}