Skip to content

Commit

Permalink
Get rid of porter
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 25, 2019
1 parent 8c02d73 commit d7e140b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
45 changes: 17 additions & 28 deletions pkg/app2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/netutil"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/app2/network"
Expand All @@ -13,13 +12,12 @@ import (

// Client is used by skywire apps.
type Client struct {
log *logging.Logger
pk cipher.PubKey
pid ProcID
rpc RPCClient
lm *idManager // contains listeners associated with their IDs
cm *idManager // contains connections associated with their IDs
porter *netutil.Porter
log *logging.Logger
pk cipher.PubKey
pid ProcID
rpc RPCClient
lm *idManager // contains listeners associated with their IDs
cm *idManager // contains connections associated with their IDs
}

// NewClient creates a new `Client`. The `Client` needs to be provided with:
Expand All @@ -29,13 +27,12 @@ type Client struct {
// - rpc: RPC client to communicate with the server.
func NewClient(log *logging.Logger, localPK cipher.PubKey, pid ProcID, rpc RPCClient) *Client {
return &Client{
log: log,
pk: localPK,
pid: pid,
rpc: rpc,
lm: newIDManager(),
cm: newIDManager(),
porter: netutil.NewPorter(netutil.PorterMinEphemeral),
log: log,
pk: localPK,
pid: pid,
rpc: rpc,
lm: newIDManager(),
cm: newIDManager(),
}
}

Expand Down Expand Up @@ -73,11 +70,6 @@ func (c *Client) Dial(remote network.Addr) (net.Conn, error) {

// Listen listens on the specified `port` for the incoming connections.
func (c *Client) Listen(n network.Type, port routing.Port) (net.Listener, error) {
ok, freePort := c.porter.Reserve(uint16(port), nil)
if !ok {
return nil, ErrPortAlreadyBound
}

local := network.Addr{
Net: n,
PubKey: c.pk,
Expand All @@ -86,17 +78,15 @@ func (c *Client) Listen(n network.Type, port routing.Port) (net.Listener, error)

lisID, err := c.rpc.Listen(local)
if err != nil {
freePort()
return nil, err
}

listener := &Listener{
log: c.log,
id: lisID,
rpc: c.rpc,
addr: local,
cm: newIDManager(),
freePort: freePort,
log: c.log,
id: lisID,
rpc: c.rpc,
addr: local,
cm: newIDManager(),
}

freeLis, err := c.lm.add(lisID, listener)
Expand All @@ -105,7 +95,6 @@ func (c *Client) Listen(n network.Type, port routing.Port) (net.Listener, error)
c.log.WithError(err).Error("error closing listener")
}

freePort()
return nil, err
}

Expand Down
14 changes: 6 additions & 8 deletions pkg/app2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
// Listener is a listener for app server connections.
// Implements `net.Listener`.
type Listener struct {
log *logging.Logger
id uint16
rpc RPCClient
addr network.Addr
cm *idManager // contains conns associated with their IDs
freePort func()
freeLis func()
log *logging.Logger
id uint16
rpc RPCClient
addr network.Addr
cm *idManager // contains conns associated with their IDs
freeLis func()
}

func (l *Listener) Accept() (net.Conn, error) {
Expand Down Expand Up @@ -49,7 +48,6 @@ func (l *Listener) Accept() (net.Conn, error) {

func (l *Listener) Close() error {
defer func() {
l.freePort()
if l.freeLis != nil {
l.freeLis()
}
Expand Down

0 comments on commit d7e140b

Please sign in to comment.