Exemplo n.º 1
0
// ReadHeader reads a protocol.ID header from an io.Reader. This is so
// multiple protocols can be multiplexed on top of the same transport.
// See WriteHeader.
func ReadHeader(r io.Reader) (ID, error) {
	vr := msgio.NewVarintReader(r)
	msg, err := vr.ReadMsg()
	if err != nil {
		return ID(""), err
	}
	msg = msg[:len(msg)-1] // remove \n
	return ID(msg), nil
}
Exemplo n.º 2
0
func (cs *cliqueService) listen(portName string) (<-chan *incomingMessage, error) {
	portString := cs.servicePortString(portName)
	listener, err := corenet.Listen(cs.ipfsNode, portString)
	if err != nil {
		return nil, err
	}

	messageOut := make(chan *incomingMessage)
	go func() {
		for {
			stream, err := listener.Accept()
			if err != nil {
				// ipfsNode's context was canceled
				close(messageOut)
				return
			}

			reader := msgio.NewVarintReader(stream)
			message, err := reader.ReadMsg()
			if err != nil {
				log.Printf("Cliqueservice failed to read message: %s\n", err)
				continue
			}

			reader.Close()
			if err != nil {
				log.Printf("Cliqueservice failed to close stream: %s\n", err)
				continue
			}

			messageOut <- &incomingMessage{id: stream.Conn().RemotePeer(), data: message}
		}
	}()

	return messageOut, nil
}