2023-02-16 21:16:06 +00:00
|
|
|
|
using SharpChat.Misuzu;
|
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System;
|
2023-02-16 22:33:48 +00:00
|
|
|
|
using System.Collections.Generic;
|
2023-02-16 21:16:06 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.PacketHandlers {
|
|
|
|
|
public class PingHandler : IChatPacketHandler {
|
|
|
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
|
|
|
|
|
|
private readonly object BumpAccess = new();
|
|
|
|
|
private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1);
|
|
|
|
|
private DateTimeOffset LastBump = DateTimeOffset.MinValue;
|
|
|
|
|
|
|
|
|
|
public PingHandler(MisuzuClient msz) {
|
|
|
|
|
Misuzu = msz ?? throw new ArgumentNullException(nameof(msz));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(ChatPacketHandlerContext ctx) {
|
|
|
|
|
return ctx.CheckPacketId("0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(ChatPacketHandlerContext ctx) {
|
|
|
|
|
string[] parts = ctx.SplitText(2);
|
|
|
|
|
|
|
|
|
|
if(!int.TryParse(parts.FirstOrDefault(), out int pTime))
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.Connection.BumpPing();
|
|
|
|
|
ctx.Connection.Send(new PongPacket(ctx.Connection.LastPing));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
lock(BumpAccess) {
|
|
|
|
|
if(LastBump < DateTimeOffset.UtcNow - BumpInterval) {
|
|
|
|
|
(string, string)[] bumpList;
|
2023-02-16 22:33:48 +00:00
|
|
|
|
lock(ctx.Chat.UsersAccess) {
|
|
|
|
|
IEnumerable<ChatUser> filtered = ctx.Chat.Users.Where(u => u.Status == ChatUserStatus.Online);
|
|
|
|
|
|
|
|
|
|
lock(ctx.Chat.ConnectionsAccess)
|
|
|
|
|
filtered = filtered.Where(u => ctx.Chat.Connections.Any(c => c.User == u));
|
|
|
|
|
|
|
|
|
|
bumpList = filtered
|
|
|
|
|
.Select(u => (u.UserId.ToString(), ctx.Chat.GetRemoteAddresses(u).FirstOrDefault()?.ToString() ?? string.Empty))
|
2023-02-16 21:16:06 +00:00
|
|
|
|
.ToArray();
|
2023-02-16 22:33:48 +00:00
|
|
|
|
}
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
if(bumpList.Any())
|
|
|
|
|
Task.Run(async () => {
|
|
|
|
|
await Misuzu.BumpUsersOnlineAsync(bumpList);
|
|
|
|
|
}).Wait();
|
|
|
|
|
|
|
|
|
|
LastBump = DateTimeOffset.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|