net.go 733 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. This Source Code Form is subject to the terms of the Mozilla Public
  3. License, v. 2.0. If a copy of the MPL was not distributed with this
  4. file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package internal
  7. import (
  8. "fmt"
  9. "io"
  10. )
  11. func Receive(c io.Reader, buf []byte) (b []byte, err error) {
  12. n, err := c.Read(buf)
  13. if err != nil {
  14. err = fmt.Errorf("receive: %w", err)
  15. }
  16. b = buf[:n]
  17. return
  18. }
  19. func Send(c io.Writer, buf []byte) error {
  20. n, err := c.Write(buf)
  21. if err != nil {
  22. return fmt.Errorf("send: %w", err)
  23. }
  24. if n < len(buf) {
  25. return fmt.Errorf("send: sent incomplete message: %v", buf[:n])
  26. }
  27. if n > len(buf) {
  28. return fmt.Errorf("send: sent message with excess bytes: %v", buf[:n])
  29. }
  30. return nil
  31. }