Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Data / System / Data / SqlClient / TdsParserSafeHandles.cs / 5 / TdsParserSafeHandles.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
// [....]
//-----------------------------------------------------------------------------
namespace System.Data.SqlClient {
using System;
using System.Data.Common;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using System.Runtime.ConstrainedExecution;
internal sealed class SNILoadHandle : SafeHandle {
internal static readonly SNILoadHandle SingletonInstance = new SNILoadHandle();
internal readonly SNINativeMethodWrapper.SqlAsyncCallbackDelegate ReadAsyncCallbackDispatcher = new SNINativeMethodWrapper.SqlAsyncCallbackDelegate(ReadDispatcher);
internal readonly SNINativeMethodWrapper.SqlAsyncCallbackDelegate WriteAsyncCallbackDispatcher = new SNINativeMethodWrapper.SqlAsyncCallbackDelegate(WriteDispatcher);
private readonly UInt32 _sniStatus = TdsEnums.SNI_UNINITIALIZED;
private readonly EncryptionOptions _encryptionOption;
private SNILoadHandle() : base(IntPtr.Zero, true) {
// SQL BU DT 346588 - from security review - SafeHandle guarantees this is only called once.
// The reason for the safehandle is guaranteed initialization and termination of SNI to
// ensure SNI terminates and cleans up properly.
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_sniStatus = SNINativeMethodWrapper.SNIInitialize();
UInt32 value = 0;
// Query OS to find out whether encryption is supported.
SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value);
_encryptionOption = (value == 0) ? EncryptionOptions.NOT_SUP : EncryptionOptions.OFF;
base.handle = (IntPtr) 1; // Initialize to non-zero dummy variable.
}
}
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
override protected bool ReleaseHandle() {
if (base.handle != IntPtr.Zero) {
if (TdsEnums.SNI_SUCCESS == _sniStatus) {
SNINativeMethodWrapper.SNITerminate();
}
base.handle = IntPtr.Zero;
}
return true;
}
public UInt32 SNIStatus {
get {
return _sniStatus;
}
}
public EncryptionOptions Options {
get {
return _encryptionOption;
}
}
static private void ReadDispatcher(IntPtr key, IntPtr packet, UInt32 error) {
// This is the app-domain dispatcher for all async read callbacks, It
// simply gets the state object from the key that it is passed, and
// calls the state object's read callback.
Debug.Assert(IntPtr.Zero != key, "no key passed to read callback dispatcher?");
if (IntPtr.Zero != key) {
// NOTE: we will get a null ref here if we don't get a key that
// contains a GCHandle to TDSParserStateObject; that is
// very bad, and we want that to occur so we can catch it.
GCHandle gcHandle = (GCHandle)key;
TdsParserStateObject stateObj = (TdsParserStateObject)gcHandle.Target;
if (null != stateObj) {
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
}
}
}
static private void WriteDispatcher(IntPtr key, IntPtr packet, UInt32 error) {
// This is the app-domain dispatcher for all async write callbacks,
// It simply gets the state object from the key that it is passed,
// and calls the state object's write callback.
Debug.Assert(IntPtr.Zero != key, "no key passed to write callback dispatcher?");
if (IntPtr.Zero != key) {
// NOTE: we will get a null ref here if we don't get a key that
// contains a GCHandle to TDSParserStateObject; that is
// very bad, and we want that to occur so we can catch it.
GCHandle gcHandle = (GCHandle)key;
TdsParserStateObject stateObj = (TdsParserStateObject)gcHandle.Target;
if (null != stateObj) {
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
}
}
}
}
internal sealed class SNIHandle : SafeHandle {
private readonly UInt32 _status = TdsEnums.SNI_UNINITIALIZED;
private readonly bool _fSync = false;
internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, bool integratedSecurity,
byte[] serverUserName, bool ignoreSniOpenTimeout, int timeout, out byte[] instanceName,
bool flushCache, bool fSync)
: base(IntPtr.Zero, true) {
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_fSync = fSync;
instanceName = new byte[256]; // Size as specified by netlibs.
if (ignoreSniOpenTimeout) {
//
_status = SNINativeMethodWrapper.SNIOpenEx (myInfo, serverName, ref base.handle,
integratedSecurity, serverUserName, instanceName, flushCache, fSync);
}
else {
_status = SNINativeMethodWrapper.SNIOpenSyncEx (myInfo, serverName, ref base.handle, integratedSecurity,
serverUserName, instanceName, flushCache, fSync, timeout);
}
}
}
internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, SNIHandle parent) : base(IntPtr.Zero, true) {
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_status = SNINativeMethodWrapper.SNIOpen(myInfo, serverName, parent, ref base.handle, parent._fSync);
}
}
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
override protected bool ReleaseHandle() {
// NOTE: The SafeHandle class guarantees this will be called exactly once.
IntPtr ptr = base.handle;
base.handle = IntPtr.Zero;
if (IntPtr.Zero != ptr) {
if (0 != SNINativeMethodWrapper.SNIClose(ptr)) {
return false; // SNIClose should never fail.
}
}
return true;
}
internal UInt32 Status {
get {
return _status;
}
}
}
internal sealed class SNIPacket : SafeHandle {
internal SNIPacket(SafeHandle sniHandle) : base(IntPtr.Zero, true) {
SNINativeMethodWrapper.SNIPacketAllocate(sniHandle, SNINativeMethodWrapper.IOType.WRITE, ref base.handle);
if (IntPtr.Zero == base.handle) {
throw SQL.SNIPacketAllocationFailure();
}
}
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
override protected bool ReleaseHandle() {
// NOTE: The SafeHandle class guarantees this will be called exactly once.
IntPtr ptr = base.handle;
base.handle = IntPtr.Zero;
if (IntPtr.Zero != ptr) {
SNINativeMethodWrapper.SNIPacketRelease(ptr);
}
return true;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
// [....]
//-----------------------------------------------------------------------------
namespace System.Data.SqlClient {
using System;
using System.Data.Common;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using System.Runtime.ConstrainedExecution;
internal sealed class SNILoadHandle : SafeHandle {
internal static readonly SNILoadHandle SingletonInstance = new SNILoadHandle();
internal readonly SNINativeMethodWrapper.SqlAsyncCallbackDelegate ReadAsyncCallbackDispatcher = new SNINativeMethodWrapper.SqlAsyncCallbackDelegate(ReadDispatcher);
internal readonly SNINativeMethodWrapper.SqlAsyncCallbackDelegate WriteAsyncCallbackDispatcher = new SNINativeMethodWrapper.SqlAsyncCallbackDelegate(WriteDispatcher);
private readonly UInt32 _sniStatus = TdsEnums.SNI_UNINITIALIZED;
private readonly EncryptionOptions _encryptionOption;
private SNILoadHandle() : base(IntPtr.Zero, true) {
// SQL BU DT 346588 - from security review - SafeHandle guarantees this is only called once.
// The reason for the safehandle is guaranteed initialization and termination of SNI to
// ensure SNI terminates and cleans up properly.
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_sniStatus = SNINativeMethodWrapper.SNIInitialize();
UInt32 value = 0;
// Query OS to find out whether encryption is supported.
SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value);
_encryptionOption = (value == 0) ? EncryptionOptions.NOT_SUP : EncryptionOptions.OFF;
base.handle = (IntPtr) 1; // Initialize to non-zero dummy variable.
}
}
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
override protected bool ReleaseHandle() {
if (base.handle != IntPtr.Zero) {
if (TdsEnums.SNI_SUCCESS == _sniStatus) {
SNINativeMethodWrapper.SNITerminate();
}
base.handle = IntPtr.Zero;
}
return true;
}
public UInt32 SNIStatus {
get {
return _sniStatus;
}
}
public EncryptionOptions Options {
get {
return _encryptionOption;
}
}
static private void ReadDispatcher(IntPtr key, IntPtr packet, UInt32 error) {
// This is the app-domain dispatcher for all async read callbacks, It
// simply gets the state object from the key that it is passed, and
// calls the state object's read callback.
Debug.Assert(IntPtr.Zero != key, "no key passed to read callback dispatcher?");
if (IntPtr.Zero != key) {
// NOTE: we will get a null ref here if we don't get a key that
// contains a GCHandle to TDSParserStateObject; that is
// very bad, and we want that to occur so we can catch it.
GCHandle gcHandle = (GCHandle)key;
TdsParserStateObject stateObj = (TdsParserStateObject)gcHandle.Target;
if (null != stateObj) {
stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error);
}
}
}
static private void WriteDispatcher(IntPtr key, IntPtr packet, UInt32 error) {
// This is the app-domain dispatcher for all async write callbacks,
// It simply gets the state object from the key that it is passed,
// and calls the state object's write callback.
Debug.Assert(IntPtr.Zero != key, "no key passed to write callback dispatcher?");
if (IntPtr.Zero != key) {
// NOTE: we will get a null ref here if we don't get a key that
// contains a GCHandle to TDSParserStateObject; that is
// very bad, and we want that to occur so we can catch it.
GCHandle gcHandle = (GCHandle)key;
TdsParserStateObject stateObj = (TdsParserStateObject)gcHandle.Target;
if (null != stateObj) {
stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error);
}
}
}
}
internal sealed class SNIHandle : SafeHandle {
private readonly UInt32 _status = TdsEnums.SNI_UNINITIALIZED;
private readonly bool _fSync = false;
internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, bool integratedSecurity,
byte[] serverUserName, bool ignoreSniOpenTimeout, int timeout, out byte[] instanceName,
bool flushCache, bool fSync)
: base(IntPtr.Zero, true) {
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_fSync = fSync;
instanceName = new byte[256]; // Size as specified by netlibs.
if (ignoreSniOpenTimeout) {
//
_status = SNINativeMethodWrapper.SNIOpenEx (myInfo, serverName, ref base.handle,
integratedSecurity, serverUserName, instanceName, flushCache, fSync);
}
else {
_status = SNINativeMethodWrapper.SNIOpenSyncEx (myInfo, serverName, ref base.handle, integratedSecurity,
serverUserName, instanceName, flushCache, fSync, timeout);
}
}
}
internal SNIHandle(SNINativeMethodWrapper.ConsumerInfo myInfo, string serverName, SNIHandle parent) : base(IntPtr.Zero, true) {
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_status = SNINativeMethodWrapper.SNIOpen(myInfo, serverName, parent, ref base.handle, parent._fSync);
}
}
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
override protected bool ReleaseHandle() {
// NOTE: The SafeHandle class guarantees this will be called exactly once.
IntPtr ptr = base.handle;
base.handle = IntPtr.Zero;
if (IntPtr.Zero != ptr) {
if (0 != SNINativeMethodWrapper.SNIClose(ptr)) {
return false; // SNIClose should never fail.
}
}
return true;
}
internal UInt32 Status {
get {
return _status;
}
}
}
internal sealed class SNIPacket : SafeHandle {
internal SNIPacket(SafeHandle sniHandle) : base(IntPtr.Zero, true) {
SNINativeMethodWrapper.SNIPacketAllocate(sniHandle, SNINativeMethodWrapper.IOType.WRITE, ref base.handle);
if (IntPtr.Zero == base.handle) {
throw SQL.SNIPacketAllocationFailure();
}
}
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
override protected bool ReleaseHandle() {
// NOTE: The SafeHandle class guarantees this will be called exactly once.
IntPtr ptr = base.handle;
base.handle = IntPtr.Zero;
if (IntPtr.Zero != ptr) {
SNINativeMethodWrapper.SNIPacketRelease(ptr);
}
return true;
}
}
}
// 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
- DataTableCollection.cs
- FrameworkRichTextComposition.cs
- MasterPageBuildProvider.cs
- FieldMetadata.cs
- ObjectSet.cs
- FilteredSchemaElementLookUpTable.cs
- EndpointAddressMessageFilterTable.cs
- AccessKeyManager.cs
- RemoteWebConfigurationHostStream.cs
- WsiProfilesElement.cs
- _OSSOCK.cs
- httpapplicationstate.cs
- OperationContractAttribute.cs
- WebServiceParameterData.cs
- SymLanguageVendor.cs
- HostUtils.cs
- XamlPathDataSerializer.cs
- Misc.cs
- InfoCardRSAPKCS1KeyExchangeFormatter.cs
- EncoderBestFitFallback.cs
- XPathArrayIterator.cs
- mactripleDES.cs
- DataGridCaption.cs
- Model3D.cs
- _HelperAsyncResults.cs
- TextWriterTraceListener.cs
- FormsAuthenticationConfiguration.cs
- MonthChangedEventArgs.cs
- UnaryNode.cs
- TimeSpanStorage.cs
- DataGridViewTopLeftHeaderCell.cs
- wpf-etw.cs
- SingleAnimation.cs
- DispatcherExceptionFilterEventArgs.cs
- Function.cs
- DataSourceCacheDurationConverter.cs
- FolderBrowserDialog.cs
- PageBuildProvider.cs
- ApplicationServicesHostFactory.cs
- NotEqual.cs
- Attributes.cs
- ProtocolsConfigurationEntry.cs
- DataComponentMethodGenerator.cs
- DecryptedHeader.cs
- ProviderConnectionPoint.cs
- EqualityComparer.cs
- _HTTPDateParse.cs
- DictionaryCustomTypeDescriptor.cs
- TextTreeTextNode.cs
- OletxCommittableTransaction.cs
- ToolStripDropDownItemDesigner.cs
- ObjectListField.cs
- SqlRowUpdatingEvent.cs
- KnownAssembliesSet.cs
- HostSecurityManager.cs
- unsafenativemethodsother.cs
- SerializationFieldInfo.cs
- DataSourceUtil.cs
- RoutedEventHandlerInfo.cs
- WrapPanel.cs
- AuthenticationServiceManager.cs
- SimpleHandlerFactory.cs
- RewritingSimplifier.cs
- ProcessHostFactoryHelper.cs
- SimplePropertyEntry.cs
- LoadRetryConstantStrategy.cs
- ContactManager.cs
- Baml2006KnownTypes.cs
- ContentType.cs
- VarRefManager.cs
- TdsParserSafeHandles.cs
- DynamicValueConverter.cs
- QilFunction.cs
- PropertyEntry.cs
- TreeIterator.cs
- ColumnMapCopier.cs
- QilLiteral.cs
- InvalidAsynchronousStateException.cs
- FixedDocumentPaginator.cs
- ADMembershipProvider.cs
- PagerSettings.cs
- Transform3D.cs
- XmlQueryTypeFactory.cs
- Crc32Helper.cs
- LingerOption.cs
- ServiceChannel.cs
- SmtpFailedRecipientException.cs
- WpfXamlMember.cs
- IndentedTextWriter.cs
- RequestResizeEvent.cs
- BoolExpr.cs
- StoreItemCollection.cs
- streamingZipPartStream.cs
- TraceLevelStore.cs
- Operand.cs
- XmlSchemaComplexContent.cs
- ParsedAttributeCollection.cs
- FixedSOMPageElement.cs
- LogEntrySerializer.cs
- StylusCaptureWithinProperty.cs