using EventBus; using ICommunication; using System.Runtime.Serialization; namespace LocalCommunication { [CommunicationType(true)] public class Communication : ICommunication.ICommunication { public string IP => eventBus == null ? "" : eventBus.IP; public int Port => eventBus == null ? 0 : eventBus.Port; public bool IsLan { get; } = true; public bool IsConnected => eventBus == null ? false : eventBus.IsConnected; private TcpEventBus.TcpEventBus eventBus; public bool IsService { get; private set; } public event EventHandler Connected; public event EventHandler Disconnected; public void Close() { eventBus?.Close(); } public void Connect(string ip, int port) { IsService = false; eventBus = TcpEventBus.TcpEventBus.CreateClient(ip, port, 5000); eventBus.Connected += (_, _) => this.Connected?.Invoke(this, EventArgs.Empty); eventBus.Disconnected += (_, _) => this.Disconnected?.Invoke(this, EventArgs.Empty); } public IEventData? GetEvent() { return eventBus?.GetEvent(); } public IEventData? GetEvent() { return eventBus?.GetEvent(); } public IAnonymousEventData? GetEvent(string eventName) { return eventBus?.GetEvent(eventName); } public IAnonymousEventData? GetEvent(string eventName) { return eventBus?.GetEvent(eventName); } public void Start() { eventBus?.Start(); } public void StartService(string ip,int port) { IsService = true; eventBus = TcpEventBus.TcpEventBus.CreateService(ip,port, 5000); eventBus.Connected += (_, _) => this.Connected?.Invoke(this, EventArgs.Empty); eventBus.Disconnected += (_, _) => this.Disconnected?.Invoke(this, EventArgs.Empty); } } }