forked from iKadmium/kadmium-sacn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SACNSender.cs
56 lines (49 loc) · 1.67 KB
/
SACNSender.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace kadmium_sacn
{
public class SACNSender
{
public Guid UUID { get; set; }
private UdpClient Socket { get; set; }
public IPAddress UnicastAddress { get; set; }
public bool Multicast { get { return UnicastAddress == null; } }
public int Port { get; set; }
public string SourceName { get; set; }
byte sequenceID = 0;
public SACNSender(Guid uuid, string sourceName, int port)
{
SourceName = sourceName;
UUID = uuid;
Socket = new UdpClient();
Port = port;
}
public SACNSender(Guid uuid, string sourceName) : this(uuid, sourceName, SACNCommon.SACN_PORT) { }
public void Send(Int16 universeID, byte[] data)
{
SACNPacket packet = new SACNPacket(universeID, SourceName, UUID, sequenceID++, data);
byte[] packetBytes = packet.ToArray();
SACNPacket parsed = SACNPacket.Parse(packetBytes);
Socket.Send(packetBytes, packetBytes.Length, GetEndPoint(universeID, Port));
}
private IPEndPoint GetEndPoint(Int16 universeID, int port)
{
if(Multicast)
{
return new IPEndPoint(SACNCommon.GetMulticastAddress(universeID), port);
}
else
{
return new IPEndPoint(UnicastAddress, port);
}
}
public void Close()
{
Socket.Close();
}
}
}