/// <summary> /// Serializes the object to JSON. /// </summary> /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param> /// <param name="obj">The object to serialize to JSON.</param> internal static void Serialize(JsonWriter writer, GatewayProperties obj) { // Required properties are always serialized, optional properties are serialized when not null. writer.WriteStartObject(); writer.WriteProperty(obj.SourceNetwork, "sourceNetwork", NetworkRefConverter.Serialize); writer.WriteProperty(obj.DestinationNetwork, "destinationNetwork", NetworkRefConverter.Serialize); writer.WriteProperty(obj.Status, "status", ResourceStatusConverter.Serialize); if (obj.Description != null) { writer.WriteProperty(obj.Description, "description", JsonWriterExtensions.WriteStringValue); } if (obj.Tcp != null) { writer.WriteEnumerableProperty(obj.Tcp, "tcp", TcpConfigConverter.Serialize); } if (obj.Http != null) { writer.WriteEnumerableProperty(obj.Http, "http", HttpConfigConverter.Serialize); } if (obj.StatusDetails != null) { writer.WriteProperty(obj.StatusDetails, "statusDetails", JsonWriterExtensions.WriteStringValue); } if (obj.IpAddress != null) { writer.WriteProperty(obj.IpAddress, "ipAddress", JsonWriterExtensions.WriteStringValue); } writer.WriteEndObject(); }
internal static GatewayResourceData DeserializeGatewayResourceData(JsonElement element) { Optional <GatewayProperties> properties = default; Optional <AppPlatformSku> sku = default; ResourceIdentifier id = default; string name = default; ResourceType type = default; SystemData systemData = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("properties")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } properties = GatewayProperties.DeserializeGatewayProperties(property.Value); continue; } if (property.NameEquals("sku")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } sku = AppPlatformSku.DeserializeAppPlatformSku(property.Value); continue; } if (property.NameEquals("id")) { id = new ResourceIdentifier(property.Value.GetString()); continue; } if (property.NameEquals("name")) { name = property.Value.GetString(); continue; } if (property.NameEquals("type")) { type = new ResourceType(property.Value.GetString()); continue; } if (property.NameEquals("systemData")) { systemData = JsonSerializer.Deserialize <SystemData>(property.Value.ToString()); continue; } } return(new GatewayResourceData(id, name, type, systemData, properties.Value, sku.Value)); }
public GatewayConnectionCluster(GatewayProperties properties, IEnumerable <int> allShardIds) { packetReceieved = new Subject <GatewayMessage>(); packetEventSubscription = new List <IDisposable>(); // Spawn connection shards foreach (var i in allShardIds) { connections.Add(new GatewayConnection(new GatewayProperties { AllowNonDispatchEvents = properties.AllowNonDispatchEvents, Compressed = properties.Compressed, Encoding = properties.Encoding, Ratelimiter = properties.Ratelimiter, ShardCount = properties.ShardCount, ShardId = i, Token = properties.Token, Version = properties.Version, Intents = properties.Intents })); } }
internal GatewayResourceData(ResourceIdentifier id, string name, ResourceType resourceType, SystemData systemData, GatewayProperties properties, AppPlatformSku sku) : base(id, name, resourceType, systemData) { Properties = properties; Sku = sku; }
public async Task LoadServicesAsync(MikiAppBuilder app) { new LogBuilder() .AddLogEvent((msg, lvl) => { if (lvl >= Global.Config.LogLevel) { Console.WriteLine(msg); } }) .SetLogHeader((msg) => $"[{msg}]: ") .SetTheme(new LogTheme()) .Apply(); var cache = new StackExchangeCacheClient( new ProtobufSerializer(), await ConnectionMultiplexer.ConnectAsync(Global.Config.RedisConnectionString) ); // Setup Redis { app.AddSingletonService <ICacheClient>(cache); app.AddSingletonService <IExtendedCacheClient>(cache); } // Setup Entity Framework { app.Services.AddDbContext <MikiDbContext>(x => x.UseNpgsql(Global.Config.ConnString, b => b.MigrationsAssembly("Miki.Bot.Models"))); app.Services.AddDbContext <DbContext, MikiDbContext>(x => x.UseNpgsql(Global.Config.ConnString, b => b.MigrationsAssembly("Miki.Bot.Models"))); } // Setup Miki API { if (!string.IsNullOrWhiteSpace(Global.Config.MikiApiBaseUrl) && !string.IsNullOrWhiteSpace(Global.Config.MikiApiKey)) { app.AddSingletonService(new MikiApiClient(Global.Config.MikiApiKey)); } else { Log.Warning("No Miki API parameters were supplied, ignoring Miki API."); } } // Setup Discord { app.AddSingletonService <IApiClient>(new DiscordApiClient(Global.Config.Token, cache)); if (Global.Config.SelfHosted) { var gatewayConfig = new GatewayProperties(); gatewayConfig.ShardCount = 1; gatewayConfig.ShardId = 0; gatewayConfig.Token = Global.Config.Token; gatewayConfig.Compressed = true; gatewayConfig.AllowNonDispatchEvents = true; app.AddSingletonService <IGateway>(new GatewayCluster(gatewayConfig)); } else { app.AddSingletonService <IGateway>(new DistributedGateway(new MessageClientConfiguration { ConnectionString = new Uri(Global.Config.RabbitUrl.ToString()), QueueName = "gateway", ExchangeName = "consumer", ConsumerAutoAck = false, PrefetchCount = 25, })); } } // Setup web services { app.AddSingletonService(new UrbanDictionaryAPI()); app.AddSingletonService(new BunnyCDNClient(Global.Config.BunnyCdnKey)); } // Setup miscellanious services { app.AddSingletonService(new ConfigurationManager()); app.AddSingletonService(new EventSystem()); app.AddSingletonService(new BackgroundStore()); if (!string.IsNullOrWhiteSpace(Global.Config.SharpRavenKey)) { app.AddSingletonService(new RavenClient(Global.Config.SharpRavenKey)); } else { Log.Warning("Sentry.io key not provided, ignoring distributed error logging..."); } } }
/// <summary> /// Gets the object from Json properties. /// </summary> /// <param name="reader">The <see cref="T: Newtonsoft.Json.JsonReader" /> to read from, reader must be placed at first property.</param> /// <returns>The object Value.</returns> internal static GatewayProperties GetFromJsonProperties(JsonReader reader) { var description = default(string); var sourceNetwork = default(NetworkRef); var destinationNetwork = default(NetworkRef); var tcp = default(IEnumerable <TcpConfig>); var http = default(IEnumerable <HttpConfig>); var status = default(ResourceStatus?); var statusDetails = default(string); var ipAddress = default(string); do { var propName = reader.ReadPropertyName(); if (string.Compare("description", propName, StringComparison.Ordinal) == 0) { description = reader.ReadValueAsString(); } else if (string.Compare("sourceNetwork", propName, StringComparison.Ordinal) == 0) { sourceNetwork = NetworkRefConverter.Deserialize(reader); } else if (string.Compare("destinationNetwork", propName, StringComparison.Ordinal) == 0) { destinationNetwork = NetworkRefConverter.Deserialize(reader); } else if (string.Compare("tcp", propName, StringComparison.Ordinal) == 0) { tcp = reader.ReadList(TcpConfigConverter.Deserialize); } else if (string.Compare("http", propName, StringComparison.Ordinal) == 0) { http = reader.ReadList(HttpConfigConverter.Deserialize); } else if (string.Compare("status", propName, StringComparison.Ordinal) == 0) { status = ResourceStatusConverter.Deserialize(reader); } else if (string.Compare("statusDetails", propName, StringComparison.Ordinal) == 0) { statusDetails = reader.ReadValueAsString(); } else if (string.Compare("ipAddress", propName, StringComparison.Ordinal) == 0) { ipAddress = reader.ReadValueAsString(); } else { reader.SkipPropertyValue(); } }while (reader.TokenType != JsonToken.EndObject); var gatewayProperties = new GatewayProperties( description: description, sourceNetwork: sourceNetwork, destinationNetwork: destinationNetwork, tcp: tcp, http: http); gatewayProperties.Status = status; gatewayProperties.StatusDetails = statusDetails; gatewayProperties.IpAddress = ipAddress; return(gatewayProperties); }