7 g "github.com/anacrolix/generics"
9 pp "github.com/anacrolix/torrent/peer_protocol"
12 type LocalLtepProtocolMap struct {
13 // 1-based mapping from extension number to extension name (subtract one from the extension ID
14 // to find the corresponding protocol name). The first LocalLtepProtocolBuiltinCount of these
15 // are use builtin handlers. If you want to handle builtin protocols yourself, you would move
16 // them above the threshold. You can disable them by removing them entirely, and add your own.
17 // These changes should be done in the PeerConnAdded callback.
18 Index []pp.ExtensionName
19 // How many of the protocols are using the builtin handlers.
23 func (me *LocalLtepProtocolMap) toSupportedExtensionDict() (m map[pp.ExtensionName]pp.ExtensionNumber) {
24 g.MakeMapWithCap(&m, len(me.Index))
25 for i, name := range me.Index {
26 old := g.MapInsert(m, name, pp.ExtensionNumber(i+1))
28 panic(fmt.Sprintf("extension %q already defined with id %v", name, old.Value))
34 // Returns the local extension name for the given ID. If builtin is true, the implementation intends
35 // to handle it itself. For incoming messages with extension ID 0, the message is a handshake, and
36 // should be treated specially.
37 func (me *LocalLtepProtocolMap) LookupId(id pp.ExtensionNumber) (name pp.ExtensionName, builtin bool, err error) {
39 err = fmt.Errorf("extension ID 0 is handshake")
43 protocolIndex := int(id - 1)
44 if protocolIndex >= len(me.Index) {
45 err = fmt.Errorf("unexpected extended message ID: %v", id)
48 builtin = protocolIndex < me.NumBuiltin
49 name = me.Index[protocolIndex]
53 func (me *LocalLtepProtocolMap) builtin() []pp.ExtensionName {
54 return me.Index[:me.NumBuiltin]
57 func (me *LocalLtepProtocolMap) user() []pp.ExtensionName {
58 return me.Index[me.NumBuiltin:]
61 func (me *LocalLtepProtocolMap) AddUserProtocol(name pp.ExtensionName) {
62 builtin := slices.DeleteFunc(me.builtin(), func(delName pp.ExtensionName) bool {
63 return delName == name
65 user := slices.DeleteFunc(me.user(), func(delName pp.ExtensionName) bool {
66 return delName == name
68 me.Index = append(append(builtin, user...), name)
69 me.NumBuiltin = len(builtin)