func EchoStreamHandler(stream inet.Stream) { go func() { defer stream.Close() // pull out the ipfs conn c := stream.Conn() log.Infof("%s ponging to %s", c.LocalPeer(), c.RemotePeer()) buf := make([]byte, 4) for { if _, err := stream.Read(buf); err != nil { if err != io.EOF { log.Info("ping receive error:", err) } return } if !bytes.Equal(buf, []byte("ping")) { log.Infof("ping receive error: ping != %s %v", buf, buf) return } if _, err := stream.Write([]byte("pong")); err != nil { log.Info("pond send error:", err) return } } }() }
// HandleSync reads the next name off the Stream, and calls a handler function // This is done synchronously. The handler function will return before // HandleSync returns. func (m *Mux) HandleSync(s inet.Stream) { ctx := context.Background() name, handler, err := m.ReadHeader(s) if err != nil { err = fmt.Errorf("protocol mux error: %s", err) log.Event(ctx, "muxError", lgbl.Error(err)) s.Close() return } log.Debugf("muxer handle protocol %s: %s", s.Conn().RemotePeer(), name) handler(s) }
func (dht *IpfsDHT) handleNewMessage(s inet.Stream) { defer s.Close() ctx := dht.Context() cr := ctxio.NewReader(ctx, s) // ok to use. we defer close stream in this func cw := ctxio.NewWriter(ctx, s) // ok to use. we defer close stream in this func r := ggio.NewDelimitedReader(cr, inet.MessageSizeMax) w := ggio.NewDelimitedWriter(cw) mPeer := s.Conn().RemotePeer() // receive msg pmes := new(pb.Message) if err := r.ReadMsg(pmes); err != nil { log.Debugf("Error unmarshaling data: %s", err) return } // update the peer (on valid msgs only) dht.updateFromMessage(ctx, mPeer, pmes) // get handler for this msg type. handler := dht.handlerForMsgType(pmes.GetType()) if handler == nil { log.Debug("got back nil handler from handlerForMsgType") return } // dispatch handler. rpmes, err := handler(ctx, mPeer, pmes) if err != nil { log.Debugf("handle message error: %s", err) return } // if nil response, return it before serializing if rpmes == nil { log.Debug("Got back nil response from request.") return } // send out response msg if err := w.WriteMsg(rpmes); err != nil { log.Debugf("send response error: %s", err) return } return }
func (lb *Loopback) HandleStream(s inet.Stream) { defer s.Close() pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax) var incoming dhtpb.Message if err := pbr.ReadMsg(&incoming); err != nil { log.Debug(err) return } ctx := context.TODO() outgoing := lb.Handler.HandleRequest(ctx, s.Conn().RemotePeer(), &incoming) pbw := ggio.NewDelimitedWriter(s) if err := pbw.WriteMsg(outgoing); err != nil { return // TODO logerr } }
// handleStream is our own handler, which returns an error for simplicity. func (rs *RelayService) handleStream(s inet.Stream) error { defer s.Close() // read the header (src and dst peer.IDs) src, dst, err := ReadHeader(s) if err != nil { return fmt.Errorf("stream with bad header: %s", err) } local := rs.host.ID() switch { case src == local: return fmt.Errorf("relaying from self") case dst == local: // it's for us! yaaay. log.Debugf("%s consuming stream from %s", local, src) return rs.consumeStream(s) default: // src and dst are not local. relay it. log.Debugf("%s relaying stream %s <--> %s", local, src, dst) return rs.pipeStream(src, dst, s) } }
func (p *standard) HandleStream(s inet.Stream) { // TODO(brian): Should clients be able to satisfy requests? log.Error("supernode client received (dropped) a routing message from", s.Conn().RemotePeer()) s.Close() }