예제 #1
0
        public override IEnumerator OnBeforeTranslate(IHttpTranslationContext context)
        {
            var timeSinceLast = Time.realtimeSinceStartup - _lastRequestTimestamp;

            if (timeSinceLast < _delay)
            {
                var delay = _delay - timeSinceLast;

                var instruction = CoroutineHelper.CreateWaitForSecondsRealtime(delay);
                if (instruction != null)
                {
                    yield return(instruction);
                }
                else
                {
                    float start = Time.realtimeSinceStartup;
                    var   end   = start + delay;
                    while (Time.realtimeSinceStartup < end)
                    {
                        yield return(null);
                    }
                }
            }

            _lastRequestTimestamp = Time.realtimeSinceStartup;
        }
예제 #2
0
        /// <summary>
        /// Attempt to translated the provided untranslated text. Will be used in a "coroutine",
        /// so it can be implemented in an asynchronous fashion.
        /// </summary>
        public IEnumerator Translate(ITranslationContext context)
        {
            EnsureInitialized();

            while (_initializing && !_failed)
            {
                var instruction = CoroutineHelper.CreateWaitForSecondsRealtime(0.2f);
                if (instruction != null)
                {
                    yield return(instruction);
                }
                else
                {
                    yield return(null);
                }
            }

            if (_failed)
            {
                context.Fail("External process failed.");
            }

            var totalDelay    = (float)(Rng.Next((int)((MaxDelay - MinDelay) * 1000)) + (MinDelay * 1000)) / 1000;
            var timeSinceLast = TimeSupport.Time.realtimeSinceStartup - _lastRequestTimestamp;

            if (timeSinceLast < totalDelay)
            {
                var remainingDelay = totalDelay - timeSinceLast;

                var instruction = CoroutineHelper.CreateWaitForSecondsRealtime(remainingDelay);
                if (instruction != null)
                {
                    yield return(instruction);
                }
                else
                {
                    float start = TimeSupport.Time.realtimeSinceStartup;
                    var   end   = start + remainingDelay;
                    while (TimeSupport.Time.realtimeSinceStartup < end)
                    {
                        yield return(null);
                    }
                }
            }
            _lastRequestTimestamp = TimeSupport.Time.realtimeSinceStartup;

            var result = new ProtocolTransactionHandle();
            var id     = Guid.NewGuid();

            lock ( _sync )
            {
                _transactionHandles[id] = result;
            }

            try
            {
                var request = new TranslationRequest
                {
                    Id                    = id,
                    SourceLanguage        = context.SourceLanguage,
                    DestinationLanguage   = context.DestinationLanguage,
                    UntranslatedTextInfos = context.UntranslatedTextInfos.Select(x => x.ToTransmittable()).ToArray()
                };
                var payload = ExtProtocolConvert.Encode(request);

                _process.StandardInput.WriteLine(payload);
            }
            catch (Exception e)
            {
                result.SetCompleted(null, e.Message, StatusCode.Unknown);
            }

            // yield-wait for completion
            var iterator = result.GetSupportedEnumerator();

            while (iterator.MoveNext())
            {
                yield return(iterator.Current);
            }

            if (!result.Succeeded)
            {
                context.Fail("Error occurred while retrieving translation. " + result.Error);
            }

            context.Complete(result.Results);
        }