src/Library/NetPro.Utility/Helpers/Internal/ObjectId.cs
(开头部分) 19KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace NetPro.Utility.Helpers.Internal
{
/// <summary>
/// Id生成器,代码出自:https://github.com/tangxuehua/ecommon/blob/master/src/ECommon/Utilities/ObjectId.cs
/// </summary>
internal struct ObjectId : IComparable<ObjectId>, IEquatable<ObjectId>
{
// private static fields
private static readonly DateTime UnixEpoch;
private static readonly ObjectId EmptyInstance = default;
private static readonly int StaticMachine;
private static readonly short StaticPid;
private static int _staticIncrement; // high byte will be masked out when generating new ObjectId
private static readonly uint[] Lookup32 = Enumerable.Range(0, 256).Select(i =>
{
string s = i.ToString("x2");
return s[0] + ((uint)s[1] << 16);
}).ToArray();
// we're using 14 bytes instead of 12 to hold the ObjectId in memory but unlike a byte[] there is no additional object on the heap
// the extra two bytes are not visible to anyone outside of this class and they buy us considerable simplification
// an additional advantage of this representation is that it will serialize to JSON without any 64 bit overflow problems
private readonly int _timestamp;
private readonly int _machine;
private readonly short _pid;
private readonly int _increment;
// static constructor
static ObjectId()
{
UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
StaticMachine = GetMachineHash();
_staticIncrement = (new Random()).Next();
StaticPid = (short)GetCurrentProcessId();
}
// constructors
/// <summary>
/// Initializes a new instance of the ObjectId class.
/// </summary>
/// <param name="bytes">The bytes.</param>
public ObjectId(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
Unpack(bytes, out _timestamp, out _machine, out _pid, out _increment);
}
/// <summary>
/// Initializes a new instance of the ObjectId class.
后面还有 475 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。