Overlapped.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // System.Threading.Overlapped.cs
  3. //
  4. // Authors:
  5. // Dick Porter ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected]);
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Runtime.InteropServices;
  34. using System.Security.Permissions;
  35. namespace System.Threading
  36. {
  37. [ComVisible (true)]
  38. public class Overlapped
  39. {
  40. IAsyncResult ares;
  41. int offsetL;
  42. int offsetH;
  43. int evt;
  44. IntPtr evt_ptr;
  45. public Overlapped ()
  46. {
  47. }
  48. [Obsolete ("Not 64bit compatible. Please use the constructor that takes IntPtr for the event handle")]
  49. public Overlapped(int offsetLo, int offsetHi, int hEvent, IAsyncResult ar)
  50. {
  51. offsetL = offsetLo;
  52. offsetH = offsetHi;
  53. evt = hEvent;
  54. ares = ar;
  55. }
  56. public Overlapped (int offsetLo, int offsetHi, IntPtr hEvent,
  57. IAsyncResult ar)
  58. {
  59. offsetL = offsetLo;
  60. offsetH = offsetHi;
  61. evt_ptr = hEvent;
  62. ares = ar;
  63. }
  64. [CLSCompliant(false)]
  65. unsafe public static void Free (NativeOverlapped *nativeOverlappedPtr)
  66. {
  67. if ((IntPtr) nativeOverlappedPtr == IntPtr.Zero)
  68. throw new ArgumentNullException ("nativeOverlappedPtr");
  69. Marshal.FreeHGlobal ((IntPtr) nativeOverlappedPtr);
  70. }
  71. [CLSCompliant(false)]
  72. unsafe public static Overlapped Unpack (NativeOverlapped *nativeOverlappedPtr)
  73. {
  74. if ((IntPtr) nativeOverlappedPtr == IntPtr.Zero)
  75. throw new ArgumentNullException ("nativeOverlappedPtr");
  76. Overlapped result = new Overlapped ();
  77. result.offsetL = nativeOverlappedPtr->OffsetLow;
  78. result.offsetH = nativeOverlappedPtr->OffsetHigh;
  79. result.evt = (int)nativeOverlappedPtr->EventHandle;
  80. return result;
  81. }
  82. [CLSCompliant(false)]
  83. [Obsolete ("Use Pack(iocb, userData) instead")]
  84. [MonoTODO ("Security - we need to propagate the call stack")]
  85. unsafe public NativeOverlapped *Pack (IOCompletionCallback iocb)
  86. {
  87. NativeOverlapped *result = (NativeOverlapped *) Marshal.AllocHGlobal (Marshal.SizeOf (typeof (NativeOverlapped)));
  88. result->OffsetLow = offsetL;
  89. result->OffsetHigh = offsetH;
  90. result->EventHandle = (IntPtr)evt;
  91. return result;
  92. }
  93. [CLSCompliant (false)]
  94. [ComVisible (false)]
  95. [MonoTODO ("handle userData")]
  96. unsafe public NativeOverlapped *Pack (IOCompletionCallback iocb, object userData)
  97. {
  98. NativeOverlapped *result = (NativeOverlapped *) Marshal.AllocHGlobal (Marshal.SizeOf(typeof(NativeOverlapped)));
  99. result->OffsetLow = offsetL;
  100. result->OffsetHigh = offsetH;
  101. result->EventHandle = evt_ptr;
  102. return(result);
  103. }
  104. [CLSCompliant(false)]
  105. [Obsolete ("Use UnsafePack(iocb, userData) instead")]
  106. [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
  107. unsafe public NativeOverlapped *UnsafePack (IOCompletionCallback iocb)
  108. {
  109. // no need to propagate the call stack in the unsafe version
  110. return Pack (iocb);
  111. }
  112. [ComVisible (false)]
  113. [CLSCompliant (false)]
  114. unsafe public NativeOverlapped *UnsafePack (IOCompletionCallback iocb, object userData)
  115. {
  116. return Pack (iocb, userData);
  117. }
  118. public IAsyncResult AsyncResult {
  119. get { return ares; }
  120. set { ares = value; }
  121. }
  122. [Obsolete ("Not 64bit compatible. Use EventHandleIntPtr instead.")]
  123. public int EventHandle {
  124. get { return evt; }
  125. set { evt = value; }
  126. }
  127. [ComVisible (false)]
  128. public IntPtr EventHandleIntPtr
  129. {
  130. get{
  131. return(evt_ptr);
  132. }
  133. set{
  134. evt_ptr = value;
  135. }
  136. }
  137. public int OffsetHigh {
  138. get { return offsetH; }
  139. set { offsetH = value; }
  140. }
  141. public int OffsetLow {
  142. get { return offsetL; }
  143. set { offsetL = value; }
  144. }
  145. }
  146. }