Skip to content

Commit 010e1cf

Browse files
committed
TCP - split decoder API to progress and buffer.
This may be more intuituve to understand.
1 parent af8ed1f commit 010e1cf

File tree

8 files changed

+23
-21
lines changed

8 files changed

+23
-21
lines changed

src/plugins/input/tcp/src/Connection.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ bool Connection::receive(ipx_ctx_t *ctx) {
105105
);
106106
}
107107

108-
auto &buffer = m_decoder->decode();
108+
m_decoder->progress();
109+
auto &buffer = m_decoder->buffer();
109110
buffer.process_decoded([=](ByteVector &&msg) { send_msg(ctx, std::move(msg)); });
110111
return !buffer.is_eof_reached();
111112
}

src/plugins/input/tcp/src/Decoder.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ namespace tcp_in {
1717
/** Interface for IPFIX message decoders. */
1818
class Decoder {
1919
public:
20+
/** Reads all available data from TCP stream and returns buffer with decoded messages. */
21+
virtual void progress() = 0;
22+
2023
/**
21-
* @brief Reads all available data from TCP stream and returns buffer with decoded messages.
22-
* @returns Buffer with decoded messages.
24+
* @brief Retrieves decode buffer with decoded data.
25+
* @return Buffer with decoded data.
2326
*/
24-
virtual DecodeBuffer &decode() = 0;
27+
virtual DecodeBuffer &buffer() = 0;
2528

2629
/** Gets the name of the decoder. */
2730
virtual const char *get_name() const = 0;

src/plugins/input/tcp/src/IpfixDecoder.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace tcp_in {
2626

27-
DecodeBuffer &IpfixDecoder::decode() {
27+
void IpfixDecoder::progress() {
2828
while (!m_decoded.enough_data()) {
2929
// Read the header of the message.
3030
if (!read_header()) {
@@ -42,8 +42,6 @@ DecodeBuffer &IpfixDecoder::decode() {
4242
if (m_decoded.is_eof_reached() && m_part_readed.size() != 0) {
4343
throw std::runtime_error("Received incomplete message.");
4444
}
45-
46-
return m_decoded;
4745
}
4846

4947
bool IpfixDecoder::read_header() {

src/plugins/input/tcp/src/IpfixDecoder.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class IpfixDecoder : public Decoder {
3131
*/
3232
IpfixDecoder(int fd) : m_fd(fd), m_decoded(), m_part_readed(), m_msg_size(0) {}
3333

34-
virtual DecodeBuffer &decode() override;
34+
virtual void progress() override;
35+
36+
virtual DecodeBuffer &buffer() override { return m_decoded; };
3537

3638
virtual const char *get_name() const override {
3739
return "IPFIX";

src/plugins/input/tcp/src/Lz4Decoder.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Lz4Decoder::Lz4Decoder(int fd) :
5252
}
5353
}
5454

55-
DecodeBuffer &Lz4Decoder::decode() {
55+
void Lz4Decoder::progress() {
5656
while (!m_decoded.enough_data()) {
5757
// Read the header.
5858
if (!read_header()) {
@@ -73,8 +73,6 @@ DecodeBuffer &Lz4Decoder::decode() {
7373
if (m_decoded.is_eof_reached() && m_compressed.size() != 0) {
7474
throw std::runtime_error("Incomplete compressed message received");
7575
}
76-
77-
return m_decoded;
7876
}
7977

8078
bool Lz4Decoder::read_header() {

src/plugins/input/tcp/src/Lz4Decoder.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ class Lz4Decoder : public Decoder {
4343

4444
/**
4545
* @brief Decodes the next blocks of data until there is no data or when enough data is readed
46-
* @return decoded data
4746
* @throws when fails to read from file descriptor
4847
* @throws when fails to decompress the data
4948
*/
50-
virtual DecodeBuffer &decode() override;
49+
virtual void progress() override;
50+
51+
virtual DecodeBuffer &buffer() override { return m_decoded; }
5152

5253
virtual const char *get_name() const override {
5354
return "LZ4";

src/plugins/input/tcp/src/tls/TlsDecoder.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ TlsDecoder::TlsDecoder(SslCtx &ctx, int fd) :
2727
m_handshake_complete = m_ssl.accept();
2828
}
2929

30-
DecodeBuffer &TlsDecoder::decode() {
30+
void TlsDecoder::progress() {
3131
if (!m_handshake_complete) {
3232
m_handshake_complete = m_ssl.accept();
3333
if (!m_handshake_complete) {
34-
return m_decoded;
34+
return;
3535
}
3636
}
3737

@@ -45,18 +45,16 @@ DecodeBuffer &TlsDecoder::decode() {
4545
case ReadResult::READ:
4646
break;
4747
case ReadResult::WAIT:
48-
return m_decoded;
48+
return;
4949
case ReadResult::FINISHED:
5050
// FIXME: properly check that the shutdown is complete.
5151
m_ssl.shutdown();
5252
// Intentional falltrough
5353
case ReadResult::CLOSED:
5454
m_decoded.signal_eof();
55-
return m_decoded;
55+
return;
5656
}
5757
}
58-
59-
return m_decoded;
6058
}
6159

6260
} // namespace tls

src/plugins/input/tcp/src/tls/TlsDecoder.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ class TlsDecoder : public Decoder {
3636

3737
/**
3838
* @brief Reads all available data from TLS stream and returns buffer with decoded messages.
39-
* @returns Buffer with decoded messages.
4039
*/
41-
virtual DecodeBuffer &decode() override;
40+
virtual void progress() override;
41+
42+
virtual DecodeBuffer &buffer() override { return m_decoded; }
4243

4344
/**
4445
* @brief Gets the name of the decoder.

0 commit comments

Comments
 (0)