示例#1
0
文件: RayGrain.cs 项目: mlpk1/Ray2
 protected virtual Task <bool> PublishEventAsync(IEvent @event)
 {
     if (@event == null)
     {
         throw new ArgumentNullException("PublishEventAsync event cannot be empty");
     }
     return(MQPublisher.Publish(@event));
 }
示例#2
0
    /// <summary>
    /// This Function runs in Publisher thread and publishes data continueously.
    /// Calling "StopSendingCommands()" function will stop this thread.
    /// </summary>
    private static void Publisher_CoreFcn()
    {
        Stopwatch watch        = Stopwatch.StartNew();
        Stopwatch TimeoutWatch = Stopwatch.StartNew();

        while (IsPublisherEnabled)
        {
            if (IsDataUpdated)
            {
                IsDataUpdated = false;
                byte[] data = new byte[LenMouseData + NumKeys];
                Array.Copy(Keys, 0, data, 0, NumKeys);
                byte[] mouseData = PrepareMouseData();
                Array.Copy(mouseData, 0, data, NumKeys, LenMouseData);
                Publisher.Publish(data);
                for (int i = 0; i < Keys.Length; i++)
                {
                    Key key = (Key)i;
                    if (key == Key.LeftAlt || key == Key.RightAlt || key == Key.LeftCtrl || key == Key.RightCtrl || key == Key.LeftShift || key == Key.RightShift)
                    {
                    }
                    else
                    {
                        Keys[i] = 0;
                    }
                }
                TimeoutWatch.Restart();
            }
            else
            {
                if (TimeoutWatch.Elapsed.TotalSeconds > 10)
                {
                    TimeoutWatch.Restart();
                    byte[] data = Encoding.ASCII.GetBytes("ImAlive");
                    Publisher.Publish(data);
                    Thread.Sleep(1);
                    Publisher.Publish(data);
                }
            }
            while (watch.Elapsed.TotalSeconds <= ControllerPeriod)
            {
                Thread.Sleep(1);
            }
        }
    }
    /// <summary>
    /// This will be used by publisher thread as long as it is activated. Data will be prepared and published here in this function.
    /// </summary>
    private static void PublisherCoreFcn()
    {
        Stopwatch watch          = Stopwatch.StartNew();
        Stopwatch stopwatch      = Stopwatch.StartNew();
        int       totalBytesSent = 0;

        while (IsPublisherEnabled)
        {
            byte[] screenBytes = ImageProcessing.GetScreenBytes();
            if (screenBytes != null && Publisher != null)
            {
                string time      = DateTime.UtcNow.TimeOfDay.ToString();
                byte[] timeBytes = Encoding.ASCII.GetBytes(time);
                byte[] data      = new byte[timeBytes.Length + screenBytes.Length];
                timeBytes.CopyTo(data, 0);
                screenBytes.CopyTo(data, timeBytes.Length);
                Publisher.Publish(data);
                totalBytesSent += data.Length;
                FpsCounter++;
                if (stopwatch.ElapsedMilliseconds > 1000)
                {
                    UpdateStats(totalBytesSent, stopwatch.Elapsed.TotalSeconds);
                    stopwatch.Restart();
                    totalBytesSent = 0;
                    string timeBase      = DateTime.UtcNow.TimeOfDay.ToString();
                    byte[] timeBaseBytes = Encoding.ASCII.GetBytes(timeBase);
                    TimeBasePublisher.Publish(timeBaseBytes);
                }
            }
            else
            {
                if (Publisher == null)
                {
                    Debug.WriteLine("Capturer or Publisher was null. Transfer aborted!");
                    break;
                }
            }
            while (watch.Elapsed.TotalSeconds <= CommunicationPeriod)
            {
                Thread.Sleep(1);
            }
            watch.Restart();
        }
        Debug.WriteLine("While loop in Publisheer Core Fcn is broken");
    }