diff options
Diffstat (limited to 'src/csharp/GrpcCore/Internal/Timespec.cs')
-rw-r--r-- | src/csharp/GrpcCore/Internal/Timespec.cs | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/csharp/GrpcCore/Internal/Timespec.cs b/src/csharp/GrpcCore/Internal/Timespec.cs index 5a197e121c..c45926707f 100644 --- a/src/csharp/GrpcCore/Internal/Timespec.cs +++ b/src/csharp/GrpcCore/Internal/Timespec.cs @@ -13,13 +13,20 @@ namespace Google.GRPC.Core.Internal const int nanosPerSecond = 1000 * 1000 * 1000; const int nanosPerTick = 100; - [DllImport("gpr.dll")] - static extern Timespec gpr_now(); + [DllImport("grpc_csharp_ext.dll")] + static extern Timespec gprsharp_now(); - // TODO: this only works on 64bit linux, can we autoselect the right size of ints? - // perhaps using IntPtr would work. - public System.Int64 tv_sec; - public System.Int64 tv_nsec; + [DllImport("grpc_csharp_ext.dll")] + static extern Timespec gprsharp_inf_future(); + + [DllImport("grpc_csharp_ext.dll")] + static extern int gprsharp_sizeof_timespec(); + + // TODO: revisit this. + // 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. + public System.IntPtr tv_sec; + public System.IntPtr tv_nsec; /// <summary> /// Timespec a long time in the future. @@ -28,8 +35,7 @@ namespace Google.GRPC.Core.Internal { get { - // TODO: set correct value based on the length of the struct - return new Timespec { tv_sec = Int32.MaxValue, tv_nsec = 0 }; + return gprsharp_inf_future(); } } @@ -37,7 +43,15 @@ namespace Google.GRPC.Core.Internal { get { - return gpr_now(); + return gprsharp_now(); + } + } + + internal static int NativeSize + { + get + { + return gprsharp_sizeof_timespec(); } } @@ -54,12 +68,12 @@ namespace Google.GRPC.Core.Internal } public Timespec Add(TimeSpan timeSpan) { - long nanos = tv_nsec + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * nanosPerTick; + long nanos = tv_nsec.ToInt64() + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * nanosPerTick; long overflow_sec = (nanos > nanosPerSecond) ? 1 : 0; Timespec result; - result.tv_nsec = nanos % nanosPerSecond; - result.tv_sec = tv_sec + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec; + result.tv_nsec = new IntPtr(nanos % nanosPerSecond); + result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec); return result; } } |