SafeDispatch/AndroidLIB/PreParser.cs

109 lines
4.1 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 System.IO;
namespace AndroidLIB
{
public class PreParser
{
public static InterthreadMessageQueue<AndroidMSG> AndroMessageQueue;
public PreParser()
{
AndroMessageQueue = new InterthreadMessageQueue<AndroidMSG>();
}
public void FeedData(byte[] data, int recv, string sourceIP)
{
String stringData = System.Text.Encoding.ASCII.GetString(data, 0, recv);
String usefulData = stringData.Trim();
int bProcessed =0;
int messLen = 0;
string[] tempArr = usefulData.Split('#');
try
{
messLen = Convert.ToInt32(tempArr[1]);
}
catch (Exception ex)
{
SMd.Debug("Incorect message len!!!!");
SMd.Debug(ex.ToString());
return;
}
if (messLen == 0) return;
if (recv != messLen)
{
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("message length({0}) != actual length({1})", messLen, recv);
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
Console.WriteLine("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
if (messLen < recv)//we have multiple messages in one pack
{
byte[] tempDAta = data;
int tempRecv = recv;
while ((bProcessed < recv) && (tempDAta.Length > 0))
{
String stringLen = System.Text.Encoding.ASCII.GetString(tempDAta, 1, 3);
try
{
int tempLen = Convert.ToInt32(stringLen.Replace("#", ""));
byte[] buff = new byte[tempLen];
Array.Copy(tempDAta, 0, buff, 0, tempLen);
Add2Queue(buff, tempLen,sourceIP);
tempRecv = tempRecv - tempLen;
if (tempRecv > 0)
{
byte[] buffNew = new byte[tempRecv];
Array.Copy(tempDAta, tempLen, buffNew, 0, tempRecv);
tempDAta = buffNew;
}
bProcessed += messLen;
}
catch (Exception ex)
{
SMd.Debug("Incorect message len!!!!");
SMd.Debug(ex.ToString());
return;
}
}
//process left over
}
else//incomplet message drop it
{
return;
}
}
else
{
Add2Queue(data, recv,sourceIP);
}
}
private void Add2Queue(byte[] data, int recv, string sourceIP)
{
AndroidMSG amsg = new AndroidMSG(data, recv,sourceIP);
if (amsg.OK)
{
//add message to queue
AndroMessageQueue.PostItem(amsg);
}
else
{
SMd.Debug("ERROR in PreParser.cs error:" + amsg.error);
}
}
}
}