readFull reads len(p) bytes from the client or server reader, enforcing a shared byte limit across both. Returns an error if the limit is exceeded. Safe for both the client and server to call concurrently.
(isClient bool, p []byte)
| 663 | // byte limit across both. Returns an error if the limit is exceeded. Safe for both |
| 664 | // the client and server to call concurrently. |
| 665 | func (w *websocket) readFull(isClient bool, p []byte) (int, error) { |
| 666 | l := uint64(len(p)) |
| 667 | |
| 668 | for { |
| 669 | remaining := w.n.Load() |
| 670 | if remaining < l { |
| 671 | return 0, fmt.Errorf("%w: limit=%d, have=%d, want=%d, msgsRead=%d", errWebsocketPayloadLimitExceeded, tracer.PayloadLimitBytes, remaining, l, len(w.getMessages())) |
| 672 | } |
| 673 | |
| 674 | if w.n.CompareAndSwap(remaining, remaining-l) { |
| 675 | var r io.Reader |
| 676 | if isClient { |
| 677 | r = w.cli |
| 678 | } else { |
| 679 | r = w.srv |
| 680 | } |
| 681 | |
| 682 | n, err := io.ReadFull(r, p) |
| 683 | if err != nil { |
| 684 | w.n.Add(l - uint64(n)) |
| 685 | return n, err |
| 686 | } |
| 687 | return n, nil |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | func (w *websocket) readFrame(isClient bool) (*websocketFrame, error) { |
| 693 | header := make([]byte, 2) |
no test coverage detected