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 string TargetName => Name;
public ChatChannel() { public ChatChannel() { }
}
public ChatChannel(string name) { public ChatChannel(string name) {
Name = name; Name = name;

View file

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

View file

@ -107,22 +107,9 @@ namespace SharpChat {
return null; 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) { public static void LogEvent(IChatEvent evt) {
if(evt.SequenceId < 1) if(evt.SequenceId < 1)
evt.SequenceId = GenerateId(); evt.SequenceId = SharpId.Next();
RunCommand( RunCommand(
"INSERT INTO `sqc_events` (`event_id`, `event_created`, `event_type`, `event_target`, `event_flags`, `event_data`" "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);
}
}