<commit_before>package websocket

import (
	"io"
	"net"
	"time"

	"github.com/gorilla/websocket"
	"v2ray.com/core/common/errors"
)

// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
	wsc    *websocket.Conn
	reader io.Reader
}

// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
	for {
		reader, err := c.getReader()
		if err != nil {
			return 0, err
		}

		nBytes, err := reader.Read(b)
		if errors.Cause(err) == io.EOF {
			c.reader = nil
			continue
		}
		return nBytes, err
	}
}

func (c *connection) getReader() (io.Reader, error) {
	if c.reader != nil {
		return c.reader, nil
	}

	_, reader, err := c.wsc.NextReader()
	if err != nil {
		return nil, err
	}
	c.reader = reader
	return reader, nil
}

func (c *connection) Write(b []byte) (int, error) {
	if err := c.wsc.WriteMessage(websocket.BinaryMessage, b); err != nil {
		return 0, err
	}
	return len(b), nil
}

func (c *connection) Close() error {
	c.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
	return c.wsc.Close()
}

func (c *connection) LocalAddr() net.Addr {
	return c.wsc.LocalAddr()
}
<commit_msg>implement write multi buffer in websocket<commit_after>package websocket

import (
	"io"
	"net"
	"time"

	"github.com/gorilla/websocket"
	"v2ray.com/core/common/buf"
	"v2ray.com/core/common/errors"
)

// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
	wsc         *websocket.Conn
	reader      io.Reader
	writeBuffer []byte
}

// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
	for {
		reader, err := c.getReader()
		if err != nil {
			return 0, err
		}

		nBytes, err := reader.Read(b)
		if errors.Cause(err) == io.EOF {
			c.reader = nil
			continue
		}
		return nBytes, err
	}
}

func (c *connection) getReader() (io.Reader, error) {
	if c.reader != nil {
		return c.reader, nil
	}

	_, reader, err := c.wsc.NextReader()
	if err != nil {
		return nil, err
	}
	c.reader = reader
	return reader, nil
}

func (c *connection) Write(b []byte) (int, error) {
	if err := c.wsc.WriteMessage(websocket.BinaryMessage, b); err != nil {
		return 0, err
	}
	return len(b), nil
}

func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) {
	defer mb.Release()

	if c.writeBuffer == nil {
		c.writeBuffer = make([]byte, 4096)
	}
	totalBytes := 0
	for !mb.IsEmpty() {
		nBytes, err := mb.Read(c.writeBuffer)
		if err != nil {
			return totalBytes, err
		}
		totalBytes += nBytes
		if _, err := c.Write(c.writeBuffer[:nBytes]); err != nil {
			return totalBytes, err
		}
	}
	return totalBytes, nil
}

func (c *connection) Close() error {
	c.wsc.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5))
	return c.wsc.Close()
}

func (c *connection) LocalAddr() net.Addr {
	return c.wsc.LocalAddr()
}

{'fraction_non_alphanumeric': 0.149798927613941, 'fraction_numerical': 0.004691689008042895, 'mean_word_length': 3.5433789954337898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6122623', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1105, 'n_words': 285}