Kaynağa Gözat

remove redundant code

arthur.wu 2 yıl önce
ebeveyn
işleme
aa49f0ef4e

+ 0 - 62
Adapter/MessageAdapter.cs

@@ -1,62 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using WingInterfaceLibrary.Enum.NotificationEnum;
-using WingInterfaceLibrary.Request.Notification;
-
-namespace WingNotificationModule.Adapter
-{
-    public class MessageAdapter
-    {
-        private Memory<byte> _memoryData;
-        private int _spanIndex = 0;
-        public MessageAdapter(Memory<byte> data)
-        {
-            _memoryData = data;
-        }
-
-        public List<NotifyMessage> GetNotifyMessages()
-        {
-            List<NotifyMessage> notifies = new();
-            while (_spanIndex < _memoryData.Length)
-            {
-                try
-                {
-                    notifies.Add(new NotifyMessage
-                    {
-                        MessageType = GetMessageType(),
-                        NotificationType = GetNotificationType(),
-                        Message = GetMessage()
-                    });
-                }
-                catch (Exception ex)
-                {
-                    Console.WriteLine($"{ex}");
-                }
-            }
-            return notifies;
-        }
-
-        public MessageTypeEnum GetMessageType()
-        {
-            var msgTypeByte = _memoryData.Span[0];
-            return (MessageTypeEnum)msgTypeByte;
-        }
-
-        public NotificationTypeEnum GetNotificationType()
-        {
-            var notifyByte = _memoryData.Span[1];
-            return (NotificationTypeEnum)notifyByte;
-        }
-
-        private string GetMessage()
-        {
-            var messageLength = BitConverter.ToInt32(_memoryData.Span.Slice(2,4));
-            var msgBytes = _memoryData.Span.Slice(6, messageLength);
-            _spanIndex = 6 + messageLength;
-            //后期加上压缩
-            var msgStr = Encoding.UTF8.GetString(msgBytes);
-            return msgStr;
-        }
-    }
-}

+ 0 - 22
Adapter/MessageContentAdapter.cs

@@ -1,22 +0,0 @@
-using System;
-using System.Text;
-
-namespace WingNotificationModule.Adapter
-{
-    public class MessageContentAdapter
-    {
-        private Memory<byte> _memoryData;
-        public MessageContentAdapter(Memory<byte> data)
-        {
-            _memoryData = data;
-        }
-
-        public string GetMessageContent()
-        {
-            var msgBytes = _memoryData.Span;
-            //后期加上压缩
-            var msgStr = Encoding.UTF8.GetString(msgBytes);
-            return msgStr;
-        }
-    }
-}

+ 0 - 26
Adapter/MessageHeadAdapter.cs

@@ -1,26 +0,0 @@
-using System;
-using WingInterfaceLibrary.Enum.NotificationEnum;
-
-namespace WingNotificationModule.Adapter
-{
-    public class MessageHeadAdapter
-    {
-        private Memory<byte> _memoryData;
-        public MessageHeadAdapter(Memory<byte> data)
-        {
-            _memoryData = data;
-        }
-
-        public MessageTypeEnum GetMessageType()
-        {
-            var msgTypeByte = _memoryData.Span[0];
-            return (MessageTypeEnum)msgTypeByte;
-        }
-
-        public NotificationTypeEnum GetNotificationType()
-        {
-            var notifyByte = _memoryData.Span[1];
-            return (NotificationTypeEnum)notifyByte;
-        }
-    }
-}

+ 0 - 18
Adapter/MessageLengthAdapter.cs

@@ -1,18 +0,0 @@
-using System;
-
-namespace WingNotificationModule.Adapter
-{
-    public class MessageLengthAdapter
-    {
-        private Memory<byte> _memoryData;
-        public MessageLengthAdapter(Memory<byte> data)
-        {
-            _memoryData = data;
-        }
-
-        public int GetLength()
-        {
-            return BitConverter.ToInt32(_memoryData.Span);
-        }
-    }
-}

+ 0 - 16
Channel/IChannel.cs

@@ -1,16 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using WingInterfaceLibrary.Request.Notification;
-
-namespace WingNotificationModule.Channel
-{
-    /// <summary>
-    /// 信道
-    /// </summary>
-    public interface IChannel : IDisposable
-    {
-        Task<List<NotifyMessage>> GetMessageAsync();
-        Task<bool> SendAsync(NotifyMessage message, string clientId = null);
-    }
-}

+ 0 - 56
Channel/WebSocket/ApplyWSToken.cs

