Example #1
0
        public void SetTraceHandler(
            LIBSSH2_TRACE mask,
            Action <string> handler)
        {
            this.sessionHandle.CheckCurrentThreadOwnsHandle();
            Utilities.ThrowIfNull(handler, nameof(handler));

            using (SshTraceSources.Default.TraceMethod().WithParameters(mask))
            {
                // Store this delegate in a field to prevent it from being
                // garbage collected. Otherwise callbacks will suddenly
                // start hitting GC'ed memory.
                this.TraceHandlerDelegate = (sessionPtr, contextPtr, dataPtr, length) =>
                {
                    Debug.Assert(contextPtr == IntPtr.Zero);

                    var data = new byte[length.ToInt32()];
                    Marshal.Copy(dataPtr, data, 0, length.ToInt32());

                    handler(Encoding.ASCII.GetString(data));
                };

                UnsafeNativeMethods.libssh2_trace_sethandler(
                    this.sessionHandle,
                    IntPtr.Zero,
                    this.TraceHandlerDelegate);

                UnsafeNativeMethods.libssh2_trace(
                    this.sessionHandle,
                    mask);
            }
        }
Example #2
0
 public static extern void libssh2_trace(
     SshSessionHandle session,
     LIBSSH2_TRACE bitmask);