tube v0.1.1 Tube.Frame

Represents a full frame of the WebSocket protocol

Struct

fin

Indicates that this is the final fragment in a message. The first fragment MAY also be the final fragment.

opcode

Defines the interpretation of the “Payload data”. If an unknown opcode is received, the receiving endpoint MUST Fail the WebSocket Connection. The following values are defined.

  • 0x0 denotes a continuation frame
  • 0x1 denotes a text frame
  • 0x2 denotes a binary frame
  • 0x3-7 are reserved for further non-control frames
  • 0x8 denotes a connection close
  • 0x9 denotes a ping
  • 0xA denotes a pong
  • 0xB-F are reserved for further control frames

mask and mask_key

If mask is true, the mask_key will be a 4 byte long key. This will be used to unmask the payload data from client to server.

len

Length of the payload

payload

Binary of the frame’s application data

control_frame?

If true, this frame’s opcode means that this is a control frame.

Control frames can be interleaved into fragmented messages.

Summary

Functions

Applies the mask to the given payload

Parses the given binary into a %Tube.Frame{} struct

Generates a random mask using :crypto.strong_rand_bytes/1 and adds it to the given frame

Converts the Elixir.Tube.Frame struct to a binary

Functions

mask_payload(payload, mask_key)

Specs

mask_payload(binary, binary) :: binary

Applies the mask to the given payload.

This can be done to either mask or unmask the payload.

parse(binary)

Specs

parse(binary) ::
  {:ok, struct, binary} |
  {:error, term}

Parses the given binary into a %Tube.Frame{} struct.

Example

iex(1)> Tube.Frame.parse(<<129, 139, 71, 28, 66, 60, 15, 121, 46, 80, 40, 60, 21, 83, 53, 112, 38>>)
{:ok,
 %Tube.Frame{control_frame?: false, fin: true, len: 11, mask: 1,
  mask_key: <<71, 28, 66, 60>>, opcode: 1, payload: "Hello World"}, ""}

Returns

When parsed with no issues, it will return

{:ok, %Tube.Frame{}, rest}

rest will contain superflous bytes that are not part of the frame and should be kept until more TCP chops arrive.

If there was an error,

{:error, reson}

will be returned

put_mask(struct)

Specs

put_mask(struct) :: struct

Generates a random mask using :crypto.strong_rand_bytes/1 and adds it to the given frame

to_binary(struct)

Specs

to_binary(struct) :: binary

Converts the Elixir.Tube.Frame struct to a binary.

If the given frame has a mask_key, it will apply this key.