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()); }
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 createStreamDto = new ClientMessageDto.CreateStream( Guid.Empty, eventStreamId, Encoding.UTF8.GetBytes(metadata ?? string.Format("{{\"StreamName\": \"{0}\"}}", eventStreamId))); var package = new TcpPackage(TcpCommand.CreateStream, 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; }