diff options
author | Brandon Williams <bmwill@google.com> | 2018-03-14 11:31:39 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2018-03-14 14:15:06 -0700 |
commit | 77dabc14c497d2498b814967bf21a3ed1618207f (patch) | |
tree | cdf228f08b4093861cd8658f7691f23376e29d5f /pkt-line.c | |
parent | 2153d478b74cfef58ee49ee0305cccf5e8a77b4f (diff) | |
download | git-77dabc14c497d2498b814967bf21a3ed1618207f.tar.gz |
pkt-line: allow peeking a packet line without consuming it
Sometimes it is advantageous to be able to peek the next packet line
without consuming it (e.g. to be able to determine the protocol version
a server is speaking). In order to do that introduce 'struct
packet_reader' which is an abstraction around the normal packet reading
logic. This enables a caller to be able to peek a single line at a time
using 'packet_reader_peek()' and having a caller consume a line by
calling 'packet_reader_read()'.
Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'pkt-line.c')
-rw-r--r-- | pkt-line.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/pkt-line.c b/pkt-line.c index db2fb29ac3..1881dc8813 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -400,3 +400,53 @@ ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out) } return sb_out->len - orig_len; } + +/* Packet Reader Functions */ +void packet_reader_init(struct packet_reader *reader, int fd, + char *src_buffer, size_t src_len, + int options) +{ + memset(reader, 0, sizeof(*reader)); + + reader->fd = fd; + reader->src_buffer = src_buffer; + reader->src_len = src_len; + reader->buffer = packet_buffer; + reader->buffer_size = sizeof(packet_buffer); + reader->options = options; +} + +enum packet_read_status packet_reader_read(struct packet_reader *reader) +{ + if (reader->line_peeked) { + reader->line_peeked = 0; + return reader->status; + } + + reader->status = packet_read_with_status(reader->fd, + &reader->src_buffer, + &reader->src_len, + reader->buffer, + reader->buffer_size, + &reader->pktlen, + reader->options); + + if (reader->status == PACKET_READ_NORMAL) + reader->line = reader->buffer; + else + reader->line = NULL; + + return reader->status; +} + +enum packet_read_status packet_reader_peek(struct packet_reader *reader) +{ + /* Only allow peeking a single line */ + if (reader->line_peeked) + return reader->status; + + /* Peek a line by reading it and setting peeked flag */ + packet_reader_read(reader); + reader->line_peeked = 1; + return reader->status; +} |