/// <summary> /// Enable MARS support on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint EnableMars(SNIHandle handle) { if (SNIMarsManager.Singleton.CreateMarsConnection(handle) == TdsEnums.SNI_SUCCESS_IO_PENDING) { return TdsEnums.SNI_SUCCESS; } return TdsEnums.SNI_ERROR; }
/// <summary> /// Enable SSL on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint EnableSsl(SNIHandle handle, uint options) { try { return handle.EnableSsl(options); } catch (Exception e) { return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, SNICommon.HandshakeFailureError, e); } }
/// <summary> /// Enable SSL on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint EnableSsl(SNIHandle handle, uint options) { try { return handle.EnableSsl(options); } catch { return SNICommon.ReportSNIError(SNIProviders.SSL_PROV, 0, 31, SR.SNI_ERROR_31); } }
/// <summary> /// Enable SSL on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint EnableSsl(SNIHandle handle, uint options) { try { return handle.EnableSsl(options); } catch (Exception e) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, 0, string.Format("Encryption(ssl/tls) handshake failed: {0}", e.ToString())); return TdsEnums.SNI_ERROR; } }
/// <summary> /// Enable SSL on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint EnableSsl(SNIHandle handle, uint options) { try { return handle.EnableSsl(options); } catch { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SSL_PROV, 0, 31, SR.SNI_ERROR_31); return TdsEnums.SNI_ERROR; } }
/// <summary> /// Create a MARS connection /// </summary> /// <param name="lowerHandle">Lower SNI handle</param> /// <returns>SNI error code</returns> public uint CreateMarsConnection(SNIHandle lowerHandle) { SNIMarsConnection connection = new SNIMarsConnection(lowerHandle); if (_connections.TryAdd(lowerHandle, connection)) { return connection.StartReceive(); } else { return TdsEnums.SNI_ERROR; } }
/// <summary> /// Generate SSPI context /// </summary> /// <param name="handle">SNI connection handle</param> /// <param name="receivedBuff">Receive buffer</param> /// <param name="receivedLength">Received length</param> /// <param name="sendBuff">Send buffer</param> /// <param name="sendLength">Send length</param> /// <param name="serverName">Service Principal Name buffer</param> /// <param name="serverNameLength">Length of Service Principal Name</param> /// <returns>SNI error code</returns> public uint GenSspiClientContext(SNIHandle handle, byte[] receivedBuff, uint receivedLength, byte[] sendBuff, ref uint sendLength, byte[] serverName, uint serverNameLength) { throw new PlatformNotSupportedException(); }
/// <summary> /// Constructor /// </summary> /// <param name="handle">Owning SNI handle</param> public SNIPacket(SNIHandle handle) { _offset = 0; }
/// <summary> /// Read packet asynchronously /// </summary> /// <param name="handle">SNI handle</param> /// <param name="packet">Packet</param> /// <returns>SNI error status</returns> public uint ReadAsync(SNIHandle handle, ref SNIPacket packet) { packet = new SNIPacket(null); return handle.ReceiveAsync(ref packet); }
/// <summary> /// Create MARS handle /// </summary> /// <param name="callbackObject">Asynchronous I/O callback object</param> /// <param name="physicalConnection">SNI connection handle</param> /// <param name="defaultBufferSize">Default buffer size</param> /// <param name="async">Asynchronous connection</param> /// <returns>SNI error status</returns> public SNIHandle CreateMarsHandle(object callbackObject, SNIHandle physicalConnection, int defaultBufferSize, bool async) { SNIMarsConnection connection = SNIMarsManager.Singleton.GetConnection(physicalConnection); return connection.CreateSession(callbackObject, async); }
/// <summary> /// Send a packet /// </summary> /// <param name="handle">SNI handle</param> /// <param name="packet">SNI packet</param> /// <param name="sync">true if synchronous, false if asynchronous</param> /// <returns>SNI error status</returns> public uint WritePacket(SNIHandle handle, SNIPacket packet, bool sync) { if (sync) { return handle.Send(packet.Clone()); } else { return handle.SendAsync(packet.Clone()); } }
/// <summary> /// Read synchronously /// </summary> /// <param name="handle">SNI handle</param> /// <param name="packet">SNI packet</param> /// <param name="timeout">Timeout</param> /// <returns>SNI error status</returns> public uint ReadSyncOverAsync(SNIHandle handle, out SNIPacket packet, int timeout) { return handle.Receive(out packet, timeout); }
/// <summary> /// Get SNI connection ID /// </summary> /// <param name="handle">SNI handle</param> /// <param name="clientConnectionId">Client connection ID</param> /// <returns>SNI error status</returns> public uint GetConnectionId(SNIHandle handle, ref Guid clientConnectionId) { clientConnectionId = handle.ConnectionId; return(TdsEnums.SNI_SUCCESS); }
/// <summary> /// Read synchronously /// </summary> /// <param name="handle">SNI handle</param> /// <param name="packet">SNI packet</param> /// <param name="timeout">Timeout</param> /// <returns>SNI error status</returns> public uint ReadSyncOverAsync(SNIHandle handle, out SNIPacket packet, int timeout) { return(handle.Receive(out packet, timeout)); }
/// <summary> /// Set connection buffer size /// </summary> /// <param name="handle">SNI handle</param> /// <param name="bufferSize">Buffer size</param> /// <returns>SNI error code</returns> public uint SetConnectionBufferSize(SNIHandle handle, uint bufferSize) { handle.SetBufferSize((int)bufferSize); return(TdsEnums.SNI_SUCCESS); }
/// <summary> /// Process a receive completion /// </summary> /// <param name="packet">SNI packet</param> /// <param name="sniErrorCode">SNI error code</param> public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { SNISMUXHeader currentHeader = null; SNIPacket currentPacket = null; SNIMarsHandle currentSession = null; if (sniErrorCode != TdsEnums.SNI_SUCCESS) { lock (this) { HandleReceiveError(); return; } } while (true) { lock (this) { if (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH) { currentHeader = null; currentPacket = null; currentSession = null; while (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH) { int bytesTaken = packet.TakeData(_headerBytes, _currentHeaderByteCount, SNISMUXHeader.HEADER_LENGTH - _currentHeaderByteCount); _currentHeaderByteCount += bytesTaken; if (bytesTaken == 0) { sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(); return; } } _currentHeader = new SNISMUXHeader() { SMID = _headerBytes[0], flags = _headerBytes[1], sessionId = BitConverter.ToUInt16(_headerBytes, 2), length = BitConverter.ToUInt32(_headerBytes, 4) - SNISMUXHeader.HEADER_LENGTH, sequenceNumber = BitConverter.ToUInt32(_headerBytes, 8), highwater = BitConverter.ToUInt32(_headerBytes, 12) }; _dataBytesLeft = (int)_currentHeader.length; _currentPacket = new SNIPacket(null); _currentPacket.Allocate((int)_currentHeader.length); if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_FIN) { _sessions.Remove(_currentHeader.sessionId); } } currentHeader = _currentHeader; currentPacket = _currentPacket; if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { if (_dataBytesLeft > 0) { int length = packet.TakeData(_currentPacket, _dataBytesLeft); _dataBytesLeft -= length; if (_dataBytesLeft > 0) { sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(); return; } } } _currentHeaderByteCount = 0; if (!_sessions.ContainsKey(_currentHeader.sessionId)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, 0, "Packet for unknown MARS session received"); HandleReceiveError(); _lowerHandle.Dispose(); _lowerHandle = null; return; } currentSession = _sessions[_currentHeader.sessionId]; } if (currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { currentSession.HandleReceiveComplete(currentPacket, currentHeader); } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_ACK) { try { currentSession.HandleAck(currentHeader.highwater); } catch (Exception e) { SNICommon.ReportSNIError(SNIProviders.TCP_PROV, 0, 0, e.Message); } } lock (this) { if (packet.DataLeft == 0) { sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(); return; } } } }
/// <summary> /// Generate SSPI context /// </summary> /// <param name="handle">SNI connection handle</param> /// <param name="receivedBuff">Receive buffer</param> /// <param name="receivedLength">Received length</param> /// <param name="sendBuff">Send buffer</param> /// <param name="sendLength">Send length</param> /// <param name="serverName">Service Principal Name buffer</param> /// <param name="serverNameLength">Length of Service Principal Name</param> /// <returns>SNI error code</returns> public uint GenSspiClientContext(SNIHandle handle, byte[] receivedBuff, uint receivedLength, byte[] sendBuff, ref uint sendLength, byte[] serverName, uint serverNameLength) { throw new PlatformNotSupportedException(); }
/// <summary> /// Read packet asynchronously /// </summary> /// <param name="handle">SNI handle</param> /// <param name="packet">Packet</param> /// <returns>SNI error status</returns> public uint ReadAsync(SNIHandle handle, ref SNIPacket packet) { packet = new SNIPacket(null); return(handle.ReceiveAsync(ref packet)); }
/// <summary> /// Create MARS handle /// </summary> /// <param name="callbackObject">Asynchronous I/O callback object</param> /// <param name="physicalConnection">SNI connection handle</param> /// <param name="defaultBufferSize">Default buffer size</param> /// <param name="async">Asynchronous connection</param> /// <returns>SNI error status</returns> public SNIHandle CreateMarsHandle(object callbackObject, SNIHandle physicalConnection, int defaultBufferSize, bool async) { SNIMarsConnection connection = SNIMarsManager.Singleton.GetConnection(physicalConnection); return(connection.CreateSession(callbackObject, async)); }
/// <summary> /// Read packet asynchronously /// </summary> /// <param name="handle">SNI handle</param> /// <param name="packet">Packet</param> /// <returns>SNI error status</returns> public uint ReadAsync(SNIHandle handle, out SNIPacket packet) { packet = null; return(handle.ReceiveAsync(ref packet)); }
/// <summary> /// Check SNI handle connection /// </summary> /// <param name="handle"></param> /// <returns>SNI error status</returns> public uint CheckConnection(SNIHandle handle) { return(handle.CheckConnection()); }
/// <summary> /// Get a MARS connection by lower handle /// </summary> /// <param name="lowerHandle">Lower SNI handle</param> /// <returns>MARS connection</returns> public SNIMarsConnection GetConnection(SNIHandle lowerHandle) { return _connections[lowerHandle]; }
/// <summary> /// Disable SSL on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint DisableSsl(SNIHandle handle) { handle.DisableSsl(); return(TdsEnums.SNI_SUCCESS); }
/// <summary> /// Get SNI connection ID /// </summary> /// <param name="handle">SNI handle</param> /// <param name="clientConnectionId">Client connection ID</param> /// <returns>SNI error status</returns> public uint GetConnectionId(SNIHandle handle, ref Guid clientConnectionId) { clientConnectionId = handle.ConnectionId; return TdsEnums.SNI_SUCCESS; }
/// <summary> /// Process a receive completion /// </summary> /// <param name="packet">SNI packet</param> /// <param name="sniErrorCode">SNI error code</param> public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { SNISMUXHeader currentHeader = null; SNIPacket currentPacket = null; SNIMarsHandle currentSession = null; if (sniErrorCode != TdsEnums.SNI_SUCCESS) { lock (this) { HandleReceiveError(packet); return; } } while (true) { lock (this) { if (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH) { currentHeader = null; currentPacket = null; currentSession = null; while (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH) { int bytesTaken = packet.TakeData(_headerBytes, _currentHeaderByteCount, SNISMUXHeader.HEADER_LENGTH - _currentHeaderByteCount); _currentHeaderByteCount += bytesTaken; if (bytesTaken == 0) { packet.Dispose(); packet = null; sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(packet); return; } } _currentHeader.Read(_headerBytes); _dataBytesLeft = (int)_currentHeader.length; _currentPacket = new SNIPacket((int)_currentHeader.length); } currentHeader = _currentHeader; currentPacket = _currentPacket; if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { if (_dataBytesLeft > 0) { int length = packet.TakeData(_currentPacket, _dataBytesLeft); _dataBytesLeft -= length; if (_dataBytesLeft > 0) { packet.Dispose(); packet = null; sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(packet); return; } } } _currentHeaderByteCount = 0; if (!_sessions.ContainsKey(_currentHeader.sessionId)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, string.Empty); HandleReceiveError(packet); _lowerHandle.Dispose(); _lowerHandle = null; return; } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_FIN) { _sessions.Remove(_currentHeader.sessionId); } else { currentSession = _sessions[_currentHeader.sessionId]; } } if (currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { currentSession.HandleReceiveComplete(currentPacket, currentHeader); } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_ACK) { try { currentSession.HandleAck(currentHeader.highwater); } catch (Exception e) { SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } } lock (this) { if (packet.DataLeft == 0) { packet.Dispose(); packet = null; sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(packet); return; } } } }
/// <summary> /// Reset a packet /// </summary> /// <param name="handle">SNI handle</param> /// <param name="write">true if packet is for write</param> /// <param name="packet">SNI packet</param> public void PacketReset(SNIHandle handle, bool write, SNIPacket packet) { packet.Reset(); }
/// <summary> /// Constructor /// </summary> /// <param name="lowerHandle">Lower handle</param> public SNIMarsConnection(SNIHandle lowerHandle) { _lowerHandle = lowerHandle; _lowerHandle.SetAsyncCallbacks(HandleReceiveComplete, HandleSendComplete); }
/// <summary> /// Get a MARS connection by lower handle /// </summary> /// <param name="lowerHandle">Lower SNI handle</param> /// <returns>MARS connection</returns> public SNIMarsConnection GetConnection(SNIHandle lowerHandle) { return(_connections[lowerHandle]); }
/// <summary> /// Process a receive completion /// </summary> /// <param name="packet">SNI packet</param> /// <param name="sniErrorCode">SNI error code</param> public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode) { SNISMUXHeader currentHeader = null; SNIPacket currentPacket = null; SNIMarsHandle currentSession = null; if (sniErrorCode != TdsEnums.SNI_SUCCESS) { lock (this) { HandleReceiveError(); return; } } while (true) { lock (this) { if (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH) { currentHeader = null; currentPacket = null; currentSession = null; while (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH) { int bytesTaken = packet.TakeData(_headerBytes, _currentHeaderByteCount, SNISMUXHeader.HEADER_LENGTH - _currentHeaderByteCount); _currentHeaderByteCount += bytesTaken; if (bytesTaken == 0) { sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(); return; } } _currentHeader = new SNISMUXHeader() { SMID = _headerBytes[0], flags = _headerBytes[1], sessionId = BitConverter.ToUInt16(_headerBytes, 2), length = BitConverter.ToUInt32(_headerBytes, 4) - SNISMUXHeader.HEADER_LENGTH, sequenceNumber = BitConverter.ToUInt32(_headerBytes, 8), highwater = BitConverter.ToUInt32(_headerBytes, 12) }; _dataBytesLeft = (int)_currentHeader.length; _currentPacket = new SNIPacket(null); _currentPacket.Allocate((int)_currentHeader.length); } currentHeader = _currentHeader; currentPacket = _currentPacket; if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { if (_dataBytesLeft > 0) { int length = packet.TakeData(_currentPacket, _dataBytesLeft); _dataBytesLeft -= length; if (_dataBytesLeft > 0) { sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(); return; } } } _currentHeaderByteCount = 0; if (!_sessions.ContainsKey(_currentHeader.sessionId)) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.SMUX_PROV, 0, SNICommon.InvalidParameterError, string.Empty); HandleReceiveError(); _lowerHandle.Dispose(); _lowerHandle = null; return; } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_FIN) { _sessions.Remove(_currentHeader.sessionId); } else { currentSession = _sessions[_currentHeader.sessionId]; } } if (currentHeader.flags == (byte)SNISMUXFlags.SMUX_DATA) { currentSession.HandleReceiveComplete(currentPacket, currentHeader); } if (_currentHeader.flags == (byte)SNISMUXFlags.SMUX_ACK) { try { currentSession.HandleAck(currentHeader.highwater); } catch (Exception e) { SNICommon.ReportSNIError(SNIProviders.SMUX_PROV, SNICommon.InternalExceptionError, e); } } lock (this) { if (packet.DataLeft == 0) { sniErrorCode = ReceiveAsync(ref packet); if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING) { return; } HandleReceiveError(); return; } } } }
/// <summary> /// Check SNI handle connection /// </summary> /// <param name="handle"></param> /// <returns>SNI error status</returns> public uint CheckConnection(SNIHandle handle) { return handle.CheckConnection(); }
/// <summary> /// Create a SNI connection handle /// </summary> /// <param name="callbackObject">Asynchronous I/O callback object</param> /// <param name="fullServerName">Full server name from connection string</param> /// <param name="ignoreSniOpenTimeout">Ignore open timeout</param> /// <param name="timerExpire">Timer expiration</param> /// <param name="instanceName">Instance name</param> /// <param name="spnBuffer">SPN</param> /// <param name="flushCache">Flush packet cache</param> /// <param name="async">Asynchronous connection</param> /// <param name="parallel">Attempt parallel connects</param> /// <returns>SNI handle</returns> public SNIHandle CreateConnectionHandle(object callbackObject, string fullServerName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[] spnBuffer, bool flushCache, bool async, bool parallel, bool isIntegratedSecurity) { instanceName = new byte[1]; SNIHandle sniHandle = null; if (fullServerName.IndexOf(':') == -1) { // default to using tcp if no protocol is provided sniHandle = CreateTcpHandle(fullServerName, timerExpire, callbackObject, parallel, ref spnBuffer, isIntegratedSecurity); } else { string serverNameWithOutProtocol = null; // when tcp protocol is specified if ((serverNameWithOutProtocol = GetServerNameWithOutProtocol(fullServerName, TdsEnums.TCP + ":")) != null) { sniHandle = CreateTcpHandle(serverNameWithOutProtocol, timerExpire, callbackObject, parallel, ref spnBuffer, isIntegratedSecurity); } // when np protocol is specified else if ((serverNameWithOutProtocol = GetServerNameWithOutProtocol(fullServerName, TdsEnums.NP + ":\\\\")) != null || (serverNameWithOutProtocol = GetServerNameWithOutProtocol(fullServerName, "\\\\")) != null) { sniHandle = CreateNpHandle(serverNameWithOutProtocol, timerExpire, callbackObject, parallel); } // possibly error case else { int portOrInstanceNameIndex = Math.Max(fullServerName.LastIndexOf(','), fullServerName.LastIndexOf('\\')); string serverNameWithOutPortOrInstanceName = portOrInstanceNameIndex > 0 ? fullServerName.Substring(0, portOrInstanceNameIndex) : fullServerName; IPAddress address = null; // when no protocol is specified, and fullServerName is IPv6 if (IPAddress.TryParse(serverNameWithOutPortOrInstanceName, out address) && address.AddressFamily == AddressFamily.InterNetworkV6) { // default to using tcp if no protocol is provided sniHandle = CreateTcpHandle(fullServerName, timerExpire, callbackObject, parallel, ref spnBuffer, isIntegratedSecurity); } // error case for sure else { // when invalid protocol is specified if (IsOccursOnce(fullServerName, ':')) { SNICommon.ReportSNIError( SNIProviders.INVALID_PROV, 0, (uint)(parallel ? SNICommon.MultiSubnetFailoverWithNonTcpProtocol : SNICommon.ProtocolNotSupportedError), string.Empty); } // when fullServerName is in invalid format else { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, 0, SNICommon.InvalidConnStringError, string.Empty); } } } } return(sniHandle); }
/// <summary> /// Disable SSL on a connection /// </summary> /// <param name="handle">Connection handle</param> /// <returns>SNI error code</returns> public uint DisableSsl(SNIHandle handle) { handle.DisableSsl(); return TdsEnums.SNI_SUCCESS; }
/// <summary> /// Set connection buffer size /// </summary> /// <param name="handle">SNI handle</param> /// <param name="bufferSize">Buffer size</param> /// <returns>SNI error code</returns> public uint SetConnectionBufferSize(SNIHandle handle, uint bufferSize) { if (handle is SNITCPHandle) { (handle as SNITCPHandle).SetBufferSize((int)bufferSize); } return TdsEnums.SNI_SUCCESS; }
/// <summary> /// Set connection buffer size /// </summary> /// <param name="handle">SNI handle</param> /// <param name="bufferSize">Buffer size</param> /// <returns>SNI error code</returns> public uint SetConnectionBufferSize(SNIHandle handle, uint bufferSize) { handle.SetBufferSize((int)bufferSize); return TdsEnums.SNI_SUCCESS; }
/// <summary> /// Reset a packet /// </summary> /// <param name="handle">SNI handle</param> /// <param name="write">true if packet is for write</param> /// <param name="packet">SNI packet</param> public void PacketReset(SNIHandle handle, bool write, SNIPacket packet) { packet.Reset(); }
/// <summary> /// Constructor /// </summary> /// <param name="lowerHandle">Lower handle</param> public SNIMarsConnection(SNIHandle lowerHandle) { _lowerHandle = lowerHandle; _lowerHandle.SetAsyncCallbacks(HandleReceiveComplete, HandleSendComplete); }
/// <summary> /// Create a SNI connection handle /// </summary> /// <param name="callbackObject">Asynchronous I/O callback object</param> /// <param name="fullServerName">Full server name from connection string</param> /// <param name="ignoreSniOpenTimeout">Ignore open timeout</param> /// <param name="timerExpire">Timer expiration</param> /// <param name="instanceName">Instance name</param> /// <param name="spnBuffer">SPN</param> /// <param name="flushCache">Flush packet cache</param> /// <param name="async">Asynchronous connection</param> /// <param name="parallel">Attempt parallel connects</param> /// <returns>SNI handle</returns> public SNIHandle CreateConnectionHandle(object callbackObject, string fullServerName, bool ignoreSniOpenTimeout, long timerExpire, out byte[] instanceName, ref byte[] spnBuffer, bool flushCache, bool async, bool parallel, bool isIntegratedSecurity) { instanceName = new byte[1]; bool errorWithLocalDBProcessing; string localDBDataSource = GetLocalDBDataSource(fullServerName, out errorWithLocalDBProcessing); if (errorWithLocalDBProcessing) { return(null); } // If a localDB Data source is available, we need to use it. fullServerName = localDBDataSource ?? fullServerName; DataSource details = DataSource.ParseServerName(fullServerName); if (details == null) { return(null); } if (isIntegratedSecurity) { string hostName = details.ServerName; int connPort = details.Port; string connInstanceName = details.InstanceName; try { spnBuffer = GetSqlServerSPN(hostName, (connPort >= 0 ? connPort.ToString() : connInstanceName)); } catch (Exception e) { SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.INVALID_PROV, SNICommon.ErrorSpnLookup, e); return(null); } } SNIHandle sniHandle = null; switch (details.ConnectionProtocol) { case DataSource.Protocol.TCP: sniHandle = CreateTcpHandle(details, timerExpire, callbackObject, parallel); break; case DataSource.Protocol.NP: sniHandle = CreateNpHandle(details, timerExpire, callbackObject, parallel); break; case DataSource.Protocol.None: // default to using tcp if no protocol is provided sniHandle = CreateTcpHandle(details, timerExpire, callbackObject, parallel); break; default: Debug.Fail($"Unexpected connection protocol: {details.ConnectionProtocol}"); break; } return(sniHandle); }