SafeDispatch/TCPClientService/ReadThread.cs

83 lines
3.5 KiB
C#
Raw Permalink 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.Net.Sockets;
using System.Threading;
using System.Net;
using System.Collections;
using System.Diagnostics;
namespace WindowsService1
{
class ReadThread
{
private Socket socket;
public ReadThread(Socket _socket)
{
socket = _socket;
EventLog.WriteEntry("Safemobile", "face Thread-ul de read", EventLogEntryType.Information, 34500);
}
public void handleConnection()
{
while (true)
{
try
{
byte[] data = new byte[1024];
int receive = socket.Receive(data);
EventLog.WriteEntry("Safemobile", "Recived data on TCP and Send on MULTICAST PACK value: " + receive, EventLogEntryType.Information, 34500);
EventLog.WriteEntry("Safemobile", "Data: X" + System.Text.Encoding.ASCII.GetString(data, 0, receive) + "X", EventLogEntryType.Information, 34500);
String str = System.Text.Encoding.ASCII.GetString(data, 0, receive);
if (str.Contains("##"))
{
System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] buf = null;
while (str.Contains("##"))
{
String send = str.Remove(str.IndexOf("##"));
send += "#";
Service1.MessageSend.Enqueue(send);
buf = enc.GetBytes(send);
Service1.UdpMulti.Send(buf, buf.Length);
foreach (Socket obj in Service1.listOFSocket)
if (obj!=socket)
obj.Send(buf, buf.Length, SocketFlags.None);
str = str.Remove(0, str.IndexOf("##")+1);
}
Service1.MessageSend.Enqueue(str);
buf = enc.GetBytes(str);
Service1.UdpMulti.Send(buf, buf.Length);
foreach (Socket obj in Service1.listOFSocket)
if (obj != socket)
obj.Send(buf, buf.Length, SocketFlags.None);
}
else
{
if (str.Length <= 0) break;
Service1.MessageSend.Enqueue(str);
System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] buf = enc.GetBytes(str);
Service1.UdpMulti.Send(buf, buf.Length);
foreach (Socket obj in Service1.listOFSocket)
if (obj != socket)
obj.Send(buf, buf.Length, SocketFlags.None);
}
}
catch (Exception e)
{
EventLog.WriteEntry("Safemobile", "Error on recive data:" + e.ToString(), EventLogEntryType.Error, 23456);
//Form1.appDead = true;
break;
}
Thread.Sleep(1);
}
//myListener.Stop();
}
}
}