internal void EnableMars()
{
if (_fMARS)
{
// Cache physical stateObj and connection.
_pMarsPhysicalConObj = _physicalStateObj;
uint error = 0;
#if MANAGED_SNI
_pMarsPhysicalConObj.IncrementPendingCallbacks();
error = SNIProxy.Singleton.EnableMars(_pMarsPhysicalConObj.Handle);
#else
// Add SMUX (MARS) SNI provider.
UInt32 info = 0;
error = SNINativeMethodWrapper.SNIAddProvider(_pMarsPhysicalConObj.Handle, SNINativeMethodWrapper.ProviderEnum.SMUX_PROV, ref info);
#endif // MANAGED_SNI
if (error != TdsEnums.SNI_SUCCESS)
{
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
#if !MANAGED_SNI
// HACK HACK HACK - for Async only
// Have to post read to initialize MARS - will get callback on this when connection goes
// down or is closed.
IntPtr temp = IntPtr.Zero;
try { }
finally
{
_pMarsPhysicalConObj.IncrementPendingCallbacks();
error = SNINativeMethodWrapper.SNIReadAsync(_pMarsPhysicalConObj.Handle, ref temp);
if (temp != IntPtr.Zero)
{
// Be sure to release packet, otherwise it will be leaked by native.
SNINativeMethodWrapper.SNIPacketRelease(temp);
}
}
Debug.Assert(IntPtr.Zero == temp, "unexpected syncReadPacket without corresponding SNIPacketRelease");
if (TdsEnums.SNI_SUCCESS_IO_PENDING != error)
{
Debug.Assert(TdsEnums.SNI_SUCCESS != error, "Unexpected successful read async on physical connection before enabling MARS!");
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
#endif // MANAGED_SNI
_physicalStateObj = CreateSession(); // Create and open default MARS stateObj and connection.
}
}