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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="_AcceptOverlappedAsyncResult.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.Runtime.InteropServices;
  20.     using System.Threading;
  21.     using Microsoft.Win32;
  22.    
  23.     //
  24.     // AcceptOverlappedAsyncResult - used to take care of storage for async Socket BeginAccept call.
  25.     //
  26.     internal class AcceptOverlappedAsyncResult : BaseOverlappedAsyncResult
  27.     {
  28.        
  29.         //
  30.         // internal class members
  31.         //
  32.        
  33.         private int m_LocalBytesTransferred;
  34.         private Socket m_ListenSocket;
  35.         private Socket m_AcceptSocket;
  36.        
  37.         private int m_AddressBufferLength;
  38.         private byte[] m_Buffer;
  39.        
  40.         // Constructor. We take in the socket that's creating us, the caller's
  41.         // state object, and the buffer on which the I/O will be performed.
  42.         // We save the socket and state, pin the callers's buffer, and allocate
  43.         // an event for the WaitHandle.
  44.         //
  45.         internal AcceptOverlappedAsyncResult(Socket listenSocket, object asyncState, AsyncCallback asyncCallback) : base(listenSocket, asyncState, asyncCallback)
  46.         {
  47.             m_ListenSocket = listenSocket;
  48.         }
  49.        
  50.        
  51.        
  52.         //
  53.         // SetUnmanagedStructures -
  54.         // Fills in Overlapped Structures used in an Async Overlapped Winsock call
  55.         // these calls are outside the runtime and are unmanaged code, so we need
  56.         // to prepare specific structures and ints that lie in unmanaged memory
  57.         // since the Overlapped calls can be Async
  58.         //
  59.         internal void SetUnmanagedStructures(byte[] buffer, int addressBufferLength)
  60.         {
  61.            
  62.             // has to be called first to pin memory
  63.             base.SetUnmanagedStructures(buffer);
  64.            
  65.             //
  66.             // Fill in Buffer Array structure that will be used for our send/recv Buffer
  67.             //
  68.             m_AddressBufferLength = addressBufferLength;
  69.             m_Buffer = buffer;
  70.         }
  71.        
  72. /*
  73.         //                                   
  74.         internal void SetUnmanagedStructures(byte[] buffer, int addressBufferLength, ref OverlappedCache overlappedCache)
  75.         {
  76.             SetupCache(ref overlappedCache);
  77.             SetUnmanagedStructures(buffer, addressBufferLength);
  78.         }
  79.         */       
  80.        
  81.         void LogBuffer(long size)
  82.         {
  83.             GlobalLog.Assert(Logging.On, "AcceptOverlappedAsyncResult#{0}::LogBuffer()|Logging is off!", ValidationHelper.HashString(this));
  84.             IntPtr pinnedBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(m_Buffer, 0);
  85.             if (pinnedBuffer != IntPtr.Zero) {
  86.                 if (size > -1) {
  87.                     Logging.Dump(Logging.Sockets, m_ListenSocket, "PostCompletion", pinnedBuffer, (int)Math.Min(size, (long)m_Buffer.Length));
  88.                 }
  89.                 else {
  90.                     Logging.Dump(Logging.Sockets, m_ListenSocket, "PostCompletion", pinnedBuffer, (int)m_Buffer.Length);
  91.                 }
  92.             }
  93.         }
  94.        
  95.         internal byte[] Buffer {
  96.             get { return m_Buffer; }
  97.         }
  98.        
  99.         internal int BytesTransferred {
  100.             get { return m_LocalBytesTransferred; }
  101.         }
  102.        
  103.         internal Socket AcceptSocket {
  104.             set { m_AcceptSocket = value; }
  105.         }
  106.     }
  107. }

Developer Fusion