Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 2 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 3 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 9 | // * 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 18 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 19 | // 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 Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 30 | #endregion |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 31 | using System; |
| 32 | using System.Runtime.InteropServices; |
| 33 | using System.Threading; |
| 34 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 35 | namespace Grpc.Core.Internal |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 36 | { |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 37 | /// <summary> |
| 38 | /// gpr_timespec from grpc/support/time.h |
| 39 | /// </summary> |
| 40 | [StructLayout(LayoutKind.Sequential)] |
| 41 | internal struct Timespec |
| 42 | { |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 43 | const int NanosPerSecond = 1000 * 1000 * 1000; |
| 44 | const int NanosPerTick = 100; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 45 | |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 46 | [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 Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 54 | |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 55 | // NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8 |
Jan Tattermusch | 61f93b2 | 2015-02-12 12:45:52 -0800 | [diff] [blame] | 56 | // so IntPtr seems to have the right size to work on both. |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 57 | public System.IntPtr tv_sec; |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 58 | public int tv_nsec; |
| 59 | public GPRClockType clock_type; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 60 | |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 61 | /// <summary> |
| 62 | /// Timespec a long time in the future. |
| 63 | /// </summary> |
| 64 | public static Timespec InfFuture |
| 65 | { |
| 66 | get |
| 67 | { |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 68 | return gprsharp_inf_future(); |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 71 | |
| 72 | public static Timespec Now |
| 73 | { |
| 74 | get |
| 75 | { |
Jan Tattermusch | e1e1187 | 2015-02-12 10:23:37 -0800 | [diff] [blame] | 76 | return gprsharp_now(); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Jan Tattermusch | 86a249f | 2015-02-12 10:36:48 -0800 | [diff] [blame] | 80 | internal static int NativeSize |
| 81 | { |
| 82 | get |
| 83 | { |
| 84 | return gprsharp_sizeof_timespec(); |
| 85 | } |
| 86 | } |
| 87 | |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 88 | /// <summary> |
| 89 | /// Creates a GPR deadline from current instant and given timeout. |
| 90 | /// </summary> |
| 91 | /// <returns>The from timeout.</returns> |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 92 | public static Timespec DeadlineFromTimeout(TimeSpan timeout) |
| 93 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 94 | if (timeout == Timeout.InfiniteTimeSpan) |
| 95 | { |
| 96 | return Timespec.InfFuture; |
| 97 | } |
| 98 | return Timespec.Now.Add(timeout); |
| 99 | } |
| 100 | |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 101 | public Timespec Add(TimeSpan timeSpan) |
| 102 | { |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 103 | long nanos = (long)tv_nsec + (timeSpan.Ticks % TimeSpan.TicksPerSecond) * NanosPerTick; |
Jan Tattermusch | 075dde4 | 2015-03-11 18:21:00 -0700 | [diff] [blame] | 104 | long overflow_sec = (nanos > NanosPerSecond) ? 1 : 0; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 105 | |
| 106 | Timespec result; |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 107 | result.tv_nsec = (int)(nanos % NanosPerSecond); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 108 | result.tv_sec = new IntPtr(tv_sec.ToInt64() + (timeSpan.Ticks / TimeSpan.TicksPerSecond) + overflow_sec); |
Craig Tiller | 2510ce4 | 2015-07-17 14:24:36 -0700 | [diff] [blame] | 109 | result.clock_type = GPRClockType.Realtime; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 110 | return result; |
| 111 | } |
Jan Tattermusch | 13cd125 | 2015-03-06 10:11:50 -0800 | [diff] [blame] | 112 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 113 | } |