-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunicatorBase.cs
More file actions
123 lines (101 loc) · 3.82 KB
/
CommunicatorBase.cs
File metadata and controls
123 lines (101 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using CommsLIB.Base;
using CommsLIB.Communications.FrameWrappers;
using CommsLIB.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace CommsLIB.Communications
{
public static class CommunicatorFactory
{
public static CommunicatorBase<T> CreateCommunicator<T>(ConnUri uri, FrameWrapperBase<T> frameWrapper, bool circular = false)
{
CommunicatorBase<T> c = null ;
switch (uri.UriType)
{
case ConnUri.TYPE.TCP:
c = new TCPNETCommunicator<T>(frameWrapper, circular);
break;
case ConnUri.TYPE.UDP:
c = new UDPNETCommunicator<T>(frameWrapper, circular);
break;
}
return c;
}
}
public abstract class CommunicatorBase<T> : ICommunicator
{
public event DataReadyEventHandler DataReadyEvent;
public event ConnectionStateDelegate ConnectionStateEvent;
public event DataRateDelegate DataRateEvent;
protected readonly ILogger<CommunicatorBase<T>> logger;
public CommunicatorBase()
{
logger = this.GetLogger();
}
public enum STATE
{
RUNNING,
STOP
}
public STATE State;
public ConnUri CommsUri { get; protected set; }
public ushort[] IpChunks { get; protected set; } = new ushort[4];
public string ID { get; set; }
public bool WaitForAnswer { get; set; }
public abstract void Init(ConnUri uri, bool persistent, string ID, int inactivityMS, int sendGAP = 0);
public abstract void Start();
public abstract Task Stop();
public abstract void SendASync(byte[] bytes, int length);
public abstract bool SendSync(byte[] bytes, int offset, int length);
public abstract void SendSync(T protoBufMessage);
public abstract void SendASync(T protoBufMessage);
public abstract FrameWrapperBase<T> FrameWrapper { get; }
public virtual void FireDataEvent(string ip, int port, long time, byte[] bytes, int offset, int length, string ID, ushort[] ipChunks = null)
{
DataReadyEvent?.Invoke(ip, port, time, bytes, offset, length, ID, ipChunks);
}
public virtual void FireConnectionEvent(string ID, ConnUri uri, bool connected)
{
ConnectionStateEvent?.Invoke(ID, uri, connected);
}
public virtual void FireDataRateEvent(string ID, float dataRateMbpsRX, float dataRateMbpsTX)
{
DataRateEvent?.Invoke(ID, dataRateMbpsRX, dataRateMbpsTX);
}
protected virtual void SetIPChunks(string _ip)
{
string[] chunks = _ip.Split('.');
if (chunks.Length == 4)
for (int i = 0; i < 4; i++)
IpChunks[i] = ushort.Parse(chunks[i]);
}
public void UnsubscribeEventHandlers()
{
if (DataReadyEvent != null)
foreach (var d in DataReadyEvent.GetInvocationList())
DataReadyEvent -= (d as DataReadyEventHandler);
if (ConnectionStateEvent != null)
foreach (var d in ConnectionStateEvent.GetInvocationList())
ConnectionStateEvent -= (d as ConnectionStateDelegate);
}
#region IDisposable Support
protected bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
UnsubscribeEventHandlers();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
}