Пример #1
0
func (s *streamSession) createPipe() (Receiver, Sender) {
	r, w := io.Pipe()
	s.pipeLock.Lock()
	pipeID := s.pipeCount + 1
	s.pipeCount = pipeID
	s.pipeReaders[pipeID] = r
	s.pipeWriters[pipeID] = w
	s.pipeLock.Unlock()

	recv := &pipeReceiver{pipeID, s, r, codec.NewDecoder(r, s.handler)}
	send := &pipeSender{pipeID, s, w, codec.NewEncoder(w, s.handler)}
	return recv, send
}
Пример #2
0
func (s *streamSession) decodeReceiver(v reflect.Value, b []byte) error {
	pipeID, readN := binary.Uvarint(b)
	if readN == 0 {
		return errors.New("bad reference id")
	}

	r, ok := s.pipeReaders[pipeID]
	if !ok {
		return errors.New("receiver does not exist")
	}

	v.Set(reflect.ValueOf(pipeReceiver{pipeID, s, r, codec.NewDecoder(r, s.handler)}))

	return nil
}