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@xxxxxxxxxx> --- pkt-line.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkt-line.h | 20 ++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/pkt-line.c b/pkt-line.c index ac619f05b..518109bbe 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -406,3 +406,64 @@ 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) +{ + 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 = PACKET_READ_CHOMP_NEWLINE | PACKET_READ_GENTLE_ON_EOF; + + reader->line = NULL; + reader->line_peeked = 0; + reader->pktlen = 0; + reader->status = PACKET_READ_ERROR; +} + +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); + + switch (reader->status) { + case PACKET_READ_ERROR: + reader->pktlen = -1; + reader->line = NULL; + break; + case PACKET_READ_NORMAL: + reader->line = reader->buffer; + break; + case PACKET_READ_FLUSH: + reader->pktlen = 0; + reader->line = NULL; + break; + } + + 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; +} diff --git a/pkt-line.h b/pkt-line.h index f1545929b..2b5c7cf11 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -104,6 +104,26 @@ char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size); */ ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out); +struct packet_reader { + int fd; + char *src_buffer; + size_t src_len; + + char *buffer; + unsigned buffer_size; + int options; + + enum packet_read_status status; + int pktlen; + const char *line; + int line_peeked; +}; + +extern void packet_reader_init(struct packet_reader *reader, int fd, + char *src_buffer, size_t src_len); +extern enum packet_read_status packet_reader_read(struct packet_reader *reader); +extern enum packet_read_status packet_reader_peek(struct packet_reader *reader); + #define DEFAULT_PACKET_MAX 1000 #define LARGE_PACKET_MAX 65520 #define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - 4) -- 2.15.1.424.g9478a66081-goog