Beispiel #1
0
        public void CancelStream <T>(StreamItemContainer <T> container)
        {
            Messages.Message message = new Messages.Message {
                type         = Messages.MessageTypes.CancelInvocation,
                invocationId = container.id.ToString()
            };

            container.IsCanceled = true;

            SendMessage(message);
        }
        internal UploadItemController <StreamItemContainer <TResult> > UploadStreamWithDownStream <TResult>(string target, int paramCount)
        {
            Future <StreamItemContainer <TResult> > future = new Future <StreamItemContainer <TResult> >();

            Action <Message> callback = (Message message) => {
                switch (message.type)
                {
                // StreamItem message contains only one item.
                case MessageTypes.StreamItem:
                {
                    StreamItemContainer <TResult> container = future.value;

                    if (container.IsCanceled)
                    {
                        break;
                    }

                    container.AddItem((TResult)this.Protocol.ConvertTo(typeof(TResult), message.item));

                    // (re)assign the container to raise OnItem event
                    future.AssignItem(container);
                    break;
                }

                case MessageTypes.Completion:
                {
                    bool isSuccess = string.IsNullOrEmpty(message.error);
                    if (isSuccess)
                    {
                        StreamItemContainer <TResult> container = future.value;

                        // While completion message must not contain any result, this should be future-proof
                        if (!container.IsCanceled && message.result != null)
                        {
                            TResult result = (TResult)this.Protocol.ConvertTo(typeof(TResult), message.result);

                            container.AddItem(result);
                        }

                        future.Assign(container);
                    }
                    else
                    {
                        future.Fail(new Exception(message.error));
                    }
                    break;
                }
                }
            };

            long invocationId = System.Threading.Interlocked.Increment(ref this.lastInvocationId);

            int[] streamIds = new int[paramCount];
            for (int i = 0; i < paramCount; i++)
            {
                streamIds[i] = System.Threading.Interlocked.Increment(ref this.lastStreamId);
            }

            var controller = new UploadItemController <StreamItemContainer <TResult> >(this, invocationId, streamIds, future);

            var messageToSend = new Message
            {
                type         = MessageTypes.StreamInvocation,
                invocationId = invocationId.ToString(),
                target       = target,
                arguments    = new object[0],
                streamIds    = streamIds,
                nonblocking  = false,
            };

            SendMessage(messageToSend);

            this.invocations.Add(invocationId, callback);

            future.BeginProcess(new StreamItemContainer <TResult>(invocationId));
            return(controller);
        }