51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using SharpChat.Misuzu;
|
|
using SharpChat.Packet;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SharpChat.PacketHandlers {
|
|
public class PingHandler : IPacketHandler {
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1);
|
|
private DateTimeOffset LastBump = DateTimeOffset.MinValue;
|
|
|
|
public PingHandler(MisuzuClient msz) {
|
|
Misuzu = msz;
|
|
}
|
|
|
|
public bool IsMatch(PacketHandlerContext ctx) {
|
|
return ctx.CheckPacketId("0");
|
|
}
|
|
|
|
public void Handle(PacketHandlerContext ctx) {
|
|
string[] parts = ctx.SplitText(2);
|
|
|
|
if(!int.TryParse(parts.FirstOrDefault(), out int pTime))
|
|
return;
|
|
|
|
ctx.Connection.BumpPing();
|
|
ctx.Connection.Send(new PongPacket());
|
|
|
|
ctx.Chat.ContextAccess.Wait();
|
|
try {
|
|
if(LastBump < DateTimeOffset.UtcNow - BumpInterval) {
|
|
(string, string)[] bumpList = ctx.Chat.Users.All
|
|
.Where(u => u.Status == UserStatus.Online && ctx.Chat.Connections.Any(c => c.User == u))
|
|
.Select(u => (u.UserId.ToString(), ctx.Chat.GetRemoteAddresses(u).FirstOrDefault()?.ToString() ?? string.Empty))
|
|
.ToArray();
|
|
|
|
if(bumpList.Any())
|
|
Task.Run(async () => {
|
|
await Misuzu.BumpUsersOnlineAsync(bumpList);
|
|
}).Wait();
|
|
|
|
LastBump = DateTimeOffset.UtcNow;
|
|
}
|
|
} finally {
|
|
ctx.Chat.ContextAccess.Release();
|
|
}
|
|
}
|
|
}
|
|
}
|