|
@@ -1,276 +1,276 @@
|
|
|
-using SecsGem.Core;
|
|
|
-using SecsGem.Core.Encoder;
|
|
|
-using SecsGem.Core.EnumData;
|
|
|
-using SecsGem.Core.ItemModel;
|
|
|
-using SecsGem.Core.SecsEnumData;
|
|
|
-using System;
|
|
|
-using System.Buffers.Binary;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
-using System.Net.Sockets;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
-
|
|
|
-namespace EapClientSimulator
|
|
|
-{
|
|
|
- public class EapClient
|
|
|
- {
|
|
|
- Socket clientSocket = null;
|
|
|
- byte[] receiveBuffer = new byte[8192];
|
|
|
-
|
|
|
- byte[] buffer=new byte[8192];
|
|
|
- int bufferLength = 0;
|
|
|
- public event EventHandler<string> OnDisConnected;
|
|
|
- public event EventHandler<string> OnReceiveData;
|
|
|
- private MessageEncoder encoder = new SecsMessageEncoder();
|
|
|
- public EapClient()
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public bool Connect(string ip,int Port)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
- clientSocket.Connect(ip, Port);
|
|
|
- clientSocket.BeginReceive(receiveBuffer,0,receiveBuffer.Length, SocketFlags.None, ReceiveCallback, clientSocket);
|
|
|
- SendSelectedRequestMessage();
|
|
|
- return true;
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void SendSelectedRequestMessage()
|
|
|
- {
|
|
|
- byte[] buffer = EncodeControlMessageResponse(SType.SelectRequest, IdGenerator.GenerateTransactionId());
|
|
|
- try
|
|
|
- {
|
|
|
- clientSocket.Send(buffer);
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- Close();
|
|
|
- }
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 控制消息长度
|
|
|
- /// </summary>
|
|
|
- private readonly byte[] ControlMessageLengthBytes = new byte[] { 0, 0, 0, 10 };
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 控制消息回复编码
|
|
|
- /// </summary>
|
|
|
- /// <param name="msgType"></param>
|
|
|
- /// <param name="id"></param>
|
|
|
- /// <returns></returns>
|
|
|
- private byte[] EncodeControlMessageResponse(SType msgType, int id)
|
|
|
- {
|
|
|
- var buffer = new byte[14];
|
|
|
- Array.Copy(ControlMessageLengthBytes, 0, buffer, 0, ControlMessageLengthBytes.Length);
|
|
|
- buffer[4] = 0xff;
|
|
|
- buffer[5] = 0xff;
|
|
|
- buffer[6] = 0;
|
|
|
- buffer[7] = 0;
|
|
|
- buffer[8] = 0;
|
|
|
- buffer[9] = (byte)msgType;
|
|
|
- byte[] ids = BitConverter.GetBytes(id);
|
|
|
- Array.Reverse(ids);
|
|
|
- Array.Copy(ids, 0, buffer, 10, ids.Length);
|
|
|
- return buffer;
|
|
|
- }
|
|
|
-
|
|
|
- private void ReceiveCallback(IAsyncResult ar)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- int count= clientSocket.EndReceive(ar);
|
|
|
- if(count!=0)
|
|
|
- {
|
|
|
- byte[] byt= new byte[count];
|
|
|
- Array.Copy(receiveBuffer, 0, byt, 0, count);
|
|
|
-
|
|
|
- if (count+bufferLength < 14)
|
|
|
- {
|
|
|
- Array.Copy(byt,0,buffer,bufferLength,byt.Length);
|
|
|
- bufferLength += byt.Length;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- byte[] bytes = new byte[count + bufferLength] ;
|
|
|
- Array.Copy(buffer, 0, bytes, 0, bufferLength);
|
|
|
- Array.Copy(byt, 0, bytes, bufferLength, count);
|
|
|
- bool result=AnalyseMessage(bytes);
|
|
|
- if (result)
|
|
|
- {
|
|
|
- bufferLength = 0;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Array.Copy(bytes, 0, buffer, 0, bytes.Length);
|
|
|
- bufferLength = bytes.Length;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Close();
|
|
|
- return;
|
|
|
- }
|
|
|
- clientSocket.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ReceiveCallback, clientSocket);
|
|
|
-
|
|
|
- }
|
|
|
- catch(Exception ex)
|
|
|
- {
|
|
|
- Close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private bool AnalyseMessage(byte[] byt)
|
|
|
- {
|
|
|
- if(byt.Length>=14)
|
|
|
- {
|
|
|
- byte[] lengthByt = new byte[4];
|
|
|
- Array.Copy(byt, 0, lengthByt, 0, 4);
|
|
|
- uint messageLength = BinaryPrimitives.ReadUInt32BigEndian(lengthByt);
|
|
|
-
|
|
|
-
|
|
|
- if(messageLength>byt.Length-4)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- byte[] headerByt = new byte[10];
|
|
|
- Array.Copy(byt, 4, headerByt, 0, 10);
|
|
|
- SecsMessageHeaderDecoder decode = new SecsMessageHeaderDecoder();
|
|
|
- decode.DecodeHsmsHeader(headerByt, out var header);
|
|
|
-
|
|
|
-
|
|
|
- if (byt.Length > 14)
|
|
|
- {
|
|
|
- byte[] dataBuffer = new byte[byt.Length - 14];
|
|
|
- Array.Copy(byt, 14, dataBuffer, 0, dataBuffer.Length);
|
|
|
- Item item = null;
|
|
|
- int startIndex = 0;
|
|
|
- try
|
|
|
- {
|
|
|
- item = ItemDecoder.Decode(dataBuffer, ref startIndex);
|
|
|
- if (item == null)
|
|
|
- {
|
|
|
- Close();
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- SecsMessage secsMessage = new SecsMessage(header, item);
|
|
|
- OnReceiveData(this, SmlSerializationUtil.SerializeItemToString(secsMessage));
|
|
|
- if (secsMessage.Header.ReplyFlag)
|
|
|
- {
|
|
|
- Reply(secsMessage);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- SecsMessage secsMessage=new SecsMessage(header,null);
|
|
|
- if(header.SType==SType.LinkTestRequest)
|
|
|
- {
|
|
|
- OnReceiveData(this, "received link test request");
|
|
|
- SecsMessage replyMessage = SecsMessageReplyUtil.CreateLinkTestReplyMessage(header.TransactionId);
|
|
|
- SendMessage(replyMessage);
|
|
|
- OnReceiveData(this, "send link test response");
|
|
|
- }
|
|
|
- else if(header.SType==SType.DataMessage)
|
|
|
- {
|
|
|
- OnReceiveData(this, SmlSerializationUtil.SerializeItemToString(secsMessage));
|
|
|
- if (header.ReplyFlag)
|
|
|
- {
|
|
|
- Reply(secsMessage);
|
|
|
- }
|
|
|
- }
|
|
|
- else if(header.SType==SType.SelectResponse)
|
|
|
- {
|
|
|
- OnReceiveData(this,"received selected response");
|
|
|
- SecsMessage s1f13Message = SecsMessageSendUtil.CreateS1F13Message();
|
|
|
- SendMessage(s1f13Message);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- private void Reply(SecsMessage secsMessage)
|
|
|
- {
|
|
|
- SecsMessage replyMessage = null;
|
|
|
- if(secsMessage.Header.Stream==1)
|
|
|
- {
|
|
|
- if (secsMessage.Header.Function == 13)
|
|
|
- {
|
|
|
- // replyMessage= SecsMessageReplyUtil.CreateS1F13ReplyMessage(secsMessage.Header.TransactionId, SecsCOMMACK.Accepted);
|
|
|
- }
|
|
|
- else if(secsMessage.Header.Function==1)
|
|
|
- {
|
|
|
- replyMessage = SecsMessageReplyUtil.CreateS1F1ReplyMessage(secsMessage.Header.TransactionId);
|
|
|
- }
|
|
|
- }
|
|
|
- else if(secsMessage.Header.Stream==5)
|
|
|
- {
|
|
|
- if(secsMessage.Header.Function==1)
|
|
|
- {
|
|
|
- replyMessage = SecsMessageReplyUtil.CreateS5F1ReplyMessage(secsMessage.Header.TransactionId);
|
|
|
- }
|
|
|
- }
|
|
|
- else if (secsMessage.Header.Stream == 6)
|
|
|
- {
|
|
|
- if (secsMessage.Header.Function == 11)
|
|
|
- {
|
|
|
- replyMessage = SecsMessageReplyUtil.CreateS6F11ReplyMessage(secsMessage.Header.TransactionId);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- replyMessage = SecsReplyMessageFactory.CreateSxF0Message(secsMessage.Header);
|
|
|
- }
|
|
|
-
|
|
|
- if (replyMessage != null)
|
|
|
- {
|
|
|
- OnReceiveData(this, SmlSerializationUtil.SerializeItemToString(replyMessage));
|
|
|
- SendMessage(replyMessage);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void SendMessage(SecsMessage secsMessage)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- byte[] bytes = encoder.EncodeMessage(secsMessage);
|
|
|
- clientSocket.Send(bytes);
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- Close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void Close()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- clientSocket.Close();
|
|
|
- clientSocket.Dispose();
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
- if(OnDisConnected!=null)
|
|
|
- {
|
|
|
- OnDisConnected(this, "");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+using SecsGem.Core;
|
|
|
+using SecsGem.Core.Encoder;
|
|
|
+using SecsGem.Core.EnumData;
|
|
|
+using SecsGem.Core.ItemModel;
|
|
|
+using SecsGem.Core.SecsEnumData;
|
|
|
+using System;
|
|
|
+using System.Buffers.Binary;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Sockets;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace EapClientSimulator
|
|
|
+{
|
|
|
+ public class EapClient
|
|
|
+ {
|
|
|
+ Socket clientSocket = null;
|
|
|
+ byte[] receiveBuffer = new byte[8192];
|
|
|
+
|
|
|
+ byte[] buffer=new byte[8192];
|
|
|
+ int bufferLength = 0;
|
|
|
+ public event EventHandler<string> OnDisConnected;
|
|
|
+ public event EventHandler<string> OnReceiveData;
|
|
|
+ private MessageEncoder encoder = new SecsMessageEncoder();
|
|
|
+ public EapClient()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Connect(string ip,int Port)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
+ clientSocket.Connect(ip, Port);
|
|
|
+ clientSocket.BeginReceive(receiveBuffer,0,receiveBuffer.Length, SocketFlags.None, ReceiveCallback, clientSocket);
|
|
|
+ SendSelectedRequestMessage();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SendSelectedRequestMessage()
|
|
|
+ {
|
|
|
+ byte[] buffer = EncodeControlMessageResponse(SType.SelectRequest, IdGenerator.GenerateTransactionId());
|
|
|
+ try
|
|
|
+ {
|
|
|
+ clientSocket.Send(buffer);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 控制消息长度
|
|
|
+ /// </summary>
|
|
|
+ private readonly byte[] ControlMessageLengthBytes = new byte[] { 0, 0, 0, 10 };
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 控制消息回复编码
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="msgType"></param>
|
|
|
+ /// <param name="id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private byte[] EncodeControlMessageResponse(SType msgType, int id)
|
|
|
+ {
|
|
|
+ var buffer = new byte[14];
|
|
|
+ Array.Copy(ControlMessageLengthBytes, 0, buffer, 0, ControlMessageLengthBytes.Length);
|
|
|
+ buffer[4] = 0xff;
|
|
|
+ buffer[5] = 0xff;
|
|
|
+ buffer[6] = 0;
|
|
|
+ buffer[7] = 0;
|
|
|
+ buffer[8] = 0;
|
|
|
+ buffer[9] = (byte)msgType;
|
|
|
+ byte[] ids = BitConverter.GetBytes(id);
|
|
|
+ Array.Reverse(ids);
|
|
|
+ Array.Copy(ids, 0, buffer, 10, ids.Length);
|
|
|
+ return buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ReceiveCallback(IAsyncResult ar)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ int count= clientSocket.EndReceive(ar);
|
|
|
+ if(count!=0)
|
|
|
+ {
|
|
|
+ byte[] byt= new byte[count];
|
|
|
+ Array.Copy(receiveBuffer, 0, byt, 0, count);
|
|
|
+
|
|
|
+ if (count+bufferLength < 14)
|
|
|
+ {
|
|
|
+ Array.Copy(byt,0,buffer,bufferLength,byt.Length);
|
|
|
+ bufferLength += byt.Length;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ byte[] bytes = new byte[count + bufferLength] ;
|
|
|
+ Array.Copy(buffer, 0, bytes, 0, bufferLength);
|
|
|
+ Array.Copy(byt, 0, bytes, bufferLength, count);
|
|
|
+ bool result=AnalyseMessage(bytes);
|
|
|
+ if (result)
|
|
|
+ {
|
|
|
+ bufferLength = 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Array.Copy(bytes, 0, buffer, 0, bytes.Length);
|
|
|
+ bufferLength = bytes.Length;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Close();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ clientSocket.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ReceiveCallback, clientSocket);
|
|
|
+
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool AnalyseMessage(byte[] byt)
|
|
|
+ {
|
|
|
+ if(byt.Length>=14)
|
|
|
+ {
|
|
|
+ byte[] lengthByt = new byte[4];
|
|
|
+ Array.Copy(byt, 0, lengthByt, 0, 4);
|
|
|
+ uint messageLength = BinaryPrimitives.ReadUInt32BigEndian(lengthByt);
|
|
|
+
|
|
|
+
|
|
|
+ if(messageLength>byt.Length-4)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] headerByt = new byte[10];
|
|
|
+ Array.Copy(byt, 4, headerByt, 0, 10);
|
|
|
+ SecsMessageHeaderDecoder decode = new SecsMessageHeaderDecoder();
|
|
|
+ decode.DecodeHsmsHeader(headerByt, out var header);
|
|
|
+
|
|
|
+
|
|
|
+ if (byt.Length > 14)
|
|
|
+ {
|
|
|
+ byte[] dataBuffer = new byte[byt.Length - 14];
|
|
|
+ Array.Copy(byt, 14, dataBuffer, 0, dataBuffer.Length);
|
|
|
+ Item item = null;
|
|
|
+ int startIndex = 0;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ item = ItemDecoder.Decode(dataBuffer, ref startIndex);
|
|
|
+ if (item == null)
|
|
|
+ {
|
|
|
+ Close();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ SecsMessage secsMessage = new SecsMessage(header, item);
|
|
|
+ OnReceiveData(this, SmlSerializationUtil.SerializeItemToString(secsMessage));
|
|
|
+ if (secsMessage.Header.ReplyFlag)
|
|
|
+ {
|
|
|
+ Reply(secsMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ SecsMessage secsMessage=new SecsMessage(header,null);
|
|
|
+ if(header.SType==SType.LinkTestRequest)
|
|
|
+ {
|
|
|
+ OnReceiveData(this, "received link test request");
|
|
|
+ SecsMessage replyMessage = SecsMessageReplyUtil.CreateLinkTestReplyMessage(header.TransactionId);
|
|
|
+ SendMessage(replyMessage);
|
|
|
+ OnReceiveData(this, "send link test response");
|
|
|
+ }
|
|
|
+ else if(header.SType==SType.DataMessage)
|
|
|
+ {
|
|
|
+ OnReceiveData(this, SmlSerializationUtil.SerializeItemToString(secsMessage));
|
|
|
+ if (header.ReplyFlag)
|
|
|
+ {
|
|
|
+ Reply(secsMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(header.SType==SType.SelectResponse)
|
|
|
+ {
|
|
|
+ OnReceiveData(this,"received selected response");
|
|
|
+ SecsMessage s1f13Message = SecsMessageSendUtil.CreateS1F13Message();
|
|
|
+ SendMessage(s1f13Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Reply(SecsMessage secsMessage)
|
|
|
+ {
|
|
|
+ SecsMessage replyMessage = null;
|
|
|
+ if(secsMessage.Header.Stream==1)
|
|
|
+ {
|
|
|
+ if (secsMessage.Header.Function == 13)
|
|
|
+ {
|
|
|
+ // replyMessage= SecsMessageReplyUtil.CreateS1F13ReplyMessage(secsMessage.Header.TransactionId, SecsCOMMACK.Accepted);
|
|
|
+ }
|
|
|
+ else if(secsMessage.Header.Function==1)
|
|
|
+ {
|
|
|
+ replyMessage = SecsMessageReplyUtil.CreateS1F1ReplyMessage(secsMessage.Header.TransactionId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(secsMessage.Header.Stream==5)
|
|
|
+ {
|
|
|
+ if(secsMessage.Header.Function==1)
|
|
|
+ {
|
|
|
+ replyMessage = SecsMessageReplyUtil.CreateS5F1ReplyMessage(secsMessage.Header.TransactionId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (secsMessage.Header.Stream == 6)
|
|
|
+ {
|
|
|
+ if (secsMessage.Header.Function == 11)
|
|
|
+ {
|
|
|
+ replyMessage = SecsMessageReplyUtil.CreateS6F11ReplyMessage(secsMessage.Header.TransactionId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ replyMessage = SecsReplyMessageFactory.CreateSxF0Message(secsMessage.Header);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (replyMessage != null)
|
|
|
+ {
|
|
|
+ OnReceiveData(this, SmlSerializationUtil.SerializeItemToString(replyMessage));
|
|
|
+ SendMessage(replyMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SendMessage(SecsMessage secsMessage)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ byte[] bytes = encoder.EncodeMessage(secsMessage);
|
|
|
+ clientSocket.Send(bytes);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ Close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Close()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ clientSocket.Close();
|
|
|
+ clientSocket.Dispose();
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ if(OnDisConnected!=null)
|
|
|
+ {
|
|
|
+ OnDisConnected(this, "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|