func (b *Buffer) Write(packet []byte) (n int, err error) {
	packet = append([]byte{}, packet...)
	b.mutex.Lock()
	if b.closed {
		b.mutex.Unlock()
		return 0, io.ErrClosedPipe
	}
	if b.limitCount != 0 && len(b.packets)+1 > b.limitCount {
		b.mutex.Unlock()
		return 0, ErrFull
	}
	if b.limitSize != 0 && b.size+len(packet) > b.limitSize {
		b.mutex.Unlock()
		return 0, ErrFull
	}
	var notify chan struct{}
	if b.subs {
		notify = b.notify
		b.notify = make(chan struct{})
		b.subs = false
	}
	b.packets = append(b.packets, packet)
	b.size += len(packet)
	b.mutex.Unlock()
	if notify != nil {
		close(notify)
	}
	return len(packet), nil
}
Write appends a copy of the packet data to the buffer.
If any defined limits are hit, returns ErrFull.
package packetio
import (
	"errors"
	"io"
	"sync"
)
var ErrFull = errors.New("packetio.Buffer is full, discarding write")
type Buffer struct {
	mutex   sync.Mutex
	packets [][]byte
	notify chan struct{}
	subs   bool
	closed bool
	size int
	limitCount int
	limitSize  int
}
func NewBuffer() *Buffer {
	return &Buffer{
		notify: make(chan struct{}),
	}
}