func NewWriterBufferSize(dest io.Writer, state ws.State, op ws.OpCode, n int) *Writer {
	if n <= ws.MinHeaderSize {
		n = DefaultWriteBuffer
	}
	return NewWriterBuffer(dest, state, op, make([]byte, n))
}
NewWriterBufferSize returns a new Writer whose buffer size is equal to n.
If n <= ws.MinHeaderSize then the default buffer size is used.

Note that Writer will reserve x bytes for header data, where x is in range
[ws.MinHeaderSize,ws.MaxHeaderSize]. That is, frames flushed by Writer
will not have payload length equal to n, except the case when Write() is
called on empty Writer with len(p) > n.
package wsutil
import (
	"fmt"
	"github.com/ronaksoft/rony/pools"
	"io"
	"github.com/gobwas/pool"
	"github.com/gobwas/ws"
)
var DefaultWriteBuffer = 4096
var (
	ErrNotEmpty = fmt.Errorf("writer not empty")
	ErrControlOverflow = fmt.Errorf("control frame payload overflow")
)
const (
	len7  = int64(125)  
	len16 = int64(^uint16(0))
)
type ControlWriter struct {
	w     *Writer
	limit int
	n     int
}
func NewControlWriter(dest io.Writer, state ws.State, op ws.OpCode) *ControlWriter {
	return &ControlWriter{
		w:     NewWriterSize(dest, state, op, ws.MaxControlFramePayloadSize),
		limit: ws.MaxControlFramePayloadSize,
	}
}
func NewControlWriterBuffer(dest io.Writer, state ws.State, op ws.OpCode, buf []byte) *ControlWriter {
	max := ws.MaxControlFramePayloadSize + headerSize(state, ws.MaxControlFramePayloadSize)
	if len(buf) > max {
		buf = buf[:max]
	}
	w := NewWriterBuffer(dest, state, op, buf)
	return &ControlWriter{
		w:     w,
		limit: len(w.buf),
	}
}
func (c *ControlWriter) Write(p []byte) (n int, err error) {
	if c.n+len(p) > c.limit {
		return 0, ErrControlOverflow
	}
	return c.w.Write(p)
}
func (c *ControlWriter) Flush() error {
	return c.w.Flush()
}
type Writer struct {
	dest io.Writer
	n   int     
	raw []byte  
	buf []byte  
	op    ws.OpCode
	state ws.State
	dirty      bool
	fragmented bool
	err error
}
var writers = pool.New(128, 65536)
func GetWriter(dest io.Writer, state ws.State, op ws.OpCode, n int) *Writer {
	x, m := writers.Get(n)
	if x != nil {
		w := x.(*Writer)
		w.Reset(dest, state, op)
		return w
	}
	return NewWriterBufferSize(dest, state, op, m)
}
func PutWriter(w *Writer) {
	w.Reset(nil, 0, 0)
	writers.Put(w, w.Size())
}
func NewWriter(dest io.Writer, state ws.State, op ws.OpCode) *Writer {
	return NewWriterBufferSize(dest, state, op, 0)
}
func NewWriterSize(dest io.Writer, state ws.State, op ws.OpCode, n int) *Writer {
	if n > 0 {
		n += headerSize(state, n)
	}
	return NewWriterBufferSize(dest, state, op, n)
}