Skip to content

ElesiqArt/websocket

Repository files navigation

Websocket++

A small C++ 11 header-only library under MIT license to handle websockets as per RFC6455.

Reference

All code is scoped in namespace websocket.

Frame

Hold control and information data as well as the size and the mask of the payload.

//Defined in <websocket/frame.hpp>

struct frame_t
{
  bool is_final() const;
  bool has_rsv1() const;
  bool has_rsv2() const;
  bool has_rsv3() const;
  opcode opcode_() const;
  bool is_masked() const;
  uint64_t length() const;
  uint32_t payload_mask() const;

  void is_final(bool value);
  void has_rsv1(bool value);
  void has_rsv2(bool value);
  void has_rsv3(bool value);
  void opcode_(opcode value);
  void is_masked(bool value);
  bool length(uint64_t value);
  void payload_mask(uint32_t value);

  size::type size_type() const;
  std::size_t header_size() const;
  std::size_t total_size() const;

  static constexpr std::size_t max_header_size;

  uint8_t control;
  uint8_t info;
  uint64_t size;
  uint32_t mask;
};
  • The bool length(uint64_t value) function returns true if the size does not exceed the maximum allowed value.

Opcodes

Type of frame and associated behaviour.

//Declared in <websocket/opcode.hpp>

enum class opcode
{
  continuation,
  text,
  binary,
  close,
  ping,
  pong
};

const char * opcode_name(opcode value);

template<typename T>
bool is_valid_opcode(T value);

bool is_control(opcode value);
bool is_non_control(opcode value);

bool is_data(opcode value);

Parser and writer

//Defined in <websocket/parser.hpp> and <websocket/writer.hpp>

const uint8_t * parse(const uint8_t * in, frame_t & frame);

uint8_t * write_forward(frame_t frame, uint8_t * out);
uint8_t * write_backward(frame_t frame, uint8_t * out);

Codec

Mask or unmask a payload buffer (can be done in place).

//Defined in <websocket/codec.hpp>

void xcode(const uint8_t * in, uint64_t size, uint32_t mask, uint8_t * out);
void xcode(const uint8_t * in, uint64_t size, const uint8_t mask[4], uint8_t * out);

void xcode(uint8_t * in, uint64_t size, uint32_t mask);
void xcode(uint8_t * in, uint64_t size, const uint8_t mask[4]);

Status code

Closing status and reason.

namespace status_code
{
  typedef uint16_t type;

  bool is_unused(type code);
  bool is_protocol(type code);
  bool is_registered(type code);
  bool is_unregistered(type code);

  constexpr type any;

  constexpr type normal;
  constexpr type going_away;
  constexpr type protocol_error;
  constexpr type invalid_data;
  constexpr type reserved;
  constexpr type none; // do not send in a Close control frame
  constexpr type abnormal; // do not send in a Close control frame
  constexpr type inconsistent;
  constexpr type policy_violation;
  constexpr type too_big;
  constexpr type extension_negociation_failure;
  constexpr type unexpected_condition;
  constexpr type tls_handshake_failed; // do not send in a Close control frame

  struct rfc6455_t
  {
    static const type max;
    static const std::size_t max_reason_length;
    static const char * reason(type code);
  };
};

Printer

//Defined in <websocket/printer.hpp>

std::ostream & print_header(std::ostream & os, const frame_t & frame);

std::ostream & print(std::ostream & os, const frame_t & frame, const char * payload, bool encoded);

Key

Convenient variables to build the handshake response header.

//Declared in <websocket/key.hpp>

constexpr char guid[];

constexpr std::size_t key_size;
constexpr std::size_t concatenated_key_guid_size;
constexpr std::size_t header_field_value_size;

Common

General types, masks and constants.

//Declared in <websocket/helper.hpp>

namespace mask
{
  constexpr uint8_t fin;
  constexpr uint8_t rsv1;
  constexpr uint8_t rsv2;
  constexpr uint8_t rsv3;
  constexpr uint8_t opcode;

  constexpr uint8_t masked;
  constexpr uint8_t length;
};

namespace size
{
  typedef uint8_t type;

  namespace medium
  {
    constexpr type delimiter;
  };

  namespace large
  {
    constexpr type delimiter;
  };

  namespace max
  {
    constexpr uint64_t small;
    constexpr uint64_t medium;
    constexpr uint64_t large;
  };
};

Tests

Run make test to compile and make run-test to execute, or simply make.

Dependencies

To change the path of this dependency, create a config.mk file and then assign the CATCH variable with the appropriate location (. is used by default).

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors