vitess.io/vitess@v0.16.2/go/vt/grpcoptionaltls/conn_wrapper.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 package grpcoptionaltls 16 17 import ( 18 "bytes" 19 "io" 20 "net" 21 ) 22 23 // WrappedConn imitates MSG_PEEK behaviour 24 // Unlike net.Conn is not thread-safe for reading already peeked bytes 25 type WrappedConn struct { 26 net.Conn 27 rd io.Reader 28 } 29 30 func NewWrappedConn(conn net.Conn, peeked []byte) net.Conn { 31 var rd = io.MultiReader(bytes.NewReader(peeked), conn) 32 return &WrappedConn{ 33 Conn: conn, 34 rd: rd, 35 } 36 } 37 38 func (wc *WrappedConn) Read(b []byte) (n int, err error) { 39 return wc.rd.Read(b) 40 }