using System; using MxGateway.Contracts; using MxGateway.Worker.Bootstrap; namespace MxGateway.Worker.Ipc; public sealed class WorkerFrameProtocolOptions { public const int DefaultMaxMessageBytes = 16 * 1024 * 1024; public WorkerFrameProtocolOptions(WorkerOptions options) : this( options?.SessionId ?? throw new ArgumentNullException(nameof(options)), options.ProtocolVersion, options.Nonce, DefaultMaxMessageBytes) { } public WorkerFrameProtocolOptions( string sessionId, uint protocolVersion, string nonce) : this( sessionId, protocolVersion, nonce, DefaultMaxMessageBytes) { } public WorkerFrameProtocolOptions( string sessionId, uint protocolVersion, string nonce, int maxMessageBytes) { if (string.IsNullOrWhiteSpace(sessionId)) { throw new WorkerFrameProtocolException( WorkerFrameProtocolErrorCode.InvalidConfiguration, "Worker frame protocol requires a session id."); } if (protocolVersion == 0) { throw new WorkerFrameProtocolException( WorkerFrameProtocolErrorCode.InvalidConfiguration, "Worker frame protocol requires a non-zero protocol version."); } if (protocolVersion != GatewayContractInfo.WorkerProtocolVersion) { throw new WorkerFrameProtocolException( WorkerFrameProtocolErrorCode.ProtocolVersionMismatch, $"Worker frame protocol version {protocolVersion} is not supported."); } if (string.IsNullOrWhiteSpace(nonce)) { throw new WorkerFrameProtocolException( WorkerFrameProtocolErrorCode.InvalidConfiguration, "Worker frame protocol requires a nonce."); } if (maxMessageBytes <= 0) { throw new WorkerFrameProtocolException( WorkerFrameProtocolErrorCode.InvalidConfiguration, "Worker frame protocol max message size must be greater than zero."); } SessionId = sessionId; ProtocolVersion = protocolVersion; Nonce = nonce; MaxMessageBytes = maxMessageBytes; } public string SessionId { get; } public uint ProtocolVersion { get; } public string Nonce { get; } public int MaxMessageBytes { get; } }