The Labs \ Source Viewer \ SSCLI \ System.Net.Sockets \ TransmitFileOverlappedAsyncResult

  1. //------------------------------------------------------------------------------
  2. // <copyright file="_OverlappedAsyncResult.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net.Sockets
  16. {
  17.     using System;
  18.     using System.Net;
  19.     using System.IO;
  20.     using System.Runtime.InteropServices;
  21.     using System.Threading;
  22.     using Microsoft.Win32;
  23.    
  24.     //
  25.     // OverlappedAsyncResult - used to take care of storage for async Socket operation
  26.     // from the BeginSend, BeginSendTo, BeginReceive, BeginReceiveFrom calls.
  27.     //
  28.     internal class TransmitFileOverlappedAsyncResult : BaseOverlappedAsyncResult
  29.     {
  30.        
  31.         //
  32.         // internal class members
  33.         //
  34.        
  35.         private FileStream m_fileStream;
  36.         private TransmitFileOptions m_flags;
  37.         private TransmitFileBuffers m_buffers;
  38.        
  39.         // Constructor. We take in the socket that's creating us, the caller's
  40.         // state object, and the buffer on which the I/O will be performed.
  41.         // We save the socket and state, pin the callers's buffer, and allocate
  42.         // an event for the WaitHandle.
  43.         //
  44.         internal TransmitFileOverlappedAsyncResult(Socket socket, object asyncState, AsyncCallback asyncCallback) : base(socket, asyncState, asyncCallback)
  45.         {
  46.         }
  47.        
  48.         internal TransmitFileOverlappedAsyncResult(Socket socket) : base(socket)
  49.         {
  50.         }
  51.        
  52.        
  53.         //
  54.         // SetUnmanagedStructures -
  55.         // Fills in Overlapped Structures used in an Async Overlapped Winsock call
  56.         // these calls are outside the runtime and are unmanaged code, so we need
  57.         // to prepare specific structures and ints that lie in unmanaged memory
  58.         // since the Overlapped calls can be Async
  59.         //
  60.         internal void SetUnmanagedStructures(byte[] preBuffer, byte[] postBuffer, FileStream fileStream, TransmitFileOptions flags, bool sync)
  61.         {
  62.             //
  63.             // fill in flags if we use it.
  64.             //
  65.             m_fileStream = fileStream;
  66.             m_flags = flags;
  67.            
  68.             //
  69.             // Fill in Buffer Array structure that will be used for our send/recv Buffer
  70.             //
  71.             m_buffers = null;
  72.             int buffsNumber = 0;
  73.            
  74.             if (preBuffer != null && preBuffer.Length > 0)
  75.                 ++buffsNumber;
  76.            
  77.             if (postBuffer != null && postBuffer.Length > 0)
  78.                 ++buffsNumber;
  79.            
  80.             object[] objectsToPin = null;
  81.             if (buffsNumber != 0) {
  82.                 ++buffsNumber;
  83.                 objectsToPin = new object[buffsNumber];
  84.                
  85.                 m_buffers = new TransmitFileBuffers();
  86.                
  87.                 objectsToPin[--buffsNumber] = m_buffers;
  88.                
  89.                 if (preBuffer != null && preBuffer.Length > 0) {
  90.                     m_buffers.preBufferLength = preBuffer.Length;
  91.                     objectsToPin[--buffsNumber] = preBuffer;
  92.                 }
  93.                
  94.                 if (postBuffer != null && postBuffer.Length > 0) {
  95.                     m_buffers.postBufferLength = postBuffer.Length;
  96.                     objectsToPin[--buffsNumber] = postBuffer;
  97.                 }
  98.                
  99.                 if (sync) {
  100.                     base.PinUnmanagedObjects(objectsToPin);
  101.                 }
  102.                 else {
  103.                     base.SetUnmanagedStructures(objectsToPin);
  104.                 }
  105.                
  106.                 if (preBuffer != null && preBuffer.Length > 0) {
  107.                     m_buffers.preBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(preBuffer, 0);
  108.                 }
  109.                
  110.                 if (postBuffer != null && postBuffer.Length > 0) {
  111.                     m_buffers.postBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(postBuffer, 0);
  112.                 }
  113.             }
  114.             else if (!sync) {
  115.                 base.SetUnmanagedStructures(null);
  116.             }
  117.            
  118.         }
  119.         // SetUnmanagedStructures()
  120.         internal void SetUnmanagedStructures(byte[] preBuffer, byte[] postBuffer, FileStream fileStream, TransmitFileOptions flags, ref OverlappedCache overlappedCache)
  121.         {
  122.             SetupCache(ref overlappedCache);
  123.             SetUnmanagedStructures(preBuffer, postBuffer, fileStream, flags, false);
  124.         }
  125.        
  126.         // Utility cleanup routine. Frees pinned and unmanged memory.
  127.         //
  128.         protected override void ForceReleaseUnmanagedStructures()
  129.         {
  130.             if (m_fileStream != null) {
  131.                 m_fileStream.Close();
  132.                 m_fileStream = null;
  133.             }
  134.             //
  135.             // clenaup base class
  136.             //
  137.             base.ForceReleaseUnmanagedStructures();
  138.            
  139.         }
  140.         // CleanupUnmanagedStructures()
  141.         internal void SyncReleaseUnmanagedStructures()
  142.         {
  143.             ForceReleaseUnmanagedStructures();
  144.         }
  145.        
  146.         internal TransmitFileBuffers TransmitFileBuffers {
  147.             get { return m_buffers; }
  148.         }
  149.        
  150.         internal TransmitFileOptions Flags {
  151.             get { return m_flags; }
  152.         }
  153.        
  154.        
  155.     }
  156.     // class OverlappedAsyncResult
  157.    
  158.    
  159.    
  160. }
  161. // namespace System.Net.Sockets

Developer Fusion