-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathKnxReceiverRouting.cs
96 lines (84 loc) · 2.71 KB
/
KnxReceiverRouting.cs
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
using KNXLib.Log;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace KNXLib
{
internal class KnxReceiverRouting : KnxReceiver
{
private static readonly string ClassName = typeof(KnxReceiverRouting).ToString();
private readonly IList<UdpClient> _udpClients;
internal KnxReceiverRouting(KnxConnection connection, IList<UdpClient> udpClients)
: base(connection)
{
_udpClients = udpClients;
}
public override void ReceiverThreadFlow()
{
try
{
foreach (var client in _udpClients)
client.BeginReceive(OnReceive, new object[] { client });
// just wait to be aborted
while (true)
Thread.Sleep(60000);
}
catch (ThreadAbortException)
{
Thread.ResetAbort();
}
catch (Exception e)
{
Logger.Error(ClassName, e);
}
}
private void OnReceive(IAsyncResult result)
{
IPEndPoint endPoint = null;
var args = (object[])result.AsyncState;
var session = (UdpClient)args[0];
try
{
var datagram = session.EndReceive(result, ref endPoint);
ProcessDatagram(datagram);
// We make the next call to the begin receive
session.BeginReceive(OnReceive, args);
}
catch (ObjectDisposedException)
{
// ignore and exit, session was disposed
}
catch (Exception e)
{
Logger.Error(ClassName, e);
}
}
private void ProcessDatagram(byte[] datagram)
{
try
{
ProcessDatagramHeaders(datagram);
}
catch (Exception e)
{
Logger.Error(ClassName, e);
}
}
private void ProcessDatagramHeaders(byte[] datagram)
{
// HEADER
var knxDatagram = new KnxDatagram
{
header_length = datagram[0],
protocol_version = datagram[1],
service_type = new[] { datagram[2], datagram[3] },
total_length = datagram[4] + datagram[5]
};
var cemi = new byte[datagram.Length - 6];
Array.Copy(datagram, 6, cemi, 0, datagram.Length - 6);
ProcessCEMI(knxDatagram, cemi);
}
}
}