bitstream

package module
v0.0.0-...-3559d57 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2013 License: MIT Imports: 3 Imported by: 0

README

BitStream

A simple library for sending byte data across the wire.

Install

go get github.com/boj/bitstream

Usage

View the docs at godoc.org.

Author

Brian 'bojo' Jones mojobojo@gmail.com

Documentation

Overview

Package bitstream is a simple library for sending byte data across the wire.

Bitstream messages are built using the following guidelines for primitive types:

[total message length]  - 4 bytes
  [byte][value]         - 1 byte + 1 byte
  [int32][value]        - 1 byte + 4 bytes
  [float32][value]      - 1 byte + 4 bytes
  [bool (byte)][value]  - 1 byte + 1 byte
  [string][size][value] - 1 byte + 4 bytes + N bytes
  ...

Message reading and writing is completely user driven.

The user defined network loop handles reading the first 4 bytes to determine the message size. The following data should then be passed to the instantiated Read() method.

Example:

// Sender
bs := NewBitStream()
bs.WriteByte(messageType)
bs.WriteInt(objectId)
bs.WriteFloat(position.x)
bs.WriteFloat(position.y)
bs.WriteFloat(position.z)
msg := bs.Write()

// Network loop reads first 4 bytes, passes rest of data to MessageHandler()

// Receiver
func MessageHandler(msg []byte) error {
  bs := NewBitStream()
  if err := bs.Read(msg); err != nil {
    return err
  } else {
    if msgType, err := bs.ReadByte(); err != nil {
      return err
    } else {
      switch msgType {
        case ENTITY_MOVED:
          // read id
          id, err := bs.ReadInt()
          if err != nil {
            return err
          }
          e := entity[id]
          // read position
          x, err := bs.ReadFloat()
          if err != nil {
            return err
          }
          y, err := bs.ReadFloat()
          if err != nil {
            return err
          }
          z, err := bs.ReadFloat()
          if err != nil {
            return err
          }
          e.SetPosition(x, y, z)
          break
      }
    }
  }
}

This library assumes the reciever understands the order the message parts are received in.

Index

Constants

View Source
const (
	TYPE_BYTE   byte = 1 // byte
	TYPE_INT    byte = 2 // int32
	TYPE_FLOAT  byte = 3 // float32
	TYPE_BOOL   byte = 4 // byte
	TYPE_STRING byte = 5 // len(string) + string
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BitStream

type BitStream struct {
	// contains filtered or unexported fields
}

func NewBitStream

func NewBitStream() *BitStream

NewBitStream creates a new bitstream.

func (*BitStream) Length

func (self *BitStream) Length() int

Length returns the length of the byte data contained in the bitstream. This does not include the first 4 bytes read by the network loop.

func (*BitStream) Read

func (self *BitStream) Read(b []byte) error

Read takes in a stream of bytes read in from the network, not including the initial 4 bytes read by the network loop. The library can then parse and return the results.

func (*BitStream) ReadBool

func (self *BitStream) ReadBool() (bool, error)

ReadBool returns a bool.

func (*BitStream) ReadByte

func (self *BitStream) ReadByte() (byte, error)

ReadByte returns a byte.

func (*BitStream) ReadFloat

func (self *BitStream) ReadFloat() (float32, error)

ReadFloat returns a float32.

func (*BitStream) ReadInt

func (self *BitStream) ReadInt() (int32, error)

ReadInt returns an int32.

func (*BitStream) ReadString

func (self *BitStream) ReadString() (string, error)

ReadString returns a string.

func (*BitStream) Reset

func (self *BitStream) Reset()

Reset resets the state of the bitstream to nothing.

func (*BitStream) Write

func (self *BitStream) Write() []byte

Write returns the bitstream data as a byte array.

func (*BitStream) WriteBool

func (self *BitStream) WriteBool(b bool)

WriteBool writes a bool to the bitstream.

func (*BitStream) WriteByte

func (self *BitStream) WriteByte(b byte)

WriteByte writes a byte to the bitstream.

func (*BitStream) WriteFloat

func (self *BitStream) WriteFloat(f float32)

WriteFloat writes a float32 to the bitstream.

func (*BitStream) WriteInt

func (self *BitStream) WriteInt(i int32)

WriteInt writes a in32 to the bitstream.

func (*BitStream) WriteString

func (self *BitStream) WriteString(s string)

WriteString writes a string to the bitstream.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL