Reflect new gpr_timespec type in C#

pull/2403/head^2
Craig Tiller 9 years ago
parent e95933587a
commit 2510ce477b
  1. 12
      src/csharp/Grpc.Core.Tests/TimespecTest.cs
  2. 15
      src/csharp/Grpc.Core/Internal/Enums.cs
  3. 8
      src/csharp/Grpc.Core/Internal/Timespec.cs

@ -61,28 +61,28 @@ namespace Grpc.Core.Internal.Tests
[Test] [Test]
public void Add() public void Add()
{ {
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(123456789) }; var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 };
var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10)); var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10));
Assert.AreEqual(result.tv_sec, new IntPtr(12355)); Assert.AreEqual(result.tv_sec, new IntPtr(12355));
Assert.AreEqual(result.tv_nsec, new IntPtr(123456789)); Assert.AreEqual(result.tv_nsec, 123456789);
} }
[Test] [Test]
public void Add_Nanos() public void Add_Nanos()
{ {
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(123456789) }; var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 123456789 };
var result = t.Add(TimeSpan.FromTicks(10)); var result = t.Add(TimeSpan.FromTicks(10));
Assert.AreEqual(result.tv_sec, new IntPtr(12345)); Assert.AreEqual(result.tv_sec, new IntPtr(12345));
Assert.AreEqual(result.tv_nsec, new IntPtr(123456789 + 1000)); Assert.AreEqual(result.tv_nsec, 123456789 + 1000);
} }
[Test] [Test]
public void Add_NanosOverflow() public void Add_NanosOverflow()
{ {
var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = new IntPtr(999999999) }; var t = new Timespec { tv_sec = new IntPtr(12345), tv_nsec = 999999999 };
var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10 + 10)); var result = t.Add(TimeSpan.FromTicks(TimeSpan.TicksPerSecond * 10 + 10));
Assert.AreEqual(result.tv_sec, new IntPtr(12356)); Assert.AreEqual(result.tv_sec, new IntPtr(12356));
Assert.AreEqual(result.tv_nsec, new IntPtr(999)); Assert.AreEqual(result.tv_nsec, 999);
} }
} }
} }

@ -90,4 +90,19 @@ namespace Grpc.Core.Internal
/* operation completion */ /* operation completion */
OpComplete OpComplete
} }
/// <summary>
/// gpr_clock_type from grpc/support/time.h
/// </summary>
internal enum GPRClockType
{
/* Monotonic clock */
Monotonic,
/* Realtime clock */
Realtime,
/* Timespan - the distance between two time points */
Timespan
}
} }

@ -55,7 +55,8 @@ namespace Grpc.Core.Internal
// NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8 // NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8
// so IntPtr seems to have the right size to work on both. // so IntPtr seems to have the right size to work on both.
public System.IntPtr tv_sec; public System.IntPtr tv_sec;
public System.IntPtr tv_nsec; public int tv_nsec;
public GPRClockType clock_type;
/// <summary> /// <summary>
/// Timespec a long time in the future. /// Timespec a long time in the future.
@ -99,12 +100,13 @@ namespace Grpc.Core.Internal
public Timespec Add(TimeSpan timeSpan) public Timespec Add(TimeSpan timeSpan)
{ {
long nanos = tv_nsec.ToInt64() + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * NanosPerTick; long nanos = (long)tv_nsec + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * NanosPerTick;
long overflow_sec = (nanos > NanosPerSecond) ? 1 : 0; long overflow_sec = (nanos > NanosPerSecond) ? 1 : 0;
Timespec result; Timespec result;
result.tv_nsec = new IntPtr(nanos % NanosPerSecond); result.tv_nsec = (int)(nanos % NanosPerSecond);
result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec); result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec);
result.clock_type = GPRClockType.Realtime;
return result; return result;
} }
} }

Loading…
Cancel
Save