<commit_before>// Copyright © 2017 Circonus, Inc. <<EMAIL>>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package reverse

import (
	"crypto/tls"
	"encoding/binary"
	"io"
	"time"

	"github.com/pkg/errors"
)

// buildFrame creates a frame to send to broker.
// recipe:
// bytes 1-6 header
//      2 bytes channel id and command flag
//      4 bytes length of data
// bytes 7-n are data, where 0 < n <= maxPayloadLen
func buildFrame(channelID uint16, isCommand bool, payload []byte) []byte {
	frame := make([]byte, len(payload)+6)

	var cmdFlag uint16
	if isCommand {
		cmdFlag = 0x8000
	}

	copy(frame[6:], payload)
	binary.BigEndian.PutUint16(frame[0:], channelID&0x7fff|cmdFlag)
	binary.BigEndian.PutUint32(frame[2:], uint32(len(payload)))

	return frame
}

// readFrameFromBroker reads a frame(header + payload) from broker
func (c *Connection) readFrameFromBroker(r io.Reader) (*noitFrame, error) {
	if conn, ok := r.(*tls.Conn); ok {
		conn.SetDeadline(time.Now().Add(c.commTimeout))
	}
	hdr, err := readFrameHeader(r)
	if err != nil {
		return nil, err
	}

	if hdr.payloadLen > maxPayloadLen {
		return nil, errors.Errorf("received oversized frame (%d len)", hdr.payloadLen) // restart the connection
	}

	if conn, ok := r.(*tls.Conn); ok {<commit_msg>upd: move pkg const to struct<commit_after>// Copyright © 2017 Circonus, Inc. <<EMAIL>>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package reverse

import (
	"crypto/tls"
	"encoding/binary"
	"io"
	"time"

	"github.com/pkg/errors"
)

// buildFrame creates a frame to send to broker.
// recipe:
// bytes 1-6 header
//      2 bytes channel id and command flag
//      4 bytes length of data
// bytes 7-n are data, where 0 < n <= maxPayloadLen
func buildFrame(channelID uint16, isCommand bool, payload []byte) []byte {
	frame := make([]byte, len(payload)+6)

	var cmdFlag uint16
	if isCommand {
		cmdFlag = 0x8000
	}

	copy(frame[6:], payload)
	binary.BigEndian.PutUint16(frame[0:], channelID&0x7fff|cmdFlag)
	binary.BigEndian.PutUint32(frame[2:], uint32(len(payload)))

	return frame
}

// readFrameFromBroker reads a frame(header + payload) from broker
func (c *Connection) readFrameFromBroker(r io.Reader) (*noitFrame, error) {
	if conn, ok := r.(*tls.Conn); ok {
		conn.SetDeadline(time.Now().Add(c.commTimeout))
	}
	hdr, err := readFrameHeader(r)
	if err != nil {
		return nil, err
	}

	if hdr.payloadLen > c.maxPayloadLen {
		return nil, errors.Errorf("received oversized frame (%d len)", hdr.payloadLen) // restart the connection
	}

	if conn, ok := r.(*tls.Conn); ok {
{'fraction_non_alphanumeric': 0.1378012048192771, 'fraction_numerical': 0.023343373493975902, 'mean_word_length': 3.7873873873873873, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '3711286', 'n_tokens_mistral': 967, 'n_tokens_neox': 911, 'n_words': 285}