| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Runtime.ExceptionServices; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | using Renci.SshNet.Abstractions; |
| | | 7 | | using Renci.SshNet.Channels; |
| | | 8 | | using Renci.SshNet.Common; |
| | | 9 | | |
| | | 10 | | namespace Renci.SshNet |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Base class for SSH subsystem implementations. |
| | | 14 | | /// </summary> |
| | | 15 | | internal abstract class SubsystemSession : ISubsystemSession |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Holds the number of system wait handles that are returned as the leading entries in the array returned |
| | | 19 | | /// in <see cref="CreateWaitHandleArray(WaitHandle[])"/>. |
| | | 20 | | /// </summary> |
| | | 21 | | private const int SystemWaitHandleCount = 3; |
| | | 22 | | |
| | | 23 | | private readonly string _subsystemName; |
| | | 24 | | private ISession _session; |
| | | 25 | | private IChannelSession _channel; |
| | | 26 | | private Exception _exception; |
| | 945 | 27 | | private EventWaitHandle _errorOccuredWaitHandle = new ManualResetEvent(initialState: false); |
| | 945 | 28 | | private EventWaitHandle _sessionDisconnectedWaitHandle = new ManualResetEvent(initialState: false); |
| | 945 | 29 | | private EventWaitHandle _channelClosedWaitHandle = new ManualResetEvent(initialState: false); |
| | | 30 | | private bool _isDisposed; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets or set the number of seconds to wait for an operation to complete. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <value> |
| | | 36 | | /// The number of seconds to wait for an operation to complete, or -1 to wait indefinitely. |
| | | 37 | | /// </value> |
| | 29339 | 38 | | public int OperationTimeout { get; private set; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Occurs when an error occurred. |
| | | 42 | | /// </summary> |
| | | 43 | | public event EventHandler<ExceptionEventArgs> ErrorOccurred; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Occurs when the server has disconnected from the session. |
| | | 47 | | /// </summary> |
| | | 48 | | public event EventHandler<EventArgs> Disconnected; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the channel associated with this session. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <value> |
| | | 54 | | /// The channel associated with this session. |
| | | 55 | | /// </value> |
| | | 56 | | internal IChannelSession Channel |
| | | 57 | | { |
| | | 58 | | get |
| | 627 | 59 | | { |
| | 627 | 60 | | EnsureNotDisposed(); |
| | | 61 | | |
| | 627 | 62 | | return _channel; |
| | 627 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets a value indicating whether this session is open. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <value> |
| | | 70 | | /// <see langword="true"/> if this session is open; otherwise, <see langword="false"/>. |
| | | 71 | | /// </value> |
| | | 72 | | public bool IsOpen |
| | | 73 | | { |
| | 102633 | 74 | | get { return _channel is not null && _channel.IsOpen; } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Initializes a new instance of the <see cref="SubsystemSession"/> class. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="session">The session.</param> |
| | | 81 | | /// <param name="subsystemName">Name of the subsystem.</param> |
| | | 82 | | /// <param name="operationTimeout">The number of milliseconds to wait for a given operation to complete, or -1 t |
| | | 83 | | /// <exception cref="ArgumentNullException"><paramref name="session" /> or <paramref name="subsystemName" /> is |
| | 945 | 84 | | protected SubsystemSession(ISession session, string subsystemName, int operationTimeout) |
| | 945 | 85 | | { |
| | 945 | 86 | | if (session is null) |
| | 0 | 87 | | { |
| | 0 | 88 | | throw new ArgumentNullException(nameof(session)); |
| | | 89 | | } |
| | | 90 | | |
| | 945 | 91 | | if (subsystemName is null) |
| | 0 | 92 | | { |
| | 0 | 93 | | throw new ArgumentNullException(nameof(subsystemName)); |
| | | 94 | | } |
| | | 95 | | |
| | 945 | 96 | | _session = session; |
| | 945 | 97 | | _subsystemName = subsystemName; |
| | 945 | 98 | | OperationTimeout = operationTimeout; |
| | 945 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Connects the subsystem using a new SSH channel session. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <exception cref="InvalidOperationException">The session is already connected.</exception> |
| | | 105 | | /// <exception cref="ObjectDisposedException">The method was called after the session was disposed.</exception> |
| | | 106 | | /// <exception cref="SshException">The channel session could not be opened, or the subsystem could not be execut |
| | | 107 | | public void Connect() |
| | 948 | 108 | | { |
| | 948 | 109 | | EnsureNotDisposed(); |
| | | 110 | | |
| | 936 | 111 | | if (IsOpen) |
| | 18 | 112 | | { |
| | 18 | 113 | | throw new InvalidOperationException("The session is already connected."); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // reset waithandles in case we're reconnecting |
| | 918 | 117 | | _ = _errorOccuredWaitHandle.Reset(); |
| | 918 | 118 | | _ = _sessionDisconnectedWaitHandle.Reset(); |
| | 918 | 119 | | _ = _sessionDisconnectedWaitHandle.Reset(); |
| | 918 | 120 | | _ = _channelClosedWaitHandle.Reset(); |
| | | 121 | | |
| | 918 | 122 | | _session.ErrorOccured += Session_ErrorOccured; |
| | 918 | 123 | | _session.Disconnected += Session_Disconnected; |
| | | 124 | | |
| | 918 | 125 | | _channel = _session.CreateChannelSession(); |
| | 918 | 126 | | _channel.DataReceived += Channel_DataReceived; |
| | 918 | 127 | | _channel.Exception += Channel_Exception; |
| | 918 | 128 | | _channel.Closed += Channel_Closed; |
| | 918 | 129 | | _channel.Open(); |
| | | 130 | | |
| | 918 | 131 | | if (!_channel.SendSubsystemRequest(_subsystemName)) |
| | 25 | 132 | | { |
| | | 133 | | // close channel session |
| | 25 | 134 | | Disconnect(); |
| | | 135 | | |
| | | 136 | | // signal subsystem failure |
| | 25 | 137 | | throw new SshException(string.Format(CultureInfo.InvariantCulture, |
| | 25 | 138 | | "Subsystem '{0}' could not be executed.", |
| | 25 | 139 | | _subsystemName)); |
| | | 140 | | } |
| | | 141 | | |
| | 893 | 142 | | OnChannelOpen(); |
| | 893 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Disconnects the subsystem channel. |
| | | 147 | | /// </summary> |
| | | 148 | | public void Disconnect() |
| | 826 | 149 | | { |
| | 826 | 150 | | UnsubscribeFromSessionEvents(_session); |
| | | 151 | | |
| | 826 | 152 | | var channel = _channel; |
| | 826 | 153 | | if (channel is not null) |
| | 771 | 154 | | { |
| | 771 | 155 | | _channel = null; |
| | 771 | 156 | | channel.DataReceived -= Channel_DataReceived; |
| | 771 | 157 | | channel.Exception -= Channel_Exception; |
| | 771 | 158 | | channel.Closed -= Channel_Closed; |
| | 771 | 159 | | channel.Dispose(); |
| | 771 | 160 | | } |
| | 826 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Sends data to the subsystem. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="data">The data to be sent.</param> |
| | | 167 | | public void SendData(byte[] data) |
| | 32204 | 168 | | { |
| | 32204 | 169 | | EnsureNotDisposed(); |
| | 32192 | 170 | | EnsureSessionIsOpen(); |
| | | 171 | | |
| | 32177 | 172 | | _channel.SendData(data); |
| | 32175 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Called when channel is open. |
| | | 177 | | /// </summary> |
| | | 178 | | protected abstract void OnChannelOpen(); |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Called when data is received. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="data">The data.</param> |
| | | 184 | | protected abstract void OnDataReceived(byte[] data); |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Raises the error. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <param name="error">The error.</param> |
| | | 190 | | protected void RaiseError(Exception error) |
| | 45 | 191 | | { |
| | 45 | 192 | | _exception = error; |
| | | 193 | | |
| | 45 | 194 | | DiagnosticAbstraction.Log("Raised exception: " + error); |
| | | 195 | | |
| | 45 | 196 | | _ = _errorOccuredWaitHandle?.Set(); |
| | | 197 | | |
| | 45 | 198 | | SignalErrorOccurred(error); |
| | 45 | 199 | | } |
| | | 200 | | |
| | | 201 | | private void Channel_DataReceived(object sender, ChannelDataEventArgs e) |
| | 31914 | 202 | | { |
| | | 203 | | try |
| | 31914 | 204 | | { |
| | 31914 | 205 | | OnDataReceived(e.Data); |
| | 31905 | 206 | | } |
| | 9 | 207 | | catch (Exception ex) |
| | 9 | 208 | | { |
| | 9 | 209 | | RaiseError(ex); |
| | 9 | 210 | | } |
| | 31914 | 211 | | } |
| | | 212 | | |
| | | 213 | | private void Channel_Exception(object sender, ExceptionEventArgs e) |
| | 15 | 214 | | { |
| | 15 | 215 | | RaiseError(e.Exception); |
| | 15 | 216 | | } |
| | | 217 | | |
| | | 218 | | private void Channel_Closed(object sender, ChannelEventArgs e) |
| | 0 | 219 | | { |
| | 0 | 220 | | _ = _channelClosedWaitHandle?.Set(); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Waits a specified time for a given <see cref="WaitHandle"/> to get signaled. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <param name="waitHandle">The handle to wait for.</param> |
| | | 227 | | /// <param name="millisecondsTimeout">To number of milliseconds to wait for <paramref name="waitHandle"/> to get |
| | | 228 | | /// <exception cref="SshException">The connection was closed by the server.</exception> |
| | | 229 | | /// <exception cref="SshException">The channel was closed.</exception> |
| | | 230 | | /// <exception cref="SshOperationTimeoutException">The handle did not get signaled within the specified timeout. |
| | | 231 | | public void WaitOnHandle(WaitHandle waitHandle, int millisecondsTimeout) |
| | 24774 | 232 | | { |
| | 24774 | 233 | | var waitHandles = new[] |
| | 24774 | 234 | | { |
| | 24774 | 235 | | _errorOccuredWaitHandle, |
| | 24774 | 236 | | _sessionDisconnectedWaitHandle, |
| | 24774 | 237 | | _channelClosedWaitHandle, |
| | 24774 | 238 | | waitHandle |
| | 24774 | 239 | | }; |
| | | 240 | | |
| | 24774 | 241 | | var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); |
| | 24773 | 242 | | switch (result) |
| | | 243 | | { |
| | | 244 | | case 0: |
| | 0 | 245 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 246 | | break; |
| | | 247 | | case 1: |
| | 0 | 248 | | throw new SshException("Connection was closed by the server."); |
| | | 249 | | case 2: |
| | 0 | 250 | | throw new SshException("Channel was closed."); |
| | | 251 | | case 3: |
| | 24773 | 252 | | break; |
| | | 253 | | case WaitHandle.WaitTimeout: |
| | 0 | 254 | | throw new SshOperationTimeoutException("Operation has timed out."); |
| | | 255 | | default: |
| | 0 | 256 | | throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "WaitAny return value |
| | | 257 | | } |
| | 24773 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Blocks the current thread until the specified <see cref="WaitHandle"/> gets signaled, using a |
| | | 262 | | /// 32-bit signed integer to specify the time interval in milliseconds. |
| | | 263 | | /// </summary> |
| | | 264 | | /// <param name="waitHandle">The handle to wait for.</param> |
| | | 265 | | /// <param name="millisecondsTimeout">To number of milliseconds to wait for <paramref name="waitHandle"/> to get |
| | | 266 | | /// <returns> |
| | | 267 | | /// <see langword="true"/> if <paramref name="waitHandle"/> received a signal within the specified timeout; |
| | | 268 | | /// otherwise, <see langword="false"/>. |
| | | 269 | | /// </returns> |
| | | 270 | | /// <exception cref="SshException">The connection was closed by the server.</exception> |
| | | 271 | | /// <exception cref="SshException">The channel was closed.</exception> |
| | | 272 | | /// <remarks> |
| | | 273 | | /// The blocking wait is also interrupted when either the established channel is closed, the current |
| | | 274 | | /// session is disconnected or an unexpected <see cref="Exception"/> occurred while processing a channel |
| | | 275 | | /// or session event. |
| | | 276 | | /// </remarks> |
| | | 277 | | public bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout) |
| | 0 | 278 | | { |
| | 0 | 279 | | var waitHandles = new[] |
| | 0 | 280 | | { |
| | 0 | 281 | | _errorOccuredWaitHandle, |
| | 0 | 282 | | _sessionDisconnectedWaitHandle, |
| | 0 | 283 | | _channelClosedWaitHandle, |
| | 0 | 284 | | waitHandle |
| | 0 | 285 | | }; |
| | | 286 | | |
| | 0 | 287 | | var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); |
| | 0 | 288 | | switch (result) |
| | | 289 | | { |
| | | 290 | | case 0: |
| | 0 | 291 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 292 | | return false; // unreached |
| | | 293 | | case 1: |
| | 0 | 294 | | throw new SshException("Connection was closed by the server."); |
| | | 295 | | case 2: |
| | 0 | 296 | | throw new SshException("Channel was closed."); |
| | | 297 | | case 3: |
| | 0 | 298 | | return true; |
| | | 299 | | case WaitHandle.WaitTimeout: |
| | 0 | 300 | | return false; |
| | | 301 | | default: |
| | 0 | 302 | | throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "WaitAny return value |
| | | 303 | | } |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <summary> |
| | | 307 | | /// Blocks the current thread until the specified <see cref="WaitHandle"/> gets signaled, using a |
| | | 308 | | /// 32-bit signed integer to specify the time interval in milliseconds. |
| | | 309 | | /// </summary> |
| | | 310 | | /// <param name="waitHandleA">The first handle to wait for.</param> |
| | | 311 | | /// <param name="waitHandleB">The second handle to wait for.</param> |
| | | 312 | | /// <param name="millisecondsTimeout">To number of milliseconds to wait for a <see cref="WaitHandle"/> to get si |
| | | 313 | | /// <returns> |
| | | 314 | | /// <c>0</c> if <paramref name="waitHandleA"/> received a signal within the specified timeout, and <c>1</c> |
| | | 315 | | /// if <paramref name="waitHandleB"/> received a signal within the specified timeout. |
| | | 316 | | /// </returns> |
| | | 317 | | /// <exception cref="SshException">The connection was closed by the server.</exception> |
| | | 318 | | /// <exception cref="SshException">The channel was closed.</exception> |
| | | 319 | | /// <exception cref="SshOperationTimeoutException">The handle did not get signaled within the specified timeout. |
| | | 320 | | /// <remarks> |
| | | 321 | | /// <para> |
| | | 322 | | /// The blocking wait is also interrupted when either the established channel is closed, the current |
| | | 323 | | /// session is disconnected or an unexpected <see cref="Exception"/> occurred while processing a channel |
| | | 324 | | /// or session event. |
| | | 325 | | /// </para> |
| | | 326 | | /// <para> |
| | | 327 | | /// When both <paramref name="waitHandleA"/> and <paramref name="waitHandleB"/> are signaled during the call, |
| | | 328 | | /// then <c>0</c> is returned. |
| | | 329 | | /// </para> |
| | | 330 | | /// </remarks> |
| | | 331 | | public int WaitAny(WaitHandle waitHandleA, WaitHandle waitHandleB, int millisecondsTimeout) |
| | 0 | 332 | | { |
| | 0 | 333 | | var waitHandles = new[] |
| | 0 | 334 | | { |
| | 0 | 335 | | _errorOccuredWaitHandle, |
| | 0 | 336 | | _sessionDisconnectedWaitHandle, |
| | 0 | 337 | | _channelClosedWaitHandle, |
| | 0 | 338 | | waitHandleA, |
| | 0 | 339 | | waitHandleB |
| | 0 | 340 | | }; |
| | | 341 | | |
| | 0 | 342 | | var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); |
| | 0 | 343 | | switch (result) |
| | | 344 | | { |
| | | 345 | | case 0: |
| | 0 | 346 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 347 | | return -1; // unreached |
| | | 348 | | case 1: |
| | 0 | 349 | | throw new SshException("Connection was closed by the server."); |
| | | 350 | | case 2: |
| | 0 | 351 | | throw new SshException("Channel was closed."); |
| | | 352 | | case 3: |
| | 0 | 353 | | return 0; |
| | | 354 | | case 4: |
| | 0 | 355 | | return 1; |
| | | 356 | | case WaitHandle.WaitTimeout: |
| | 0 | 357 | | throw new SshOperationTimeoutException("Operation has timed out."); |
| | | 358 | | default: |
| | 0 | 359 | | throw new NotImplementedException(string.Format(CultureInfo.InvariantCulture, "WaitAny return value |
| | | 360 | | } |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Waits for any of the elements in the specified array to receive a signal, using a 32-bit signed |
| | | 365 | | /// integer to specify the time interval. |
| | | 366 | | /// </summary> |
| | | 367 | | /// <param name="waitHandles">A <see cref="WaitHandle"/> array - constructed using <see cref="CreateWaitHandleAr |
| | | 368 | | /// <param name="millisecondsTimeout">To number of milliseconds to wait for a <see cref="WaitHandle"/> to get si |
| | | 369 | | /// <returns> |
| | | 370 | | /// The array index of the first non-system object that satisfied the wait. |
| | | 371 | | /// </returns> |
| | | 372 | | /// <exception cref="SshException">The connection was closed by the server.</exception> |
| | | 373 | | /// <exception cref="SshException">The channel was closed.</exception> |
| | | 374 | | /// <exception cref="SshOperationTimeoutException">No object satified the wait and a time interval equivalent to |
| | | 375 | | /// <remarks> |
| | | 376 | | /// For the return value, the index of the first non-system object is considered to be zero. |
| | | 377 | | /// </remarks> |
| | | 378 | | public int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout) |
| | 3695 | 379 | | { |
| | 3695 | 380 | | var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout); |
| | 3695 | 381 | | switch (result) |
| | | 382 | | { |
| | | 383 | | case 0: |
| | 0 | 384 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 385 | | return -1; // unreached |
| | | 386 | | case 1: |
| | 0 | 387 | | throw new SshException("Connection was closed by the server."); |
| | | 388 | | case 2: |
| | 0 | 389 | | throw new SshException("Channel was closed."); |
| | | 390 | | case WaitHandle.WaitTimeout: |
| | 0 | 391 | | throw new SshOperationTimeoutException("Operation has timed out."); |
| | | 392 | | default: |
| | 3695 | 393 | | return result - SystemWaitHandleCount; |
| | | 394 | | } |
| | 3695 | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// Creates a <see cref="WaitHandle"/> array that is composed of system objects and the specified |
| | | 399 | | /// elements. |
| | | 400 | | /// </summary> |
| | | 401 | | /// <param name="waitHandle1">The first <see cref="WaitHandle"/> to wait for.</param> |
| | | 402 | | /// <param name="waitHandle2">The second <see cref="WaitHandle"/> to wait for.</param> |
| | | 403 | | /// <returns> |
| | | 404 | | /// A <see cref="WaitHandle"/> array that is composed of system objects and the specified elements. |
| | | 405 | | /// </returns> |
| | | 406 | | public WaitHandle[] CreateWaitHandleArray(WaitHandle waitHandle1, WaitHandle waitHandle2) |
| | 46 | 407 | | { |
| | 46 | 408 | | return new WaitHandle[] |
| | 46 | 409 | | { |
| | 46 | 410 | | _errorOccuredWaitHandle, |
| | 46 | 411 | | _sessionDisconnectedWaitHandle, |
| | 46 | 412 | | _channelClosedWaitHandle, |
| | 46 | 413 | | waitHandle1, |
| | 46 | 414 | | waitHandle2 |
| | 46 | 415 | | }; |
| | 46 | 416 | | } |
| | | 417 | | |
| | | 418 | | /// <summary> |
| | | 419 | | /// Creates a <see cref="WaitHandle"/> array that is composed of system objects and the specified |
| | | 420 | | /// elements. |
| | | 421 | | /// </summary> |
| | | 422 | | /// <param name="waitHandles">A <see cref="WaitHandle"/> array containing the objects to wait for.</param> |
| | | 423 | | /// <returns> |
| | | 424 | | /// A <see cref="WaitHandle"/> array that is composed of system objects and the specified elements. |
| | | 425 | | /// </returns> |
| | | 426 | | public WaitHandle[] CreateWaitHandleArray(params WaitHandle[] waitHandles) |
| | 0 | 427 | | { |
| | 0 | 428 | | var array = new WaitHandle[waitHandles.Length + SystemWaitHandleCount]; |
| | 0 | 429 | | array[0] = _errorOccuredWaitHandle; |
| | 0 | 430 | | array[1] = _sessionDisconnectedWaitHandle; |
| | 0 | 431 | | array[2] = _channelClosedWaitHandle; |
| | | 432 | | |
| | 0 | 433 | | for (var i = 0; i < waitHandles.Length; i++) |
| | 0 | 434 | | { |
| | 0 | 435 | | array[i + SystemWaitHandleCount] = waitHandles[i]; |
| | 0 | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | return array; |
| | 0 | 439 | | } |
| | | 440 | | |
| | | 441 | | private void Session_Disconnected(object sender, EventArgs e) |
| | 15 | 442 | | { |
| | 15 | 443 | | _ = _sessionDisconnectedWaitHandle?.Set(); |
| | | 444 | | |
| | 15 | 445 | | SignalDisconnected(); |
| | 15 | 446 | | } |
| | | 447 | | |
| | | 448 | | private void Session_ErrorOccured(object sender, ExceptionEventArgs e) |
| | 21 | 449 | | { |
| | 21 | 450 | | RaiseError(e.Exception); |
| | 21 | 451 | | } |
| | | 452 | | |
| | | 453 | | private void SignalErrorOccurred(Exception error) |
| | 45 | 454 | | { |
| | 45 | 455 | | ErrorOccurred?.Invoke(this, new ExceptionEventArgs(error)); |
| | 45 | 456 | | } |
| | | 457 | | |
| | | 458 | | private void SignalDisconnected() |
| | 15 | 459 | | { |
| | 15 | 460 | | Disconnected?.Invoke(this, EventArgs.Empty); |
| | 15 | 461 | | } |
| | | 462 | | |
| | | 463 | | private void EnsureSessionIsOpen() |
| | 32192 | 464 | | { |
| | 32192 | 465 | | if (!IsOpen) |
| | 15 | 466 | | { |
| | 15 | 467 | | throw new InvalidOperationException("The session is not open."); |
| | | 468 | | } |
| | 32177 | 469 | | } |
| | | 470 | | |
| | | 471 | | /// <summary> |
| | | 472 | | /// Unsubscribes the current <see cref="SubsystemSession"/> from session events. |
| | | 473 | | /// </summary> |
| | | 474 | | /// <param name="session">The session.</param> |
| | | 475 | | /// <remarks> |
| | | 476 | | /// Does nothing when <paramref name="session"/> is <see langword="null"/>. |
| | | 477 | | /// </remarks> |
| | | 478 | | private void UnsubscribeFromSessionEvents(ISession session) |
| | 826 | 479 | | { |
| | 826 | 480 | | if (session is null) |
| | 12 | 481 | | { |
| | 12 | 482 | | return; |
| | | 483 | | } |
| | | 484 | | |
| | 814 | 485 | | session.Disconnected -= Session_Disconnected; |
| | 814 | 486 | | session.ErrorOccured -= Session_ErrorOccured; |
| | 826 | 487 | | } |
| | | 488 | | |
| | | 489 | | /// <summary> |
| | | 490 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 491 | | /// </summary> |
| | | 492 | | public void Dispose() |
| | 750 | 493 | | { |
| | 750 | 494 | | Dispose(disposing: true); |
| | 750 | 495 | | GC.SuppressFinalize(this); |
| | 750 | 496 | | } |
| | | 497 | | |
| | | 498 | | /// <summary> |
| | | 499 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 500 | | /// </summary> |
| | | 501 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 502 | | protected virtual void Dispose(bool disposing) |
| | 957 | 503 | | { |
| | 957 | 504 | | if (_isDisposed) |
| | 12 | 505 | | { |
| | 12 | 506 | | return; |
| | | 507 | | } |
| | | 508 | | |
| | 945 | 509 | | if (disposing) |
| | 738 | 510 | | { |
| | 738 | 511 | | Disconnect(); |
| | | 512 | | |
| | 738 | 513 | | _session = null; |
| | | 514 | | |
| | 738 | 515 | | var errorOccuredWaitHandle = _errorOccuredWaitHandle; |
| | 738 | 516 | | if (errorOccuredWaitHandle != null) |
| | 738 | 517 | | { |
| | 738 | 518 | | _errorOccuredWaitHandle = null; |
| | 738 | 519 | | errorOccuredWaitHandle.Dispose(); |
| | 738 | 520 | | } |
| | | 521 | | |
| | 738 | 522 | | var sessionDisconnectedWaitHandle = _sessionDisconnectedWaitHandle; |
| | 738 | 523 | | if (sessionDisconnectedWaitHandle != null) |
| | 738 | 524 | | { |
| | 738 | 525 | | _sessionDisconnectedWaitHandle = null; |
| | 738 | 526 | | sessionDisconnectedWaitHandle.Dispose(); |
| | 738 | 527 | | } |
| | | 528 | | |
| | 738 | 529 | | var channelClosedWaitHandle = _channelClosedWaitHandle; |
| | 738 | 530 | | if (channelClosedWaitHandle != null) |
| | 738 | 531 | | { |
| | 738 | 532 | | _channelClosedWaitHandle = null; |
| | 738 | 533 | | channelClosedWaitHandle.Dispose(); |
| | 738 | 534 | | } |
| | | 535 | | |
| | 738 | 536 | | _isDisposed = true; |
| | 738 | 537 | | } |
| | 957 | 538 | | } |
| | | 539 | | |
| | | 540 | | /// <summary> |
| | | 541 | | /// Finalizes an instance of the <see cref="SubsystemSession" /> class. |
| | | 542 | | /// </summary> |
| | | 543 | | ~SubsystemSession() |
| | 414 | 544 | | { |
| | 207 | 545 | | Dispose(disposing: false); |
| | 414 | 546 | | } |
| | | 547 | | |
| | | 548 | | private void EnsureNotDisposed() |
| | 33779 | 549 | | { |
| | | 550 | | #if NET7_0_OR_GREATER |
| | 33626 | 551 | | ObjectDisposedException.ThrowIf(_isDisposed, this); |
| | | 552 | | #else |
| | 153 | 553 | | if (_isDisposed) |
| | 8 | 554 | | { |
| | 8 | 555 | | throw new ObjectDisposedException(GetType().FullName); |
| | | 556 | | } |
| | | 557 | | #endif // NET7_0_OR_GREATER |
| | 33755 | 558 | | } |
| | | 559 | | } |
| | | 560 | | } |