<commit_before>﻿namespace ZeroMQ.Sockets
{
    using System;
    using System.Threading;

    using ZeroMQ.Proxy;

    /// <summary>
    /// Forwards messages received by a front-end socket to a back-end socket, from which
    /// they are then sent.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The base implementation of <see cref="ZmqDevice"/> is <b>not</b> threadsafe. It is
    /// possible to construct a device with sockets that were created in separate threads or
    /// separate contexts.
    /// </para>
    /// <para>
    /// For this reason, the preferred way to create devices is to inherit from <see cref="ZmqDevice"/>
    /// and create the frontend and backend sockets from within the subclass using a single context.
    /// </para>
    /// </remarks>
    public abstract class ZmqDevice
    {
        private readonly IDeviceProxy device;
        private readonly ZmqErrorProvider errorProvider;
        private readonly ManualResetEvent runningEvent;

        internal ZmqDevice(IDeviceProxy device, IErrorProviderProxy errorProvider)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (errorProvider == null)
            {
                throw new ArgumentNullException("errorProvider");
            }

            this.device = device;
            this.errorProvider = new ZmqErrorProvider(errorProvider);
            this.runningEvent = new ManualResetEvent(true);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ZmqDevice"/> class.
        /// </summary>
        /// <param name="frontend">
        /// A <see cref="ZmqSocket"/> that will pass incoming messages to <paramref name="backend"/>.
        /// </param>
        /// <param name="backend">
        /// A <see cref="ZmqSocket"/> that will receive messages from (and optionally send replies
        /// to) <paramref name="frontend"/>.
        /// </param>
        protected ZmqDevice(ZmqSocket frontend, ZmqSocket backend)
        {
            if (frontend == null)
            {
                throw new ArgumentNullException("frontend");
            }

            if (backend == null)
            {
                throw new ArgumentNullException("backend");
            }

            this.device = ZmqContext.ProxyFactory.CreateDevice(frontend.Handle, backend.Handle);
            this.errorProvider = new ZmqErrorProvider(ZmqContext.ProxyFactory.ErrorProvider);
            this.runningEvent = new ManualResetEvent(true);
        }

        /// <summary>
        /// Gets a value indicating whether the device loop is running;
        /// </summary>
        public bool IsRunning
        {
            get { return this.device.IsRunning; }
            private set { this.device.IsRunning = value; }
        }

        /// <summary>
        /// Start the device in the current thread.
        /// </summary>
        public virtual void Start()
        {
            this.Run();
        }

        /// <summary>
        /// Blocks the calling thread until the device terminates.
        /// </summary>
        public void Join()
        {
            this.runningEvent.WaitOne();
        }

        /// <summary>
        /// Blocks the calling thread until the device terminates or the specified time elapses.
        /// </summary>
        /// <param name="timeout">
        /// A <see cref="TimeSpan"/> set to the amount of time to wait for the device to terminate.
        /// </param>
        /// <returns>
        /// true if the device terminated; false if the device has not terminated after
        /// the amount of time specified by <paramref name="timeout"/> has elapsed.
        /// </returns>
        public bool Join(TimeSpan timeout)
        {
            return this.runningEvent.WaitOne(timeout);
        }

        /// <summary>
        /// Terminate the device safely.
        /// </summary>
        public void Stop()
        {
            this.IsRunning = false;
        }

        /// <summary>
        /// Start the device in the current thread. Should be used by implementations of
        /// the <see cref="Start"/> method.
        /// </summary>
        protected void Run()
        {
            this.runningEvent.Reset();
            this.IsRunning = true;

            int errorCode = this.device.Run();

            this.IsRunning = false;
            this.runningEvent.Set();

            if (errorCode == -1 && !this.errorProvider.ContextWasTerminated)
            {
                throw this.errorProvider.GetLastError();
            }
        }
    }
}
<commit_msg>Make ZmqDevice control methods virtual.<commit_after>﻿namespace ZeroMQ.Sockets
{
    using System;
    using System.Threading;

    using ZeroMQ.Proxy;

    /// <summary>
    /// Forwards messages received by a front-end socket to a back-end socket, from which
    /// they are then sent.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The base implementation of <see cref="ZmqDevice"/> is <b>not</b> threadsafe. It is
    /// possible to construct a device with sockets that were created in separate threads or
    /// separate contexts.
    /// </para>
    /// <para>
    /// For this reason, the preferred way to create devices is to inherit from <see cref="ZmqDevice"/>
    /// and create the frontend and backend sockets from within the subclass using a single context.
    /// </para>
    /// </remarks>
    public abstract class ZmqDevice
    {
        private readonly IDeviceProxy device;
        private readonly ZmqErrorProvider errorProvider;
        private readonly ManualResetEvent runningEvent;

        internal ZmqDevice(IDeviceProxy device, IErrorProviderProxy errorProvider)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (errorProvider == null)
            {
                throw new ArgumentNullException("errorProvider");
            }

            this.device = device;
            this.errorProvider = new ZmqErrorProvider(errorProvider);
            this.runningEvent = new ManualResetEvent(true);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ZmqDevice"/> class.
        /// </summary>
        /// <param name="frontend">
        /// A <see cref="ZmqSocket"/> that will pass incoming messages to <paramref name="backend"/>.
        /// </param>
        /// <param name="backend">
        /// A <see cref="ZmqSocket"/> that will receive messages from (and optionally send replies
        /// to) <paramref name="frontend"/>.
        /// </param>
        protected ZmqDevice(ZmqSocket frontend, ZmqSocket backend)
        {
            if (frontend == null)
            {
                throw new ArgumentNullException("frontend");
            }

            if (backend == null)
            {
                throw new ArgumentNullException("backend");
            }

            this.device = ZmqContext.ProxyFactory.CreateDevice(frontend.Handle, backend.Handle);
            this.errorProvider = new ZmqErrorProvider(ZmqContext.ProxyFactory.ErrorProvider);
            this.runningEvent = new ManualResetEvent(true);
        }

        /// <summary>
        /// Gets a value indicating whether the device loop is running;
        /// </summary>
        public bool IsRunning
        {
            get { return this.device.IsRunning; }
            private set { this.device.IsRunning = value; }
        }

        /// <summary>
        /// Start the device in the current thread.
        /// </summary>
        public virtual void Start()
        {
            this.Run();
        }

        /// <summary>
        /// Blocks the calling thread until the device terminates.
        /// </summary>
        public virtual void Join()
        {
            this.runningEvent.WaitOne();
        }

        /// <summary>
        /// Blocks the calling thread until the device terminates or the specified time elapses.
        /// </summary>
        /// <param name="timeout">
        /// A <see cref="TimeSpan"/> set to the amount of time to wait for the device to terminate.
        /// </param>
        /// <returns>
        /// true if the device terminated; false if the device has not terminated after
        /// the amount of time specified by <paramref name="timeout"/> has elapsed.
        /// </returns>
        public virtual bool Join(TimeSpan timeout)
        {
            return this.runningEvent.WaitOne(timeout);
        }

        /// <summary>
        /// Terminate the device safely.
        /// </summary>
        public virtual void Stop()
        {
            this.IsRunning = false;
        }

        /// <summary>
        /// Start the device in the current thread. Should be used by implementations of
        /// the <see cref="Start"/> method.
        /// </summary>
        protected void Run()
        {
            this.runningEvent.Reset();
            this.IsRunning = true;

            int errorCode = this.device.Run();

            this.IsRunning = false;
            this.runningEvent.Set();

            if (errorCode == -1 && !this.errorProvider.ContextWasTerminated)
            {
                throw this.errorProvider.GetLastError();
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.10827959226128563, 'fraction_numerical': 0.00020802995631370917, 'mean_word_length': 1.512411810817873, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '112738', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2262, 'n_words': 733}