1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 09:34:03 +08:00

allow removal of stream handlers

This commit is contained in:
Jeromy
2015-02-15 01:50:19 +00:00
parent 03c0b2d3e0
commit 2309266f73
4 changed files with 27 additions and 0 deletions

View File

@ -118,6 +118,10 @@ func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler
h.Mux().SetHandler(pid, handler) h.Mux().SetHandler(pid, handler)
} }
func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
h.Mux().RemoveHandler(pid)
}
// NewStream opens a new stream to given peer p, and writes a p2p/protocol // NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts // header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header. // to create one. If ProtocolID is "", writes no header.

View File

@ -46,6 +46,10 @@ type Host interface {
// (Threadsafe) // (Threadsafe)
SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler)
// RemoveStreamHandler removes a handler on the mux that was set by
// SetStreamHandler
RemoveStreamHandler(pid protocol.ID)
// NewStream opens a new stream to given peer p, and writes a p2p/protocol // NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts // header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header. // to create one. If ProtocolID is "", writes no header.

View File

@ -84,21 +84,31 @@ func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err e
func (rh *RoutedHost) ID() peer.ID { func (rh *RoutedHost) ID() peer.ID {
return rh.host.ID() return rh.host.ID()
} }
func (rh *RoutedHost) Peerstore() peer.Peerstore { func (rh *RoutedHost) Peerstore() peer.Peerstore {
return rh.host.Peerstore() return rh.host.Peerstore()
} }
func (rh *RoutedHost) Addrs() []ma.Multiaddr { func (rh *RoutedHost) Addrs() []ma.Multiaddr {
return rh.host.Addrs() return rh.host.Addrs()
} }
func (rh *RoutedHost) Network() inet.Network { func (rh *RoutedHost) Network() inet.Network {
return rh.host.Network() return rh.host.Network()
} }
func (rh *RoutedHost) Mux() *protocol.Mux { func (rh *RoutedHost) Mux() *protocol.Mux {
return rh.host.Mux() return rh.host.Mux()
} }
func (rh *RoutedHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) { func (rh *RoutedHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler) {
rh.host.SetStreamHandler(pid, handler) rh.host.SetStreamHandler(pid, handler)
} }
func (rh *RoutedHost) RemoveStreamHandler(pid protocol.ID) {
rh.host.RemoveStreamHandler(pid)
}
func (rh *RoutedHost) NewStream(pid protocol.ID, p peer.ID) (inet.Stream, error) { func (rh *RoutedHost) NewStream(pid protocol.ID, p peer.ID) (inet.Stream, error) {
return rh.host.NewStream(pid, p) return rh.host.NewStream(pid, p)
} }

View File

@ -90,6 +90,15 @@ func (m *Mux) SetHandler(p ID, h inet.StreamHandler) {
m.lock.Unlock() m.lock.Unlock()
} }
// RemoveHandler removes the protocol handler on the Network's Muxer.
// This operation is threadsafe.
func (m *Mux) RemoveHandler(p ID) {
log.Debugf("%s removing handler for protocol: %s (%d)", m, p, len(p))
m.lock.Lock()
delete(m.handlers, p)
m.lock.Unlock()
}
// Handle reads the next name off the Stream, and calls a handler function // Handle reads the next name off the Stream, and calls a handler function
// This is done in its own goroutine, to avoid blocking the caller. // This is done in its own goroutine, to avoid blocking the caller.
func (m *Mux) Handle(s inet.Stream) { func (m *Mux) Handle(s inet.Stream) {