@@ -1,56 +0,0 @@
-using System;
-using System.Collections.Concurrent;
-using System.Linq;
-
-namespace WingNotificationModule.Channel.WebSocket
-{
-    public class ApplyWSToken : IApplyToken
-    {
-        private ConcurrentDictionary<string, WSTokenInfo> _tokenDic = new();
-
-        /// <summary>
-        /// Generate token
-        /// </summary>
-        /// <param name="clientIpAndPort"></param>
-        /// <param name="clientDeviceId"></param>
-        /// <param name="clientSource"></param>
-        /// <returns>token</returns>
-        public string GenerateToken(string clientIpAndPort, string clientDeviceId, string clientSource)
-        {
-            string token = GetTokenByDeviceId(clientDeviceId);
-            if (string.IsNullOrWhiteSpace(token))
-            {
-                token = Guid.NewGuid().ToString("N");
-                var tokenInfo = new WSTokenInfo
-                {
-                    Token = token,
-                    ClientSource = clientSource,
-                    ClientDeviceId = clientDeviceId,
-                    ClientIpAndPort = clientIpAndPort
-                };
-                _tokenDic.AddOrUpdate(token, tokenInfo, (k, v) => tokenInfo);
-            }
-            return token;
-        }
-
-        /// <summary>
-        /// Get token by device id
-        /// </summary>
-        /// <param name="clientDeviceId"></param>
-        /// <returns></returns>
-        public string GetTokenByDeviceId(string clientDeviceId)
-        {
-            return _tokenDic.Values.FirstOrDefault(ws => ws.ClientDeviceId == clientDeviceId)?.Token;
-        }
-
-        /// <summary>
-        /// Validate token
-        /// </summary>
-        /// <param name="token"></param>
-        /// <returns></returns>
-        public bool ValidateToken(string token)
-        {
-            return _tokenDic.TryGetValue(token, out _);
-        }
-    }
-}

+ 0 - 9
Channel/WebSocket/IApplyToken.cs

@@ -1,9 +0,0 @@
-namespace WingNotificationModule.Channel.WebSocket
-{
-    public interface IApplyToken
-    {
-        string GetTokenByDeviceId(string clientDeviceId);
-
-        bool ValidateToken(string token);
-    }
-}

+ 0 - 25
Channel/WebSocket/WSTokenInfo.cs

@@ -1,25 +0,0 @@
-namespace WingNotificationModule.Channel.WebSocket
-{
-    public class WSTokenInfo
-    {
-        /// <summary>
-        /// Token value
-        /// </summary>
-        public string Token { get; set; }
-
-        /// <summary>
-        /// Client ip and port
-        /// </summary>
-        public string ClientIpAndPort { get; set; }
-
-        /// <summary>
-        /// Client device id
-        /// </summary>
-        public string ClientDeviceId { get; set; }
-
-        /// <summary>
-        /// Client source
-        /// </summary>
-        public string ClientSource { get; set; }
-    }
-}

+ 0 - 70
Channel/WebSocket/WebSocketClient.cs

@@ -1,70 +0,0 @@
-using System;
-using System.Net.WebSockets;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace WingNotificationModule.Channel.WebSocket
-{
-    public class WebSocketClient : IDisposable
-    {
-        private string _serverUrl = string.Empty;
-        private WebSocketProcessor _webSocketProcessor;
-        public WebSocketClient(string serverUrl)
-        {
-            _serverUrl = serverUrl;
-        }
-
-        /// <summary>
-        /// IsConnection state 
-        /// </summary>
-        public bool IsDisconnected => _webSocketProcessor.IsDisconnected;
-
-        /// <summary>
-        /// Connect to websocket server
-        /// </summary>
-        public async Task ConnectAsync()
-        {
-            var clientWebSocket = new ClientWebSocket();
-            await clientWebSocket.ConnectAsync(new Uri(_serverUrl), CancellationToken.None);
-            _webSocketProcessor = new WebSocketProcessor(clientWebSocket, ()=> {
-                clientWebSocket.Dispose();
-            });
-        }
-
-        /// <summary>
-        /// Disconnect websocket server
-        /// </summary>
-        public void Disconnect()
-        {
-            _webSocketProcessor?.Dispose();
-        }
-
-        /// <summary>
-        /// Dispose
-        /// </summary>
-        public void Dispose()
-        {
-            _webSocketProcessor?.Dispose();
-        }
-
-        /// <summary>
-        /// Receive data
-        /// </summary>
-        /// <returns></returns>
-        public async Task<WebSocketMessage> ReceiveAsync()
-        {
-            return await _webSocketProcessor?.ReceiveAsync();
-        }
-
-        /// <summary>
-        /// Send data
-        /// </summary>
-        /// <param name="data"></param>
-        /// <param name="messageType"></param>
-        /// <returns></returns>
-        public async Task SendAsync(byte[] data, WebSocketMessageType messageType)
-        {
-            await _webSocketProcessor?.SendAsync(data, messageType);
-        }
-    }
-}

