Skip to main content

Class Connection

Abstract base class for connections to Brainboxes devices. Provides caching for Brainboxes.IO.Connection.IsConnected and Brainboxes.IO.Connection.IsAvailable properties, and a background polling timer that raises Brainboxes.IO.Connection.ConnectionStatusChangedEvent when connection status changes.

Assembly: Brainboxes.IO.dll​
View Source​
Declaration
public abstract class Connection : IConnection, IDisposable

Derived:
Brainboxes.IO.SerialConnection, Brainboxes.IO.TCPConnection

Implements:
Brainboxes.IO.IConnection, System.IDisposable

Properties​

IsConnected​

Whether this connection instance to a Brainboxes Device is active

View Source​
Declaration
public bool IsConnected { get; protected set; }

IsAvailable​

Whether this connection to a Brainboxes Device is available, e.g. online or offline​. In case of network TCP connection: this is if the device is pingable on the network. In case of serial connection: this is if the device COM port is listed on the system and not open by another process.

View Source​
Declaration
public bool IsAvailable { get; protected set; }

Stream​

The Connections underlying stream class

View Source​
Declaration
public Stream Stream { get; }

Timeout​

The timeout for Stream reads and writes, and connection availability test

View Source​
Declaration
public abstract int Timeout { get; set; }

Fields​

_timeout​

2000 is the default timeout for the stream in milliseconds it is applied to both the read and write timeout

View Source​
Declaration
protected int _timeout

_stream​

The stream which can be accessed once a connection is made

View Source​
Declaration
protected Stream _stream

_lastCacheIsConnected​

last cached value of the isConnected flag

View Source​
Declaration
protected bool _lastCacheIsConnected

_isConnectedCacheTimeoutMs​

The timeout for the isConnected Cache, this should be set to something useful in the child class

View Source​
Declaration
protected int _isConnectedCacheTimeoutMs

_isAvailableCacheTimeoutMs​

The timeout for the isAvailable Cache, this should be set to something useful in the child class

View Source​
Declaration
protected int _isAvailableCacheTimeoutMs

_numberOfEventsRegistered​

The number of registered connection status changed events

View Source​
Declaration
protected int _numberOfEventsRegistered

_pollingTimer​

The timer thread used for polling IO

View Source​
Declaration
protected Timer _pollingTimer

_threadShouldBeRunning​

flag to indicate whether the pollingThread should be running

View Source​
Declaration
protected volatile bool _threadShouldBeRunning

Methods​

_computeIsConnectedValue()​

Computes the current IsConnected state. Override in derived classes. This method should not have side effects - just compute and return the value.

View Source​
Declaration
protected virtual bool _computeIsConnectedValue()
Returns​

System.Boolean

_newValueIsConnected()​

Implementation specific method to update the status whether the connection is connected. Called by polling to refresh the cache. May have side effects (e.g., disconnect on error).

View Source​
Declaration
protected virtual void _newValueIsConnected()

_computeIsAvailableValue()​

Computes the current IsAvailable state. Override in derived classes. This method should not have side effects - just compute and return the value.

View Source​
Declaration
protected abstract bool _computeIsAvailableValue()
Returns​

System.Boolean

_newValueIsAvailable()​

Connection specific implementation to determine if the connection is available. Called by polling to refresh the cache. Default implementation uses _computeIsAvailableValue().

View Source​
Declaration
protected virtual void _newValueIsAvailable()

_startPollingForChanges()​

start polling the connection for changes to the status of IsAvailable or IsConnected. Must be called while holding _eventRegistrationLock.

View Source​
Declaration
protected void _startPollingForChanges()

_stopPollingForChanges()​

Stop the thread which is monitoring the connection. Must be called while holding _eventRegistrationLock.

View Source​
Declaration
protected void _stopPollingForChanges()

Connect()​

Connect to the ED Device

View Source​
Declaration
public void Connect()

Disconnect()​

Disconnect from the ED Device

View Source​
Declaration
public void Disconnect()

_connect()​

Override in the derived connection class

View Source​
Declaration
protected abstract void _connect()

_disconnect()​

Override in the derived connection class

View Source​
Declaration
protected abstract void _disconnect()

Dispose()​

Dispose of this Brainboxes device

View Source​
Declaration
public void Dispose()

Dispose(bool)​

Dispose of this Brainboxes device

View Source​
Declaration
protected void Dispose(bool itIsSafeToAlsoFreeManagedObjects)
Parameters​
TypeName
System.BooleanitIsSafeToAlsoFreeManagedObjects

ToString()​

ToString

View Source​
Declaration
public override string ToString()
Returns​

System.String

Create(string, int, int)​

Supply a connection IP address or COM port and the correct Brainboxes.IO.IConnection concrete class will be created and returned.

View Source​
Declaration
public static IConnection Create(string ipAddressOrComPort, int portOrBaudRate = 0, int timeout = 2000)
Returns​

Brainboxes.IO.IConnection: A Brainboxes.IO.TCPConnection if an IP address is provided, or a Brainboxes.IO.SerialConnection if a COM port name is provided.

Parameters​
TypeNameDescription
System.StringipAddressOrComPortEither an IP address e.g. "192.168.0.1" or a COM port name e.g. "COM3".
System.Int32portOrBaudRateOptional. Either the IP port number (e.g. 9500 for ASCII, 502 for Modbus) or the COM port baud rate (e.g. 115200).
System.Int32timeoutOptional. The read/write timeout for the connection in milliseconds. Defaults to 2000ms.

Events​

_connectionStatusChangedEvent​

Connection Status Changed Event Handler

View Source​
Declaration
protected event ConnectionStatusChangedEventHandler _connectionStatusChangedEvent
Event Type​

Brainboxes.IO.ConnectionStatusChangedEventHandler

ConnectionStatusChangedEvent​

When the status of the connection changes this event is raised e.g. when IsConnected changes from false to true or when IsAvailable changes:

  • for example for a TCPConnection if the IP address is goes from on-line to off-line
  • or for example for a SerialConnection when the COM name is no longer present or in use by another program
View Source​
Declaration
public event ConnectionStatusChangedEventHandler ConnectionStatusChangedEvent
Event Type​

Brainboxes.IO.ConnectionStatusChangedEventHandler

Implements​