Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable consumption into secretless-broker #533

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type header struct {
Pad uint8
}

// tdsBuffer reads and writes TDS packets of data to the transport.
// TdsBuffer reads and writes TDS packets of data to the transport.
// The write and read buffers are separate to make sending attn signals
// possible without locks. Currently attn signals are only sent during
// reads, not writes.
type tdsBuffer struct {
type TdsBuffer struct {
transport io.ReadWriteCloser

packetSize int
Expand All @@ -39,14 +39,14 @@ type tdsBuffer struct {
final bool
rPacketType packetType

// afterFirst is assigned to right after tdsBuffer is created and
// afterFirst is assigned to right after TdsBuffer is created and
// before the first use. It is executed after the first packet is
// written and then removed.
afterFirst func()
}

func newTdsBuffer(bufsize uint16, transport io.ReadWriteCloser) *tdsBuffer {
return &tdsBuffer{
func NewTdsBuffer(bufsize uint16, transport io.ReadWriteCloser) *TdsBuffer {
return &TdsBuffer{
packetSize: int(bufsize),
wbuf: make([]byte, 1<<16),
rbuf: make([]byte, 1<<16),
Expand All @@ -55,15 +55,15 @@ func newTdsBuffer(bufsize uint16, transport io.ReadWriteCloser) *tdsBuffer {
}
}

func (rw *tdsBuffer) ResizeBuffer(packetSize int) {
func (rw *TdsBuffer) ResizeBuffer(packetSize int) {
rw.packetSize = packetSize
}

func (w *tdsBuffer) PackageSize() int {
func (w *TdsBuffer) PackageSize() int {
return w.packetSize
}

func (w *tdsBuffer) flush() (err error) {
func (w *TdsBuffer) flush() (err error) {
// Write packet size.
w.wbuf[0] = byte(w.wPacketType)
binary.BigEndian.PutUint16(w.wbuf[2:], uint16(w.wpos))
Expand All @@ -88,7 +88,7 @@ func (w *tdsBuffer) flush() (err error) {
return nil
}

func (w *tdsBuffer) Write(p []byte) (total int, err error) {
func (w *TdsBuffer) Write(p []byte) (total int, err error) {
for {
copied := copy(w.wbuf[w.wpos:w.packetSize], p)
w.wpos += copied
Expand All @@ -103,7 +103,7 @@ func (w *tdsBuffer) Write(p []byte) (total int, err error) {
}
}

func (w *tdsBuffer) WriteByte(b byte) error {
func (w *TdsBuffer) WriteByte(b byte) error {
if int(w.wpos) == len(w.wbuf) || w.wpos == w.packetSize {
if err := w.flush(); err != nil {
return err
Expand All @@ -114,7 +114,7 @@ func (w *tdsBuffer) WriteByte(b byte) error {
return nil
}

func (w *tdsBuffer) BeginPacket(packetType packetType, resetSession bool) {
func (w *TdsBuffer) BeginPacket(packetType packetType, resetSession bool) {
status := byte(0)
if resetSession {
switch packetType {
Expand All @@ -129,14 +129,14 @@ func (w *tdsBuffer) BeginPacket(packetType packetType, resetSession bool) {
w.wPacketType = packetType
}

func (w *tdsBuffer) FinishPacket() error {
func (w *TdsBuffer) FinishPacket() error {
w.wbuf[1] |= 1 // Mark this as the last packet in the message.
return w.flush()
}

var headerSize = binary.Size(header{})

func (r *tdsBuffer) readNextPacket() error {
func (r *TdsBuffer) ReadNextPacket() error {
h := header{}
var err error
err = binary.Read(r.transport, binary.BigEndian, &h)
Expand All @@ -160,20 +160,20 @@ func (r *tdsBuffer) readNextPacket() error {
return nil
}

func (r *tdsBuffer) BeginRead() (packetType, error) {
err := r.readNextPacket()
func (r *TdsBuffer) BeginRead() (packetType, error) {
err := r.ReadNextPacket()
if err != nil {
return 0, err
}
return r.rPacketType, nil
}

func (r *tdsBuffer) ReadByte() (res byte, err error) {
func (r *TdsBuffer) ReadByte() (res byte, err error) {
if r.rpos == r.rsize {
if r.final {
return 0, io.EOF
}
err = r.readNextPacket()
err = r.ReadNextPacket()
if err != nil {
return 0, err
}
Expand All @@ -183,44 +183,44 @@ func (r *tdsBuffer) ReadByte() (res byte, err error) {
return res, nil
}

func (r *tdsBuffer) byte() byte {
func (r *TdsBuffer) byte() byte {
b, err := r.ReadByte()
if err != nil {
badStreamPanic(err)
}
return b
}

func (r *tdsBuffer) ReadFull(buf []byte) {
func (r *TdsBuffer) ReadFull(buf []byte) {
_, err := io.ReadFull(r, buf[:])
if err != nil {
badStreamPanic(err)
}
}

func (r *tdsBuffer) uint64() uint64 {
func (r *TdsBuffer) uint64() uint64 {
var buf [8]byte
r.ReadFull(buf[:])
return binary.LittleEndian.Uint64(buf[:])
}

func (r *tdsBuffer) int32() int32 {
func (r *TdsBuffer) int32() int32 {
return int32(r.uint32())
}

func (r *tdsBuffer) uint32() uint32 {
func (r *TdsBuffer) uint32() uint32 {
var buf [4]byte
r.ReadFull(buf[:])
return binary.LittleEndian.Uint32(buf[:])
}

func (r *tdsBuffer) uint16() uint16 {
func (r *TdsBuffer) uint16() uint16 {
var buf [2]byte
r.ReadFull(buf[:])
return binary.LittleEndian.Uint16(buf[:])
}

func (r *tdsBuffer) BVarChar() string {
func (r *TdsBuffer) BVarChar() string {
return readBVarCharOrPanic(r)
}

Expand All @@ -240,18 +240,18 @@ func readUsVarCharOrPanic(r io.Reader) string {
return s
}

func (r *tdsBuffer) UsVarChar() string {
func (r *TdsBuffer) UsVarChar() string {
return readUsVarCharOrPanic(r)
}

func (r *tdsBuffer) Read(buf []byte) (copied int, err error) {
func (r *TdsBuffer) Read(buf []byte) (copied int, err error) {
copied = 0
err = nil
if r.rpos == r.rsize {
if r.final {
return 0, io.EOF
}
err = r.readNextPacket()
err = r.ReadNextPacket()
if err != nil {
return
}
Expand Down
14 changes: 7 additions & 7 deletions buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func (failBuffer) Close() error {
return nil
}

func makeBuf(bufSize uint16, testData []byte) *tdsBuffer {
func makeBuf(bufSize uint16, testData []byte) *TdsBuffer {
buffer := closableBuffer{bytes.NewBuffer(testData)}
return newTdsBuffer(bufSize, &buffer)
return NewTdsBuffer(bufSize, &buffer)
}

func TestStreamShorterThanHeader(t *testing.T) {
//buffer := closableBuffer{*bytes.NewBuffer([]byte{0xFF, 0xFF})}
//buffer := closableBuffer{*bytes.NewBuffer([]byte{0x6F, 0x96, 0x19, 0xFF, 0x8B, 0x86, 0xD0, 0x11, 0xB4, 0x2D, 0x00, 0xC0, 0x4F, 0xC9, 0x64, 0xFF})}
//tdsBuffer := newTdsBuffer(100, &buffer)
//TdsBuffer := NewTdsBuffer(100, &buffer)
buffer := makeBuf(100, []byte{0xFF, 0xFF})
_, err := buffer.BeginRead()
if err == nil {
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestReadFailsOnSecondPacket(t *testing.T) {

func TestWrite(t *testing.T) {
memBuf := bytes.NewBuffer([]byte{})
buf := newTdsBuffer(11, closableBuffer{memBuf})
buf := NewTdsBuffer(11, closableBuffer{memBuf})
buf.BeginPacket(1, false)
err := buf.WriteByte(2)
if err != nil {
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestWrite(t *testing.T) {

func TestWriteErrors(t *testing.T) {
// write should fail if underlying transport fails
buf := newTdsBuffer(uint16(headerSize)+1, failBuffer{})
buf := NewTdsBuffer(uint16(headerSize)+1, failBuffer{})
buf.BeginPacket(1, false)
wrote, err := buf.Write([]byte{0, 0})
// may change from error to panic in future
Expand All @@ -245,7 +245,7 @@ func TestWriteErrors(t *testing.T) {
}

// writebyte should fail if underlying transport fails
buf = newTdsBuffer(uint16(headerSize)+1, failBuffer{})
buf = NewTdsBuffer(uint16(headerSize)+1, failBuffer{})
buf.BeginPacket(1, false)
// first write should not fail because if fits in the buffer
err = buf.WriteByte(0)
Expand All @@ -261,7 +261,7 @@ func TestWriteErrors(t *testing.T) {

func TestWrite_BufferBounds(t *testing.T) {
memBuf := bytes.NewBuffer([]byte{})
buf := newTdsBuffer(11, closableBuffer{memBuf})
buf := NewTdsBuffer(11, closableBuffer{memBuf})

buf.BeginPacket(1, false)
// write bytes enough to complete a package
Expand Down
5 changes: 5 additions & 0 deletions mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ type Conn struct {
returnStatus *ReturnStatus
}

// NetConn exposes the underlying transport to the backend
func (c *Conn) NetConn() net.Conn {
return c.sess.buf.transport.(net.Conn)
}

func (c *Conn) setReturnStatus(s ReturnStatus) {
if c.returnStatus == nil {
return
Expand Down
6 changes: 3 additions & 3 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

type timeoutConn struct {
c net.Conn
timeout time.Duration
c net.Conn
timeout time.Duration
}

func newTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c timeoutConn) SetWriteDeadline(t time.Time) error {
// this connection is used during TLS Handshake
// TDS protocol requires TLS handshake messages to be sent inside TDS packets
type tlsHandshakeConn struct {
buf *tdsBuffer
buf *TdsBuffer
packetPending bool
continueRead bool
}
Expand Down
2 changes: 1 addition & 1 deletion rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
)

// http://msdn.microsoft.com/en-us/library/dd357576.aspx
func sendRpc(buf *tdsBuffer, headers []headerStruct, proc procId, flags uint16, params []param, resetSession bool) (err error) {
func sendRpc(buf *TdsBuffer, headers []headerStruct, proc procId, flags uint16, params []param, resetSession bool) (err error) {
buf.BeginPacket(packRPCRequest, resetSession)
writeAllHeaders(buf, headers)
if len(proc.name) == 0 {
Expand Down
Loading