<commit_before>package mssql

import (
	"fmt"
	"net"
	"time"
)

type timeoutConn struct {
	c             net.Conn
	timeout       time.Duration
	buf           *tdsBuffer
	packetPending bool
}

func NewTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
	return &timeoutConn{
		c:       conn,
		timeout: timeout,
	}
}

func (c *timeoutConn) Read(b []byte) (n int, err error) {
	if c.buf != nil {
		if c.packetPending {
			c.packetPending = false
			err = c.buf.FinishPacket()
			if err != nil {
				return
			}
		}
		var packet uint8
		packet, err = c.buf.BeginRead()
		if err != nil {
			return
		}
		if packet != packPrelogin {
			err = fmt.Errorf("unexpected packet %d, expecting prelogin", packet)
			return
		}
		n, err = c.buf.Read(b)
		return
	}
	err = c.c.SetDeadline(time.Now().Add(c.timeout))
	if err != nil {
		return
	}
	return c.c.Read(b)<commit_msg>better error messages for handshake<commit_after>package mssql

import (
	"fmt"
	"net"
	"time"
)

type timeoutConn struct {
	c             net.Conn
	timeout       time.Duration
	buf           *tdsBuffer
	packetPending bool
}

func NewTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
	return &timeoutConn{
		c:       conn,
		timeout: timeout,
	}
}

func (c *timeoutConn) Read(b []byte) (n int, err error) {
	if c.buf != nil {
		if c.packetPending {
			c.packetPending = false
			err = c.buf.FinishPacket()
			if err != nil {
				err = fmt.Errorf("Cannot send handshake packet: %s", err.Error())
				return
			}
		}
		var packet uint8
		packet, err = c.buf.BeginRead()
		if err != nil {
			err = fmt.Errorf("Cannot read handshake packet: %s", err.Error())
			return
		}
		if packet != packPrelogin {
			err = fmt.Errorf("unexpected packet %d, expecting prelogin", packet)
			return
		}
		n, err = c.buf.Read(b)
		return
	}
	err = c.c.SetDeadline(time.Now().Add(c.timeout))
	if err != nil {
		return
	}
	return c.c.Read(b)
{'fraction_non_alphanumeric': 0.12781561026715557, 'fraction_numerical': 0.0010476689366160294, 'mean_word_length': 2.7450980392156863, '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': '7398351', 'n_tokens_mistral': 762, 'n_tokens_neox': 702, 'n_words': 185}