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
- type BitStream
- func (self *BitStream) Length() int
- func (self *BitStream) Read(b []byte) error
- func (self *BitStream) ReadBool() (bool, error)
- func (self *BitStream) ReadByte() (byte, error)
- func (self *BitStream) ReadFloat() (float32, error)
- func (self *BitStream) ReadInt() (int32, error)
- func (self *BitStream) ReadString() (string, error)
- func (self *BitStream) Reset()
- func (self *BitStream) Write() []byte
- func (self *BitStream) WriteBool(b bool)
- func (self *BitStream) WriteByte(b byte)
- func (self *BitStream) WriteFloat(f float32)
- func (self *BitStream) WriteInt(i int32)
- func (self *BitStream) WriteString(s string)
Constants ¶
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BitStream ¶
type BitStream struct {
// contains filtered or unexported fields
}
func (*BitStream) Length ¶
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 ¶
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) ReadString ¶
ReadString returns a string.
func (*BitStream) Reset ¶
func (self *BitStream) Reset()
Reset resets the state of the bitstream to nothing.
func (*BitStream) WriteFloat ¶
WriteFloat writes a float32 to the bitstream.
func (*BitStream) WriteString ¶
WriteString writes a string to the bitstream.