(peer *Peer, handshake *Handshake)
| 56 | } |
| 57 | |
| 58 | func (table *IndexTable) NewIndexForHandshake(peer *Peer, handshake *Handshake) (uint32, error) { |
| 59 | for { |
| 60 | // generate random index |
| 61 | |
| 62 | index, err := randUint32() |
| 63 | if err != nil { |
| 64 | return index, err |
| 65 | } |
| 66 | |
| 67 | // check if index used |
| 68 | |
| 69 | table.RLock() |
| 70 | _, ok := table.table[index] |
| 71 | table.RUnlock() |
| 72 | if ok { |
| 73 | continue |
| 74 | } |
| 75 | |
| 76 | // check again while locked |
| 77 | |
| 78 | table.Lock() |
| 79 | _, found := table.table[index] |
| 80 | if found { |
| 81 | table.Unlock() |
| 82 | continue |
| 83 | } |
| 84 | table.table[index] = IndexTableEntry{ |
| 85 | peer: peer, |
| 86 | handshake: handshake, |
| 87 | keypair: nil, |
| 88 | } |
| 89 | table.Unlock() |
| 90 | return index, nil |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func (table *IndexTable) Lookup(id uint32) IndexTableEntry { |
| 95 | table.RLock() |
no test coverage detected