You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
6.6 KiB
179 lines
6.6 KiB
2 weeks ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Diagnostics;
|
||
|
using System.Net;
|
||
|
using System.Net.Sockets;
|
||
|
using System.Text;
|
||
|
using System.Threading;
|
||
|
using _4ELogger;
|
||
|
|
||
|
namespace iMonitorApp
|
||
|
{
|
||
|
// Token: 0x0200000B RID: 11
|
||
|
public class AsynchronousSocketListenerStackAlarms
|
||
|
{
|
||
|
// Token: 0x17000017 RID: 23
|
||
|
// (get) Token: 0x06000045 RID: 69 RVA: 0x000032D9 File Offset: 0x000014D9
|
||
|
// (set) Token: 0x06000046 RID: 70 RVA: 0x000032E0 File Offset: 0x000014E0
|
||
|
public static int Port { get; set; }
|
||
|
|
||
|
// Token: 0x17000018 RID: 24
|
||
|
// (get) Token: 0x06000047 RID: 71 RVA: 0x000032E8 File Offset: 0x000014E8
|
||
|
// (set) Token: 0x06000048 RID: 72 RVA: 0x000032EF File Offset: 0x000014EF
|
||
|
public static string IPAddress { get; set; }
|
||
|
|
||
|
// Token: 0x14000002 RID: 2
|
||
|
// (add) Token: 0x06000049 RID: 73 RVA: 0x000032F8 File Offset: 0x000014F8
|
||
|
// (remove) Token: 0x0600004A RID: 74 RVA: 0x0000332C File Offset: 0x0000152C
|
||
|
// [DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||
|
public static event EventHandler Received;
|
||
|
|
||
|
// Token: 0x0600004B RID: 75 RVA: 0x0000335F File Offset: 0x0000155F
|
||
|
public AsynchronousSocketListenerStackAlarms(string ipaddress, int port)
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.IPAddress = ipaddress;
|
||
|
AsynchronousSocketListenerStackAlarms.Port = port;
|
||
|
}
|
||
|
|
||
|
// Token: 0x0600004C RID: 76 RVA: 0x00003378 File Offset: 0x00001578
|
||
|
protected static void OnReceived(string s, EventArgs e)
|
||
|
{
|
||
|
bool flag = AsynchronousSocketListenerStackAlarms.Received != null;
|
||
|
if (flag)
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.Received(s, e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Token: 0x0600004D RID: 77 RVA: 0x000033A0 File Offset: 0x000015A0
|
||
|
public static void StartListening()
|
||
|
{
|
||
|
// byte[] array = new byte[150000000];
|
||
|
// string hostName = Dns.GetHostName();
|
||
|
Debug.WriteLine("Server socket IP is: " + AsynchronousSocketListenerStackAlarms.IPAddress.ToString());
|
||
|
IPHostEntry iphostEntry = Dns.Resolve(AsynchronousSocketListenerStackAlarms.IPAddress.ToString());
|
||
|
IPAddress address = iphostEntry.AddressList[0];
|
||
|
IPEndPoint localEP = new IPEndPoint(address, AsynchronousSocketListenerStackAlarms.Port);
|
||
|
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
|
try
|
||
|
{
|
||
|
socket.Bind(localEP);
|
||
|
socket.Listen(10);
|
||
|
for (;;)
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.allDone.Reset();
|
||
|
Debug.WriteLine("Waiting for Clients...");
|
||
|
socket.BeginAccept(new AsyncCallback(AsynchronousSocketListenerStackAlarms.AcceptCallback), socket);
|
||
|
AsynchronousSocketListenerStackAlarms.allDone.WaitOne();
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine(ex.ToString());
|
||
|
}
|
||
|
Console.WriteLine("\nPress ENTER to continue...");
|
||
|
Console.Read();
|
||
|
}
|
||
|
|
||
|
// Token: 0x0600004E RID: 78 RVA: 0x00003494 File Offset: 0x00001694
|
||
|
public static void AcceptCallback(IAsyncResult ar)
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.allDone.Set();
|
||
|
Socket socket = (Socket)ar.AsyncState;
|
||
|
Socket socket2 = socket.EndAccept(ar);
|
||
|
StateObjectStackAlarms stateObjectStackAlarms = new StateObjectStackAlarms();
|
||
|
stateObjectStackAlarms.workSocket = socket2;
|
||
|
socket2.BeginReceive(stateObjectStackAlarms.buffer, 0, 20480, SocketFlags.None, new AsyncCallback(AsynchronousSocketListenerStackAlarms.ReadCallback), stateObjectStackAlarms);
|
||
|
}
|
||
|
|
||
|
// Token: 0x0600004F RID: 79 RVA: 0x000034F0 File Offset: 0x000016F0
|
||
|
public static void ReadCallback(IAsyncResult ar)
|
||
|
{
|
||
|
string text = string.Empty;
|
||
|
bool flag = AsynchronousSocketListenerStackAlarms.alarms_received.Count >= 999;
|
||
|
if (flag)
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.alarms_received.Clear();
|
||
|
}
|
||
|
StateObjectStackAlarms stateObjectStackAlarms = (StateObjectStackAlarms)ar.AsyncState;
|
||
|
Socket workSocket = stateObjectStackAlarms.workSocket;
|
||
|
int num = workSocket.EndReceive(ar);
|
||
|
bool flag2 = num > 0;
|
||
|
if (flag2)
|
||
|
{
|
||
|
stateObjectStackAlarms.sb.Append(Encoding.Default.GetString(stateObjectStackAlarms.buffer, 0, num));
|
||
|
text = stateObjectStackAlarms.sb.ToString();
|
||
|
bool flag3 = text.IndexOf("<EOF>") > -1;
|
||
|
if (flag3)
|
||
|
{
|
||
|
Console.WriteLine("Read {0} bytes from socket (sender:" + workSocket.RemoteEndPoint.ToString() + "). \n Data : {1}", text.Length, text);
|
||
|
Logger.Log(string.Concat(new string[]
|
||
|
{
|
||
|
"Read {",
|
||
|
text.Length.ToString(),
|
||
|
"} bytes from socket (sender:",
|
||
|
workSocket.RemoteEndPoint.ToString(),
|
||
|
"). \n Data : ",
|
||
|
text.Substring(0, 100)
|
||
|
}), "iMonitor_Receiver", "iMonitor");
|
||
|
Debug.WriteLine(text);
|
||
|
bool flag4 = text.Contains("Basket overloaded");
|
||
|
if (!flag4)
|
||
|
{
|
||
|
bool flag5 = AsynchronousSocketListenerStackAlarms.alarms_received.ContainsKey(workSocket.RemoteEndPoint.AddressFamily.ToString() + text);
|
||
|
if (flag5)
|
||
|
{
|
||
|
bool flag6 = (DateTime.Now - AsynchronousSocketListenerStackAlarms.alarms_received[workSocket.RemoteEndPoint.AddressFamily.ToString() + text]).TotalSeconds <= 600.0;
|
||
|
if (!flag6)
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.alarms_received[workSocket.RemoteEndPoint.AddressFamily.ToString() + text] = DateTime.Now;
|
||
|
AsynchronousSocketListenerStackAlarms.OnReceived(text, EventArgs.Empty);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
AsynchronousSocketListenerStackAlarms.alarms_received.Add(workSocket.RemoteEndPoint.AddressFamily.ToString() + text, DateTime.Now);
|
||
|
AsynchronousSocketListenerStackAlarms.OnReceived(text, EventArgs.Empty);
|
||
|
}
|
||
|
AsynchronousSocketListenerStackAlarms.Send(workSocket, "Instruction Received");
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
workSocket.BeginReceive(stateObjectStackAlarms.buffer, 0, 20480, SocketFlags.None, new AsyncCallback(AsynchronousSocketListenerStackAlarms.ReadCallback), stateObjectStackAlarms);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Token: 0x06000050 RID: 80 RVA: 0x00003778 File Offset: 0x00001978
|
||
|
private static void Send(Socket handler, string data)
|
||
|
{
|
||
|
byte[] bytes = Encoding.ASCII.GetBytes(data);
|
||
|
handler.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, new AsyncCallback(AsynchronousSocketListenerStackAlarms.SendCallback), handler);
|
||
|
}
|
||
|
|
||
|
// Token: 0x06000051 RID: 81 RVA: 0x000037AC File Offset: 0x000019AC
|
||
|
private static void SendCallback(IAsyncResult ar)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
Socket socket = (Socket)ar.AsyncState;
|
||
|
int num = socket.EndSend(ar);
|
||
|
Console.WriteLine("Sent {0} bytes to client.", num);
|
||
|
socket.Shutdown(SocketShutdown.Both);
|
||
|
socket.Close();
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Console.WriteLine(ex.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Token: 0x04000024 RID: 36
|
||
|
public static ManualResetEvent allDone = new ManualResetEvent(false);
|
||
|
|
||
|
// Token: 0x04000028 RID: 40
|
||
|
private static Dictionary<string, DateTime> alarms_received = new Dictionary<string, DateTime>();
|
||
|
}
|
||
|
}
|