Git Product home page Git Product logo

packet_decoder's Introduction

This repository contains a simple packet decoder.

It takes a stream of bytes, decodes the bytes into valid packets, and calls some callback function each time it successfully decodes a packet.

Execution

The example.cpp file contains a few tests using the library.

To run the tests:

g++ -std=c++11 example.cpp pkt_decoder.cpp -o example.o

./example.o

Alternatively, there is a make file. To use the make file, build with make target and run with make run.

Packet Decoder

The packet decoder interface can be found in pkt_decoder.h. The implementation is in pkt_decoder.cpp.

The packet decoder stores the necessary state for processing streams of packets in a pkt_decoder struct.

There are three main functions for interacting with the packet decoder:

  1. pkt_decoder_create :

    • Creates a new packet decoder.
    • Arguments:
      • callback - a function that is called each time a complete packet is decoded
      • callback_ctx - a pointer to some context that will be passed to callback
  2. pkt_decoder_destroy

    • Cleans up memory used by the decoder.
    • Arguments:
      • decoder - decoder to destruct
  3. pkt_decoder_write_bytes

    • Decodes packets from a byte stream. The function can be called repeatedly to process multiple streams.
    • Arguments:
      • decoder - the packet decoder currently being used
      • len - length of the byte stream to be decoded
      • data - pointer to the byte stream to be decoded
    • Notes:
      • Silently drops invalid packets.

Packets

Packets are expected to come in a stream of bytes.

Packets have the following constraints to be considered valid:

  • Packets must start with 0x02
  • Packets must end with 0x03
  • Packets must be no longer than 512 bytes, including the starting and ending byte.
  • The 0x10 byte is treated as an escape character. The byte that follows it is in the format 0x20 | value where value is the actual byte value that we want to add to our decoded packet. This byte value will be decoded with the operation value & ~0x20 and added to the decoded packet.

Invalid packets are discarded. The packet decoder will continue processing additional bytes in the stream after discarding an invalid packet.

packet_decoder's People

Contributors

maccum avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.