# The C# SqlServer Class

### For an overview see page C# Classes

## Class Definition

        // ENUM
        public enum SessionState { Wait, Active, Done, Canceled }

        // Nested class OpenParameters
        public class OpenParameters {
            // Configuration Fields
            public SqlLocalConnection conn;
            public int port;
            public int bufsize;
            public int nThreads;
            public int listenQueueSize;
            public bool localDomain;
            public Database.SSLParameters sslParameters;
            public bool authenticationRequired;
            public bool authorizationRequired;
            public int interruptTimeout;
            public String netInterface;
            public int compressionLevel;
            public bool protocolCompatibility;
            public int maxWaitListLength;

            // Constructor
            public OpenParameters(SqlLocalConnection conn, int port);
        }

        // Nested class SessionInfo
        public class SessionInfo {
            public SessionState state;
            public String peerAddr;
            public int peerPort;

            public SessionInfo(SessionState state, String peerAddr, int peerPort);
        }

        // Class SqlServer
        public class SqlServer {
            // Constructors
            public SqlServer(SqlLocalConnection conn, int port, int bufsize);
            public SqlServer(SqlLocalConnection conn, int port);
            public SqlServer(OpenParameters op);

            // Lifecycle Methods
            public void start();
            public void stop();

            // Monitoring Methods
            public SessionInfo[] getSessionsInfo();
        }

## Member Descriptions

SessionState enum
Represents the current state of a client session: `Wait` (idle), `Active` (processing query), `Done` (completed), or `Canceled` (terminated by client).
OpenParameters class
Configuration container for initializing the SQL server with fine-grained control over networking, security, and performance parameters.
OpenParameters.conn
The local database connection that the SQL server will use to execute queries. Must be a valid `SqlLocalConnection` instance.
OpenParameters.port
TCP port number on which the SQL server will listen for incoming client connections.
OpenParameters.bufsize
Size of the internal I/O buffer in bytes (default: 65536). Affects network throughput and memory usage per connection.
OpenParameters.nThreads
Number of worker threads for handling concurrent client requests (default: 4). Adjust based on CPU cores and expected load.
OpenParameters.listenQueueSize
Maximum length of the pending connection queue (default: 5). Connections exceeding this limit may be rejected under heavy load.
OpenParameters.localDomain
If `true`, the server listens on a Unix domain socket instead of TCP. Useful for local-only, high-performance IPC.
OpenParameters.sslParameters
Optional SSL/TLS configuration for encrypting client-server communication. Set to `null` for unencrypted connections.
OpenParameters.authenticationRequired
If `true`, clients must provide valid credentials before executing queries. Requires user management setup.
OpenParameters.authorizationRequired
If `true`, enforces role-based access control on database operations. Works in conjunction with authentication.
OpenParameters.interruptTimeout
Timeout in milliseconds for detecting unresponsive client connections (default: 100ms). Helps free resources from stalled sessions.
OpenParameters.netInterface
Optional network interface name or IP address to bind the server to. If `null`, binds to all available interfaces.
OpenParameters.compressionLevel
Level of network traffic compression (0 = disabled, higher = more compression). Trade-off between CPU usage and bandwidth savings.
OpenParameters.protocolCompatibility
If `true`, enables backward-compatible protocol mode for older clients. May disable newer optimizations.
OpenParameters.maxWaitListLength
Maximum number of queries allowed in the wait queue per session. Prevents resource exhaustion from queued requests (0 = unlimited).
OpenParameters(SqlLocalConnection, int)
Initializes server configuration with mandatory connection and port. All other fields receive sensible defaults.
SessionInfo class
Represents runtime information about an active client session connected to the SQL server.
SessionInfo.state
Current processing state of the session (see `SessionState` enum).
SessionInfo.peerAddr
IP address or hostname of the connected client.
SessionInfo.peerPort
Source port number used by the client for this connection.
SessionInfo(SessionState, String, int)
Constructs a session info object. Typically instantiated internally by the server wrapper.
SqlServer(SqlLocalConnection, int, int)
Creates a new SQL server instance bound to a local connection, listening on the specified port with a custom buffer size.
SqlServer(SqlLocalConnection, int)
Convenience constructor: creates a server with default buffer size (65536 bytes).
SqlServer(OpenParameters)
Creates a server using full configuration via `OpenParameters`. Enables advanced tuning of networking, security, and performance.
start()
Starts accepting client connections on the configured port/interface. The server runs asynchronously in background threads.
stop()
Gracefully shuts down the SQL server: stops accepting new connections, waits for active sessions to complete, and releases resources.
getSessionsInfo()
Returns an array of `SessionInfo` objects describing all currently connected or recently active client sessions. Useful for monitoring and diagnostics.
Thread Safety
The `SqlServer` instance is thread-safe for concurrent client access. However, the underlying `SqlLocalConnection` must remain dedicated to the server and not be used directly by application threads.
Resource Management
Always call `stop()` before disposing the associated database connection to ensure clean shutdown and prevent resource leaks.