The common operation of byte is usually required in network communication
function probablyWrite Int16,int32,int64,byte Read int16,int32,int64,byte
I do this here, if you need to add it later
Package mynet Import ("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}//read func (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}//write Func (self *netstream) WriteInt16 (p int16) error {if self. Wpos+2 > Self. Capacity {return errors. New ("Not caps 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 caps 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 caps 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 caps 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 caps 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) prefixeddataavailables Hort () bool {return self. Prefixeddataavailable (2)} func (self *netstream) prefixeddataavailableint () bool {return self.
Prefixeddataavailable (4)}