WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
Engine.cs
Go to the documentation of this file.
1 using System;
2 using System.Net;
3 using System.Net.Sockets;
4 using System.Text;
5 using System.Timers;
6 
7 
8 namespace DirectOutput.Cab.Out.DMX.ArtnetEngine
9 {
14  public class Engine
15  {
16 
17  private static readonly Lazy<Engine> lazy = new Lazy<Engine>(() => new Engine());
18 
25  public static Engine Instance { get { return lazy.Value; } }
26 
27 
28 
29 
30 
31  #region "Public"
32 
37  private Engine()
38  {
39  ArtNetHeader = new byte[] { 0x41, 0x72, 0x74, 0x2d, 0x4e, 0x65, 0x74, 0 };
40  //ArtNetAddress = new byte[] { 0x7f, 0, 0, 1, Convert.ToByte(this.LoByte(0x1936)), Convert.ToByte(this.HiByte(0x1936)) };
41 
42  lock (UdpServerLocker)
43  {
44  try
45  {
46  //Log.Write("Init artnet engine");
47  UdpServer = new UdpClient();
48  UdpServer.ExclusiveAddressUse = false;
49 
50  UdpServer.EnableBroadcast = true;
51  UdpServer.Client.SendTimeout = 100;
52  UdpServer.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
53 
54  }
55  catch (Exception E)
56  {
57  Log.Exception("Could not initialize UdpServer for ArtNet (Port: 6454).", E);
58  UdpServer = null;
59  }
60  }
61  }
62 
66  ~Engine()
67  {
68  lock (UdpServerLocker)
69  {
70  if (UdpServer != null)
71  {
72  try
73  {
74  UdpServer.Close();
75  }
76  catch { }
77  UdpServer = null;
78  //Log.Write("drop artnet engine");
79  }
80  }
81  }
82 
83 
84  int SendExceptionCnt = 0;
92  public void SendDMX(string BroadcastAdress, short Universe, byte[] Data, int DataLength)
93  {
94  if (UdpServer != null)
95  {
96  byte[] packet = new byte[(0x11 + DataLength) + 1];
97  Buffer.BlockCopy(this.ArtNetHeader, 0, packet, 0, this.ArtNetHeader.Length);
98  packet[8] = Convert.ToByte(this.LoByte(0x5000));
99  packet[9] = Convert.ToByte(this.HiByte(0x5000));
100  packet[10] = 0; //ProtVerHi
101  packet[11] = 14; //ProtVerLo
102  packet[12] = 0; //Sequence
103  packet[13] = 0; //Physical
104  packet[14] = Convert.ToByte(this.LoByte(Universe));
105  packet[15] = Convert.ToByte(this.HiByte(Universe));
106  packet[0x10] = Convert.ToByte(this.HiByte(DataLength));
107  packet[0x11] = Convert.ToByte(this.LoByte(DataLength));
108 
109  try
110  {
111  Buffer.BlockCopy(Data, 0, packet, 0x12, DataLength);
112  }
113  catch (Exception exception1)
114  {
115  Log.Exception("A exception occured in the ArtNet Engine class.", exception1);
116  //Error(exception1, new EventArgs());
117  }
118 
119  lock (UdpServerLocker)
120  {
121  if (UdpServer != null)
122  {
123  try
124  {
125 
126  UdpServer.Send(packet, packet.Length, BroadcastAdress, 0x1936);
127  SendExceptionCnt = 0;
128  }
129  catch (Exception E)
130  {
131  SendExceptionCnt++;
132  if (SendExceptionCnt > 10)
133  {
134  try
135  {
136  Log.Exception("More than 10 consuecutive transmissions of ArtNet data have failed. ArtNet enigine will be disabled for the session.", E);
137  }
138  catch { }
139 
140  if (UdpServer != null)
141  {
142  try
143  {
144  UdpServer.Close();
145  }
146  catch { }
147  UdpServer = null;
148  }
149  }
150  else
151  {
152  Log.Exception("A exception has occured when sending ArtNet data.", E);
153  }
154 
155  }
156 
157  }
158  }
159  }
160  }
161 
162  #endregion
163 
164 
165 
166 
167  #region "Work Around"
168 
169 
170  private object LoByte(int wParam)
171  {
172  return (wParam & 0xffL);
173  }
174 
175  private object HiByte(int wParam)
176  {
177  return ((wParam / 0x100) & 0xffL);
178  }
179 
180 
181 
182  #endregion
183 
184 
185 
186  private object UdpServerLocker = new object();
187  private UdpClient UdpServer;
188 
189 
190  private byte[] ArtNetHeader;
191 
192 
193  }
194 }
Artnet Engine used for DMX output. The code of this class is based on the Engine class of eDMX...
Definition: Engine.cs:14
A simple logger used to record important events and exceptions.
Definition: Log.cs:14
static void Exception(string Message, Exception E=null)
Writes a exception message to the log.
Definition: Log.cs:144
void SendDMX(string BroadcastAdress, short Universe, byte[] Data, int DataLength)
Sends DMX data to a Art-Net node.
Definition: Engine.cs:92