Files
DP44/docs/ai/Common/DTS.Common.IConnection/EthernetConnection.md
2026-04-17 14:55:32 -04:00

76 lines
5.5 KiB
Markdown

---
source_files:
- Common/DTS.Common.IConnection/EthernetConnection/RESTConnection.cs
- Common/DTS.Common.IConnection/EthernetConnection/EthernetConnection.cs
generated_at: "2026-04-17T15:41:01.253739+00:00"
model: "zai-org/GLM-5-FP8"
schema_version: 1
sha256: "b30ad0e6e8a9dd7c"
---
# Documentation: EthernetConnection and RESTConnection
## 1. Purpose
This module provides two implementations of the `IConnection` interface for network communication within the DTS system. `EthernetConnection` is a full TCP socket wrapper that manages asynchronous socket operations including connect, disconnect, send, receive, and accept, with support for soft disconnect/reconnect functionality and TCP keepalive configuration. `RESTConnection` is a stub implementation that simulates synchronous completion of all operations without actual network I/O, designed to satisfy the `IConnection` interface for REST-based endpoints where traditional socket semantics do not apply.
---
## 2. Public Interface
### EthernetConnection (DTS.Common)
#### Properties
| Signature | Description |
|-----------|-------------|
| `bool IsSoftDisconnected { get; }` | Returns `true` if the connection has been soft-disconnected. |
| `bool RequiresKeepAliveReset { get; set; }` | When `true`, `SoftConnect()` performs a keepalive reset via a secondary command port before reconnecting. |
| `Socket Sock` | Public field exposing the underlying `System.Net.Sockets.Socket`. |
| `string ConnectString { get; }` | Returns the connection string (format: `"host:port"`). |
| `bool Connected { get; }` | Returns `true` if `Sock` is non-null and `Sock.Connected` is `true`. |
| `SocketFlags Flags { get; set; }` | Socket flags used for send/receive operations. |
| `event EventHandler OnDisconnected` | Fired when `KeepAliveErrorReceived()` is called. |
#### Methods
| Signature | Description |
|-----------|-------------|
| `void SoftDisconnect()` | Disconnects and disposes the socket if connected and soft disconnects are allowed via `HardwareConstants.AllowSoftDisconnects`. Sets `IsSoftDisconnected = true`. |
| `void SoftConnect()` | Reconnects a soft-disconnected socket. Optionally sends a keepalive reset message to port 8200 if `RequiresKeepAliveReset` is `true`. Retries connection up to 3 times on failure. |
| `void KeepAliveErrorReceived()` | Shuts down, closes, and disposes the socket; fires `OnDisconnected` event. |
| `string GetConnectionData()` | Returns a string with local and remote `EndPoint` information, or empty string on error. |
| `Socket CreateSock(string connectString, string hostIPAddress)` | Factory method creating a TCP socket with `NoDelay`, `KeepAlive`, and custom keepalive timing configured. Optionally binds to `hostIPAddress`. |
| `void Create(string connectString)` | **Note:** Contains a bug (commented-out recursive call). Does nothing. |
| `void Create(string connectString, string hostIPAddress)` | Creates and configures the internal socket; stores connection parameters. |
| `void Bind(int port)` | Binds the socket to a local endpoint on the specified port using the first IP address from `Dns.GetHostEntry`. |
| `void Listen(int backlog)` | Places the socket in listening state. |
| `IAsyncResult BeginConnect(AsyncCallback cb, object state)` | Begins an asynchronous connection. Validates socket exists and `Connect_String` format (`host:port`). |
| `void EndConnect(IAsyncResult ar)` | Completes the asynchronous connect operation. |
| `IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback cb, object state)` | Begins asynchronous disconnect. Throws `SocketException(WSAEISCONN)` if socket is already disconnected. |
| `void EndDisconnect(IAsyncResult asyncResult)` | Completes the asynchronous disconnect operation. |
| `IAsyncResult BeginAccept(AsyncCallback callback, object state)` | Begins an asynchronous accept operation. |
| `IConnection EndAccept(IAsyncResult asyncResult)` | Completes accept and returns a new `EthernetConnection` wrapping the accepted socket. |
| `IAsyncResult BeginSend(byte[] buffer, int offset, int size, AsyncCallback cb, object state)` | Begins asynchronous send. Throws if socket is null, callback is null, or socket is not connected. |
| `int EndSend(IAsyncResult ar)` | Completes the asynchronous send; returns bytes sent. Sets `Sock = null` on exception. |
| `Task<int> SendAsync(byte[] sendBuffer, int bufferStartOffset, int bufferSizeToSend)` | Task-based async send wrapper using `FromAsync`. |
| `IAsyncResult BeginReceive(byte[] buffer, int offset, int size, AsyncCallback cb, object state)` | Begins asynchronous receive. Throws if socket is null or callback is null. |
| `int EndReceive(IAsyncResult ar)` | Completes the asynchronous receive; returns bytes received. |
| `void Dispose()` | Disposes the socket and suppresses finalization. |
---
### RESTConnection (DTS.DASLib.Connection)
#### Properties
| Signature | Description |
|-----------|-------------|
| `bool IConnection.IsSoftDisconnected` | Always returns `false`. |
| `SocketFlags IConnection.Flags { get; set; }` | Defaults to `SocketFlags.None`. |
| `string IConnection.ConnectString` | Returns the stored connection string. |
| `bool IConnection.Connected` | Returns the internal `_bConnected` flag. |
| `event EventHandler OnDisconnected` | Event declared but never invoked in the source. |
#### Methods
| Signature | Description |
|-----------|-------------|
| `void IConnection.Create(string connectString)` | Stores `connectString` in `_ConnectString`. |
| `void IConnection.Create(string connectString, string hostIPAddress)` | Stores both parameters in