MCSocket.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Decompiled with JetBrains decompiler
  2. // Type: EFEMRT.Devices.MCSocket
  3. // Assembly: EfemRT, Version=1.8.0.6, Culture=neutral, PublicKeyToken=null
  4. // MVID: DDC7B1DF-D3E3-4D53-8469-205BDD64475F
  5. // Assembly location: D:\EfemRT\EfemRT.exe
  6. using Aitex.Core.RT.Log;
  7. using System;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. namespace athosRT.Devices
  11. {
  12. internal class MCSocket
  13. {
  14. private int m_nPort;
  15. private string m_strAddress;
  16. private Socket m_socket;
  17. private int m_nTimeOut;
  18. public MCSocket() => this.m_nTimeOut = 30000;
  19. ~MCSocket()
  20. {
  21. }
  22. public bool Connected => this.m_socket != null && this.m_socket.Connected;
  23. public bool Open(string strAddress, int nPort, string strLocalAddress)
  24. {
  25. if (this.Connected)
  26. return true;
  27. this.Close();
  28. this.m_strAddress = strAddress;
  29. this.m_nPort = nPort;
  30. this.m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  31. try
  32. {
  33. this.m_socket.Bind((EndPoint) new IPEndPoint(IPAddress.Any, 0));
  34. IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(this.m_strAddress), this.m_nPort);
  35. this.m_socket.SendTimeout = this.m_nTimeOut;
  36. this.m_socket.ReceiveTimeout = this.m_nTimeOut;
  37. this.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  38. this.m_socket.Connect((EndPoint) remoteEP);
  39. }
  40. catch (Exception ex)
  41. {
  42. LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Open), 783);
  43. return false;
  44. }
  45. return true;
  46. }
  47. public bool Close()
  48. {
  49. if (this.m_socket == null)
  50. return true;
  51. try
  52. {
  53. this.m_socket.Shutdown(SocketShutdown.Both);
  54. this.m_socket.Close();
  55. this.m_socket = (Socket) null;
  56. }
  57. catch (Exception ex)
  58. {
  59. LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Close), 802);
  60. return false;
  61. }
  62. return true;
  63. }
  64. public int Read(byte[] buffer)
  65. {
  66. int num = -1;
  67. return !this.Connected ? num : this.m_socket.Receive(buffer);
  68. }
  69. public bool Read(byte[] buffer, int length)
  70. {
  71. if (!this.Connected)
  72. return false;
  73. try
  74. {
  75. this.m_socket.Receive(buffer, SocketFlags.None);
  76. }
  77. catch (Exception ex)
  78. {
  79. LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Read), 837);
  80. return false;
  81. }
  82. return true;
  83. }
  84. public bool Write(byte[] buffer)
  85. {
  86. if (!this.Connected)
  87. return false;
  88. try
  89. {
  90. this.m_socket.Send(buffer, SocketFlags.None);
  91. }
  92. catch (Exception ex)
  93. {
  94. LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Write), 854);
  95. return false;
  96. }
  97. return true;
  98. }
  99. public bool Write(byte[] buffer, int length) => this.Connected && this.m_socket.Send(buffer, length, SocketFlags.None) >= 0;
  100. }
  101. }