using EventBus; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TcpEventBus { public class TcpAnonymousEventData : IAnonymousEventData,ISubscripData { ConcurrentDictionary> _list = new ConcurrentDictionary>(); private ISocket _Socket; public TcpAnonymousEventData(ISocket socket, string eventName, string hash) { _Socket = socket; EventName = eventName; Hash = hash; } public string EventName { get; } public string Hash { get; } public bool IsResultEvent => false; public bool IsAnonymous => true; public void Clear() { lock (_list) { _list.Clear(); } } public void Publish(object sender, Properties? properties = null, params object[] data) { if (data == null || _Socket == null) return; _Socket.Send(this.GetAnonyMsg(data, properties).GetBytes()); } public Task PublishAsync(object sender, Properties? properties = null, params object[] data) { if (data == null || _Socket == null) return Task.CompletedTask; return Task.Run(()=> _Socket.Send(this.GetAnonyMsg(data, properties).GetBytes())); } public Guid Subscrip(Action> action, Properties? properties = null) { lock (_list) { ArgumentNullException.ThrowIfNull(action); properties ??= Properties.Default; var temp = new ActionValue(action, properties); _list[temp.IntPtr] = temp; return temp.IntPtr; } } public void Subscrip(byte[] data, Properties properties) { lock (_list) { object[] t = MsgTool.GetAnonyDatas(data); var temp = _list.Where(x => x.Value.Properties.FilterRule(properties) && x.Value.HasDelegate).Select(x => x.Value).ToList(); if (temp.Count == 0) return; temp.ForEach(x => x.Action.Invoke(this, new EventArgs(t, x.IntPtr, this))); } } public void SubscripData(byte[] data, Properties properties) { throw new NotImplementedException(); } public void UnSubscrip(Guid guid) { lock (_list) { if (guid == Guid.Empty) return; _list.TryRemove(guid, out _); } } } }