blob: de783f5a4bcfe0d1e6252f6501fe5f4b339a2cb4 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08002// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08003// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
Craig Tiller190d3602015-02-18 09:23:38 -080018//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080030#endregion
Jan Tattermuscha7608b02015-02-03 17:54:38 -080031using System;
32using System.Runtime.InteropServices;
33using System.Threading;
34
Jan Tattermusch30868622015-02-19 09:22:33 -080035namespace Grpc.Core.Internal
Jan Tattermuscha7608b02015-02-03 17:54:38 -080036{
Jan Tattermusch13cd1252015-03-06 10:11:50 -080037 /// <summary>
38 /// gpr_timespec from grpc/support/time.h
39 /// </summary>
40 [StructLayout(LayoutKind.Sequential)]
41 internal struct Timespec
42 {
Jan Tattermusch075dde42015-03-11 18:21:00 -070043 const int NanosPerSecond = 1000 * 1000 * 1000;
44 const int NanosPerTick = 100;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080045
Jan Tattermusche1e11872015-02-12 10:23:37 -080046 [DllImport("grpc_csharp_ext.dll")]
47 static extern Timespec gprsharp_now();
48
49 [DllImport("grpc_csharp_ext.dll")]
50 static extern Timespec gprsharp_inf_future();
51
52 [DllImport("grpc_csharp_ext.dll")]
53 static extern int gprsharp_sizeof_timespec();
Jan Tattermuschd3677482015-06-01 19:27:40 -070054
Jan Tattermusch13cd1252015-03-06 10:11:50 -080055 // NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8
Jan Tattermusch61f93b22015-02-12 12:45:52 -080056 // so IntPtr seems to have the right size to work on both.
Jan Tattermusch13cd1252015-03-06 10:11:50 -080057 public System.IntPtr tv_sec;
Craig Tiller2510ce42015-07-17 14:24:36 -070058 public int tv_nsec;
59 public GPRClockType clock_type;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080060
Jan Tattermusch13cd1252015-03-06 10:11:50 -080061 /// <summary>
62 /// Timespec a long time in the future.
63 /// </summary>
64 public static Timespec InfFuture
65 {
66 get
67 {
Jan Tattermusche1e11872015-02-12 10:23:37 -080068 return gprsharp_inf_future();
Jan Tattermusch13cd1252015-03-06 10:11:50 -080069 }
70 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080071
72 public static Timespec Now
73 {
74 get
75 {
Jan Tattermusche1e11872015-02-12 10:23:37 -080076 return gprsharp_now();
Jan Tattermuscha7608b02015-02-03 17:54:38 -080077 }
78 }
79
Jan Tattermusch86a249f2015-02-12 10:36:48 -080080 internal static int NativeSize
81 {
82 get
83 {
84 return gprsharp_sizeof_timespec();
85 }
86 }
87
Jan Tattermuscha7608b02015-02-03 17:54:38 -080088 /// <summary>
89 /// Creates a GPR deadline from current instant and given timeout.
90 /// </summary>
91 /// <returns>The from timeout.</returns>
Jan Tattermusch13cd1252015-03-06 10:11:50 -080092 public static Timespec DeadlineFromTimeout(TimeSpan timeout)
93 {
Jan Tattermuscha7608b02015-02-03 17:54:38 -080094 if (timeout == Timeout.InfiniteTimeSpan)
95 {
96 return Timespec.InfFuture;
97 }
98 return Timespec.Now.Add(timeout);
99 }
100
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800101 public Timespec Add(TimeSpan timeSpan)
102 {
Craig Tiller2510ce42015-07-17 14:24:36 -0700103 long nanos = (long)tv_nsec + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * NanosPerTick;
Jan Tattermusch075dde42015-03-11 18:21:00 -0700104 long overflow_sec = (nanos > NanosPerSecond) ? 1 : 0;
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800105
106 Timespec result;
Craig Tiller2510ce42015-07-17 14:24:36 -0700107 result.tv_nsec = (int)(nanos % NanosPerSecond);
Craig Tiller190d3602015-02-18 09:23:38 -0800108 result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec);
Craig Tiller2510ce42015-07-17 14:24:36 -0700109 result.clock_type = GPRClockType.Realtime;
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800110 return result;
111 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800112 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800113}