示例#1
0
        public Task <CreateStreamResult> CreateStreamAsync(string stream, byte[] metadata)
        {
            var correlationId = Guid.NewGuid();

            var dto = new ClientMessageDto.CreateStream(correlationId,
                                                        stream,
                                                        metadata);

            var package = new TcpPackage(TcpCommand.CreateStream, correlationId, dto.Serialize());
            var taskCompletionSource = new TaskCompletionSource <CreateStreamResult>();
            var taskWrapper          = new CreateStreamCompletionWrapper(taskCompletionSource);

            RegisterHandler(correlationId, taskWrapper);
            EnqueueForSend(package);

            return(taskCompletionSource.Task);
        }
        private static TcpPackage WrapCreateStream(ClientMessage.CreateStream msg)
        {
            var dto = new ClientMessageDto.CreateStream(msg.CorrelationId, msg.EventStreamId, msg.Metadata);

            return(new TcpPackage(TcpCommand.CreateStream, msg.CorrelationId, dto.Serialize()));
        }
示例#3
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var    eventStreamId = "test-stream";
            string metadata      = null;

            if (args.Length > 0)
            {
                if (args.Length > 2)
                {
                    return(false);
                }
                eventStreamId = args[0];
                if (args.Length > 1)
                {
                    metadata = args[1];
                }
            }

            context.IsAsync();
            var corrid          = Guid.NewGuid();
            var createStreamDto = new ClientMessageDto.CreateStream(
                corrid,
                eventStreamId,
                Encoding.UTF8.GetBytes(metadata ?? string.Format("{{\"StreamName\": \"{0}\"}}", eventStreamId)));
            var package = new TcpPackage(TcpCommand.CreateStream, corrid, createStreamDto.Serialize());

            var sw = new Stopwatch();

            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
            {
                context.Log.Info("[{0}]: Trying to create stream '{1}'...", conn.EffectiveEndPoint, eventStreamId);
                sw.Start();
                conn.EnqueueSend(package.AsByteArray());
            },
                handlePackage: (conn, pkg) =>
            {
                if (pkg.Command != TcpCommand.CreateStreamCompleted)
                {
                    context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                    return;
                }

                sw.Stop();

                var dto = pkg.Data.Deserialize <ClientMessageDto.CreateStreamCompleted>();
                if ((OperationErrorCode)dto.ErrorCode == OperationErrorCode.Success)
                {
                    context.Log.Info("Successfully created stream '{0}'.", dto.EventStreamId);
                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds);
                }
                else
                {
                    context.Log.Info("Error while creating stream {0}: {1} ({2}).",
                                     eventStreamId,
                                     dto.Error,
                                     (OperationErrorCode)dto.ErrorCode);
                }

                context.Log.Info("Create stream request took: {0}.", sw.Elapsed);
                conn.Close();
                context.Success();
            },
                connectionClosed: (connection, error) =>
            {
                if (error == SocketError.Success)
                {
                    context.Success();
                }
                else
                {
                    context.Fail();
                }
            });

            context.WaitForCompletion();
            return(true);
        }