12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 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<TData>? GetEvent<TData>()
- {
- return eventBus?.GetEvent<TData>();
- }
- public IEventData<TData, T>? GetEvent<TData, T>()
- {
- return eventBus?.GetEvent<TData, T>();
- }
- public IAnonymousEventData? GetEvent(string eventName)
- {
- return eventBus?.GetEvent(eventName);
- }
- public IAnonymousEventData<T>? GetEvent<T>(string eventName)
- {
- return eventBus?.GetEvent<T>(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);
- }
- }
- }
|