| 123456789101112131415161718192021222324252627282930313233 |
- package p2p
- import (
- "blockchain-go/p2p/discover"
- "errors"
- "net"
- )
- // sharedUDPConn implements a shared connection. Write sends messages to the underlying connection while read returns
- // messages that were found unprocessable and sent to the unhandled channel by the primary listener.
- type sharedUDPConn struct {
- *net.UDPConn
- unhandled chan discover.ReadPacket
- }
- // ReadFromUDP implements discover.UDPConn
- func (s *sharedUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
- packet, ok := <-s.unhandled
- if !ok {
- return 0, nil, errors.New("connection was closed")
- }
- l := len(packet.Data)
- if l > len(b) {
- l = len(b)
- }
- copy(b[:l], packet.Data[:l])
- return l, packet.Addr, nil
- }
- // Close implements discover.UDPConn
- func (s *sharedUDPConn) Close() error {
- return nil
- }
|