111 lines
4.4 KiB
C#
111 lines
4.4 KiB
C#
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 TCPServerService
|
|
{
|
|
class ReadThread
|
|
{
|
|
|
|
public Socket socket;
|
|
public static readonly int PING_INTERVAL_SEC = 5;
|
|
public DateTime lastPingTime = DateTime.Now;
|
|
|
|
public bool isRunning = false;
|
|
public String clientIP;
|
|
public ReadThread(Socket _socket)
|
|
{
|
|
socket = _socket;
|
|
|
|
clientIP = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
|
|
Utils.WriteEventLog(Program.COMPANY, $"Create Read Thread {clientIP}", EventLogEntryType.Information, 34500);
|
|
}
|
|
|
|
public void handleConnection()
|
|
{
|
|
System.Text.Encoding enc = System.Text.Encoding.ASCII;
|
|
isRunning = true;
|
|
while (isRunning)
|
|
{
|
|
try
|
|
{
|
|
byte[] data = new byte[1024];
|
|
int receive = socket.Receive(data);
|
|
//Utils.WriteEventLog(Program.COMPANY, "Recived data on TCP and Send on MULTICAST PACK value: " + receive, EventLogEntryType.Information, 34500);
|
|
//Utils.WriteEventLog(Program.COMPANY, "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("PING"))
|
|
{
|
|
lastPingTime = DateTime.Now;
|
|
|
|
byte[] buff = enc.GetBytes("PONG");
|
|
Utils.WriteEventLog(Program.COMPANY, $"[TCPServer] PING received from {clientIP}", EventLogEntryType.Information, 23456);
|
|
|
|
|
|
socket.Send(buff, buff.Length, SocketFlags.None);
|
|
//socket.Send("PONG"....);
|
|
|
|
}
|
|
else if (str.Contains("##"))
|
|
{
|
|
|
|
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);
|
|
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)
|
|
{
|
|
Utils.WriteEventLog(Program.COMPANY, $"[{clientIP}] Error on recive data: {e.ToString()}", EventLogEntryType.Error, 23456);
|
|
//Form1.appDead = true;
|
|
|
|
OnReadException?.Invoke(clientIP);
|
|
break;
|
|
}
|
|
Thread.Sleep(1);
|
|
}
|
|
//myListener.Stop();
|
|
}
|
|
|
|
|
|
public delegate void ReadExceptionDelegate(String clientIP);
|
|
public event ReadExceptionDelegate OnReadException;
|
|
}
|
|
}
|