--- source_files: - Common/DTS.Common/Interface/Connection/IConnection.cs generated_at: "2026-04-17T16:52:23.062559+00:00" model: "zai-org/GLM-5-FP8" schema_version: 1 sha256: "c785cd9a26c94c57" --- # Documentation: IConnection Interface ## 1. Purpose The `IConnection` interface defines an abstraction for network connection management within the DTS system. It provides a comprehensive contract for socket-based communication, supporting both APM (Asynchronous Programming Model) and Task-based asynchronous patterns. The interface encapsulates connection lifecycle management (creation, connection, disconnection), data transfer operations (send/receive), server-side operations (bind/listen/accept), and introduces a "soft disconnect" concept for temporary disconnections with the expectation of reconnection. This interface exists to decouple higher-level application logic from specific connection implementations, enabling testability and implementation flexibility. ## 2. Public Interface ### Properties | Signature | Description | |-----------|-------------| | `bool IsSoftDisconnected { get; }` | Returns `true` if the connection is in a soft-disconnected state (voluntarily disconnected with expectation of reconnecting). | | `System.Net.Sockets.SocketFlags Flags { get; set; }` | Gets or sets socket flags used for send/receive operations. | | `string ConnectString { get; }` | Returns the connection string associated with this connection. | | `bool Connected { get; }` | Returns `true` if the connection is currently active. | ### Events | Signature | Description | |-----------|-------------| | `event EventHandler OnDisconnected` | Raised when the connection is disconnected. | ### Methods - Connection Lifecycle | Signature | Description | |-----------|-------------| | `void Create(string connectString)` | Initializes the connection using the provided connection string. | | `void Create(string connectString, string hostIPAddress)` | Initializes the connection with a connection string and specific host IP address. | | `void SoftDisconnect()` | Performs a soft disconnect (voluntary disconnection with intent to reconnect later). | | `void SoftConnect()` | Reconnects a soft-disconnected connection. | | `void KeepAliveErrorReceived()` | Called to indicate the device has not received a timely response to keep-alive. | | `string GetConnectionData()` | Returns connection data as a string. | ### Methods - APM Pattern (Connect/Disconnect) | Signature | Description | |-----------|-------------| | `IAsyncResult BeginConnect(AsyncCallback callback, object callbackObject)` | Begins an asynchronous connection attempt. | | `void EndConnect(IAsyncResult ar)` | Completes an asynchronous connection attempt. | | `IAsyncResult BeginDisconnect(bool reuseSocket, AsyncCallback callback, object state)` | Begins an asynchronous disconnection. | | `void EndDisconnect(IAsyncResult asyncResult)` | Completes an asynchronous disconnection. | ### Methods - APM Pattern (Server Operations) | Signature | Description | |-----------|-------------| | `void Bind(int port)` | Binds the connection to a specific port.