Backported SharpId class

This commit is contained in:
flash 2023-02-08 04:32:12 +01:00
parent 40c7ba4ded
commit 56a818254e
4 changed files with 17 additions and 19 deletions

View File

@ -17,8 +17,7 @@ namespace SharpChat {
public string TargetName => Name;
public ChatChannel() {
}
public ChatChannel() { }
public ChatChannel(string name) {
Name = name;

View File

@ -4,15 +4,13 @@ using System;
using System.Collections.Generic;
namespace SharpChat {
public class ChatContext : IDisposable, IPacketTarget {
public class ChatContext : IDisposable {
public bool IsDisposed { get; private set; }
public ChannelManager Channels { get; }
public UserManager Users { get; }
public ChatEventManager Events { get; }
public string TargetName => "@broadcast";
public ChatContext() {
Users = new(this);
Channels = new(this);

View File

@ -107,22 +107,9 @@ namespace SharpChat {
return null;
}
private const long ID_EPOCH = 1588377600000;
private static int IdCounter = 0;
public static long GenerateId() {
if(IdCounter > 200)
IdCounter = 0;
long id = 0;
id |= (DateTimeOffset.Now.ToUnixTimeMilliseconds() - ID_EPOCH) << 8;
id |= (ushort)(++IdCounter);
return id;
}
public static void LogEvent(IChatEvent evt) {
if(evt.SequenceId < 1)
evt.SequenceId = GenerateId();
evt.SequenceId = SharpId.Next();
RunCommand(
"INSERT INTO `sqc_events` (`event_id`, `event_created`, `event_type`, `event_target`, `event_flags`, `event_data`"

14
SharpChat/SharpId.cs Normal file
View File

@ -0,0 +1,14 @@
using System;
using System.Threading;
namespace SharpChat {
public static class SharpId {
private const long EPOCH = 1588377600000;
private static int Counter = 0;
public static long Next()
=> ((DateTimeOffset.Now.ToUnixTimeMilliseconds() - EPOCH) << 8)
| (ushort)(Interlocked.Increment(ref Counter) & 0xFFFF);
}
}