網路通訊中通常需要用到byte的通用操作方式
功能大概 寫入int16,int32,int64,byte 讀取int16,int32,int64,byte
我這裡主要做了這些,如果後面需要在進行添加吧
package mynetimport ( "bytes" "encoding/binary" "errors")type NetStream struct { Data []byte Rpos int Wpos int Capacity int}func NewNetStream(capacity int) *NetStream { a := &NetStream{ Rpos: 0, Wpos: 0, Capacity: capacity, } a.Data = make([]byte, capacity) return a}// readfunc (self *NetStream) ReadInt16() (int16, error) { var p int16 if self.Rpos+2 > self.Wpos { return p, errors.New("not enough read") } binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p) self.Rpos += 2 return p, nil}func (self *NetStream) ReadInt32() (int32, error) { var p int32 if self.Rpos+4 > self.Wpos { return p, errors.New("not enough read") } binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p) self.Rpos += 4 return p, nil}func (self *NetStream) GetInt16() (int16, error) { var p int16 if self.Rpos+2 > self.Wpos { return p, errors.New("not enough read") } binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p) return p, nil}func (self *NetStream) GetInt32() (int32, error) { var p int32 if self.Rpos+4 > self.Wpos { return p, errors.New("not enough read") } binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p) return p, nil}func (self *NetStream) ReadInt64() (int64, error) { var p int64 if self.Rpos+8 > self.Wpos { return p, errors.New("not enough read") } binary.Read(bytes.NewBuffer(self.Data[self.Rpos:]), binary.BigEndian, &p) self.Rpos += 8 return p, nil}func (self *NetStream) ReadBytes(p []byte, offset int, len int) error { if self.Rpos+len > self.Wpos { return errors.New("not enough read") } copy(p[offset:offset+len], self.Data[self.Rpos:self.Rpos+len]) self.Rpos += len return nil}// writefunc (self *NetStream) WriteInt16(p int16) error { if self.Wpos+2 > self.Capacity { return errors.New("not cap to write") } buff := bytes.NewBuffer([]byte{}) binary.Write(buff, binary.BigEndian, p) self.WriteByte(buff.Bytes()) return nil}func (self *NetStream) WriteInt32(p int32) error { if self.Wpos+4 > self.Capacity { return errors.New("not cap to write") } buff := bytes.NewBuffer([]byte{}) binary.Write(buff, binary.BigEndian, p) self.WriteByte(buff.Bytes()) return nil}func (self *NetStream) WriteInt64(p int64) error { if self.Wpos+8 > self.Capacity { return errors.New("not cap to write") } buff := bytes.NewBuffer([]byte{}) binary.Write(buff, binary.BigEndian, p) self.WriteByte(buff.Bytes()) return nil}func (self *NetStream) WriteBytes(p []byte, offset int, len int) error { if self.Wpos+len > self.Capacity { return errors.New("not cap to write") } copy(self.Data[self.Wpos:], p[offset:offset+len]) self.Wpos += len return nil}func (self *NetStream) WriteByte(p []byte) error { if self.Wpos+len(p) > self.Capacity { return errors.New("not cap to write") } copy(self.Data[self.Wpos:], p) self.Wpos += len(p) return nil}func (self *NetStream) ClearRead() { if self.Rpos > 0 { copy(self.Data[0:], self.Data[self.Rpos:self.Wpos]) self.Rpos = 0 self.Wpos = self.Wpos - self.Rpos }}func (self *NetStream) AvailableNum() int { return self.Wpos - self.Rpos}func (self *NetStream) PrefixedDataAvailable(len int) bool { if self.AvailableNum() < len { return false } var b1 int16 var b2 int32 var err error if len == 2 { b1, err = self.GetInt16() } else if len == 4 { b2, err = self.GetInt32() } if err == nil { if self.AvailableNum() >= int(b1)+len || self.AvailableNum() >= int(b2)+len { return true } } return false}func (self *NetStream) PrefixedDataAvailableShort() bool { return self.PrefixedDataAvailable(2)}func (self *NetStream) PrefixedDataAvailableInt() bool { return self.PrefixedDataAvailable(4)}