private void Dispose(bool disposing) { // Raise the Disposed Flag _disposed = true; // If the Downstream Thread is still live, give it time to end quietly if ((disposing) && (null != _downstreamThread)) { if (!_downstreamThread.Join(500)) { _downstreamThread.Abort(); } _downstreamThread = null; } // Dispose the TCP/IP client if ((disposing) && (null != _client)) { _client.Dispose(); _client = null; } // Dispose of the Turnstile if ((disposing) && (null != _downstreamTurnstile)) { _downstreamTurnstile.Dispose(); _downstreamTurnstile = null; } }
internal static CoLaBClient Connect(IPEndPoint remoteEndPoint) { // Construct the Client object CoLaBClient client = new CoLaBClient(); try { // Attempt to Connect to the remote TCP/IP end-point client._client.Connect(remoteEndPoint); return(client); } catch { try { // Dispose the Client that failed to Connect client.Dispose(); } catch { // Suppress secondary exceptions } // Rethrow the original exception throw; } }
protected override void ConnectImpl() { try { // Reinitialize a Disconnected Client if ((null != _client) && (!_client.IsConnected)) { _client.Dispose(); _client = null; } // Connect when necessary if (null == _client) { _client = CoLaBClient.Connect(_remoteEndPoint); log.Debug($"{Name}: connected to {_remoteEndPoint}"); try { // Request Scan-Data Telegrams CoLaBTelegram acknowledgement = _client.SendTelegram(CoLaCommandType.Event, "LMDscandata", (telegramWriter) => { telegramWriter.Write(0x01); }, acknowledgeTimeout: _timeoutMilliseconds); if (acknowledgement.Data[acknowledgement.Offset] != 0) { log.Debug($"{Name}: CoLa (binary) telegram subscription established"); } else { throw ExceptionBuilder.Build(typeof(ConnectionFailedException), Name, "error_connectionFailed", "CoLa (binary) telegram subscription failed"); } } catch { // Dispose the TCP/IP Client immediately try { _client.Dispose(); _client = null; } catch { // Suppress secondary exceptions } // Rethrow the original exception throw; } // Begin receiving telegrams in a background thread _downstreamThread = new Thread(DownstreamThreadProc); _downstreamThread.Name = Name; _downstreamThread.IsBackground = true; _downstreamThread.Start(); } } catch (MetriCam2Exception) { // Pass MetriCam API Exceptions without alteration throw; } catch (Exception foreignException) { // Wrap and Throw other exceptions encountered during Connection throw ExceptionBuilder.Build(typeof(ConnectionFailedException), Name, foreignException.Message, foreignException); } }