# GenericUdpServer

- Adds an event GenericUdpReceiveTextExtraArgs which has args that include TextRx, BytesRx, IpAddressRecivedFrom, PortRecievedFrom
- Removes the getter property for LastMessageReceivedFrom
This commit is contained in:
Jason T Alborough
2019-04-22 10:30:39 -04:00
parent 73b9cd8c31
commit 54324d7e43

View File

@@ -11,6 +11,8 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace PepperDash.Core namespace PepperDash.Core
{ {
public class GenericUdpServer : Device, IBasicCommunication public class GenericUdpServer : Device, IBasicCommunication
@@ -24,6 +26,8 @@ namespace PepperDash.Core
/// ///
/// </summary> /// </summary>
public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived; public event EventHandler<GenericCommMethodReceiveTextArgs> TextReceived;
public event EventHandler<GenericUdpReceiveTextExtraArgs> DataRecievedExtra;
private CrestronQueue<GenericUdpReceiveTextExtraArgs> MessageQueue;
/// <summary> /// <summary>
/// ///
@@ -39,6 +43,8 @@ namespace PepperDash.Core
} }
} }
CCriticalSection DequeueLock;
/// <summary> /// <summary>
/// Address of server /// Address of server
/// </summary> /// </summary>
@@ -47,11 +53,7 @@ namespace PepperDash.Core
/// <summary> /// <summary>
/// IP Address of the sender of the last recieved message /// IP Address of the sender of the last recieved message
/// </summary> /// </summary>
public string LastMessageReceivedFrom
{
get { return Server.IPAddressLastMessageReceivedFrom; }
}
/// <summary> /// <summary>
/// Port on server /// Port on server
@@ -91,6 +93,9 @@ namespace PepperDash.Core
Port = port; Port = port;
BufferSize = buffefSize; BufferSize = buffefSize;
DequeueLock = new CCriticalSection();
MessageQueue = new CrestronQueue<GenericUdpReceiveTextExtraArgs>();
CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler);
CrestronEnvironment.EthernetEventHandler += new EthernetEventHandler(CrestronEnvironment_EthernetEventHandler); CrestronEnvironment.EthernetEventHandler += new EthernetEventHandler(CrestronEnvironment_EthernetEventHandler);
} }
@@ -171,9 +176,13 @@ namespace PepperDash.Core
if (numBytes > 0) if (numBytes > 0)
{ {
var sourceIp = Server.IPAddressLastMessageReceivedFrom;
var sourcePort = Server.IPPortLastMessageReceivedFrom;
var bytes = server.IncomingDataBuffer.Take(numBytes).ToArray(); var bytes = server.IncomingDataBuffer.Take(numBytes).ToArray();
var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length);
MessageQueue.TryToEnqueue(new GenericUdpReceiveTextExtraArgs(str, sourceIp, sourcePort, bytes));
Debug.Console(2, this, "Bytes: {0}", bytes.ToString()); Debug.Console(2, this, "Bytes: {0}", bytes.ToString());
var bytesHandler = BytesReceived; var bytesHandler = BytesReceived;
if (bytesHandler != null) if (bytesHandler != null)
bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes));
@@ -182,7 +191,7 @@ namespace PepperDash.Core
var textHandler = TextReceived; var textHandler = TextReceived;
if (textHandler != null) if (textHandler != null)
{ {
var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length);
Debug.Console(2, this, "RX: {0}", str); Debug.Console(2, this, "RX: {0}", str);
textHandler(this, new GenericCommMethodReceiveTextArgs(str)); textHandler(this, new GenericCommMethodReceiveTextArgs(str));
} }
@@ -190,8 +199,39 @@ namespace PepperDash.Core
Debug.Console(2, this, "textHandler is null"); Debug.Console(2, this, "textHandler is null");
} }
server.ReceiveDataAsync(Receive); server.ReceiveDataAsync(Receive);
CrestronInvoke.BeginInvoke(DequeueEvent);
} }
void DequeueEvent(object notUsed)
{
try
{
// Add CCritical Section
DequeueLock.TryEnter();
while (!MessageQueue.IsEmpty)
{
// Pull from Queue and fire an event.
var Message = MessageQueue.TryToDequeue();
var dataRecivedExtra = DataRecievedExtra;
if (dataRecivedExtra != null)
{
dataRecivedExtra(this, Message);
}
}
}
catch (Exception e)
{
Debug.Console(0, "GenericUdpServer DequeueEvent error: {0}\r", e);
}
finally
{
DequeueLock.Leave();
}
}
/// <summary> /// <summary>
/// General send method /// General send method
/// </summary> /// </summary>
@@ -219,6 +259,27 @@ namespace PepperDash.Core
} }
public class GenericUdpReceiveTextExtraArgs : EventArgs
{
public string Text { get; private set; }
public string IpAddress { get; private set; }
public int Port { get; private set; }
public byte[] Bytes { get; private set; }
public GenericUdpReceiveTextExtraArgs(string text, string ipAddress, int port, byte[] bytes)
{
Text = text;
IpAddress = ipAddress;
Port = port;
Bytes = bytes;
}
/// <summary>
/// Stupid S+ Constructor
/// </summary>
public GenericUdpReceiveTextExtraArgs() { }
}
public class UdpServerPropertiesConfig public class UdpServerPropertiesConfig
{ {
[JsonProperty(Required = Required.Always)] [JsonProperty(Required = Required.Always)]