Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously submits a <see cref="SharedMemMessage"/> request message to the server
        /// and waits for and returns the response <see cref="SharedMemMessage"/>.
        /// </summary>
        /// <param name="request">The request message.</param>
        /// <param name="timeout">The optional timeout to override the <see cref="Timeout"/> property.
        /// </param>
        /// <returns>The response <see cref="SharedMemMessage"/>.</returns>
        public Task <SharedMemMessage> CallAsync(SharedMemMessage request, TimeSpan?timeout = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (request.InternalRequestId != Guid.Empty)
            {
                throw new InvalidOperationException("Cannot reuse a [SharedMemMessage] request instance previously submitted for a call operation.");
            }

            request.InternalRequestId   = Guid.NewGuid();
            request.InternalClientInbox = this.ClientName;

            if (!timeout.HasValue)
            {
                timeout = this.Timeout;
            }

            var operation = new PendingOperation(request, new TaskCompletionSource <SharedMemMessage>(), stopwatch.Elapsed + timeout.Value);

            lock (syncLock)
            {
                pendingOperations.Add(request.InternalRequestId, operation);
            }

            try
            {
                using (var output = new EnhancedMemoryStream(request.SerializedCapacityHint))
                {
                    output.WriteInt32(request.TypeCode);
                    request.InternalWriteTo(output);
                    request.WriteTo(output);

                    // This call is synchronous but should execute very quickly (microseconds).

                    outbox.Send(ServerName, output.ToArray());
                }
            }
            catch (Exception e)
            {
                lock (syncLock)
                {
                    pendingOperations.Remove(operation.RequestMessage.InternalRequestId);
                }

                operation.Tcs.TrySetException(e);
            }

            return(operation.Tcs.Task);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Asynchronously submits a <see cref="SharedMemMessage"/> request message to the server
 /// and waits for and returns a response of type <typeparamref name="TResponse"/>.
 /// </summary>
 /// <typeparam name="TResponse">The response message type.</typeparam>
 /// <param name="request">The request message.</param>
 /// <param name="timeout">The optional timeout to override the <see cref="Timeout"/> property.
 /// </param>
 /// <returns>The response <see cref="SharedMemMessage"/>.</returns>
 public async Task <TResponse> CallAsync <TResponse>(SharedMemMessage request, TimeSpan?timeout = null)
     where TResponse : SharedMemMessage, new()
 {
     return((TResponse)(await CallAsync(request, timeout)));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="request">The operation's request message.</param>
 /// <param name="tcs">The <see cref="TaskCompletionSource{SharedMemMessage}"/> to be used to complete the operation.</param>
 /// <param name="ttd">
 /// The elapsed time as compared to the client's stopwatch when the
 /// operation should be terminated with a timeout (time-to-die).
 /// </param>
 public PendingOperation(SharedMemMessage request, TaskCompletionSource <SharedMemMessage> tcs, TimeSpan ttd)
 {
     this.RequestMessage = request;
     this.Tcs            = tcs;
     this.TTD            = ttd;
 }