Skip to content

Commit bc2e795

Browse files
jonathantanmygitster
authored andcommitted
pkt-line: introduce struct packet_writer
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 17069c7 commit bc2e795

File tree

3 files changed

+115
-58
lines changed

3 files changed

+115
-58
lines changed

pkt-line.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,14 @@ static void set_packet_header(char *buf, const int size)
129129
#undef hex
130130
}
131131

132-
static void format_packet(struct strbuf *out, const char *fmt, va_list args)
132+
static void format_packet(struct strbuf *out, const char *prefix,
133+
const char *fmt, va_list args)
133134
{
134135
size_t orig_len, n;
135136

136137
orig_len = out->len;
137138
strbuf_addstr(out, "0000");
139+
strbuf_addstr(out, prefix);
138140
strbuf_vaddf(out, fmt, args);
139141
n = out->len - orig_len;
140142

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

148-
static int packet_write_fmt_1(int fd, int gently,
150+
static int packet_write_fmt_1(int fd, int gently, const char *prefix,
149151
const char *fmt, va_list args)
150152
{
151153
static struct strbuf buf = STRBUF_INIT;
152154

153155
strbuf_reset(&buf);
154-
format_packet(&buf, fmt, args);
156+
format_packet(&buf, prefix, fmt, args);
155157
if (write_in_full(fd, buf.buf, buf.len) < 0) {
156158
if (!gently) {
157159
check_pipe(errno);
@@ -168,7 +170,7 @@ void packet_write_fmt(int fd, const char *fmt, ...)
168170
va_list args;
169171

170172
va_start(args, fmt);
171-
packet_write_fmt_1(fd, 0, fmt, args);
173+
packet_write_fmt_1(fd, 0, "", fmt, args);
172174
va_end(args);
173175
}
174176

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

180182
va_start(args, fmt);
181-
status = packet_write_fmt_1(fd, 1, fmt, args);
183+
status = packet_write_fmt_1(fd, 1, "", fmt, args);
182184
va_end(args);
183185
return status;
184186
}
@@ -211,7 +213,7 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
211213
va_list args;
212214

213215
va_start(args, fmt);
214-
format_packet(buf, fmt, args);
216+
format_packet(buf, "", fmt, args);
215217
va_end(args);
216218
}
217219

@@ -486,3 +488,36 @@ enum packet_read_status packet_reader_peek(struct packet_reader *reader)
486488
reader->line_peeked = 1;
487489
return reader->status;
488490
}
491+
492+
void packet_writer_init(struct packet_writer *writer, int dest_fd)
493+
{
494+
writer->dest_fd = dest_fd;
495+
}
496+
497+
void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
498+
{
499+
va_list args;
500+
501+
va_start(args, fmt);
502+
packet_write_fmt_1(writer->dest_fd, 0, "", fmt, args);
503+
va_end(args);
504+
}
505+
506+
void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
507+
{
508+
va_list args;
509+
510+
va_start(args, fmt);
511+
packet_write_fmt_1(writer->dest_fd, 0, "ERR ", fmt, args);
512+
va_end(args);
513+
}
514+
515+
void packet_writer_delim(struct packet_writer *writer)
516+
{
517+
packet_delim(writer->dest_fd);
518+
}
519+
520+
void packet_writer_flush(struct packet_writer *writer)
521+
{
522+
packet_flush(writer->dest_fd);
523+
}

pkt-line.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,18 @@ extern enum packet_read_status packet_reader_peek(struct packet_reader *reader);
183183
#define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - 4)
184184
extern char packet_buffer[LARGE_PACKET_MAX];
185185

186+
struct packet_writer {
187+
int dest_fd;
188+
};
189+
190+
void packet_writer_init(struct packet_writer *writer, int dest_fd);
191+
192+
/* These functions die upon failure. */
193+
__attribute__((format (printf, 2, 3)))
194+
void packet_writer_write(struct packet_writer *writer, const char *fmt, ...);
195+
__attribute__((format (printf, 2, 3)))
196+
void packet_writer_error(struct packet_writer *writer, const char *fmt, ...);
197+
void packet_writer_delim(struct packet_writer *writer);
198+
void packet_writer_flush(struct packet_writer *writer);
199+
186200
#endif

0 commit comments

Comments
 (0)