internal override void Connect() { if (!m_serialPort.IsOpen) { CheckPortName(m_SerialPortNames); m_serialPort.PortName = m_SerialPortNames.ToString(); try { m_serialPort.DtrEnable = true; m_serialPort.RtsEnable = true; m_serialPort.Open(); } catch (Exception ex) { throw new ComDriveExceptions(ex.Message, ComDriveExceptions.ComDriveException.PortInUse); } string text = ConnectionStatus.ConnectionOpened + " with BR" + m_serialPort.BaudRate.ToString() + "(" + m_serialPort.DataBits.ToString() + ", " + m_serialPort.Parity.ToString() + ", " + m_serialPort.StopBits.ToString() + ")"; ComDriverLogger.LogConnectionState(DateTime.Now, Utils.HelperComDriverLogger.GetLoggerChannel(this), text); } }
/// <summary> /// Closes the Socket connection and releases all associated resources. /// </summary> public override void Disconnect() { if (base.QueueCount != 0) { return; } if (m_PLCSocket != null) { if (m_PLCSocket.Connected) { m_PLCSocket.Close(); try { ComDriverLogger.LogConnectionState(DateTime.Now, Utils.HelperComDriverLogger.GetLoggerChannel(this), ConnectionStatus.ConnectionClosed.ToString()); } catch { } } } }
public override void Disconnect() { if (base.QueueCount != 0) { return; } if (m_serialPort.IsOpen) { m_serialPort.Close(); AlreadyInitialized = false; try { ComDriverLogger.LogConnectionState(DateTime.Now, Utils.HelperComDriverLogger.GetLoggerChannel(this), ConnectionStatus.ConnectionClosed.ToString()); } catch { } m_serialPort.Dispose(); } }
internal void Dispose() { // This function literally kills the socket, no matter if the message queue is not empty. try { if (!Disposed) { ListenerClientsInfo.DecrementCount(localPort); Disposed = true; worker.Close(); if (OnConnectionClosed != null) { OnConnectionClosed(this); } string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() + ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString()); } catch { } AbortAll(); } } catch (Exception ex) { string exceptionText = "InnerListenerClientEcxeption (Dispose)" + " - " + ex.Message; ComDriverLogger.LogExceptions(DateTime.Now, exceptionText); } }
internal void Listen() { if (Status == ConnectionStatus.Disconnected) { m_ConnectionFlag = ConnectionFlag.None; try { socket.Listen(m_LocalPort); Status = ConnectionStatus.Listening; string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.Listening.ToString()); } catch { } } catch { throw new ComDriveExceptions( "Failed binding local port " + m_LocalPort.ToString() + ". Please check that the port is not in use", ComDriveExceptions.ComDriveException.PortInUse); } } else { //GetPLC was called. Therefore since we already listening then there are 2 options: //1. Socket is not connected and it is listening. When connection will arive then the PLC will be returned //2. Socket is connected. Therefore On Connect event will not happen and we need to return the PLC right now. Socket worker = socket.GetWorker(); if (Status == ConnectionStatus.Connected) { try { PLC plc = PLCFactory.GetPLC(this, 0); if (OnListenerConnectionAccepted != null) { OnListenerConnectionAccepted(plc); } } catch { try { worker.Close(); } catch { } } } else { System.Diagnostics.Debug.Print("Listen request denied... Connection Status: " + Status.ToString()); } } }
private void initSocket(Socket socket, int port) { try { worker = socket; localPort = port; IPEndPoint ep = worker.RemoteEndPoint as IPEndPoint; RemoteIP = ep.Address.ToString(); string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionOpened.ToString() + ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString()); } catch { } ListenerClientsInfo.IncrementCount(localPort); waitForData(); } catch (Exception ex) { if (!Disposed) { ListenerClientsInfo.DecrementCount(localPort); } try { worker.Close(); } catch { } if (!Disposed) { string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() + ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString()); } catch { } } Disposed = true; string exceptionText = "InnerListenerClientEcxeption (initSocket)" + " - " + ex.Message; ComDriverLogger.LogExceptions(DateTime.Now, exceptionText); AbortAll(); } }
/// <summary> /// Closes the Socket connection and releases all associated resources. /// </summary> public override void Disconnect() { if (base.QueueCount != 0) { return; } Socket worker = socket.GetWorker(); try { AbortAll(); m_ConnectionFlag = ConnectionFlag.DisconnectedStopListening; socket.Close(); Status = ConnectionStatus.Disconnected; } catch { } string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString()); } catch { } if (worker != null) { System.Threading.Thread.Sleep(100); while (worker.Connected) { try { worker.Close(); } catch { } System.Threading.Thread.Sleep(100); } } }
void socket_OnSocektError(object sender, SocketError socketError) { // Socket error can be caused when Enfora closes the socket. if (m_ConnectionFlag == ConnectionFlag.DisconnectedStopListening) { AbortAll(); socket.Close(); } else { AbortAll(); System.Diagnostics.Debug.Print("Disconnected, back to listen"); if (OnListenerConnectionClosed != null) { OnListenerConnectionClosed(this); } m_ConnectionFlag = ConnectionFlag.DisconnectedResumeListening; Status = ConnectionStatus.Listening; socket.Close(); try { socket.Listen(m_LocalPort); } catch { throw new ComDriveExceptions( "Failed binding local port " + m_LocalPort.ToString() + ". Please check that the port is not in use", ComDriveExceptions.ComDriveException.PortInUse); } try { string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString()); ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.Listening.ToString()); } catch { } } }
void socket_OnConnect(object sender, EventArgs e) { Status = ConnectionStatus.Connected; Socket worker = socket.GetWorker(); string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionOpened.ToString()); } catch (Exception ex) { // The PLC did not reply... Close the connection and return to Listen mode. { worker.Close(); Console.Write(ex.Message); } } try { System.Threading.Thread.Sleep(1000); PLC plc = PLCFactory.GetPLC(this, 0); if (OnListenerConnectionAccepted != null) { OnListenerConnectionAccepted(plc); } } catch (Exception ex) { // The PLC did not reply... Close the connection and return to Listen mode. { worker.Close(); Console.Write(ex.Message); } } }
internal override void Connect() { if (m_PLCSocket != null) { if (m_PLCSocket.Connected) { return; } try { m_PLCSocket.Close(); } catch { } } if (Protocol == EthProtocol.TCP) { m_PLCSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else { m_PLCSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } SocketState socketState = new SocketState(); socketState.Socket = m_PLCSocket; socketState.State = SocketState.ConnectionState.Connecting; bool isHostNameMode = false; IPAddress ipAddress = null; try { ipAddress = IPAddress.Parse(m_remoteIPorHostName); } catch { isHostNameMode = true; } IPEndPoint PLCIPEndPoint = null; if (!isHostNameMode) { PLCIPEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(m_remotePort)); } try { if (!isHostNameMode) { IAsyncResult result = m_PLCSocket.BeginConnect(PLCIPEndPoint, socketConnect, socketState); bool success = result.AsyncWaitHandle.WaitOne(connectTimeOut, true); lock (socketState) { if (success) { try { m_PLCSocket.EndConnect(result); } catch { socketState.State = SocketState.ConnectionState.Failed; throw; } } else { socketState.State = SocketState.ConnectionState.Failed; throw new SocketException(10060); } } // Enfora modems Suck. It require a sleep after the connection before data is being sent to it // Otherwise the communication fails. Thread.Sleep(500); } else { IAsyncResult result = m_PLCSocket.BeginConnect(m_remoteIPorHostName, m_remotePort, socketConnect, socketState); bool success = result.AsyncWaitHandle.WaitOne(connectTimeOut, true); lock (socketState) { if (success) { try { m_PLCSocket.EndConnect(result); } catch { socketState.State = SocketState.ConnectionState.Failed; throw; } } else { socketState.State = SocketState.ConnectionState.Failed; throw new SocketException(10060); } } // Enfora modems Suck. It require a sleep after the connection before data is being sent to it // Otherwise the communication fails. Thread.Sleep(500); } string text = ConnectionStatus.ConnectionOpened + " on port " + m_remotePort.ToString(); ComDriverLogger.LogConnectionState(DateTime.Now, Utils.HelperComDriverLogger.GetLoggerChannel(this), text); } catch (SocketException se) { if (se.Message.Contains(m_remoteIPorHostName)) { throw new ComDriveExceptions(se.Message, ComDriveExceptions.ComDriveException.GeneralCommunicationError); } else { throw new ComDriveExceptions(se.Message + " " + m_remoteIPorHostName + ":" + m_remotePort, ComDriveExceptions.ComDriveException.GeneralCommunicationError); } } }
private void onDataReceived(IAsyncResult ar) { SocketState socketState = null; try { socketState = (SocketState)ar.AsyncState; int cbRead = 0; cbRead = worker.EndReceive(ar); if (cbRead > 0) { byte[] temp = new byte[cbRead]; Array.Copy(socketState.ReceivedData, 0, temp, 0, cbRead); OnDataReceived(temp); waitForData(); } else { try { if (!Disposed) { ListenerClientsInfo.DecrementCount(localPort); } worker.Close(); } catch { } if (!Disposed) { string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() + ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString()); } catch { } Disposed = true; AbortAll(); if (OnConnectionClosed != null) { OnConnectionClosed(this); } } } } catch (ObjectDisposedException) { try { if (!Disposed) { ListenerClientsInfo.DecrementCount(localPort); } worker.Close(); } catch { } if (!Disposed) { string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() + ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString()); } catch { } Disposed = true; AbortAll(); if (OnConnectionClosed != null) { OnConnectionClosed(this); } } } catch (SocketException se) { try { if (!Disposed) { ListenerClientsInfo.DecrementCount(localPort); } worker.Close(); } catch { } if (!Disposed) { string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() + ", Remote IP (Client): " + RemoteIP + ", Client GUID: " + guid.ToString()); } catch { } Disposed = true; AbortAll(); if (OnConnectionClosed != null) { OnConnectionClosed(this); } } } catch { try { if (!Disposed) { ListenerClientsInfo.DecrementCount(localPort); } worker.Close(); } catch { } if (!Disposed) { string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this); try { ComDriverLogger.LogConnectionState(DateTime.Now, channelLog, Unitronics.ComDriver.ConnectionStatus.ConnectionClosed.ToString() + ", Remote IP (Client): " + RemoteIP); } catch { } Disposed = true; AbortAll(); if (OnConnectionClosed != null) { OnConnectionClosed(this); } } } }