WireReadRequest reads a message from the specified reader
(ctx context.Context, conn net.Conn, waitIndefinitely bool)
| 1979 | |
| 1980 | // WireReadRequest reads a message from the specified reader |
| 1981 | func WireReadRequest(ctx context.Context, conn net.Conn, waitIndefinitely bool) (bytesRead uint32, request []byte, err error) { |
| 1982 | var n int |
| 1983 | var version []byte |
| 1984 | |
| 1985 | // Set up for reading with a timeout |
| 1986 | timeoutDuration := 30 * time.Second |
| 1987 | rdconn := io.Reader(conn) |
| 1988 | |
| 1989 | // Read the payload buffer format |
| 1990 | for { |
| 1991 | var err2 error |
| 1992 | versionLen := 1 |
| 1993 | version = make([]byte, versionLen) |
| 1994 | if err = conn.SetReadDeadline(time.Now().Add(timeoutDuration)); err != nil { |
| 1995 | if errors.Is(err, net.ErrClosed) { |
| 1996 | err = fmt.Errorf("wire read: " + note.ErrClosed + " connection closed") |
| 1997 | return |
| 1998 | } |
| 1999 | logError(ctx, "SetReadDeadline (version): %v", err) |
| 2000 | } |
| 2001 | |
| 2002 | n, err2 = rdconn.Read(version) |
| 2003 | |
| 2004 | if debugWireRead && err2 == nil { |
| 2005 | logDebug(ctx, "\n\nrdVersion(%d) %d", len(version), n) |
| 2006 | } |
| 2007 | |
| 2008 | if err2, ok := err2.(net.Error); ok && err2.Timeout() { |
| 2009 | if !waitIndefinitely { |
| 2010 | err = fmt.Errorf("wire read: " + note.ErrTimeout + " timeout on read") |
| 2011 | return |
| 2012 | } |
| 2013 | continue |
| 2014 | } |
| 2015 | if err2 == io.EOF { |
| 2016 | err = fmt.Errorf("wire read: " + note.ErrClosed + " connection closed") |
| 2017 | return |
| 2018 | } |
| 2019 | if err2 != nil { |
| 2020 | err = fmt.Errorf("wire read: can't read version: %s", err2) |
| 2021 | return |
| 2022 | } |
| 2023 | if n != versionLen { |
| 2024 | err = fmt.Errorf("wire read: insufficient data to read protocol version: %d/%d", n, versionLen) |
| 2025 | return |
| 2026 | } |
| 2027 | bytesRead += uint32(n) |
| 2028 | break |
| 2029 | } |
| 2030 | |
| 2031 | // Process the version byte to determine the length of the header that follows |
| 2032 | isValidVersion, headerLength := wireProcessVersionByte(version[0]) |
| 2033 | if !isValidVersion { |
| 2034 | err = fmt.Errorf("wire read: unrecognized protocol") |
| 2035 | return |
| 2036 | } |
| 2037 | |
| 2038 | // Read the header |
nothing calls this directly
no test coverage detected