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

  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.     // ConnectOverlappedAsyncResult - used to take care of storage for async Socket BeginAccept call.
  25.     //
  26.     internal class ConnectOverlappedAsyncResult : BaseOverlappedAsyncResult
  27.     {
  28.        
  29.         internal ConnectOverlappedAsyncResult(Socket socket, object asyncState, AsyncCallback asyncCallback) : base(socket, asyncState, asyncCallback)
  30.         {
  31.         }
  32.        
  33.        
  34.        
  35.         //
  36.         // This method is called by base.CompletionPortCallback base.OverlappedCallback as part of IO completion
  37.         //
  38.         internal override object PostCompletion(int numBytes)
  39.         {
  40.             SocketError errorCode = (SocketError)ErrorCode;
  41.             Socket socket = (Socket)AsyncObject;
  42.            
  43.             if (errorCode == SocketError.Success) {
  44.                
  45.                 //set the socket context
  46.                 try {
  47.                     errorCode = UnsafeNclNativeMethods.OSSOCK.setsockopt(socket.SafeHandle, SocketOptionLevel.Socket, SocketOptionName.UpdateConnectContext, null, 0);
  48.                     if (errorCode == SocketError.SocketError)
  49.                         errorCode = (SocketError)Marshal.GetLastWin32Error();
  50.                 }
  51.                 catch (ObjectDisposedException) {
  52.                     errorCode = SocketError.OperationAborted;
  53.                 }
  54.                
  55.                 ErrorCode = (int)errorCode;
  56.             }
  57.            
  58.             if (errorCode == SocketError.Success) {
  59.                 socket.SetToConnected();
  60.                 return socket;
  61.             }
  62.             return null;
  63.         }
  64.        
  65.     }
  66.    
  67.    
  68. }

Developer Fusion