/// <summary> /// Starts switching connection to secure. /// </summary> /// <param name="op">Asynchronous operation.</param> /// <returns>Returns true if aynchronous operation is pending (The <see cref="SwitchToSecureAsyncOP.CompletedAsync"/> event is raised upon completion of the operation). /// Returns false if operation completed synchronously.</returns> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when connection is already secure or when SSL certificate is not specified.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception> public bool SwitchToSecureAsync(SwitchToSecureAsyncOP op) { if (this.IsDisposed) { throw new ObjectDisposedException(this.GetType().Name); } if (this.IsSecureConnection) { throw new InvalidOperationException("Connection is already secure."); } if (m_pCertificate == null) { throw new InvalidOperationException("There is no certificate specified."); } if (op == null) { throw new ArgumentNullException("op"); } if (op.State != AsyncOP_State.WaitingForStart) { throw new ArgumentException("Invalid argument 'op' state, 'op' must be in 'AsyncOP_State.WaitingForStart' state.", "op"); } return(op.Start(this)); }
/// <summary> /// Switches session to secure connection. /// </summary> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when connection is already secure or when SSL certificate is not specified.</exception> public void SwitchToSecure() { if (m_IsDisposed) { throw new ObjectDisposedException("TCP_ServerSession"); } if (m_IsSecure) { throw new InvalidOperationException("Session is already SSL/TLS."); } if (m_pCertificate == null) { throw new InvalidOperationException("There is no certificate specified."); } ManualResetEvent wait = new ManualResetEvent(false); using (SwitchToSecureAsyncOP op = new SwitchToSecureAsyncOP()){ op.CompletedAsync += delegate(object s1, EventArgs <SwitchToSecureAsyncOP> e1){ wait.Set(); }; if (!this.SwitchToSecureAsync(op)) { wait.Set(); } wait.WaitOne(); wait.Close(); if (op.Error != null) { throw op.Error; } } }
/// <summary> /// This method is called from TCP server when session should start processing incoming connection. /// </summary> internal void StartI() { if (m_IsSsl) { // Log LogAddText("Starting SSL negotiation now."); DateTime startTime = DateTime.Now; // Create delegate which is called when SwitchToSecureAsync has completed. Action <SwitchToSecureAsyncOP> switchSecureCompleted = delegate(SwitchToSecureAsyncOP e) { try { // Operation failed. if (e.Error != null) { LogAddException(e.Error); if (!this.IsDisposed) { Disconnect(); } } // Operation suceeded. else { // Log LogAddText("SSL negotiation completed successfully in " + (DateTime.Now - startTime).TotalSeconds.ToString("f2") + " seconds."); Start(); } } catch (Exception x) { LogAddException(x); if (!this.IsDisposed) { Disconnect(); } } }; SwitchToSecureAsyncOP op = new SwitchToSecureAsyncOP(); op.CompletedAsync += delegate(object sender, EventArgs <TCP_ServerSession.SwitchToSecureAsyncOP> e) { switchSecureCompleted(op); }; // Switch to secure completed synchronously. if (!SwitchToSecureAsync(op)) { switchSecureCompleted(op); } } else { Start(); } }
/// <summary> /// Starts switching connection to secure. /// </summary> /// <param name="op">Asynchronous operation.</param> /// <returns>Returns true if aynchronous operation is pending (The <see cref="SwitchToSecureAsyncOP.CompletedAsync"/> event is raised upon completion of the operation). /// Returns false if operation completed synchronously.</returns> /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and and this method is accessed.</exception> /// <exception cref="InvalidOperationException">Is raised when TCP client is not connected or connection is already secure.</exception> /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception> protected bool SwitchToSecureAsync(SwitchToSecureAsyncOP op) { if(this.IsDisposed){ throw new ObjectDisposedException(this.GetType().Name); } if(!this.IsConnected){ throw new InvalidOperationException("You must connect first."); } if(this.IsSecureConnection){ throw new InvalidOperationException("Connection is already secure."); } if(op == null){ throw new ArgumentNullException("op"); } if(op.State != AsyncOP_State.WaitingForStart){ throw new ArgumentException("Invalid argument 'op' state, 'op' must be in 'AsyncOP_State.WaitingForStart' state.","op"); } return op.Start(this); }