You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.1 KiB

package peer
import (
"sync"
"net"
)
/* Listener */
type Listener struct {
listener net.Listener
handler func(net.Conn)
mtx sync.Mutex
closed bool
}
func NewListener(protocol string, laddr string, handler func(net.Conn)) *Listener {
ln, err := net.Listen(protocol, laddr)
if err != nil { panic(err) }
s := &Listener{
listener: ln,
handler: handler,
}
go s.listen()
return s
}
func (s *Listener) listen() {
for {
conn, err := s.listener.Accept()
if err != nil {
// lock & defer
s.mtx.Lock(); defer s.mtx.Unlock()
if s.closed {
return
} else {
panic(err)
}
// unlock (deferred)
}
go s.handler(conn)
}
}
func (s *Listener) LocalAddress() *NetAddress {
return NewNetAddress(s.listener.Addr())
}
func (s *Listener) Close() {
// lock
s.mtx.Lock()
s.closed = true
s.mtx.Unlock()
// unlock
s.listener.Close()
}