-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathUdpCommunicationLayer.cs
More file actions
160 lines (144 loc) · 3.72 KB
/
UdpCommunicationLayer.cs
File metadata and controls
160 lines (144 loc) · 3.72 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using Waher.Events;
using Waher.Networking.Sniffers;
using Waher.Networking;
namespace Waher.Security.DTLS
{
/// <summary>
/// Class acting as an interface for DTLS to communicate UDP.
/// </summary>
public class UdpCommunicationLayer : CommunicationLayer, IDisposable, ICommunicationLayer
{
private readonly LinkedList<KeyValuePair<byte[], IPEndPoint>> outputQueue = new LinkedList<KeyValuePair<byte[], IPEndPoint>>();
private UdpClient client;
private bool disposed = false;
private bool isWriting = false;
/// <summary>
/// Class acting as an interface for DTLS to communicate UDP.
/// </summary>
/// <param name="UdpClient">UDP client.</param>
/// <param name="Sniffers">Sniffers.</param>
public UdpCommunicationLayer(UdpClient UdpClient, params ISniffer[] Sniffers)
: base(false, Sniffers)
{
this.client = UdpClient;
this.BeginReceive();
}
/// <summary>
/// UDP Client.
/// </summary>
public UdpClient Client => this.client;
private async void BeginReceive() // Starts parallel task
{
try
{
while (!this.disposed)
{
UdpReceiveResult Data = await this.client.ReceiveAsync();
if (this.disposed)
return;
this.ReceiveBinary(true, Data.Buffer);
try
{
DataReceivedEventHandler h = this.PacketReceived;
if (!(h is null))
await h(true, Data.Buffer, Data.RemoteEndPoint);
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
}
catch (ObjectDisposedException)
{
// Session closed.
}
catch (Exception ex)
{
if (!this.disposed)
this.Exception(ex);
}
}
private async Task BeginTransmit(bool ConstantBuffer, byte[] Packet, IPEndPoint RemoteEndpoint)
{
if (this.disposed)
return;
lock (this.outputQueue)
{
if (this.isWriting)
{
if (!ConstantBuffer)
{
Packet = (byte[])Packet.Clone();
ConstantBuffer = true;
}
this.outputQueue.AddLast(new KeyValuePair<byte[], IPEndPoint>(Packet, RemoteEndpoint));
return;
}
else
this.isWriting = true;
}
try
{
while (!(Packet is null))
{
await this.client.SendAsync(Packet, Packet.Length, RemoteEndpoint);
if (this.disposed)
return;
lock (this.outputQueue)
{
if (this.outputQueue.First is null)
{
this.isWriting = false;
Packet = null;
}
else
{
Packet = this.outputQueue.First.Value.Key;
RemoteEndpoint = this.outputQueue.First.Value.Value;
this.outputQueue.RemoveFirst();
}
}
}
}
catch (Exception ex)
{
this.Exception(ex);
}
}
/// <summary>
/// Event is reaised whenever data has been received from the UDP layer.
/// </summary>
public event DataReceivedEventHandler PacketReceived;
/// <summary>
/// Method called by the DTLS layer when a datagram is to be sent.
/// </summary>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Packet">Datagram</param>
/// <param name="RemoteEndpoint">Remote endpoint.</param>
public Task SendPacket(bool ConstantBuffer, byte[] Packet, object RemoteEndpoint)
{
IPEndPoint EP = (IPEndPoint)RemoteEndpoint;
return this.BeginTransmit(ConstantBuffer, Packet, EP);
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
this.disposed = true;
if (!(this.client is null))
{
this.client.Client.Shutdown(SocketShutdown.Both);
this.client.Dispose();
this.client = null;
}
}
}
}