SafeDispatch/AppServerMobile/MessageBussHandler.cs

87 lines
2.7 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SafeMobileLib;
using System.Threading;
namespace AppServerMobile
{
class MessageBussHandler
{
string messageBusIP;
int messageBusPort;
private static UdpMulticast udp;
public static InterthreadMessageQueue<MessageBusMessage> MBMessageQueue;
public MessageBussHandler(string messageBusIP, string messageBusPort)
{
this.messageBusIP = messageBusIP;
this.messageBusPort =Convert.ToInt32(messageBusPort);
MBMessageQueue = new InterthreadMessageQueue<MessageBusMessage>();
//start messageBus listner
udp = new UdpMulticast(messageBusIP,this.messageBusPort);
udp.OnNewDataRecv += new UdpMulticast.newData4Send(udp_OnNewDataRecv);
udp.StartListen();
}
void udp_OnNewDataRecv(byte[] data, int dataLen)
{
//Console.WriteLine("got data on gw" + radioID);
string str = System.Text.Encoding.ASCII.GetString(data, 0, dataLen);
SM.Debug("RX: " + str.Trim(), false);
try
{
MessageBusMessage msm = new MessageBusMessage(data, dataLen);
if (msm.OK)
{
//add message to queue
MBMessageQueue.PostItem(msm);
}
else
{
SM.Debug("ERROR in MessageBusHandler.cs error:"+msm.error);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void SendOnMsgBuss(string seqID, string test)
{
if (udp != null)
{
byte[] message = SafeMobileLib.Utils.Convert_text_For_multicast("#" + seqID + test);
udp.Send(message, message.Length);
}
else
{
SM.Debug("Error!!! MessageBusHandler messagebus =null");
}
}
public static String Send_UDP_cmd(String cmd, string userID)
{
try
{
String seqID = userID + "." + DateTime.Now.GetSecondsLocalFromDT() + DateTime.Now.Ticks.ToString();
String cmdok = "";
byte[] buf = Utils.Convert_text_For_multicast("#" + seqID + cmd, out cmdok);
udp.Send(buf, buf.Length);
return seqID;
}
catch (Exception ex)
{
SM.Debug("Ex8:" + ex.ToString());
return "";
}
}
}
}