162 lines
4.5 KiB
C#
162 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using System.Threading;
|
|
|
|
namespace Simulator
|
|
{
|
|
class CTRL
|
|
{
|
|
private TCPServer tcp;
|
|
private int tcp_port;
|
|
|
|
public static string udpIP;
|
|
public static int udpPort;
|
|
|
|
private NetworkStream ns;
|
|
System.Threading.Timer ping_timer;
|
|
|
|
private static ArrayList unitlist = new ArrayList();
|
|
|
|
public CTRL(int tcpport)
|
|
{
|
|
tcp_port = tcpport;
|
|
|
|
|
|
|
|
tcp = new TCPServer();
|
|
tcp.OnConnectionRecv += new TCPServer.ConnectionRecv(tcp_OnConnectionRecv);
|
|
tcp.OnConnectionEnded += new TCPServer.ConnectionEnded(tcp_OnConnectionEnded);
|
|
tcp.OnMessageRecv += new TCPServer.MessageRecv(tcp_OnMessageRecv);
|
|
tcp.Start(tcp_port);
|
|
}
|
|
|
|
void tcp_OnConnectionRecv(System.Net.Sockets.NetworkStream ns, string IP)
|
|
{
|
|
try
|
|
{
|
|
this.ns = ns;
|
|
Console.WriteLine("GW with IP:" + IP + " Connected");
|
|
//start ping timer
|
|
ping_timer = new System.Threading.Timer(PingGW, null, 0, 10000);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
}
|
|
|
|
|
|
// file read
|
|
unitlist = new ArrayList();
|
|
try
|
|
{
|
|
using (StreamReader sr = new StreamReader("dataGPS.txt"))
|
|
{
|
|
String line;
|
|
while ((line = sr.ReadLine()) != null)
|
|
{
|
|
String[] tmp = line.Split('#');
|
|
Unit u = new Unit(tmp[1], Int32.Parse(tmp[2]), ns);
|
|
u.Start();
|
|
unitlist.Add(u);
|
|
Thread.Sleep(23);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("The file could not be read:");
|
|
Console.WriteLine(e.ToString());
|
|
}
|
|
|
|
}
|
|
void tcp_OnConnectionEnded(string IP)
|
|
{
|
|
try
|
|
{
|
|
|
|
Console.WriteLine("GW with IP:" + IP + " Disconnect");
|
|
this.ns = null;
|
|
ping_timer = null;
|
|
|
|
if (unitlist != null)
|
|
{
|
|
for (int i = 0; i < unitlist.Count; i++)
|
|
{
|
|
if ((Unit)unitlist[i] != null)
|
|
{
|
|
((Unit)unitlist[i]).Stop();
|
|
unitlist[i] = null;
|
|
}
|
|
|
|
}
|
|
unitlist = null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.ToString());
|
|
}
|
|
}
|
|
|
|
void tcp_OnMessageRecv(byte[] data, int recv, System.Net.Sockets.NetworkStream ns)
|
|
{
|
|
try
|
|
{
|
|
if (recv == 12)
|
|
{
|
|
if ((data[4] == 0x50) && (data[6] == 0x4f))
|
|
{
|
|
//Pong received
|
|
Console.WriteLine("Pong recv");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("tcp_OnMessageRecv:" + ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void PingGW(Object state)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine("Send PING");
|
|
byte[] msg = new byte[12];
|
|
//length
|
|
msg[0] = 0x00;
|
|
msg[1] = 0x0a;
|
|
// message ID
|
|
msg[2] = 0xff;
|
|
// body length
|
|
msg[3] = 0x08;
|
|
//PONG -utf16 LE string
|
|
//P
|
|
msg[4] = 0x50;
|
|
msg[5] = 0x00;
|
|
//I
|
|
msg[6] = 0x49;
|
|
msg[7] = 0x00;
|
|
//N
|
|
msg[8] = 0x4e;
|
|
msg[9] = 0x00;
|
|
//G
|
|
msg[10] = 0x47;
|
|
msg[11] = 0x00;
|
|
|
|
if(tcp !=null && ns != null)
|
|
TCPServer.Send(msg, msg.Length, ns);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("ERROR in PingGW:\n" + ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|