<commit_before>// +build nacl

package websocket

import (
	"net"
	"net/url"
	"runtime/ppapi"
	"sync"
	"time"
)

// Ppapi instance which must be set before the Dial is called.
var PpapiInstance ppapi.Instance

func WebsocketConn(address string, ws *ppapi.WebsocketConn) net.Conn {
	return &wrappedConn{
		address: address,
		ws:      ws,
	}
}

type wrappedConn struct {
	address    string
	ws         *ppapi.WebsocketConn
	readLock   sync.Mutex
	writeLock  sync.Mutex
	currBuffer []byte
}

func Dial(address string) (net.Conn, error) {
	inst := PpapiInstance
	u, err := url.Parse("ws://" + address)
	if err != nil {
		return nil, err
	}

	ws, err := inst.DialWebsocket(u.String())
	if err != nil {
		return nil, err
	}
	return WebsocketConn(address, ws), nil
}

func (c *wrappedConn) Read(b []byte) (int, error) {
	c.readLock.Lock()
	defer c.readLock.Unlock()

	var err error
	if len(c.currBuffer) == 0 {
		c.currBuffer, err = c.ws.ReceiveMessage()
		if err != nil {
			return 0, nil
		}
	}

	n := copy(b, c.currBuffer)
	c.currBuffer = c.currBuffer[n:]
	return n, nil
}

func (c *wrappedConn) Write(b []byte) (int, error) {
	c.writeLock.Lock()
	defer c.writeLock.Unlock()
	if err := c.ws.SendMessage(b); err != nil {
		return 0, err
	}
	return len(b), nil
}

func (c *wrappedConn) Close() error {<commit_msg>veyron/lib/websocket: Fix bug in conn_nacl.<commit_after>// +build nacl

package websocket

import (
	"net"
	"net/url"
	"runtime/ppapi"
	"sync"
	"time"
)

// Ppapi instance which must be set before the Dial is called.
var PpapiInstance ppapi.Instance

func WebsocketConn(address string, ws *ppapi.WebsocketConn) net.Conn {
	return &wrappedConn{
		address: address,
		ws:      ws,
	}
}

type wrappedConn struct {
	address    string
	ws         *ppapi.WebsocketConn
	readLock   sync.Mutex
	writeLock  sync.Mutex
	currBuffer []byte
}

func Dial(address string) (net.Conn, error) {
	inst := PpapiInstance
	u, err := url.Parse("ws://" + address)
	if err != nil {
		return nil, err
	}

	ws, err := inst.DialWebsocket(u.String())
	if err != nil {
		return nil, err
	}
	return WebsocketConn(address, ws), nil
}

func (c *wrappedConn) Read(b []byte) (int, error) {
	c.readLock.Lock()
	defer c.readLock.Unlock()

	var err error
	if len(c.currBuffer) == 0 {
		c.currBuffer, err = c.ws.ReceiveMessage()
		if err != nil {
			return 0, err
		}
	}

	n := copy(b, c.currBuffer)
	c.currBuffer = c.currBuffer[n:]
	return n, nil
}

func (c *wrappedConn) Write(b []byte) (int, error) {
	c.writeLock.Lock()
	defer c.writeLock.Unlock()
	if err := c.ws.SendMessage(b); err != nil {
		return 0, err
	}
	return len(b), nil
}

func (c *wrappedConn) Close() error {
{'fraction_non_alphanumeric': 0.14134542705971279, 'fraction_numerical': 0.0022675736961451248, 'mean_word_length': 3.4264214046822743, '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': '3919708', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1034, 'n_words': 282}