Skip to content

Commit

Permalink
pkt-line: introduce struct packet_writer
Browse files Browse the repository at this point in the history
A future patch will allow the client to request multiplexing of the
entire fetch response (and not only during packfile transmission), which
in turn allows the server to send progress and keepalive messages at any
time during the response.

It will be convenient for a future patch if writing options
(specifically, whether the written data is to be multiplexed) could be
controlled from a single place, so create struct packet_writer to serve
as that place, and modify upload-pack to use it.

Currently, it only stores the output fd, but a subsequent patch will (as
described above) introduce an option to determine if the written data is
to be multiplexed.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
jonathantanmy authored and gitster committed Jan 15, 2019
1 parent 17069c7 commit bc2e795
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 58 deletions.
47 changes: 41 additions & 6 deletions pkt-line.c
Expand Up @@ -129,12 +129,14 @@ static void set_packet_header(char *buf, const int size)
#undef hex
}

static void format_packet(struct strbuf *out, const char *fmt, va_list args)
static void format_packet(struct strbuf *out, const char *prefix,
const char *fmt, va_list args)
{
size_t orig_len, n;

orig_len = out->len;
strbuf_addstr(out, "0000");
strbuf_addstr(out, prefix);
strbuf_vaddf(out, fmt, args);
n = out->len - orig_len;

Expand All @@ -145,13 +147,13 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)
packet_trace(out->buf + orig_len + 4, n - 4, 1);
}

static int packet_write_fmt_1(int fd, int gently,
static int packet_write_fmt_1(int fd, int gently, const char *prefix,
const char *fmt, va_list args)
{
static struct strbuf buf = STRBUF_INIT;

strbuf_reset(&buf);
format_packet(&buf, fmt, args);
format_packet(&buf, prefix, fmt, args);
if (write_in_full(fd, buf.buf, buf.len) < 0) {
if (!gently) {
check_pipe(errno);
Expand All @@ -168,7 +170,7 @@ void packet_write_fmt(int fd, const char *fmt, ...)
va_list args;

va_start(args, fmt);
packet_write_fmt_1(fd, 0, fmt, args);
packet_write_fmt_1(fd, 0, "", fmt, args);
va_end(args);
}

Expand All @@ -178,7 +180,7 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
va_list args;

va_start(args, fmt);
status = packet_write_fmt_1(fd, 1, fmt, args);
status = packet_write_fmt_1(fd, 1, "", fmt, args);
va_end(args);
return status;
}
Expand Down Expand Up @@ -211,7 +213,7 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
va_list args;

va_start(args, fmt);
format_packet(buf, fmt, args);
format_packet(buf, "", fmt, args);
va_end(args);
}

Expand Down Expand Up @@ -486,3 +488,36 @@ enum packet_read_status packet_reader_peek(struct packet_reader *reader)
reader->line_peeked = 1;
return reader->status;
}

void packet_writer_init(struct packet_writer *writer, int dest_fd)
{
writer->dest_fd = dest_fd;
}

void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
packet_write_fmt_1(writer->dest_fd, 0, "", fmt, args);
va_end(args);
}

void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
packet_write_fmt_1(writer->dest_fd, 0, "ERR ", fmt, args);
va_end(args);
}

void packet_writer_delim(struct packet_writer *writer)
{
packet_delim(writer->dest_fd);
}

void packet_writer_flush(struct packet_writer *writer)
{
packet_flush(writer->dest_fd);
}
14 changes: 14 additions & 0 deletions pkt-line.h
Expand Up @@ -183,4 +183,18 @@ extern enum packet_read_status packet_reader_peek(struct packet_reader *reader);
#define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - 4)
extern char packet_buffer[LARGE_PACKET_MAX];

struct packet_writer {
int dest_fd;
};

void packet_writer_init(struct packet_writer *writer, int dest_fd);

/* These functions die upon failure. */
__attribute__((format (printf, 2, 3)))
void packet_writer_write(struct packet_writer *writer, const char *fmt, ...);
__attribute__((format (printf, 2, 3)))
void packet_writer_error(struct packet_writer *writer, const char *fmt, ...);
void packet_writer_delim(struct packet_writer *writer);
void packet_writer_flush(struct packet_writer *writer);

#endif

0 comments on commit bc2e795

Please sign in to comment.