1
0
Fork 0
forked from External/grumble

pkg/packetdata: rename from packetdatastream.

This commit is contained in:
Mikkel Krautz 2012-12-09 13:18:24 +01:00
parent 5d9df8383a
commit 3af010d3f5
4 changed files with 66 additions and 73 deletions

View file

@ -16,7 +16,7 @@ import (
"mumbleapp.com/grumble/pkg/blobstore" "mumbleapp.com/grumble/pkg/blobstore"
"mumbleapp.com/grumble/pkg/cryptstate" "mumbleapp.com/grumble/pkg/cryptstate"
"mumbleapp.com/grumble/pkg/mumbleproto" "mumbleapp.com/grumble/pkg/mumbleproto"
"mumbleapp.com/grumble/pkg/packetdatastream" "mumbleapp.com/grumble/pkg/packetdata"
"net" "net"
"runtime" "runtime"
"time" "time"
@ -318,8 +318,8 @@ func (client *Client) udpRecvLoop() {
var counter uint8 var counter uint8
outbuf := make([]byte, 1024) outbuf := make([]byte, 1024)
incoming := packetdatastream.New(buf[1 : 1+(len(buf)-1)]) incoming := packetdata.New(buf[1 : 1+(len(buf)-1)])
outgoing := packetdatastream.New(outbuf[1 : 1+(len(outbuf)-1)]) outgoing := packetdata.New(outbuf[1 : 1+(len(outbuf)-1)])
_ = incoming.GetUint32() _ = incoming.GetUint32()
if kind != mumbleproto.UDPMessageVoiceOpus { if kind != mumbleproto.UDPMessageVoiceOpus {

View file

@ -3,13 +3,13 @@
// The use of this source code is goverened by a BSD-style // The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file. // license that can be found in the LICENSE-file.
package packetdatastream package packetdata
import ( import (
"math" "math"
) )
type PacketDataStream struct { type PacketData struct {
Buf []byte Buf []byte
offset int offset int
maxsize int maxsize int
@ -17,19 +17,19 @@ type PacketDataStream struct {
ok bool ok bool
} }
func New(buf []byte) (pds *PacketDataStream) { func New(buf []byte) (pds *PacketData) {
pds = new(PacketDataStream) pds = new(PacketData)
pds.Buf = buf pds.Buf = buf
pds.maxsize = len(buf) pds.maxsize = len(buf)
pds.ok = true pds.ok = true
return return
} }
func (pds *PacketDataStream) IsValid() bool { func (pds *PacketData) IsValid() bool {
return pds.ok return pds.ok
} }
func (pds *PacketDataStream) Skip(skip int) { func (pds *PacketData) Skip(skip int) {
if pds.Left() >= skip { if pds.Left() >= skip {
pds.offset += skip pds.offset += skip
} else { } else {
@ -39,18 +39,18 @@ func (pds *PacketDataStream) Skip(skip int) {
// Returns number of bytes remaining in // Returns number of bytes remaining in
// the buffer. // the buffer.
func (pds *PacketDataStream) Left() int { func (pds *PacketData) Left() int {
return int(pds.maxsize - pds.offset) return int(pds.maxsize - pds.offset)
} }
// Returns the size of the currently-assembled data // Returns the size of the currently-assembled data
// stream // stream
func (pds *PacketDataStream) Size() int { func (pds *PacketData) Size() int {
return pds.offset return pds.offset
} }
// Get the next byte from the PacketDataStream as a uint64 // Get the next byte from the PacketData as a uint64
func (pds *PacketDataStream) next() (ret uint64) { func (pds *PacketData) next() (ret uint64) {
if pds.offset < pds.maxsize { if pds.offset < pds.maxsize {
ret = uint64(pds.Buf[pds.offset]) ret = uint64(pds.Buf[pds.offset])
pds.offset += 1 pds.offset += 1
@ -61,8 +61,8 @@ func (pds *PacketDataStream) next() (ret uint64) {
return 0 return 0
} }
// Get the next byte from the PacketDataStream as a byte (uint8) // Get the next byte from the PacketData as a byte (uint8)
func (pds *PacketDataStream) Next8() (ret uint8) { func (pds *PacketData) Next8() (ret uint8) {
if pds.offset < pds.maxsize { if pds.offset < pds.maxsize {
ret = uint8(pds.Buf[pds.offset]) ret = uint8(pds.Buf[pds.offset])
pds.offset += 1 pds.offset += 1
@ -74,8 +74,8 @@ func (pds *PacketDataStream) Next8() (ret uint8) {
} }
// Put a byte (represented in an uint64) into the // Put a byte (represented in an uint64) into the
// PacketDataStream. // PacketData.
func (pds *PacketDataStream) append(val uint64) { func (pds *PacketData) append(val uint64) {
if val > 0xff { if val > 0xff {
pds.ok = false pds.ok = false
return return
@ -91,10 +91,10 @@ func (pds *PacketDataStream) append(val uint64) {
} }
// Add a variably-sized integer to the PacketDataStream. // Add a variably-sized integer to the PacketData.
// The PacketDataStream will figure out the most efficient // The PacketData will figure out the most efficient
// encoding based on the binary representation of the value. // encoding based on the binary representation of the value.
func (pds *PacketDataStream) addVarint(val uint64) { func (pds *PacketData) addVarint(val uint64) {
i := val i := val
if (i&0x8000000000000000) != 0 && ^i < 0x100000000 { if (i&0x8000000000000000) != 0 && ^i < 0x100000000 {
@ -140,7 +140,7 @@ func (pds *PacketDataStream) addVarint(val uint64) {
} }
} }
func (pds *PacketDataStream) getVarint() (i uint64) { func (pds *PacketData) getVarint() (i uint64) {
v := pds.next() v := pds.next()
if (v & 0x80) == 0x00 { if (v & 0x80) == 0x00 {
@ -170,89 +170,89 @@ func (pds *PacketDataStream) getVarint() (i uint64) {
return return
} }
// Read a uint64 from the PacketDataStream // Read a uint64 from the PacketData
func (pds *PacketDataStream) GetUint64() uint64 { func (pds *PacketData) GetUint64() uint64 {
return pds.getVarint() return pds.getVarint()
} }
// Write a uint64 to the PacketDataStream // Write a uint64 to the PacketData
func (pds *PacketDataStream) PutUint64(val uint64) { func (pds *PacketData) PutUint64(val uint64) {
pds.addVarint(val) pds.addVarint(val)
} }
// Read a uint32 from the PacketDataStream // Read a uint32 from the PacketData
func (pds *PacketDataStream) GetUint32() uint32 { func (pds *PacketData) GetUint32() uint32 {
return uint32(pds.getVarint()) return uint32(pds.getVarint())
} }
// Write a uint32 to the PacketDataStream // Write a uint32 to the PacketData
func (pds *PacketDataStream) PutUint32(val uint32) { func (pds *PacketData) PutUint32(val uint32) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a uint16 from the PacketDataStream // Read a uint16 from the PacketData
func (pds *PacketDataStream) GetUint16() uint16 { func (pds *PacketData) GetUint16() uint16 {
return uint16(pds.getVarint()) return uint16(pds.getVarint())
} }
// Write a uint16 to the PacketDataStream // Write a uint16 to the PacketData
func (pds *PacketDataStream) PutUint16(val uint16) { func (pds *PacketData) PutUint16(val uint16) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a uint8 from the PacketDataStream // Read a uint8 from the PacketData
func (pds *PacketDataStream) GetUint8() uint8 { func (pds *PacketData) GetUint8() uint8 {
varint := pds.getVarint() varint := pds.getVarint()
return uint8(varint) return uint8(varint)
} }
// Write a uint8 to the PacketDataStream // Write a uint8 to the PacketData
func (pds *PacketDataStream) PutUint8(val uint8) { func (pds *PacketData) PutUint8(val uint8) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a int64 from the PacketDataStream // Read a int64 from the PacketData
func (pds *PacketDataStream) GetInt64() int64 { func (pds *PacketData) GetInt64() int64 {
return int64(pds.getVarint()) return int64(pds.getVarint())
} }
// Write a int64 to the PacketDataStream // Write a int64 to the PacketData
func (pds *PacketDataStream) PutInt64(val int64) { func (pds *PacketData) PutInt64(val int64) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a int32 from the PacketDataStream // Read a int32 from the PacketData
func (pds *PacketDataStream) GetInt32() int32 { func (pds *PacketData) GetInt32() int32 {
return int32(pds.getVarint()) return int32(pds.getVarint())
} }
// Write a int32 to the PacketDataStream // Write a int32 to the PacketData
func (pds *PacketDataStream) PutInt32(val int32) { func (pds *PacketData) PutInt32(val int32) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a int16 from the PacketDataStream // Read a int16 from the PacketData
func (pds *PacketDataStream) GetInt16() int16 { func (pds *PacketData) GetInt16() int16 {
return int16(pds.getVarint()) return int16(pds.getVarint())
} }
// Write a int16 to the PacketDataStream // Write a int16 to the PacketData
func (pds *PacketDataStream) PutInt16(val int16) { func (pds *PacketData) PutInt16(val int16) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a int8 from the PacketDataStream // Read a int8 from the PacketData
func (pds *PacketDataStream) GetInt8() int8 { func (pds *PacketData) GetInt8() int8 {
return int8(pds.getVarint()) return int8(pds.getVarint())
} }
// Write a int8 to the PacketDataStream // Write a int8 to the PacketData
func (pds *PacketDataStream) PutInt8(val int8) { func (pds *PacketData) PutInt8(val int8) {
pds.addVarint(uint64(val)) pds.addVarint(uint64(val))
} }
// Read a float32 from the PacketDataStream // Read a float32 from the PacketData
func (pds *PacketDataStream) GetFloat32() float32 { func (pds *PacketData) GetFloat32() float32 {
if pds.Left() < 4 { if pds.Left() < 4 {
pds.ok = false pds.ok = false
return 0 return 0
@ -264,8 +264,8 @@ func (pds *PacketDataStream) GetFloat32() float32 {
return math.Float32frombits(val) return math.Float32frombits(val)
} }
// Write a float32 to the PacketDataStream // Write a float32 to the PacketData
func (pds *PacketDataStream) PutFloat32(val float32) { func (pds *PacketData) PutFloat32(val float32) {
bits := math.Float32bits(val) bits := math.Float32bits(val)
pds.append(uint64((bits >> 24) & 0xff)) pds.append(uint64((bits >> 24) & 0xff))
pds.append(uint64((bits >> 16) & 0xff)) pds.append(uint64((bits >> 16) & 0xff))
@ -273,8 +273,8 @@ func (pds *PacketDataStream) PutFloat32(val float32) {
pds.append(uint64(bits & 0xff)) pds.append(uint64(bits & 0xff))
} }
// Read a float64 from the PacketDataStream. // Read a float64 from the PacketData.
func (pds *PacketDataStream) GetFloat64() float64 { func (pds *PacketData) GetFloat64() float64 {
if pds.Left() < 8 { if pds.Left() < 8 {
pds.ok = false pds.ok = false
return 0 return 0
@ -286,8 +286,8 @@ func (pds *PacketDataStream) GetFloat64() float64 {
return math.Float64frombits(val) return math.Float64frombits(val)
} }
// Write a float64 to the PacketDataStream // Write a float64 to the PacketData
func (pds *PacketDataStream) PutFloat64(val float64) { func (pds *PacketData) PutFloat64(val float64) {
bits := math.Float64bits(val) bits := math.Float64bits(val)
pds.append((bits >> 56) & 0xff) pds.append((bits >> 56) & 0xff)
pds.append((bits >> 48) & 0xff) pds.append((bits >> 48) & 0xff)
@ -299,8 +299,8 @@ func (pds *PacketDataStream) PutFloat64(val float64) {
pds.append(bits & 0xff) pds.append(bits & 0xff)
} }
// Copy a buffer out of the PacketDataStream into dst. // Copy a buffer out of the PacketData into dst.
func (pds *PacketDataStream) CopyBytes(dst []byte) { func (pds *PacketData) CopyBytes(dst []byte) {
if pds.Left() >= len(dst) { if pds.Left() >= len(dst) {
if copy(dst, pds.Buf[pds.offset:pds.offset+len(dst)]) != len(dst) { if copy(dst, pds.Buf[pds.offset:pds.offset+len(dst)]) != len(dst) {
pds.ok = false pds.ok = false
@ -310,9 +310,9 @@ func (pds *PacketDataStream) CopyBytes(dst []byte) {
} }
} }
// Put a buffer src into the PacketDataStream at the // Put a buffer src into the PacketData at the
// current offset. // current offset.
func (pds *PacketDataStream) PutBytes(src []byte) { func (pds *PacketData) PutBytes(src []byte) {
if pds.Left() >= len(src) { if pds.Left() >= len(src) {
if copy(pds.Buf[pds.offset:pds.offset+len(src)], src) != len(src) { if copy(pds.Buf[pds.offset:pds.offset+len(src)], src) != len(src) {
pds.ok = false pds.ok = false

View file

@ -1,4 +1,4 @@
package packetdatastream package packetdata
import ( import (
"crypto/rand" "crypto/rand"

View file

@ -1,7 +0,0 @@
include $(GOROOT)/src/Make.inc
TARG = packetdatastream
GOFILES = \
packetdatastream.go
include $(GOROOT)/src/Make.pkg