-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
196 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package app2 | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
|
||
"github.com/skycoin/skywire/pkg/routing" | ||
) | ||
|
||
const ( | ||
listenerBufSize = 1000 | ||
) | ||
|
||
var ( | ||
ErrListenerClosed = errors.New("listener closed") | ||
) | ||
|
||
type Listener struct { | ||
addr routing.Addr | ||
conns chan net.Conn | ||
lm *listenersManager | ||
} | ||
|
||
func NewListener(addr routing.Addr, lm *listenersManager) *Listener { | ||
return &Listener{ | ||
addr: addr, | ||
conns: make(chan net.Conn, listenerBufSize), | ||
lm: lm, | ||
} | ||
} | ||
|
||
func (l *Listener) Accept() (net.Conn, error) { | ||
conn, ok := <-l.conns | ||
if !ok { | ||
return nil, ErrListenerClosed | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func (l *Listener) Close() error { | ||
if err := l.lm.remove(l.addr.Port); err != nil { | ||
return err | ||
} | ||
|
||
// TODO: send ListenEnd frame | ||
close(l.conns) | ||
|
||
return nil | ||
} | ||
|
||
func (l *Listener) Addr() net.Addr { | ||
return l.addr | ||
} | ||
|
||
func (l *Listener) addConn(conn net.Conn) { | ||
l.conns <- conn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package app2 | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/skycoin/skywire/pkg/routing" | ||
) | ||
|
||
var ( | ||
ErrPortAlreadyBound = errors.New("port is already bound") | ||
ErrNoListenerOnPort = errors.New("no listener on port") | ||
) | ||
|
||
type listenersManager struct { | ||
listeners map[routing.Port]*Listener | ||
mx sync.RWMutex | ||
} | ||
|
||
func newListenersManager() *listenersManager { | ||
return &listenersManager{ | ||
listeners: make(map[routing.Port]*Listener), | ||
} | ||
} | ||
|
||
func (lm *listenersManager) portIsBound(port routing.Port) bool { | ||
lm.mx.RLock() | ||
_, ok := lm.listeners[port] | ||
lm.mx.RUnlock() | ||
return ok | ||
} | ||
|
||
func (lm *listenersManager) add(port routing.Port, l *Listener) error { | ||
lm.mx.Lock() | ||
if _, ok := lm.listeners[port]; ok { | ||
lm.mx.Unlock() | ||
return ErrPortAlreadyBound | ||
} | ||
lm.listeners[port] = l | ||
lm.mx.Unlock() | ||
return nil | ||
} | ||
|
||
func (lm *listenersManager) remove(port routing.Port) error { | ||
lm.mx.Lock() | ||
if _, ok := lm.listeners[port]; !ok { | ||
lm.mx.Unlock() | ||
return ErrNoListenerOnPort | ||
} | ||
delete(lm.listeners, port) | ||
lm.mx.Unlock() | ||
return nil | ||
} | ||
|
||
func (lm *listenersManager) addConn(port routing.Port, conn net.Conn) error { | ||
lm.mx.RLock() | ||
if _, ok := lm.listeners[port]; !ok { | ||
lm.mx.RUnlock() | ||
return ErrNoListenerOnPort | ||
} | ||
lm.listeners[port].addConn(conn) | ||
lm.mx.RUnlock() | ||
return nil | ||
} |