/// <summary> /// Invoked when a <see cref="GatewayOpCode.Heartbeat"/> OpCode is received /// </summary> /// <param name="json"></param> /// <param name="ev"></param> internal void GatewayEvents_OnHeartbeatReq(string json, DispatchGatewayEvent <object> ev) { //The gateway can request a heartbeat. Send one immediately if this occurs. //Shouldn't interrupt the regular heartbeating Queue(new GatewayEvent <object>(GatewayOpCode.HeartbeatAck), new Newtonsoft.Json.JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore }); }
/// <summary> /// Invoked when any <see cref="EventType"/> is received /// </summary> /// <param name="rawJson"></param> /// <param name="ev"></param> internal void GatewayEvents_AllEventsCallback(string rawJson, DispatchGatewayEvent <object> ev) { Debug.Write($"[RECV] [{ev.OpCode}]"); if (ev is DispatchGatewayEvent <object> e && e.Sequence.HasValue) //Only dispatch events have sequence values { _sequence = e.Sequence; Configuration.LastSequence = _sequence; Debug.Write($" [{e.Type.ToString()}] Seq={e.Sequence}"); } Debug.Write("\n"); }
/// <summary> /// Invoked when an <see cref="EventType.HELLO"/> event is received /// </summary> /// <param name="rawJson"></param> /// <param name="ev"></param> internal void GatewayEvents_OnGatewayHello(string rawJson, DispatchGatewayEvent <HelloPayload> ev) { HeartbeatInterval = ev.Payload.HeartbeatInterval; Identify(); #pragma warning disable CS4014 //There's no need to await this. Warning disabled for sanity's sake HeartbeatAsync(linkedTokenSource.Token); #pragma warning restore CS4014 }
/// <summary> /// Invokes any event handler that matches the event in the given json string /// </summary> /// <param name="json"></param> public async Task Invoke(string json) { //We need temp event to get the OpCode for this event DispatchGatewayEvent <JToken> tempEvent = JsonConvert.DeserializeObject <DispatchGatewayEvent <JToken> >(json); if (_callbacks.TryGetValue(GatewayOpCode.All, out Dictionary <Type, Delegator> callbacks)) { foreach (var callback in callbacks) { //GatewayOpCode.All doesn't get a first-class object deserialization, as the callback is used for every payload type //This object instance therefore is a DispatchGatewayEvent<object>, with Payload being a JObject which can be further //deserialized if required object ev = Activator.CreateInstance(callback.Value._eventType); JsonConvert.PopulateObject(json, ev, _serializationSettings); callback.Value.Invoke(json, ev); } } if (_asyncCallbacks.TryGetValue(tempEvent.OpCode, out callbacks)) { foreach (KeyValuePair <Type, Delegator> callback in callbacks) { object ev = Activator.CreateInstance(callback.Value._eventType); //Doing this means we effectively deserialize twice per event. //There doesn't really seem to be any other way to do it however JsonConvert.PopulateObject(json, ev, _serializationSettings); await callback.Value.InvokeAsync(json, ev); } } if (_callbacks.TryGetValue(tempEvent.OpCode, out callbacks)) { foreach (KeyValuePair <Type, Delegator> callback in callbacks) { object ev = Activator.CreateInstance(callback.Value._eventType); JsonConvert.PopulateObject(json, ev, _serializationSettings); callback.Value.Invoke(json, ev); } } if (_asyncEventCallbacks.TryGetValue(tempEvent.Type, out callbacks)) { foreach (KeyValuePair <Type, Delegator> callback in callbacks) { object ev = Activator.CreateInstance(callback.Value._eventType); JsonConvert.PopulateObject(json, ev, _serializationSettings); await callback.Value.InvokeAsync(json, ev); } } if (_eventCallbacks.TryGetValue(tempEvent.Type, out callbacks)) { foreach (KeyValuePair <Type, Delegator> callback in callbacks) { object ev = Activator.CreateInstance(callback.Value._eventType); JsonConvert.PopulateObject(json, ev, _serializationSettings); callback.Value.Invoke(json, ev); } } }
internal void Message_Create(string json, DispatchGatewayEvent <MessageDescriptor> e) { mapper.Map(e.Payload); }
internal void Guild_Delete(string json, DispatchGatewayEvent <GuildDescriptor> e) { mapper.DeleteMap(e.Payload); }
internal void Guild_Update(string json, DispatchGatewayEvent <GuildDescriptor> e) { e.Payload = mapper.UpdateMap(e.Payload); }
/// <summary> /// Invoked when a <see cref="GatewayOpCode.HeartbeatAck"/> OpCode is received /// </summary> /// <param name="json"></param> /// <param name="ev"></param> internal void GatewayEvents_OnHeartbeatAck(string json, DispatchGatewayEvent <object> ev) { _heartbeatAck = true; }
/// <summary> /// Invoked when an <see cref="EventType.READY"/> event is received /// </summary> /// <param name="json"></param> /// <param name="ready"></param> internal void GatewayEvents_OnReady(string json, DispatchGatewayEvent <GatewayReady> ready) { _session = ready.Payload.Session; Configuration.LastSession = _session; }