sharp-chat/SharpChat/C2SPacketHandlers/PingC2SPacketHandler.cs
flashwave 5a7756894b
First bits of the Context overhaul.
Reintroduces separate contexts for users, channels, connections (now split into sessions and connections) and user-channel associations.
It builds which is as much assurance as I can give about the stability of this commit, but its also the bare minimum of what i like to commit sooooo
A lot of things still need to be broadcast through events throughout the application in order to keep states consistent but we'll cross that bridge when we get to it.
I really need to stop using that phrase thingy, I'm overusing it.
2025-05-03 02:49:51 +00:00

43 lines
1.4 KiB
C#

using SharpChat.Auth;
using SharpChat.SockChat.S2CPackets;
using SharpChat.Users;
using System.Net;
namespace SharpChat.C2SPacketHandlers;
public class PingC2SPacketHandler(AuthClient authClient) : C2SPacketHandler {
private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1);
private DateTimeOffset LastBump = DateTimeOffset.MinValue;
public bool IsMatch(C2SPacketHandlerContext ctx) {
return ctx.CheckPacketId("0");
}
public async Task Handle(C2SPacketHandlerContext ctx) {
if(ctx.Session is null)
return;
string[] parts = ctx.SplitText(2);
if(!int.TryParse(parts.FirstOrDefault(), out int pTime))
return;
ctx.Session.Heartbeat();
await ctx.Connection.Send(new PongS2CPacket());
ctx.Chat.ContextAccess.Wait();
try {
if(LastBump < DateTimeOffset.UtcNow - BumpInterval) {
(IPAddress, string)[] bumpList = [.. ctx.Chat.Users.GetUsersWithStatus(UserStatus.Online)
.Select(u => (ctx.Chat.Sessions.GetRemoteEndPoints(u).Select(e => e.Address).FirstOrDefault() ?? IPAddress.None, u.UserId))];
if(bumpList.Length > 0)
await authClient.AuthBumpUsersOnline(bumpList);
LastBump = DateTimeOffset.UtcNow;
}
} finally {
ctx.Chat.ContextAccess.Release();
}
}
}