+ 0 - 75
Channel/WebSocket/WebSocketProcessor.cs

@@ -1,75 +0,0 @@
-using System;
-using System.Net.WebSockets;
-using System.Threading.Tasks;
-
-namespace WingNotificationModule.Channel.WebSocket
-{
-    /// <summary>
-    /// WebSocket process
-    /// </summary>
-    public class WebSocketProcessor : IDisposable
-    {
-        private WebSocketIO _webSocketIO;
-        private Action _disconnecting;
-
-        /// <summary>
-        /// IsConnection state 
-        /// </summary>
-        public bool IsDisconnected => _webSocketIO.IsDisconnected;
-
-        public WebSocketProcessor(System.Net.WebSockets.WebSocket webSocket, Action disconnecting)
-        {
-            _webSocketIO = new WebSocketIO(webSocket);
-            _disconnecting = disconnecting;
-        }
-
-        /// <summary>
-        /// Receive data
-        /// </summary>
-        /// <returns></returns>
-        public async Task<WebSocketMessage> ReceiveAsync()
-        {
-            if (_webSocketIO.IsDisconnected) 
-            { 
-                _disconnecting?.Invoke();
-                throw new Exception("WebSocket is closed");
-            }
-            return await _webSocketIO.ReceiveAsync();
-        }
-
-        /// <summary>
-        /// Receive data
-        /// </summary>
-        /// <returns></returns>
-        public async Task<WebSocketMessage> ReceiveAsync(int readSize)
-        {
-            if (_webSocketIO.IsDisconnected) 
-            { 
-                _disconnecting?.Invoke();
-                throw new Exception("WebSocket is closed");
-            }
-            return await _webSocketIO.ReceiveAsync(readSize);
-        }
-
-        /// <summary>
-        /// Send data
-        /// </summary>
-        /// <param name="data"></param>
-        /// <param name="messageType"></param>
-        /// <returns></returns>
-        public async Task SendAsync(byte[] data, WebSocketMessageType messageType)
-        {
-            if (_webSocketIO.IsDisconnected)
-            {
-                _disconnecting?.Invoke();
-                throw new Exception("WebSocket is closed");
-            }
-            await _webSocketIO.SendAsync(data, messageType);
-        }
-
-        public void Dispose()
-        {
-            _webSocketIO.Dispose();
-        }
-    }
-}

+ 0 - 78
Channel/WebSocketClientChannel.cs

@@ -1,78 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Net;
-using System.Threading.Tasks;
-using WingInterfaceLibrary.Request.Notification;
-using WingNotificationModule.Adapter;
-using WingNotificationModule.Channel.WebSocket;
-
-namespace WingNotificationModule.Channel
-{
-    public class WebSocketClientChannel : IChannel
-    {
-        private WebSocketClient _webSocketClient;
-        private string _reqToken = Guid.NewGuid().ToString("N");
-
-        public WebSocketClientChannel(string token)
-        {
-            _reqToken = token;
-        }
-
-        public async Task<bool> ConnectAsync(IPEndPoint serverEndpoint)
-        {
-            _webSocketClient = new WebSocketClient
-            (
-                $"ws://{serverEndpoint.Address.ToString()}:{serverEndpoint.Port}?Token={_reqToken}"
-            );
-            await _webSocketClient.ConnectAsync();
-            return true;
-        }
-
-        public async Task<bool> DisconnectAsync()
-        {
-            _webSocketClient?.Disconnect();
-            return await Task.FromResult(true);
-        }
-
-        public void Dispose()
-        {
-            _webSocketClient?.Dispose();
-        }
-
-        public async Task<List<NotifyMessage>> GetMessageAsync()
-        {
-            if(_webSocketClient!=null)
-            {
-                var receiveData = await _webSocketClient.ReceiveAsync();
-                var buffer = receiveData.Buffer;
-                var adapter = new MessageAdapter(buffer);
-                return await Task.FromResult(adapter.GetNotifyMessages());
-            }
-            return await Task.FromResult(new List<NotifyMessage>());
-        }
-
-        public async Task<bool> SendAsync(NotifyMessage message, string clientId = null)
-        {
-            var adapter = new BufferAdapter(message);
-            var buffer = adapter.GetMessageBuffer();
-            if(_webSocketClient!=null)
-            {
-                await _webSocketClient.SendAsync(buffer, System.Net.WebSockets.WebSocketMessageType.Binary);
-            }
-            return true;
-        }
-
-        class SimpleApplyToken : IApplyToken
-        {
-            public string GetTokenByDeviceId(string clientDeviceId)
-            {
-                throw new NotImplementedException();
-            }
-
-            public bool ValidateToken(string token)
-            {
-                return !string.IsNullOrWhiteSpace(token);
-            }
-        }
-    }
-}