diff options
Diffstat (limited to 'src/csharp/Grpc.Core/Internal/Timespec.cs')
-rw-r--r-- | src/csharp/Grpc.Core/Internal/Timespec.cs | 48 |
1 files changed, 23 insertions, 25 deletions
diff --git a/src/csharp/Grpc.Core/Internal/Timespec.cs b/src/csharp/Grpc.Core/Internal/Timespec.cs index b191ecde94..e6efd66f13 100644 --- a/src/csharp/Grpc.Core/Internal/Timespec.cs +++ b/src/csharp/Grpc.Core/Internal/Timespec.cs @@ -1,5 +1,4 @@ #region Copyright notice and license - // Copyright 2015, Google Inc. // All rights reserved. // @@ -28,21 +27,19 @@ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - #endregion - using System; using System.Runtime.InteropServices; using System.Threading; namespace Grpc.Core.Internal { - /// <summary> - /// gpr_timespec from grpc/support/time.h - /// </summary> - [StructLayout(LayoutKind.Sequential)] - internal struct Timespec - { + /// <summary> + /// gpr_timespec from grpc/support/time.h + /// </summary> + [StructLayout(LayoutKind.Sequential)] + internal struct Timespec + { const int nanosPerSecond = 1000 * 1000 * 1000; const int nanosPerTick = 100; @@ -54,23 +51,22 @@ namespace Grpc.Core.Internal [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 + // 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; + public System.IntPtr tv_sec; + public System.IntPtr tv_nsec; - /// <summary> - /// Timespec a long time in the future. - /// </summary> - public static Timespec InfFuture - { - get - { + /// <summary> + /// Timespec a long time in the future. + /// </summary> + public static Timespec InfFuture + { + get + { return gprsharp_inf_future(); - } - } + } + } public static Timespec Now { @@ -92,7 +88,8 @@ namespace Grpc.Core.Internal /// Creates a GPR deadline from current instant and given timeout. /// </summary> /// <returns>The from timeout.</returns> - public static Timespec DeadlineFromTimeout(TimeSpan timeout) { + public static Timespec DeadlineFromTimeout(TimeSpan timeout) + { if (timeout == Timeout.InfiniteTimeSpan) { return Timespec.InfFuture; @@ -100,7 +97,8 @@ namespace Grpc.Core.Internal return Timespec.Now.Add(timeout); } - public Timespec Add(TimeSpan timeSpan) { + public Timespec Add(TimeSpan timeSpan) + { long nanos = tv_nsec.ToInt64() + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * nanosPerTick; long overflow_sec = (nanos > nanosPerSecond) ? 1 : 0; @@ -109,6 +107,6 @@ namespace Grpc.Core.Internal result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec); return result; } - } + } } |