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
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
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
public bool IsAvailable { get; protected set; }
Stream
The Connections underlying stream class
View Source
public Stream Stream { get; }
Timeout
The timeout for Stream reads and writes, and connection availability test
View Source
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
protected int _timeout
_stream
The stream which can be accessed once a connection is made
View Source
protected Stream _stream
_lastCacheIsConnected
last cached value of the isConnected flag
View Source
protected bool _lastCacheIsConnected
_isConnectedCacheTimeoutMs
The timeout for the isConnected Cache, this should be set to something useful in the child class
View Source
protected int _isConnectedCacheTimeoutMs
_isAvailableCacheTimeoutMs
The timeout for the isAvailable Cache, this should be set to something useful in the child class
View Source
protected int _isAvailableCacheTimeoutMs
_numberOfEventsRegistered
The number of registered connection status changed events
View Source
protected int _numberOfEventsRegistered
_pollingTimer
The timer thread used for polling IO
View Source
protected Timer _pollingTimer
_threadShouldBeRunning
flag to indicate whether the pollingThread should be running
View Source
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
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
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
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
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
protected void _startPollingForChanges()
_stopPollingForChanges()
Stop the thread which is monitoring the connection. Must be called while holding _eventRegistrationLock.
View Source
protected void _stopPollingForChanges()
Connect()
Connect to the ED Device
View Source
public void Connect()
Disconnect()
Disconnect from the ED Device
View Source
public void Disconnect()
_connect()
Override in the derived connection class
View Source
protected abstract void _connect()
_disconnect()
Override in the derived connection class
View Source
protected abstract void _disconnect()
Dispose()
Dispose of this Brainboxes device
View Source
public void Dispose()
Dispose(bool)
Dispose of this Brainboxes device
View Source
protected void Dispose(bool itIsSafeToAlsoFreeManagedObjects)
Parameters
| Type | Name |
|---|---|
System.Boolean | itIsSafeToAlsoFreeManagedObjects |
ToString()
ToString
View Source
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
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
| Type | Name | Description |
|---|---|---|
System.String | ipAddressOrComPort | Either an IP address e.g. "192.168.0.1" or a COM port name e.g. "COM3". |
System.Int32 | portOrBaudRate | Optional. Either the IP port number (e.g. 9500 for ASCII, 502 for Modbus) or the COM port baud rate (e.g. 115200). |
System.Int32 | timeout | Optional. The read/write timeout for the connection in milliseconds. Defaults to 2000ms. |
Events
_connectionStatusChangedEvent
Connection Status Changed Event Handler
View Source
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
TCPConnectionif the IP address is goes from on-line to off-line - or for example for a
SerialConnectionwhen the COM name is no longer present or in use by another program
View Source
public event ConnectionStatusChangedEventHandler ConnectionStatusChangedEvent
Event Type
Brainboxes.IO.ConnectionStatusChangedEventHandler
Implements
- Brainboxes.IO.IConnection
System.IDisposable