-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpLikeTcpSecureTransportAdapter.cs
More file actions
43 lines (38 loc) · 1.22 KB
/
UdpLikeTcpSecureTransportAdapter.cs
File metadata and controls
43 lines (38 loc) · 1.22 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
using M9Studio.SecureStream;
using M9Studio.UdpLikeTcp;
using System.Net;
namespace M9Studio.ShadowTalk.Core
{
public class UdpLikeTcpSecureTransportAdapter : ISecureTransportAdapter<IPEndPoint>
{
public Socket _socket = new();
Dictionary<IPEndPoint, int> iPEndPoints = new();
public event Action<IPEndPoint> OnConnected;
public event Action<IPEndPoint> OnDisconnected;
public UdpLikeTcpSecureTransportAdapter() => _socket.OnConnected += Connected;
private void Connected(IPEndPoint sender)
{
iPEndPoints.Add(sender, 5);
OnConnected?.Invoke(sender);
}
public byte[] ReceiveFrom(IPEndPoint address) => _socket.ReceiveFrom(address);
public bool SendTo(byte[] buffer, IPEndPoint address)
{
bool _ = _socket.SendTo(address, buffer);
if (_)
{
iPEndPoints[address]= 5;
}
else
{
iPEndPoints[address]--;
}
if(iPEndPoints[address] <= 0)
{
OnDisconnected?.Invoke(address);
iPEndPoints.Remove(address);
}
return _;
}
}
}