1package moxio
2
3import (
4 "io"
5 "net"
6)
7
8// PrefixConn is a net.Conn prefixed with a reader that is first drained.
9// Used for STARTTLS where already did a buffered read of initial TLS data.
10type PrefixConn struct {
11 PrefixReader io.Reader // If not nil, reads are fulfilled from here. It is cleared when a read returns io.EOF.
12 net.Conn
13}
14
15// Read returns data when PrefixReader when not nil, and net.Conn otherwise.
16func (c *PrefixConn) Read(buf []byte) (int, error) {
17 if c.PrefixReader != nil {
18 n, err := c.PrefixReader.Read(buf)
19 if err == io.EOF {
20 c.PrefixReader = nil
21 }
22 return n, err
23 }
24 return c.Conn.Read(buf)
25}
26