Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / Net / System / Net / Sockets / _OverlappedAsyncResult.cs / 1 / _OverlappedAsyncResult.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net.Sockets { using System; using System.Net; using System.Runtime.InteropServices; using System.Threading; using Microsoft.Win32; using System.Collections.Generic; // // OverlappedAsyncResult - used to take care of storage for async Socket operation // from the BeginSend, BeginSendTo, BeginReceive, BeginReceiveFrom calls. // internal class OverlappedAsyncResult: BaseOverlappedAsyncResult { // // internal class members // private SocketAddress m_SocketAddress; private SocketAddress m_SocketAddressOriginal; // needed for partial BeginReceiveFrom/EndReceiveFrom completion // These two are used only as alternatives internal WSABuffer m_SingleBuffer; internal WSABuffer[] m_WSABuffers; // // the following two will be used only on WinNT to enable completion ports // // // Constructor. We take in the socket that's creating us, the caller's // state object, and the buffer on which the I/O will be performed. // We save the socket and state, pin the callers's buffer, and allocate // an event for the WaitHandle. // internal OverlappedAsyncResult(Socket socket, Object asyncState, AsyncCallback asyncCallback) : base(socket, asyncState, asyncCallback) { } // internal IntPtr GetSocketAddressPtr() { return Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer, 0); } // internal IntPtr GetSocketAddressSizePtr() { return Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer, m_SocketAddress.GetAddressSizeOffset()); } // internal SocketAddress SocketAddress { get { return m_SocketAddress; } } // internal SocketAddress SocketAddressOriginal { get { return m_SocketAddressOriginal; } set { m_SocketAddressOriginal = value; } } // // SetUnmanagedStructures - // Fills in Overlapped Structures used in an Async Overlapped Winsock call // these calls are outside the runtime and are unmanaged code, so we need // to prepare specific structures and ints that lie in unmanaged memory // since the Overlapped calls can be Async // internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, bool pinSocketAddress) { // // Fill in Buffer Array structure that will be used for our send/recv Buffer // m_SocketAddress = socketAddress; if (pinSocketAddress && m_SocketAddress != null) { object[] objectsToPin = null; objectsToPin = new object[2]; objectsToPin[0] = buffer; m_SocketAddress.CopyAddressSizeIntoBuffer(); objectsToPin[1] = m_SocketAddress.m_Buffer; base.SetUnmanagedStructures(objectsToPin); } else { base.SetUnmanagedStructures(buffer); } m_SingleBuffer.Length = size; m_SingleBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset); } internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, bool pinSocketAddress, ref OverlappedCache overlappedCache) { SetupCache(ref overlappedCache); SetUnmanagedStructures(buffer, offset, size, socketAddress, pinSocketAddress); } internal void SetUnmanagedStructures(BufferOffsetSize[] buffers) { // // Fill in Buffer Array structure that will be used for our send/recv Buffer // m_WSABuffers = new WSABuffer[buffers.Length]; object[] objectsToPin = new object[buffers.Length]; for (int i = 0; i < buffers.Length; i++) objectsToPin[i] = buffers[i].Buffer; // has to be called now to pin memory base.SetUnmanagedStructures(objectsToPin); for (int i = 0; i < buffers.Length; i++) { m_WSABuffers[i].Length = buffers[i].Size; m_WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffers[i].Buffer, buffers[i].Offset); } } internal void SetUnmanagedStructures(BufferOffsetSize[] buffers, ref OverlappedCache overlappedCache) { SetupCache(ref overlappedCache); SetUnmanagedStructures(buffers); } internal void SetUnmanagedStructures(IList> buffers) { // Fill in Buffer Array structure that will be used for our send/recv Buffer // //make sure we don't let the app mess up the buffer array enough to cause //corruption. int count = buffers.Count; ArraySegment [] buffersCopy = new ArraySegment [count]; for (int i=0;i > buffers, ref OverlappedCache overlappedCache) { SetupCache(ref overlappedCache); SetUnmanagedStructures(buffers); } // // This method is called after an asynchronous call is made for the user, // it checks and acts accordingly if the IO: // 1) completed synchronously. // 2) was pended. // 3) failed. // internal override object PostCompletion(int numBytes) { if (ErrorCode == 0) { if(Logging.On)LogBuffer(numBytes); } return (int)numBytes; } void LogBuffer(int size) { GlobalLog.Assert(Logging.On, "OverlappedAsyncResult#{0}::LogBuffer()|Logging is off!", ValidationHelper.HashString(this)); if (size > -1) { if (m_WSABuffers != null) { foreach (WSABuffer wsaBuffer in m_WSABuffers) { Logging.Dump(Logging.Sockets, AsyncObject, "PostCompletion", wsaBuffer.Pointer, Math.Min(wsaBuffer.Length, size)); if ((size -= wsaBuffer.Length) <=0) break; } } else { Logging.Dump(Logging.Sockets, AsyncObject, "PostCompletion", m_SingleBuffer.Pointer, Math.Min(m_SingleBuffer.Length, size)); } } } }; // class OverlappedAsyncResult } // namespace System.Net.Sockets // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net.Sockets { using System; using System.Net; using System.Runtime.InteropServices; using System.Threading; using Microsoft.Win32; using System.Collections.Generic; // // OverlappedAsyncResult - used to take care of storage for async Socket operation // from the BeginSend, BeginSendTo, BeginReceive, BeginReceiveFrom calls. // internal class OverlappedAsyncResult: BaseOverlappedAsyncResult { // // internal class members // private SocketAddress m_SocketAddress; private SocketAddress m_SocketAddressOriginal; // needed for partial BeginReceiveFrom/EndReceiveFrom completion // These two are used only as alternatives internal WSABuffer m_SingleBuffer; internal WSABuffer[] m_WSABuffers; // // the following two will be used only on WinNT to enable completion ports // // // Constructor. We take in the socket that's creating us, the caller's // state object, and the buffer on which the I/O will be performed. // We save the socket and state, pin the callers's buffer, and allocate // an event for the WaitHandle. // internal OverlappedAsyncResult(Socket socket, Object asyncState, AsyncCallback asyncCallback) : base(socket, asyncState, asyncCallback) { } // internal IntPtr GetSocketAddressPtr() { return Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer, 0); } // internal IntPtr GetSocketAddressSizePtr() { return Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer, m_SocketAddress.GetAddressSizeOffset()); } // internal SocketAddress SocketAddress { get { return m_SocketAddress; } } // internal SocketAddress SocketAddressOriginal { get { return m_SocketAddressOriginal; } set { m_SocketAddressOriginal = value; } } // // SetUnmanagedStructures - // Fills in Overlapped Structures used in an Async Overlapped Winsock call // these calls are outside the runtime and are unmanaged code, so we need // to prepare specific structures and ints that lie in unmanaged memory // since the Overlapped calls can be Async // internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, bool pinSocketAddress) { // // Fill in Buffer Array structure that will be used for our send/recv Buffer // m_SocketAddress = socketAddress; if (pinSocketAddress && m_SocketAddress != null) { object[] objectsToPin = null; objectsToPin = new object[2]; objectsToPin[0] = buffer; m_SocketAddress.CopyAddressSizeIntoBuffer(); objectsToPin[1] = m_SocketAddress.m_Buffer; base.SetUnmanagedStructures(objectsToPin); } else { base.SetUnmanagedStructures(buffer); } m_SingleBuffer.Length = size; m_SingleBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset); } internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, bool pinSocketAddress, ref OverlappedCache overlappedCache) { SetupCache(ref overlappedCache); SetUnmanagedStructures(buffer, offset, size, socketAddress, pinSocketAddress); } internal void SetUnmanagedStructures(BufferOffsetSize[] buffers) { // // Fill in Buffer Array structure that will be used for our send/recv Buffer // m_WSABuffers = new WSABuffer[buffers.Length]; object[] objectsToPin = new object[buffers.Length]; for (int i = 0; i < buffers.Length; i++) objectsToPin[i] = buffers[i].Buffer; // has to be called now to pin memory base.SetUnmanagedStructures(objectsToPin); for (int i = 0; i < buffers.Length; i++) { m_WSABuffers[i].Length = buffers[i].Size; m_WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffers[i].Buffer, buffers[i].Offset); } } internal void SetUnmanagedStructures(BufferOffsetSize[] buffers, ref OverlappedCache overlappedCache) { SetupCache(ref overlappedCache); SetUnmanagedStructures(buffers); } internal void SetUnmanagedStructures(IList> buffers) { // Fill in Buffer Array structure that will be used for our send/recv Buffer // //make sure we don't let the app mess up the buffer array enough to cause //corruption. int count = buffers.Count; ArraySegment [] buffersCopy = new ArraySegment [count]; for (int i=0;i > buffers, ref OverlappedCache overlappedCache) { SetupCache(ref overlappedCache); SetUnmanagedStructures(buffers); } // // This method is called after an asynchronous call is made for the user, // it checks and acts accordingly if the IO: // 1) completed synchronously. // 2) was pended. // 3) failed. // internal override object PostCompletion(int numBytes) { if (ErrorCode == 0) { if(Logging.On)LogBuffer(numBytes); } return (int)numBytes; } void LogBuffer(int size) { GlobalLog.Assert(Logging.On, "OverlappedAsyncResult#{0}::LogBuffer()|Logging is off!", ValidationHelper.HashString(this)); if (size > -1) { if (m_WSABuffers != null) { foreach (WSABuffer wsaBuffer in m_WSABuffers) { Logging.Dump(Logging.Sockets, AsyncObject, "PostCompletion", wsaBuffer.Pointer, Math.Min(wsaBuffer.Length, size)); if ((size -= wsaBuffer.Length) <=0) break; } } else { Logging.Dump(Logging.Sockets, AsyncObject, "PostCompletion", m_SingleBuffer.Pointer, Math.Min(m_SingleBuffer.Length, size)); } } } }; // class OverlappedAsyncResult } // namespace System.Net.Sockets // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ElapsedEventArgs.cs
- PagesChangedEventArgs.cs
- KoreanCalendar.cs
- SqlTrackingWorkflowInstance.cs
- SizeValueSerializer.cs
- ObjectStateFormatter.cs
- COM2ExtendedBrowsingHandler.cs
- HwndAppCommandInputProvider.cs
- ClientProxyGenerator.cs
- GeneralTransform.cs
- ApplicationFileCodeDomTreeGenerator.cs
- PrintDialogException.cs
- HtmlTableRow.cs
- PathGradientBrush.cs
- compensatingcollection.cs
- LinqDataSourceInsertEventArgs.cs
- ViewPort3D.cs
- ToolStripItemEventArgs.cs
- DockPanel.cs
- UserPersonalizationStateInfo.cs
- CfgParser.cs
- DataBindingHandlerAttribute.cs
- SqlReferenceCollection.cs
- CodeSnippetTypeMember.cs
- DrawItemEvent.cs
- ApplyTemplatesAction.cs
- FlowDocumentView.cs
- ResourceBinder.cs
- HttpConfigurationSystem.cs
- ScriptingAuthenticationServiceSection.cs
- CodeCommentStatementCollection.cs
- PermissionRequestEvidence.cs
- FontInfo.cs
- TypeDescriptionProvider.cs
- ProfileProvider.cs
- IPPacketInformation.cs
- ProcessModule.cs
- TextRangeProviderWrapper.cs
- ActiveXContainer.cs
- Tablet.cs
- DesignerSelectionListAdapter.cs
- TextServicesPropertyRanges.cs
- PartialCachingControl.cs
- Viewport3DAutomationPeer.cs
- ParameterExpression.cs
- FontConverter.cs
- XmlSchemaObjectCollection.cs
- SpotLight.cs
- AttachedPropertyBrowsableWhenAttributePresentAttribute.cs
- CapabilitiesSection.cs
- SafeNativeMethods.cs
- XmlSchemaCollection.cs
- FormViewUpdatedEventArgs.cs
- CustomValidator.cs
- HeaderCollection.cs
- ResourceType.cs
- ButtonBase.cs
- BindingExpressionUncommonField.cs
- ProgressBarRenderer.cs
- Size3DConverter.cs
- PermissionSetTriple.cs
- StylusCaptureWithinProperty.cs
- MemberDescriptor.cs
- SafeTimerHandle.cs
- UInt32Storage.cs
- PreviewKeyDownEventArgs.cs
- BamlRecordReader.cs
- TypeUtils.cs
- DeferredSelectedIndexReference.cs
- RandomNumberGenerator.cs
- SchemaLookupTable.cs
- ProcessModelInfo.cs
- ButtonBaseDesigner.cs
- CngKey.cs
- DataControlLinkButton.cs
- BufferCache.cs
- WebPartEditVerb.cs
- MarkupExtensionSerializer.cs
- SoapSchemaImporter.cs
- CodeTypeDeclaration.cs
- StartUpEventArgs.cs
- DiagnosticTrace.cs
- CacheMode.cs
- ComPlusServiceHost.cs
- TransactionOptions.cs
- AuthenticationModuleElementCollection.cs
- ManagementEventWatcher.cs
- TextEditorThreadLocalStore.cs
- WebPartTransformerCollection.cs
- HtmlHead.cs
- FileLevelControlBuilderAttribute.cs
- WindowsImpersonationContext.cs
- ByValueEqualityComparer.cs
- FormViewPageEventArgs.cs
- VisualStyleInformation.cs
- VerticalAlignConverter.cs
- XmlSignificantWhitespace.cs
- DataViewManager.cs
- MetaColumn.cs
- WindowsFont.cs