Example #1
0
        /// <summary>
        /// Gets the <see cref="ProgressParamInformation"/> object associated with the given progress id.
        /// </summary>
        /// <param name="progressId">The key to obtain the <see cref="ProgressParamInformation"/> object from <see cref="progressMap"/>.</param>
        /// <param name="valueType">Output parameter to store the obtained <see cref="ProgressParamInformation"/> object.</param>
        /// <returns>true if the <see cref="ProgressParamInformation"/> object was found with the specified key; otherwise, false.</returns>
        public bool TryGetProgressObject(object progressId, out ProgressParamInformation valueType)
        {
            lock (this.progressLock)
            {
                if (this.progressMap.TryGetValue(progressId, out ProgressParamInformation progressInfo))
                {
                    valueType = progressInfo;
                    return(true);
                }

                valueType = null;
                return(false);
            }
        }
        /// <summary>
        /// Gets a <see cref="long"/> type token to use as replacement of an <see cref="object"/> implementing <see cref="IProgress{T}"/> in the JSON message.
        /// </summary>
        /// <param name="value">The object which should implement <see cref="IProgress{T}"/>.</param>
        /// <returns>The assigned <see cref="long"/> typed token.</returns>
        public long GetTokenForProgress(object value)
        {
            Requires.NotNull(value, nameof(value));

            if (this.RequestIdBeingSerialized.IsEmpty)
            {
                throw new NotSupportedException(Resources.MarshaledObjectInResponseOrNotificationError);
            }

            lock (this.progressLock)
            {
                // Check whether we're being asked to tokenize a Progress<T> object for a second time (this can happen due to message logging).
                if (this.requestProgressMap.TryGetValue(this.RequestIdBeingSerialized, out ImmutableList <ProgressParamInformation>?progressInfos))
                {
                    foreach (ProgressParamInformation info in progressInfos)
                    {
                        if (info.Contains(value))
                        {
                            return(info.Token);
                        }
                    }
                }
                else
                {
                    progressInfos = ImmutableList <ProgressParamInformation> .Empty;
                }

                long progressToken = this.nextProgressId++;
                var  progressInfo  = new ProgressParamInformation(value, progressToken);

                progressInfos = progressInfos.Add(progressInfo);
                this.requestProgressMap[this.RequestIdBeingSerialized] = progressInfos;

                this.progressMap.Add(progressToken, progressInfo);

                return(progressToken);
            }
        }