123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- // Decompiled with JetBrains decompiler
- // Type: EFEMRT.Devices.MCSocket
- // Assembly: EfemRT, Version=1.8.0.6, Culture=neutral, PublicKeyToken=null
- // MVID: DDC7B1DF-D3E3-4D53-8469-205BDD64475F
- // Assembly location: D:\EfemRT\EfemRT.exe
- using Aitex.Core.RT.Log;
- using System;
- using System.Net;
- using System.Net.Sockets;
- namespace athosRT.Devices
- {
- internal class MCSocket
- {
- private int m_nPort;
- private string m_strAddress;
- private Socket m_socket;
- private int m_nTimeOut;
- public MCSocket() => this.m_nTimeOut = 30000;
- ~MCSocket()
- {
- }
- public bool Connected => this.m_socket != null && this.m_socket.Connected;
- public bool Open(string strAddress, int nPort, string strLocalAddress)
- {
- if (this.Connected)
- return true;
- this.Close();
- this.m_strAddress = strAddress;
- this.m_nPort = nPort;
- this.m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try
- {
- this.m_socket.Bind((EndPoint) new IPEndPoint(IPAddress.Any, 0));
- IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(this.m_strAddress), this.m_nPort);
- this.m_socket.SendTimeout = this.m_nTimeOut;
- this.m_socket.ReceiveTimeout = this.m_nTimeOut;
- this.m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
- this.m_socket.Connect((EndPoint) remoteEP);
- }
- catch (Exception ex)
- {
- LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Open), 783);
- return false;
- }
- return true;
- }
- public bool Close()
- {
- if (this.m_socket == null)
- return true;
- try
- {
- this.m_socket.Shutdown(SocketShutdown.Both);
- this.m_socket.Close();
- this.m_socket = (Socket) null;
- }
- catch (Exception ex)
- {
- LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Close), 802);
- return false;
- }
- return true;
- }
- public int Read(byte[] buffer)
- {
- int num = -1;
- return !this.Connected ? num : this.m_socket.Receive(buffer);
- }
- public bool Read(byte[] buffer, int length)
- {
- if (!this.Connected)
- return false;
- try
- {
- this.m_socket.Receive(buffer, SocketFlags.None);
- }
- catch (Exception ex)
- {
- LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Read), 837);
- return false;
- }
- return true;
- }
- public bool Write(byte[] buffer)
- {
- if (!this.Connected)
- return false;
- try
- {
- this.m_socket.Send(buffer, SocketFlags.None);
- }
- catch (Exception ex)
- {
- LOG.Write(ex, 2, "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", nameof (Write), 854);
- return false;
- }
- return true;
- }
- public bool Write(byte[] buffer, int length) => this.Connected && this.m_socket.Send(buffer, length, SocketFlags.None) >= 0;
- }
- }
|