diff --git a/source/MongoDB/MongoDB.csproj b/source/MongoDB/MongoDB.csproj index b3ec788e..64ec5ba2 100644 --- a/source/MongoDB/MongoDB.csproj +++ b/source/MongoDB/MongoDB.csproj @@ -301,7 +301,7 @@ - + diff --git a/source/MongoDB/OidGenerator.cs b/source/MongoDB/OidGenerator.cs deleted file mode 100644 index 9b94eb63..00000000 --- a/source/MongoDB/OidGenerator.cs +++ /dev/null @@ -1,105 +0,0 @@ - -using System; -using System.Diagnostics; -using System.Security.Cryptography; -using System.Text; -using MongoDB.Bson; - -namespace MongoDB -{ - /// - /// - /// - internal class OidGenerator - { - private int inc; - private object inclock = new object(); - private byte[] machineHash; - private byte[] procID; - - /// - /// Initializes a new instance of the class. - /// - public OidGenerator(){ - GenerateConstants(); - } - - /// - /// Generates this instance. - /// - /// - public Oid Generate(){ - //FIXME Endian issues with this code. - //.Net runs in native endian mode which is usually little endian. - //Big endian machines don't need the reversing (Linux+PPC, XNA on XBox) - byte[] oid = new byte[12]; - int copyidx = 0; - - byte[] time = BitConverter.GetBytes(GenerateTime()); - Array.Reverse(time); - Array.Copy(time,0,oid,copyidx,4); - copyidx += 4; - - Array.Copy(machineHash,0,oid,copyidx,3); - copyidx += 3; - - Array.Copy(this.procID,2,oid,copyidx,2); - copyidx += 2; - - byte[] inc = BitConverter.GetBytes(GenerateInc()); - Array.Reverse(inc); - Array.Copy(inc,1,oid,copyidx,3); - - return new Oid(oid); - } - - /// - /// Generates the time. - /// - /// - private int GenerateTime(){ - DateTime now = DateTime.UtcNow; - //DateTime nowtime = new DateTime(epoch.Year, epoch.Month, epoch.Day, now.Hour, now.Minute, now.Second, now.Millisecond); - TimeSpan diff = now - BsonInfo.Epoch; - return Convert.ToInt32(Math.Floor(diff.TotalSeconds)); - } - - /// - /// Generates the inc. - /// - /// - private int GenerateInc(){ - lock(this.inclock){ - return ++inc; - } - } - - /// - /// Generates the constants. - /// - private void GenerateConstants(){ - this.machineHash = GenerateHostHash(); - this.procID = BitConverter.GetBytes(GenerateProcId()); - Array.Reverse(this.procID); - } - - /// - /// Generates the host hash. - /// - /// - private byte[] GenerateHostHash(){ - MD5 md5 = MD5.Create(); - string host = System.Net.Dns.GetHostName(); - return md5.ComputeHash(Encoding.Default.GetBytes(host)); - } - - /// - /// Generates the proc id. - /// - /// - private int GenerateProcId(){ - Process proc = Process.GetCurrentProcess(); - return proc.Id; - } - } -} diff --git a/source/MongoDB/Util/OidGenerator.cs b/source/MongoDB/Util/OidGenerator.cs new file mode 100644 index 00000000..311aadec --- /dev/null +++ b/source/MongoDB/Util/OidGenerator.cs @@ -0,0 +1,112 @@ +using System; +using System.Diagnostics; +using System.Net; +using System.Security.Cryptography; +using System.Text; +using MongoDB.Bson; + +namespace MongoDB +{ + /// + /// + internal class OidGenerator + { + private readonly object inclock = new object(); + private int inc; + private byte[] machineHash; + private byte[] procID; + + /// + /// Initializes a new instance of the class. + /// + public OidGenerator() + { + GenerateConstants(); + } + + /// + /// Generates this instance. + /// + /// + public Oid Generate() + { + //FIXME Endian issues with this code. + //.Net runs in native endian mode which is usually little endian. + //Big endian machines don't need the reversing (Linux+PPC, XNA on XBox) + var oid = new byte[12]; + var copyidx = 0; + + var time = BitConverter.GetBytes(GenerateTime()); + Array.Reverse(time); + Array.Copy(time, 0, oid, copyidx, 4); + copyidx += 4; + + Array.Copy(machineHash, 0, oid, copyidx, 3); + copyidx += 3; + + Array.Copy(procID, 2, oid, copyidx, 2); + copyidx += 2; + + var inc = BitConverter.GetBytes(GenerateInc()); + Array.Reverse(inc); + Array.Copy(inc, 1, oid, copyidx, 3); + + return new Oid(oid); + } + + /// + /// Generates the time. + /// + /// + private int GenerateTime() + { + var now = DateTime.UtcNow; + //DateTime nowtime = new DateTime(epoch.Year, epoch.Month, epoch.Day, now.Hour, now.Minute, now.Second, now.Millisecond); + var diff = now - BsonInfo.Epoch; + return Convert.ToInt32(Math.Floor(diff.TotalSeconds)); + } + + /// + /// Generates the inc. + /// + /// + private int GenerateInc() + { + lock(inclock) + { + return ++inc; + } + } + + /// + /// Generates the constants. + /// + private void GenerateConstants() + { + machineHash = GenerateHostHash(); + procID = BitConverter.GetBytes(GenerateProcId()); + Array.Reverse(procID); + } + + /// + /// Generates the host hash. + /// + /// + private byte[] GenerateHostHash() + { + var md5 = MD5.Create(); + var host = Dns.GetHostName(); + return md5.ComputeHash(Encoding.Default.GetBytes(host)); + } + + /// + /// Generates the proc id. + /// + /// + private int GenerateProcId() + { + var proc = Process.GetCurrentProcess(); + return proc.Id; + } + } +} \ No newline at end of file