|
|
@@ -0,0 +1,576 @@
|
|
|
+using Newtonsoft.Json;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
+using System.Data;
|
|
|
+using System.Drawing;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Net.Sockets;
|
|
|
+using System.Text;
|
|
|
+using System.Windows.Forms;
|
|
|
+using UAS_MES_NEW.DataOperate;
|
|
|
+using UAS_MES_NEW.Entity;
|
|
|
+
|
|
|
+namespace UAS_MES_NEW.Make
|
|
|
+{
|
|
|
+ public partial class Make_ServerListen : Form
|
|
|
+ {
|
|
|
+ public Make_ServerListen()
|
|
|
+ {
|
|
|
+ InitializeComponent();
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder SQL = new StringBuilder();
|
|
|
+ DataTable dt;
|
|
|
+ DataHelper dh;
|
|
|
+
|
|
|
+ private SocketServer _server;
|
|
|
+ private int currCount = 0;
|
|
|
+
|
|
|
+ private void Make_ServerListen_Load(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ dh = SystemInf.dh;
|
|
|
+
|
|
|
+ _server = new SocketServer();
|
|
|
+ _server.ReceivedDatas += SocketServer_ReceivedDatas;
|
|
|
+ _server.ReceivedMsg += SocketServer_ReceivedMsg;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
|
|
|
+
|
|
|
+ foreach (IPAddress ip in hostEntry.AddressList)
|
|
|
+ {
|
|
|
+ if (ip.AddressFamily == AddressFamily.InterNetwork)
|
|
|
+ {
|
|
|
+ IpPort.Text = ip.ToString() + ":8088";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(IpPort.Text))
|
|
|
+ {
|
|
|
+ IpPort.Text = "127.0.0.1:8088";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ ShowMsg(0, $"获取本机IPv4地址失败,请手动填写,{ex.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Connet_Click(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ if (Connet.Text == "开启连接")
|
|
|
+ {
|
|
|
+ Connet.Text = "关闭连接";
|
|
|
+ }
|
|
|
+ else if (Connet.Text == "关闭连接")
|
|
|
+ {
|
|
|
+ Connet.Text = "开启连接";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Start_Click(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(IpPort.Text))
|
|
|
+ {
|
|
|
+ ShowMsg(0, "请填写服务地址");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ string ip = IpPort.Text.Split(':')[0];
|
|
|
+ int port = int.Parse(IpPort.Text.Split(':')[1]);
|
|
|
+ IPAddress ipAddress = IPAddress.Parse(ip);
|
|
|
+
|
|
|
+ if (Start.Text == "开启监听")
|
|
|
+ {
|
|
|
+ Start.Text = "关闭监听";
|
|
|
+ IpPort.Enabled = false;
|
|
|
+
|
|
|
+ string result = _server.Start(ipAddress, port, 10);
|
|
|
+ ShowMsg(result.StartsWith("OK") ? 1 : 0, result);
|
|
|
+ }
|
|
|
+ else if (Start.Text == "关闭监听")
|
|
|
+ {
|
|
|
+ Start.Text = "开启监听";
|
|
|
+ IpPort.Enabled = true;
|
|
|
+
|
|
|
+ if (_server != null)
|
|
|
+ {
|
|
|
+ string result = _server.Stop();
|
|
|
+ ShowMsg(result.StartsWith("OK") ? 1 : 0, result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SocketServer_ReceivedDatas(object sender, ReceivedDatasEventArgs e)
|
|
|
+ {
|
|
|
+ this.Invoke(new Action(() =>
|
|
|
+ {
|
|
|
+ currCount += 1;
|
|
|
+ ShowMsg(1, $"第{currCount}次接收,收到来自 {e.ClientId} 的消息: {e.Message}");
|
|
|
+
|
|
|
+ /*VerifyResultMessage message = new VerifyResultMessage
|
|
|
+ {
|
|
|
+ Name = "VerifyResult",
|
|
|
+ Data = new VerifyResultData
|
|
|
+ {
|
|
|
+ ErrorCode = "0",
|
|
|
+ ErrorMsg = "",
|
|
|
+ Serial = "BG7349116"
|
|
|
+ }
|
|
|
+ };
|
|
|
+ string jsonMessage = JsonConvert.SerializeObject(message, Formatting.None);
|
|
|
+ string sendMsg = _server.SendMessage(e.ClientId, jsonMessage);
|
|
|
+ ShowMsg(sendMsg.StartsWith("OK") ? 1 : 0, sendMsg);
|
|
|
+ currCount = currCount == 2 ? 0 : currCount;*/
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SocketServer_ReceivedMsg(object sender, ReceivedMsgEventArgs e)
|
|
|
+ {
|
|
|
+ this.Invoke(new Action(() =>
|
|
|
+ {
|
|
|
+ if (e.Msg.StartsWith("OK"))
|
|
|
+ {
|
|
|
+ ShowMsg(1, $"提示 [{e.Operation}]: {e.Msg}");
|
|
|
+ }
|
|
|
+ else if (e.Msg.StartsWith("NG"))
|
|
|
+ {
|
|
|
+ ShowMsg(2, $"提示 [{e.Operation}]: {e.Msg}");
|
|
|
+ }
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateSN(string type, string sn)
|
|
|
+ {
|
|
|
+ if (type == "C")
|
|
|
+ {
|
|
|
+ serialNumber.Text = "";
|
|
|
+ workOrder.Text = "";
|
|
|
+ productCode.Text = "";
|
|
|
+ productName.Text = "";
|
|
|
+ }
|
|
|
+ else if (type == "L")
|
|
|
+ {
|
|
|
+ SQL.Clear();
|
|
|
+ SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product
|
|
|
+ WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code");
|
|
|
+ dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
|
|
|
+ if (dt.Rows.Count > 0)
|
|
|
+ {
|
|
|
+ serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString();
|
|
|
+ workOrder.Text = dt.Rows[0]["ma_code"].ToString();
|
|
|
+ productCode.Text = dt.Rows[0]["pr_code"].ToString();
|
|
|
+ productName.Text = dt.Rows[0]["pr_spec"].ToString();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ UpdateSN("C", sn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ShowMsg(int type, string msg)
|
|
|
+ {
|
|
|
+ msg = msg.Replace("\r", "").Replace("\n", "");
|
|
|
+ string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ string showMsg = $"{msgTime}: {msg}\n";
|
|
|
+
|
|
|
+ if (type == 0)
|
|
|
+ {
|
|
|
+ OperatResult.AppendText(showMsg, Color.Red);
|
|
|
+ }
|
|
|
+ else if (type == 1)
|
|
|
+ {
|
|
|
+ OperatResult.AppendText(showMsg, Color.Green);
|
|
|
+ }
|
|
|
+ else if (type == 2)
|
|
|
+ {
|
|
|
+ OperatResult.AppendText(showMsg, Color.YellowGreen);
|
|
|
+ }
|
|
|
+ OperatResult.SelectionStart = OperatResult.Text.Length;
|
|
|
+ OperatResult.ScrollToCaret();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public class VerifyResultMessage
|
|
|
+ {
|
|
|
+ [JsonProperty("Name")]
|
|
|
+ public string Name { get; set; } = "VerifyResult";
|
|
|
+
|
|
|
+ [JsonProperty("Data")]
|
|
|
+ public VerifyResultData Data { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class VerifyResultData
|
|
|
+ {
|
|
|
+ [JsonProperty("ErrorCode")]
|
|
|
+ public string ErrorCode { get; set; } = "0";
|
|
|
+
|
|
|
+ [JsonProperty("ErrorMsg")]
|
|
|
+ public string ErrorMsg { get; set; } = "";
|
|
|
+
|
|
|
+ [JsonProperty("Serial")]
|
|
|
+ public string Serial { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class ReceivedDatasEventArgs : EventArgs
|
|
|
+ {
|
|
|
+ public string Message { get; set; }
|
|
|
+ public Socket ClientSocket { get; set; }
|
|
|
+ public string ClientId { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class ReceivedMsgEventArgs : EventArgs
|
|
|
+ {
|
|
|
+ public string Msg { get; set; }
|
|
|
+ public Exception Exception { get; set; }
|
|
|
+ public string Operation { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class SocketServer
|
|
|
+ {
|
|
|
+ private Socket _serverSocket;
|
|
|
+ private bool _isRunning = false;
|
|
|
+ private readonly Dictionary<string, Socket> _clients = new Dictionary<string, Socket>();
|
|
|
+
|
|
|
+ private class StateObject
|
|
|
+ {
|
|
|
+ public Socket ClientSocket { get; set; }
|
|
|
+ public string ClientId { get; set; }
|
|
|
+ public byte[] Buffer { get; set; }
|
|
|
+ public List<byte> DataBuffer { get; set; } = new List<byte>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public event EventHandler<ReceivedDatasEventArgs> ReceivedDatas;
|
|
|
+
|
|
|
+ public event EventHandler<ReceivedMsgEventArgs> ReceivedMsg;
|
|
|
+
|
|
|
+ public string Start(IPAddress _ipAddress, int _port, int _maxConnections)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (_isRunning)
|
|
|
+ {
|
|
|
+ return "OK,服务器已经在运行中";
|
|
|
+ }
|
|
|
+
|
|
|
+ _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
+ IPEndPoint localEndPoint = new IPEndPoint(_ipAddress, _port);
|
|
|
+ _serverSocket.Bind(localEndPoint);
|
|
|
+ _serverSocket.Listen(_maxConnections);
|
|
|
+ _isRunning = true;
|
|
|
+
|
|
|
+ _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), _serverSocket);
|
|
|
+
|
|
|
+ return "OK,服务器开始监听";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return $"NG,启动服务器失败: {ex.Message}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public string Stop()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!_isRunning)
|
|
|
+ {
|
|
|
+ return "OK,服务器未运行";
|
|
|
+ }
|
|
|
+
|
|
|
+ _isRunning = false;
|
|
|
+
|
|
|
+ List<string> clientIds;
|
|
|
+ lock (_clients)
|
|
|
+ {
|
|
|
+ clientIds = new List<string>(_clients.Keys);
|
|
|
+ foreach (var clientId in clientIds)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (_clients.TryGetValue(clientId, out var clientSocket))
|
|
|
+ {
|
|
|
+ if (clientSocket != null && clientSocket.Connected)
|
|
|
+ {
|
|
|
+ clientSocket.Shutdown(SocketShutdown.Both);
|
|
|
+ clientSocket.Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return $"NG,停止服务器异常: {ex.Message}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _clients.Clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_serverSocket != null)
|
|
|
+ {
|
|
|
+ _serverSocket.Close();
|
|
|
+ _serverSocket.Dispose();
|
|
|
+ _serverSocket = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return "OK,关闭服务器成功";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return $"NG,停止服务器失败: {ex.Message}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void AcceptCallback(IAsyncResult ar)
|
|
|
+ {
|
|
|
+ if (!_isRunning) return;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Socket serverSocket = (Socket)ar.AsyncState;
|
|
|
+ Socket clientSocket = serverSocket.EndAccept(ar);
|
|
|
+
|
|
|
+ string clientId = GenerateClientId(clientSocket);
|
|
|
+
|
|
|
+ lock (_clients)
|
|
|
+ {
|
|
|
+ _clients[clientId] = clientSocket;
|
|
|
+ }
|
|
|
+
|
|
|
+ OnReceivedMsg($"OK,客户端连接成功: {clientId}", "AcceptCallback", null);
|
|
|
+
|
|
|
+ StateObject state = new StateObject
|
|
|
+ {
|
|
|
+ ClientSocket = clientSocket,
|
|
|
+ ClientId = clientId,
|
|
|
+ Buffer = new byte[8192]
|
|
|
+ };
|
|
|
+
|
|
|
+ clientSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
|
|
|
+
|
|
|
+ serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), serverSocket);
|
|
|
+ }
|
|
|
+ catch (ObjectDisposedException)
|
|
|
+ {
|
|
|
+ return; // 服务器已关闭,正常退出
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ OnReceivedMsg("NG,接受客户端连接失败", "AcceptCallback", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ReceiveCallback(IAsyncResult ar)
|
|
|
+ {
|
|
|
+ StateObject state = null;
|
|
|
+ int bytesRead = 0;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ state = (StateObject)ar.AsyncState;
|
|
|
+ bytesRead = state.ClientSocket.EndReceive(ar);
|
|
|
+
|
|
|
+ if (bytesRead > 0)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < bytesRead; i++)
|
|
|
+ {
|
|
|
+ state.DataBuffer.Add(state.Buffer[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ ProcessReceivedData(state, bytesRead);
|
|
|
+
|
|
|
+ Array.Clear(state.Buffer, 0, state.Buffer.Length);
|
|
|
+ state.ClientSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnReceivedMsg($"客户端 {state.ClientId} 断开连接", "ReceiveCallback", null);
|
|
|
+ RemoveClient(state.ClientId, "客户端主动断开连接");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (SocketException sockEx)
|
|
|
+ {
|
|
|
+ string clientId = state?.ClientId ?? "未知客户端";
|
|
|
+ OnReceivedMsg($"NG,接收数据时Socket异常: {sockEx.Message}", "ReceiveCallback", sockEx);
|
|
|
+ RemoveClient(clientId, $"Socket异常: {sockEx.SocketErrorCode}");
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ string clientId = state?.ClientId ?? "未知客户端";
|
|
|
+ OnReceivedMsg("NG,接收数据失败", "ReceiveCallback", ex);
|
|
|
+ RemoveClient(clientId, $"接收数据错误: {ex.Message}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ProcessReceivedData(StateObject state, int bytesRead)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ byte[] allBytes = state.DataBuffer.ToArray();
|
|
|
+ string receiveRes = Encoding.ASCII.GetString(allBytes);
|
|
|
+ OnReceivedDatas(state.ClientSocket, state.ClientId, receiveRes);
|
|
|
+
|
|
|
+ state.DataBuffer.Clear();
|
|
|
+ Array.Clear(state.Buffer, 0, state.Buffer.Length);
|
|
|
+
|
|
|
+ /*if (state.DataBuffer.Count >= 2)
|
|
|
+ {
|
|
|
+ byte[] allBytes = state.DataBuffer.ToArray();
|
|
|
+ string receiveRes = Encoding.Unicode.GetString(allBytes);
|
|
|
+ OnReceivedDatas(state.ClientSocket, state.ClientId, receiveRes);
|
|
|
+
|
|
|
+ string jsonMessage = Encoding.Unicode.GetString(allBytes);
|
|
|
+ jsonMessage = jsonMessage.Replace("\0", "").Trim();
|
|
|
+ if (jsonMessage.Contains("{") && jsonMessage.Contains("}"))
|
|
|
+ {
|
|
|
+ int firstBrace = jsonMessage.IndexOf('{');
|
|
|
+ int lastBrace = jsonMessage.LastIndexOf('}');
|
|
|
+ string validJson = jsonMessage.Substring(firstBrace, lastBrace - firstBrace + 1);
|
|
|
+
|
|
|
+ state.DataBuffer.Clear();
|
|
|
+ OnReceivedDatas(state.ClientSocket, state.ClientId, validJson);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnReceivedMsg($"NG,不是有效JSON格式: {jsonMessage}", "ProcessReceivedData", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Array.Clear(state.Buffer, 0, state.Buffer.Length);*/
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ state.DataBuffer.Clear();
|
|
|
+ Array.Clear(state.Buffer, 0, state.Buffer.Length);
|
|
|
+ OnReceivedMsg($"NG,解码处理异常: {ex.Message}", "ProcessReceivedData", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public string SendMessage(string clientId, string jsonMessage)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!_isRunning)
|
|
|
+ {
|
|
|
+ return "NG,服务器未运行";
|
|
|
+ }
|
|
|
+
|
|
|
+ Socket clientSocket = null;
|
|
|
+ lock (_clients)
|
|
|
+ {
|
|
|
+ if (!_clients.TryGetValue(clientId, out clientSocket) || clientSocket == null)
|
|
|
+ {
|
|
|
+ return $"NG,客户端 {clientId} 不存在或已断开";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!clientSocket.Connected)
|
|
|
+ {
|
|
|
+ RemoveClient(clientId, "连接已断开");
|
|
|
+ return $"NG,客户端 {clientId} 连接已断开";
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] messageBytes = Encoding.Unicode.GetBytes(jsonMessage);
|
|
|
+ clientSocket.BeginSend(messageBytes, 0, messageBytes.Length, SocketFlags.None, new AsyncCallback(SendCallback),
|
|
|
+ new { ClientId = clientId, ClientSocket = clientSocket });
|
|
|
+
|
|
|
+ return $"OK,发送成功,已发送 字符数: {jsonMessage.Length}, 字节数: {messageBytes.Length} 到 {clientId}";
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return $"NG,发送失败: {ex.Message}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SendCallback(IAsyncResult ar)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ dynamic state = ar.AsyncState;
|
|
|
+ Socket clientSocket = state.ClientSocket;
|
|
|
+ string clientId = state.ClientId;
|
|
|
+
|
|
|
+ int bytesSent = clientSocket.EndSend(ar);
|
|
|
+ OnReceivedMsg($"OK,发送完成, 客户端: {clientId}, 字节数: {bytesSent}", "SendCallback", null);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ OnReceivedMsg("NG,发送回调失败", "SendCallback", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void RemoveClient(string clientId, string reason)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Socket clientSocket = null;
|
|
|
+ lock (_clients)
|
|
|
+ {
|
|
|
+ if (_clients.TryGetValue(clientId, out clientSocket))
|
|
|
+ {
|
|
|
+ _clients.Remove(clientId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (clientSocket != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (clientSocket.Connected)
|
|
|
+ {
|
|
|
+ clientSocket.Shutdown(SocketShutdown.Both);
|
|
|
+ }
|
|
|
+ clientSocket.Close();
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ }
|
|
|
+
|
|
|
+ OnReceivedMsg($"OK,移除客户端: {clientId}, 原因: {reason}", "RemoveClient", null);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ OnReceivedMsg($"NG,移除客户端 {clientId} 失败", "RemoveClient", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private string GenerateClientId(Socket clientSocket)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (clientSocket.RemoteEndPoint is IPEndPoint remoteEndPoint)
|
|
|
+ {
|
|
|
+ return $"{remoteEndPoint.Address}:{remoteEndPoint.Port}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
+ return $"Client_{DateTime.Now:yyyyMMddHHmmssfff}_{Guid.NewGuid().ToString().Substring(0, 6)}";
|
|
|
+ }
|
|
|
+
|
|
|
+ protected virtual void OnReceivedDatas(Socket clientSocket, string clientId, string message)
|
|
|
+ {
|
|
|
+ ReceivedDatas?.Invoke(this, new ReceivedDatasEventArgs
|
|
|
+ {
|
|
|
+ ClientSocket = clientSocket,
|
|
|
+ ClientId = clientId,
|
|
|
+ Message = message
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ protected virtual void OnReceivedMsg(string Message, string operation, Exception exception)
|
|
|
+ {
|
|
|
+ ReceivedMsg?.Invoke(this, new ReceivedMsgEventArgs
|
|
|
+ {
|
|
|
+ Msg = Message,
|
|
|
+ Operation = operation,
|
|
|
+ Exception = exception
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|