初始化
This commit is contained in:
12
TcpServer/Properties/launchSettings.json
Normal file
12
TcpServer/Properties/launchSettings.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"TcpServer": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:63420;http://localhost:63421"
|
||||
}
|
||||
}
|
||||
}
|
||||
834
TcpServer/TcpListener.cs
Normal file
834
TcpServer/TcpListener.cs
Normal file
@@ -0,0 +1,834 @@
|
||||
using Aliyun.Api.LogService.Domain.Log;
|
||||
using COMMON;
|
||||
using Models;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using static Models.TCPDATA;
|
||||
|
||||
namespace TcpServer
|
||||
{
|
||||
public class Tcp
|
||||
{
|
||||
public static List<TcpClient> Clients = new List<TcpClient>();
|
||||
public static Thread thread1 = null;
|
||||
// 创建 TcpListener 实例
|
||||
public static TcpListener server = null;
|
||||
static object locks = new object();
|
||||
static object send_locks = new object();
|
||||
static void Init()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
lock (locks)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
if (HTLE.PortInUse(ConfigEntity.Instance.SERVER_PROT))
|
||||
{
|
||||
throw new Exception("端口" + ConfigEntity.Instance.SERVER_PROT.ToString() + "被占用~~~");
|
||||
}
|
||||
else
|
||||
server = new TcpListener(IPAddress.Any, ConfigEntity.Instance.SERVER_PROT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception EX)
|
||||
{
|
||||
throw EX;
|
||||
}
|
||||
|
||||
}
|
||||
public static void Listener()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
lock (locks)
|
||||
{
|
||||
if (server == null)
|
||||
{
|
||||
Init();
|
||||
}
|
||||
else
|
||||
{
|
||||
XC_Redis.Redis.SetKey("udpserver", 1, -1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
XC_Redis.Redis.SetKey("udpserver", 1, -1);
|
||||
return;
|
||||
}
|
||||
thread1 = null;
|
||||
// 开始监听
|
||||
server.Start();
|
||||
|
||||
XC_Redis.Redis.SetKey("udpserver", 1, -1);
|
||||
|
||||
thread1 = new Thread(() =>
|
||||
{
|
||||
// 等待客户端的连接
|
||||
while (true)
|
||||
{
|
||||
if (thread1 == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
TcpClient client = server.AcceptTcpClient();
|
||||
Thread thread = new Thread(new
|
||||
ParameterizedThreadStart(Res));
|
||||
thread.IsBackground = true;
|
||||
thread.Start(client);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error($"\ntcp监听错误:\n{ex.ToString()}");
|
||||
server = null;
|
||||
//throw ;
|
||||
}
|
||||
}
|
||||
});
|
||||
thread1.IsBackground = true;
|
||||
thread1.Start();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error($"\ntcp监听错误:\n{ex.ToString()}");
|
||||
if (server != null)
|
||||
{
|
||||
server.Stop();
|
||||
foreach (var item in Clients)
|
||||
{
|
||||
item.Close();
|
||||
item.Dispose();
|
||||
}
|
||||
server = null;
|
||||
}
|
||||
XC_Redis.Redis.SetKey("udpserver", 0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Res(object clients)
|
||||
{
|
||||
var client = clients as TcpClient;
|
||||
var ipinfo = (IPEndPoint)client.Client.RemoteEndPoint;
|
||||
// 不是指定的IP和端口不让连接
|
||||
|
||||
// 测试阶段任意端口可以连接
|
||||
if (!ConfigEntity.Instance.DEV)
|
||||
{
|
||||
//rucs
|
||||
if (ipinfo.Address.ToString() != ConfigEntity.Instance.RUCS_IP || ipinfo.Port != ConfigEntity.Instance.RUCS_PROT)
|
||||
{
|
||||
LogHelp.Warning($"\n非法连接:\n{ipinfo.Address.ToString()}:{ipinfo.Port}");
|
||||
client.Close();
|
||||
client.Dispose();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int packcount = 0;
|
||||
|
||||
LogHelp.Warning($"\n客户端连接:\n{ipinfo.Address.ToString()}:{ipinfo.Port}");
|
||||
try
|
||||
{
|
||||
if (Clients.FirstOrDefault(x => ((IPEndPoint)x.Client.RemoteEndPoint).Address == ipinfo.Address && ipinfo.Port == ((IPEndPoint)x.Client.RemoteEndPoint).Port) == null)
|
||||
{
|
||||
Clients.Add(client);
|
||||
}
|
||||
NetworkStream sw = client.GetStream();
|
||||
string data = String.Empty;
|
||||
while (true)
|
||||
{
|
||||
if (thread1 == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (sw.DataAvailable)
|
||||
{
|
||||
|
||||
|
||||
byte[] databyte = new byte[client.Available];
|
||||
int bytes = sw.Read(databyte, 0, databyte.Length);
|
||||
data += Encoding.UTF8.GetString(databyte);
|
||||
if(!data.Contains(Environment.NewLine))
|
||||
continue;
|
||||
var item_ = data.Split(Environment.NewLine);
|
||||
data = string.Empty;
|
||||
// 最后一个必然是'' 不是 '' 说明这个包未读取完毕
|
||||
if (!string.IsNullOrEmpty(item_[item_.Length - 1]))
|
||||
{
|
||||
data = item_[item_.Length - 1];
|
||||
item_[item_.Length - 1] = String.Empty;
|
||||
}
|
||||
Task.Run(() =>
|
||||
{
|
||||
Stopwatch time = new Stopwatch();
|
||||
|
||||
foreach (var item in item_)
|
||||
{
|
||||
//开始
|
||||
time.Restart();
|
||||
if (string.IsNullOrEmpty(item))
|
||||
continue;
|
||||
TCPDATA val = new TCPDATA();
|
||||
//Send(item, client);
|
||||
var tcplog = new Models.Models.LOGDB.TBL_TCP_LOG();
|
||||
|
||||
//是否入库
|
||||
bool isdblog = true;
|
||||
|
||||
// 数据是否有效
|
||||
bool isdata = false;
|
||||
try
|
||||
{
|
||||
|
||||
if (string.IsNullOrEmpty(item))
|
||||
return;
|
||||
tcplog.CONTENT = item;
|
||||
try
|
||||
{
|
||||
// 每个包的原始数据
|
||||
tcplog.CONTENT = item;
|
||||
val = JsonConvert.DeserializeObject<TCPDATA>(item);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Warning(" 无效 " + item + ex);
|
||||
isdata = false;
|
||||
return;
|
||||
}
|
||||
if (val == null)
|
||||
return;
|
||||
LogHelp.Warning($"来自客户端器{ipinfo.Address.ToString()}:{ipinfo.Port}的消息:--CMD:{val.CMD}");
|
||||
|
||||
//记录log
|
||||
tcplog.TCP_ID = val.TCP_ID;
|
||||
isdblog = val.LOG_TO_DB;
|
||||
|
||||
switch (val.CMD.ToUpper())
|
||||
{
|
||||
case "RUCS_RCU_LIST":
|
||||
XC_Redis.Redis.SetKey("RUCS_RCU_LIST", val.PARAM);
|
||||
if (val.PARAM.RCU_INFO.FirstOrDefault() != null)
|
||||
{
|
||||
SendHelp.Send_Control_RCU_DevIssued(val.PARAM.RCU_INFO.FirstOrDefault().MAC);
|
||||
}
|
||||
break;
|
||||
case "RUCS_DEBUGLOG":
|
||||
var RUCS_DEBUGLOG = XC_Redis.Redis.GetKey<List<PARAs_Class>>("RUCS_DEBUGLOG");
|
||||
if (RUCS_DEBUGLOG == null)
|
||||
{
|
||||
RUCS_DEBUGLOG = new List<PARAs_Class>();
|
||||
}
|
||||
RUCS_DEBUGLOG.Insert(0, val.PARAM);
|
||||
XC_Redis.Redis.SetKey("RUCS_DEBUGLOG", RUCS_DEBUGLOG.Take(1000), 60 * 60 * 24 * 3);
|
||||
break;
|
||||
case "RUCS_CONN_UPGRADE_PROGRESS":
|
||||
double percent = (Convert.ToDouble(val.PARAM.The_Current_Package) / Convert.ToDouble(val.PARAM.The_Total_Packages))*100;
|
||||
percent = Convert.ToDouble(string.Format("{0:F}", percent));
|
||||
if(XC_Redis.Redis.GetKey<double>(val.PARAM.MAC)< percent)
|
||||
XC_Redis.Redis.SetKey(val.PARAM.MAC, percent,60*3);
|
||||
break;
|
||||
case "RUCS_CONN_UPGRADE_RESULTS":
|
||||
XC_Redis.Redis.SetKey("RUCS_CONN_UPGRADE_RESULTS", val.PARAM);
|
||||
break;
|
||||
default:
|
||||
isdata = false;
|
||||
break;
|
||||
}
|
||||
|
||||
tcplog.RESULT = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcplog.REMARK = ex.Message;
|
||||
tcplog.RESULT = 0;
|
||||
LogHelp.Error(ex.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
//停止
|
||||
time.Stop();
|
||||
|
||||
//获取毫秒
|
||||
tcplog.PROCESSING_TIMESPENT = int.Parse(time.ElapsedMilliseconds.ToString());
|
||||
|
||||
tcplog.RESULT = isdata ? 1 : 0;
|
||||
|
||||
if (isdata == false)
|
||||
{
|
||||
tcplog.DATA_VALID = 0;
|
||||
tcplog.RESULT = 0;
|
||||
}
|
||||
tcplog.DESTINATION_PORT = ipinfo.Port.ToString();
|
||||
tcplog.DESTINATION = ipinfo.Address.ToString();
|
||||
tcplog.SOURCE_PORT = ConfigEntity.Instance.RUCS_PROT.ToString();
|
||||
tcplog.SOURCE_IP = ConfigEntity.Instance.RUCS_IP.ToString();
|
||||
if (isdblog || ConfigEntity.Instance.DEV)
|
||||
{
|
||||
tcplog.REMARK = tcplog.REMARK != null ? tcplog.REMARK.Substring(0, tcplog.REMARK.Length > 100 ? 100 : tcplog.REMARK.Length) : null;
|
||||
var db = XC_Data.GetLogDataBase();
|
||||
db.TBL_RUCS_TCP_LOGS.Add(tcplog);
|
||||
try
|
||||
{
|
||||
db.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Warning("数据库保存失败~ " + ex.StackTrace + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
client.Close();
|
||||
client.Dispose();
|
||||
client.GetStream().Close();
|
||||
client.GetStream().Dispose();
|
||||
var S = Clients.FirstOrDefault(x => ((IPEndPoint)x.Client.RemoteEndPoint).Address == ipinfo.Address && ipinfo.Port == ((IPEndPoint)x.Client.RemoteEndPoint).Port);
|
||||
if(S != null)
|
||||
Clients.Remove(S);
|
||||
LogHelp.Warning($"\n来自客户端的异常消息:\n{ipinfo.Address.ToString()}:{ipinfo.Port}--{ex.Message}");
|
||||
}
|
||||
}
|
||||
public static bool Send(string STR, TcpClient client = null)
|
||||
{
|
||||
lock (send_locks)
|
||||
{
|
||||
LogHelp.Warning("------------------------发送——--------------" + STR);
|
||||
Listener();
|
||||
if (client == null)
|
||||
client = Clients.FirstOrDefault(x => ((IPEndPoint)x.Client.RemoteEndPoint).Address.ToString() == ConfigEntity.Instance.RUCS_IP && ConfigEntity.Instance.RUCS_PROT == ((IPEndPoint)x.Client.RemoteEndPoint).Port);
|
||||
else
|
||||
{
|
||||
var IP = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
|
||||
var PORT = ((IPEndPoint)client.Client.RemoteEndPoint).Port;
|
||||
client = Clients.FirstOrDefault(x => ((IPEndPoint)x.Client.RemoteEndPoint).Address.ToString() == IP && ((IPEndPoint)x.Client.RemoteEndPoint).Port == PORT);
|
||||
}
|
||||
if (client == null)
|
||||
{
|
||||
LogHelp.Error("客户端未连接~");
|
||||
return false;
|
||||
}
|
||||
var ipinfo = (IPEndPoint)client.Client.RemoteEndPoint;
|
||||
try
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
var sw = client.GetStream();
|
||||
byte[] sendBuffer = Encoding.UTF8.GetBytes(STR);
|
||||
sw.Write(sendBuffer);
|
||||
Task.Run(async () => {
|
||||
var stty = JsonConvert.DeserializeObject<TCPDATA>(STR);
|
||||
ASLS sLS = new ASLS();
|
||||
var logs = new LogInfo();
|
||||
logs.Contents.Add("eventlogging_id", stty.TRACKING_INFO.TRANSACTION_ID);
|
||||
logs.Contents.Add("event_type", stty.TRACKING_INFO.TRACKING_TYPE);
|
||||
logs.Contents.Add("input_datetime", DateTime.Now.ToString("yyyy-MM-hh mm:ss"));
|
||||
logs.Contents.Add("role", "Web");
|
||||
logs.Contents.Add("steprecording", "1");
|
||||
await sLS.write("rucs-eventlogging-db", logs);
|
||||
});
|
||||
return true;
|
||||
|
||||
}
|
||||
catch (Exception EX)
|
||||
{
|
||||
if (client != null)
|
||||
{
|
||||
Clients.Remove(
|
||||
Clients.First(x => ((IPEndPoint)x.Client.RemoteEndPoint).Address == ipinfo.Address && ipinfo.Port == ((IPEndPoint)x.Client.RemoteEndPoint).Port)
|
||||
);
|
||||
};
|
||||
LogHelp.Warning($"\n来自客户端的异常消息:\n{ipinfo.Address.ToString()}:{ipinfo.Port}--{EX.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void Close()
|
||||
{
|
||||
if (server != null)
|
||||
{
|
||||
server.Stop();
|
||||
}
|
||||
thread1 = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂未使用 仅仅是使用锁
|
||||
/// </summary>
|
||||
public class Client
|
||||
{
|
||||
public static string dateTime = null;
|
||||
public static object dateTimelocks = new object();
|
||||
|
||||
private static TcpClient client = null;
|
||||
private static NetworkStream sw = null;
|
||||
private static IPEndPoint ipinfo = null;
|
||||
|
||||
static object locks = new object();
|
||||
static Thread thread = null;
|
||||
|
||||
public static bool Init()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (client == null)
|
||||
{
|
||||
lock (locks)
|
||||
{
|
||||
if (client == null)
|
||||
{
|
||||
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, ConfigEntity.Instance.CLIENT_PROT);
|
||||
client = new TcpClient(ipep);
|
||||
// ConfigEntity.Instance.RUCS_PROT
|
||||
IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse(ConfigEntity.Instance.RUCS_IP), 1221);
|
||||
// client.ReceiveTimeout = 2000;
|
||||
// client.SendTimeout = 2000;
|
||||
if (client.ConnectAsync(ipep1.Address, ipep1.Port).Wait(1000))
|
||||
{
|
||||
|
||||
ipinfo = (IPEndPoint)client.Client.RemoteEndPoint;
|
||||
sw = client.GetStream();
|
||||
// sw.ReadTimeout = 2000; // 设置读取超时时间为2秒
|
||||
// sw.WriteTimeout = 2000; // 设置写入超时时间为2秒
|
||||
thread = new Thread(Res);
|
||||
thread.IsBackground = true;
|
||||
thread.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("连接失败");
|
||||
}
|
||||
LogHelp.Error(" !!!连接成功");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
XC_Redis.Redis.SetKey("udpserver", 1, -1);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error(" Client Init 错误" + ex.ToString());
|
||||
if (sw != null)
|
||||
{
|
||||
sw.Close();
|
||||
sw.Dispose();
|
||||
}
|
||||
if (client != null)
|
||||
{
|
||||
client.Client.Close();
|
||||
client.Client.Dispose();
|
||||
client.Close();
|
||||
client.Dispose();
|
||||
}
|
||||
sw = null;
|
||||
client = null;
|
||||
ipinfo = null;
|
||||
XC_Redis.Redis.SetKey("udpserver", 0, -1);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static bool Send(string msg)
|
||||
{
|
||||
try
|
||||
{
|
||||
LogHelp.Warning("------------------------发送——--------------" + msg);
|
||||
// 按照逻辑来说 后面的 sw null判断是多余的 但是未能发现具体原因
|
||||
if (Init() && sw != null)
|
||||
{
|
||||
byte[] sendBuffer = Encoding.UTF8.GetBytes(msg);
|
||||
sw.Write(sendBuffer);
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Warning(ex.Message);
|
||||
if (sw != null)
|
||||
{
|
||||
sw.Close();
|
||||
sw.Dispose();
|
||||
}
|
||||
|
||||
if (client != null)
|
||||
{
|
||||
client.Client.Disconnect(false);
|
||||
client.Client.Close();
|
||||
client.Client.Dispose();
|
||||
client.Close();
|
||||
client.Dispose();
|
||||
|
||||
}
|
||||
sw = null;
|
||||
client = null;
|
||||
ipinfo = null;
|
||||
XC_Redis.Redis.SetKey("udpserver", 0, -1);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static void Res()
|
||||
{
|
||||
LogHelp.Warning("Res线程开始");
|
||||
try
|
||||
{
|
||||
while (sw != null && client != null)
|
||||
{
|
||||
|
||||
Stopwatch time = new Stopwatch();
|
||||
var tcplog = new Models.Models.LOGDB.TBL_TCP_LOG();
|
||||
//是否入库
|
||||
bool isdblog = true;
|
||||
|
||||
// 数据是否有效
|
||||
bool isdata = false;
|
||||
|
||||
if (sw.DataAvailable)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
//开始
|
||||
time.Restart();
|
||||
string data;
|
||||
byte[] databyte = new byte[client.Available - 1];
|
||||
List<byte> bytelist = new List<byte>();
|
||||
int s = databyte.Length;
|
||||
while (sw.DataAvailable)
|
||||
{
|
||||
int bytes = sw.Read(databyte, 0, databyte.Length);
|
||||
if (bytes > 0)
|
||||
{
|
||||
bytelist.AddRange(databyte.Take(bytes));
|
||||
}
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
data = Encoding.UTF8.GetString(bytelist.ToArray());
|
||||
if (string.IsNullOrEmpty(data))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
tcplog.CONTENT = data;
|
||||
TCPDATA val = new TCPDATA();
|
||||
LogHelp.Warning("原始数据:" + data);
|
||||
|
||||
|
||||
foreach (var item in data.Split(Environment.NewLine))
|
||||
{
|
||||
if (string.IsNullOrEmpty(item))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
LogHelp.Warning("换行切割包" + item);
|
||||
try
|
||||
{
|
||||
val = JsonConvert.DeserializeObject<TCPDATA>(item);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error("原始数据解析失败~" + ex.ToString());
|
||||
isdata = false;
|
||||
continue;
|
||||
} // throw new Exception();
|
||||
|
||||
LogHelp.Warning($"来自服务器的消息:--CMD:{val.CMD}");
|
||||
|
||||
//记录log
|
||||
tcplog.TCP_ID = val.TCP_ID;
|
||||
isdblog = val.LOG_TO_DB;
|
||||
|
||||
try
|
||||
{
|
||||
switch (val.CMD.ToUpper())
|
||||
{
|
||||
case "RUCS_RCU_LIST":
|
||||
//RUCS_RCU_LIST rUCS_RCU_LIST = JsonConvert.DeserializeObject<RUCS_RCU_LIST>(val.PARAs);
|
||||
XC_Redis.Redis.SetKey("RUCS_RCU_LIST", val.PARAM);
|
||||
break;
|
||||
case "RUCS_DEBUGLOG":
|
||||
//RUCS_RCU_LIST rUCS_RCU_LIST = JsonConvert.DeserializeObject<RUCS_RCU_LIST>(val.PARAs);
|
||||
var RUCS_DEBUGLOG = XC_Redis.Redis.GetKey<List<PARAs_Class>>("RUCS_DEBUGLOG");
|
||||
if (RUCS_DEBUGLOG == null)
|
||||
{
|
||||
RUCS_DEBUGLOG = new List<PARAs_Class>();
|
||||
}
|
||||
RUCS_DEBUGLOG.Add(val.PARAM);
|
||||
XC_Redis.Redis.SetKey("RUCS_DEBUGLOG", RUCS_DEBUGLOG.Take(1000), 60 * 60 * 24 * 3);
|
||||
break;
|
||||
case "RUCS_CONN_UPGRADE_PROGRESS":
|
||||
XC_Redis.Redis.SetKey("RUCS_CONN_UPGRADE_PROGRESS", val.PARAM);
|
||||
break;
|
||||
case "RUCS_CONN_UPGRADE_RESULTS":
|
||||
XC_Redis.Redis.SetKey("RUCS_CONN_UPGRADE_RESULTS", val.PARAM);
|
||||
break;
|
||||
default:
|
||||
isdata = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
isdata = false;
|
||||
//throw new Exception("PARAs解析过程失败~" + ex.Message);
|
||||
}
|
||||
|
||||
tcplog.RESULT = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcplog.REMARK = ex.Message;
|
||||
tcplog.RESULT = 0;
|
||||
LogHelp.Error(ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
|
||||
//停止
|
||||
time.Stop();
|
||||
|
||||
//获取毫秒
|
||||
tcplog.PROCESSING_TIMESPENT = int.Parse(time.ElapsedMilliseconds.ToString());
|
||||
|
||||
tcplog.RESULT = isdata ? 1 : 0;
|
||||
|
||||
if (isdata == false)
|
||||
{
|
||||
tcplog.DATA_VALID = 0;
|
||||
tcplog.RESULT = 0;
|
||||
}
|
||||
tcplog.DESTINATION_PORT = ipinfo.Port.ToString();
|
||||
tcplog.DESTINATION = ipinfo.Address.ToString();
|
||||
tcplog.SOURCE_PORT = ConfigEntity.Instance.RUCS_PROT.ToString();
|
||||
tcplog.SOURCE_IP = ConfigEntity.Instance.RUCS_IP.ToString();
|
||||
if (isdblog || ConfigEntity.Instance.DEV)
|
||||
{
|
||||
tcplog.REMARK = tcplog.REMARK != null ? tcplog.REMARK.Substring(0, tcplog.REMARK.Length > 100 ? 100 : tcplog.REMARK.Length) : null;
|
||||
var db = XC_Data.GetLogDataBase();
|
||||
db.TBL_RUCS_TCP_LOGS.Add(tcplog);
|
||||
try
|
||||
{
|
||||
db.SaveChanges();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Warning("数据库保存失败~ " + ex.StackTrace + ex.Message);
|
||||
}
|
||||
}
|
||||
XC_Redis.Redis.SetKey("udpserver", 0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (ThreadInterruptedException ex)
|
||||
{
|
||||
LogHelp.Warning("线程中止 " + ex.Message);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
XC_Redis.Redis.SetKey("udpserver", 0, -1);
|
||||
|
||||
LogHelp.Error(" Client RES 错误" + ex.ToString());
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sw != null)
|
||||
{
|
||||
sw.Close();
|
||||
sw.Dispose();
|
||||
}
|
||||
|
||||
if (client != null)
|
||||
{
|
||||
client.Client.Close();
|
||||
client.Client.Dispose();
|
||||
client.Close();
|
||||
client.Dispose();
|
||||
}
|
||||
|
||||
sw = null;
|
||||
client = null;
|
||||
ipinfo = null;
|
||||
LogHelp.Warning("Res线程结束 finally");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送命令
|
||||
/// </summary>
|
||||
public class SendHelp
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取rcu 列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static bool Send_RUCS_RCU_LIST()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
TCPDATA data = CMD();
|
||||
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
||||
timeFormat.DateTimeFormat = "MM-dd-yyyy hh:mm:ss.fff";
|
||||
data.PARAM = new PARAs_Class { QUANTITY = 0 };
|
||||
data.TRACKING_INFO = new TrackingInfo { TRANSACTION_ID = data.TCP_ID + GetNumPwd(4), TRACKING_TYPE = incident._eventTypeDevUpgrade };
|
||||
return Tcp.Send(JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat).Replace(Environment.NewLine, "") + Environment.NewLine);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 控制下发,暂未实现
|
||||
/// </summary>
|
||||
/// <param name="mac"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Send_Control_RCU_DevIssued(string mac, string content = "~")
|
||||
{
|
||||
try
|
||||
{
|
||||
TCPDATA data = CMD();
|
||||
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
||||
timeFormat.DateTimeFormat = "MM-dd-yyyy hh:mm:ss.fff";
|
||||
data.PARAM = new PARAs_Class { MAC = mac };
|
||||
data.CMD = RCU_CMD.CONTROL_RCU_DEVISSUED.ToString();
|
||||
return Tcp.Send(JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat).Replace(Environment.NewLine, "") + Environment.NewLine);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RUCS 升级下发
|
||||
/// </summary>
|
||||
/// <param name="mac"></param>
|
||||
/// <param name="type">文件类型; 1: app固件; 2: 配置;</param>
|
||||
/// <returns></returns>
|
||||
public static bool Send_Control_UPGRADE_ISSUED(string mac, int type = 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
TCPDATA data = CMD();
|
||||
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
||||
timeFormat.DateTimeFormat = "MM-dd-yyyy hh:mm:ss.fff";
|
||||
data.PARAM = new PARAs_Class { MAC = mac };
|
||||
data.PARAM.Upgrade_File_Type = type;
|
||||
data.TRACKING_INFO= new TrackingInfo { TRANSACTION_ID =data.TCP_ID + GetNumPwd(4), TRACKING_TYPE= incident._eventTypeDevUpgrade };
|
||||
data.CMD = RCU_CMD.RCU_UPGRADE_ISSUED.ToString();
|
||||
|
||||
return Tcp.Send(JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat).Replace(Environment.NewLine, "") + Environment.NewLine);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static TCPDATA CMD()
|
||||
{
|
||||
try
|
||||
{
|
||||
TCPDATA data = new TCPDATA();
|
||||
data.TCP_ID = "WEB_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||||
data.CMD = RCU_CMD.GET_RUCS_RCU_LIST.ToString();
|
||||
data.LOG_TO_DB = true;
|
||||
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
||||
timeFormat.DateTimeFormat = "MM-dd-yyyy hh:mm:ss.fff";
|
||||
return data;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelp.Error(ex.ToString());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public static string GetNumPwd(int num)//生成数字随机数
|
||||
{
|
||||
string a = "0123456789";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class HTLE
|
||||
{
|
||||
public static bool PortInUse(int port)
|
||||
{
|
||||
bool inUse = false;
|
||||
|
||||
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
|
||||
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
|
||||
|
||||
foreach (IPEndPoint endPoint in ipEndPoints)
|
||||
{
|
||||
if (endPoint.Port == port)
|
||||
{
|
||||
inUse = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return inUse; // 返回true说明端口被占用
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
13
TcpServer/TcpServer.csproj
Normal file
13
TcpServer/TcpServer.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\COMMON\COMMON.csproj" />
|
||||
<ProjectReference Include="..\Models\Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
39
TcpServer/incident.cs
Normal file
39
TcpServer/incident.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TcpServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件追踪类型
|
||||
/// </summary>
|
||||
public static class incident
|
||||
{
|
||||
///<summary>
|
||||
/// 事件追踪类型-设备在线状态变化
|
||||
/// </summary>
|
||||
public static string _eventTypeDevOnline = "DEV_ONLINE";
|
||||
|
||||
/// <summary>
|
||||
/// 事件追踪类型-设备状态变化
|
||||
/// </summary>
|
||||
public static string _eventTypeDevStatus = "DEV_STATUS";
|
||||
|
||||
/// <summary>
|
||||
/// 事件追踪类型-设备操作
|
||||
/// </summary>
|
||||
public static string _eventTypeDevControl = "DEV_OPERATION";
|
||||
|
||||
/// <summary>
|
||||
/// 事件追踪类型-设备升级
|
||||
/// </summary>
|
||||
public static string _eventTypeDevUpgrade = "DEV_UPGRADE";
|
||||
|
||||
/// <summary>
|
||||
/// 事件追踪类型-设备通讯测试
|
||||
/// </summary>
|
||||
public static string _eventTypeDevConnTest = "DEV_CONNTEST";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user