mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-21 10:23:28 +00:00
da854b711a
Fix Renci.SshNet.NET35 project and make it compilable Add Tests for .NET 3.5
97 lines
2.8 KiB
C#
97 lines
2.8 KiB
C#
using System.Linq;
|
|
using System;
|
|
using System.Net.Sockets;
|
|
using System.Net;
|
|
using Renci.SshNet.Messages;
|
|
using Renci.SshNet.Common;
|
|
using System.Threading;
|
|
using Renci.SshNet.Messages.Transport;
|
|
using System.Reflection;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
|
|
namespace Renci.SshNet
|
|
{
|
|
/// <summary>
|
|
/// Provides functionality to connect and interact with SSH server.
|
|
/// </summary>
|
|
public partial class Session
|
|
{
|
|
private static readonly Dictionary<Type, MethodInfo> _handlers;
|
|
|
|
static Session()
|
|
{
|
|
_handlers = new Dictionary<Type, MethodInfo>();
|
|
|
|
foreach (var method in typeof(Session).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic).Where(x => x.Name == "HandleMessage"))
|
|
{
|
|
if (method.IsGenericMethod) continue;
|
|
|
|
var args = method.GetParameters();
|
|
if (args.Length != 1) continue;
|
|
|
|
var argType = args[0].ParameterType;
|
|
if (!argType.IsSubclassOf(typeof(Message))) continue;
|
|
|
|
_handlers.Add(argType, method);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles SSH messages.
|
|
/// </summary>
|
|
/// <param name="message">The message.</param>
|
|
partial void HandleMessageCore(Message message)
|
|
{
|
|
Debug.Assert(message != null);
|
|
|
|
MethodInfo method;
|
|
|
|
if (_handlers.TryGetValue(message.GetType(), out method))
|
|
{
|
|
try
|
|
{
|
|
method.Invoke(this, new object[] { message });
|
|
}
|
|
catch (TargetInvocationException ex)
|
|
{
|
|
throw ex.InnerException ?? ex;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
HandleMessage(message);
|
|
}
|
|
}
|
|
|
|
partial void ExecuteThread(Action action)
|
|
{
|
|
ThreadPool.QueueUserWorkItem((o) => { action(); });
|
|
}
|
|
|
|
partial void InternalRegisterMessage(string messageName)
|
|
{
|
|
lock (this._messagesMetadata)
|
|
{
|
|
foreach (var m in from m in this._messagesMetadata where m.Name == messageName select m)
|
|
{
|
|
m.Enabled = true;
|
|
m.Activated = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
partial void InternalUnRegisterMessage(string messageName)
|
|
{
|
|
lock (this._messagesMetadata)
|
|
{
|
|
foreach (var m in from m in this._messagesMetadata where m.Name == messageName select m)
|
|
{
|
|
m.Enabled = false;
|
|
m.Activated = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|