diff --git a/src/NATS.Server/NatsOptions.cs b/src/NATS.Server/NatsOptions.cs new file mode 100644 index 0000000..60238c0 --- /dev/null +++ b/src/NATS.Server/NatsOptions.cs @@ -0,0 +1,13 @@ +namespace NATS.Server; + +public sealed class NatsOptions +{ + public string Host { get; set; } = "0.0.0.0"; + public int Port { get; set; } = 4222; + public string? ServerName { get; set; } + public int MaxPayload { get; set; } = 1024 * 1024; // 1MB + public int MaxControlLine { get; set; } = 4096; + public int MaxConnections { get; set; } = 65536; + public TimeSpan PingInterval { get; set; } = TimeSpan.FromMinutes(2); + public int MaxPingsOut { get; set; } = 2; +} diff --git a/src/NATS.Server/Protocol/NatsProtocol.cs b/src/NATS.Server/Protocol/NatsProtocol.cs new file mode 100644 index 0000000..1d126bc --- /dev/null +++ b/src/NATS.Server/Protocol/NatsProtocol.cs @@ -0,0 +1,87 @@ +using System.Text.Json.Serialization; + +namespace NATS.Server.Protocol; + +public static class NatsProtocol +{ + public const int MaxControlLineSize = 4096; + public const int MaxPayloadSize = 1024 * 1024; // 1MB + public const int DefaultPort = 4222; + public const string Version = "0.1.0"; + public const int ProtoVersion = 1; + + // Pre-encoded protocol fragments + public static readonly byte[] CrLf = "\r\n"u8.ToArray(); + public static readonly byte[] PingBytes = "PING\r\n"u8.ToArray(); + public static readonly byte[] PongBytes = "PONG\r\n"u8.ToArray(); + public static readonly byte[] OkBytes = "+OK\r\n"u8.ToArray(); + public static readonly byte[] InfoPrefix = "INFO "u8.ToArray(); + public static readonly byte[] MsgPrefix = "MSG "u8.ToArray(); + public static readonly byte[] HmsgPrefix = "HMSG "u8.ToArray(); + public static readonly byte[] ErrPrefix = "-ERR "u8.ToArray(); +} + +public sealed class ServerInfo +{ + [JsonPropertyName("server_id")] + public required string ServerId { get; set; } + + [JsonPropertyName("server_name")] + public required string ServerName { get; set; } + + [JsonPropertyName("version")] + public required string Version { get; set; } + + [JsonPropertyName("proto")] + public int Proto { get; set; } = NatsProtocol.ProtoVersion; + + [JsonPropertyName("host")] + public required string Host { get; set; } + + [JsonPropertyName("port")] + public int Port { get; set; } + + [JsonPropertyName("headers")] + public bool Headers { get; set; } = true; + + [JsonPropertyName("max_payload")] + public int MaxPayload { get; set; } = NatsProtocol.MaxPayloadSize; + + [JsonPropertyName("client_id")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public ulong ClientId { get; set; } + + [JsonPropertyName("client_ip")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? ClientIp { get; set; } +} + +public sealed class ClientOptions +{ + [JsonPropertyName("verbose")] + public bool Verbose { get; set; } + + [JsonPropertyName("pedantic")] + public bool Pedantic { get; set; } + + [JsonPropertyName("echo")] + public bool Echo { get; set; } = true; + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("lang")] + public string? Lang { get; set; } + + [JsonPropertyName("version")] + public string? Version { get; set; } + + [JsonPropertyName("protocol")] + public int Protocol { get; set; } + + [JsonPropertyName("headers")] + public bool Headers { get; set; } + + [JsonPropertyName("no_responders")] + public bool NoResponders { get; set; } +}