// This method loops until a binary opcode comes in and returns its reader.
// While looping it will receive and process other opcodes, such as pings.
979470
func (conn *WebsocketConnection) waitForReader() error {
	// no existing readers, wait for one
	for {
		opCode, reader, err := conn.ws.NextReader()
		if err != nil {
			return err
		}

		switch opCode {
		case websocket.BinaryMessage:
			// binary packet
			conn.reader = reader
			return nil

		case websocket.TextMessage:
			// plain text package
			b, err := ioutil.ReadAll(reader)
			if err == nil {
				conn.textChan <- b
			}

		case websocket.PingMessage:
			// receeived a ping, so send a pong
			go func() {
				conn.writeMutex.Lock()
				defer conn.writeMutex.Unlock()
				conn.ws.WriteControl(websocket.PongMessage, []byte{}, time.Now().Add(conn.writeTimeout))
			}()

		case websocket.PongMessage:
			// received a pong, update read deadline
			conn.ws.SetReadDeadline(time.Now().Add(conn.readTimeout))

		case websocket.CloseMessage:
			// received close, so return EOF
			return io.EOF
		}
	}
}
1252087
func BlockingRead(fd int, b []byte) (int, *tcpip.Error) {
	for {
		n, _, e := syscall.RawSyscall(syscall.SYS_READ, uintptr(fd), uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
		if e == 0 {
			return int(n), nil
		}

		event := PollEvent{
			FD:     int32(fd),
			Events: 1, // POLLIN
		}

		_, e = BlockingPoll(&event, 1, -1)
		if e != 0 && e != syscall.EINTR {
			return 0, TranslateErrno(e)
		}
	}
}
869066
func (c *Client) read(codec ServerCodec) {
	for {
		msgs, batch, err := codec.Read()
		if _, ok := err.(*json.SyntaxError); ok {
			codec.Write(context.Background(), errorMessage(&parseError{err.Error()}))
		}
		if err != nil {
			c.readErr <- err
			return
		}
		c.readOp <- readOp{msgs, batch}
	}
}
154801
func (a *TrackingReader) Read(b []byte) (int, error) {
	a.ctx.UpdateClientActivity()
	return a.r.Read(b)
}
1025941
func (s *Socket) ReadBINC(v interface{}) (err error) {
	_, r, err := s.NextReader()
	if err != nil {
		return err
	}
	return codec.NewDecoder(r, &bh).Decode(v)
}
1037996
func (d *Decoder) PeekCode() (codes.Code, error) {
	c, err := d.s.ReadByte()
	if err != nil {
		return 0, err
	}
	return codes.Code(c), d.s.UnreadByte()
}
586173
func (pm *PickleMachine) opcode_BININT1() error {
	var v uint8
	err := pm.readBinaryInto(&v, false)
	if err != nil {
		return err
	}
	pm.push(int64(v))
	return nil
}
41076
func longBinaryReader(ior io.Reader) (int64, error) {
	var value uint64
	var shift uint
	var err error
	var b byte

	// NOTE: While benchmarks show it's more performant to invoke ReadByte when
	// available, testing whether a variable's data type implements a particular
	// method is quite slow too. So perform the test once, and branch to the
	// appropriate loop based on the results.

	if byteReader, ok := ior.(io.ByteReader); ok {
		for {
			if b, err = byteReader.ReadByte(); err != nil {
				return 0, err // NOTE: must send back unaltered error to detect io.EOF
			}
			value |= uint64(b&intMask) << shift
			if b&intFlag == 0 {
				return (int64(value>>1) ^ -int64(value&1)), nil
			}
			shift += 7
		}
	}

	// NOTE: ior does not also implement io.ByteReader, so we must allocate a
	// byte slice with a single byte, and read each byte into the slice.
	buf := make([]byte, 1)
	for {
		if _, err = ior.Read(buf); err != nil {
			return 0, err // NOTE: must send back unaltered error to detect io.EOF
		}
		b = buf[0]
		value |= uint64(b&intMask) << shift
		if b&intFlag == 0 {
			return (int64(value>>1) ^ -int64(value&1)), nil
		}
		shift += 7
	}
}
818540
func (lc *LoopbackConn) Read(b []byte) (n int, err error) {
	lc.rMutex.Lock()
	defer lc.rMutex.Unlock()

	if lc.buf.Len() > 0 {
		return lc.buf.Read(b)
	}
	msg, ok := <-lc.partnerCh
	if !ok {
		return 0, io.EOF
	}
	lc.buf.Write(msg)
	return lc.buf.Read(b)
}
1086317
func Read(r io.Reader, data ...interface{}) error {
	for _, v := range data {
		if err := binary.Read(r, binary.BigEndian, v); err != nil {
			return err
		}
	}

	return nil
}
1298759
func (c *EchoClient) ReadLoop() error {
	// start loop to read msg from server
	for {
		msg, err := c.conn.Read() // if you want set a read deadline, you can use 'connector.ReadTimeout(timeout)'
		if err != nil {
			fmt.Printf("read msg from server<%s> failure", c.serverAddr)
			return err
		}

		fmt.Printf("receive a msg<%s> from <%s>", msg, c.serverAddr)
	}
}
1122835
func (meta *Meta) ReadBinary(r io.Reader) error {
	return binary.Read(r, byteorder.Native, meta)
}
725341
func (br *bitReader) TryReadBits(nb uint) (uint, bool) {
	if br.numBits < nb {
		return 0, false
	}
	val := uint(br.bufBits & uint64(1<<nb-1))
	br.bufBits >>= nb
	br.numBits -= nb
	return val, true
}
501459
func (r *Reader) Read(b []byte) (int, error) {
	// if we have data in the buffer, just
	// return that.
	if r.buffered() != 0 {
		x := copy(b, r.data[r.n:])
		r.n += x
		return x, nil
	}
	var n int
	// we have no buffered data; determine
	// whether or not to buffer or call
	// the underlying reader directly
	if len(b) >= cap(r.data) {
		n, r.state = r.r.Read(b)
	} else {
		r.more()
		n = copy(b, r.data)
		r.n = n
	}
	if n == 0 {
		return 0, r.err()
	}
	return n, nil
}
296361
func (s *UDPSession) readLoop() {
	addr, _ := net.ResolveUDPAddr("udp", s.conn.LocalAddr().String())
	if addr.IP.To4() != nil {
		s.readLoopIPv4()
	} else {
		s.readLoopIPv6()
	}
}
628952
func (b *BitReader) ReadBit() (Bit, error) {
	if b.count == 0 {
		if n, err := b.r.Read(b.b[:]); n != 1 || (err != nil && err != io.EOF) {
			return Zero, err
		}
		b.count = 8
	}
	b.count--
	d := (b.b[0] & 0x80)
	b.b[0] <<= 1
	return d != 0, nil
}
507403
func (r *BitReader) ReadBit() (bool, error) {
	v, err := r.ReadBits(1)
	return v != 0, err
}