Skip to content

Commit

Permalink
acceptedConn -> clientConn, clientConn -> serverConn
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 10, 2019
1 parent a07d8a0 commit 8dc1513
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
23 changes: 23 additions & 0 deletions pkg/app2/client_conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package app2

import (
"net"

"github.com/skycoin/skywire/pkg/routing"
)

// clientConn serves as a wrapper for `net.Conn` being returned to the
// app client side from `Accept` func
type clientConn struct {
remote routing.Addr
local routing.Addr
net.Conn
}

func (c *clientConn) RemoteAddr() net.Addr {
return c.remote
}

func (c *clientConn) LocalAddr() net.Addr {
return c.local
}
16 changes: 3 additions & 13 deletions pkg/app2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/routing"
)

Expand All @@ -17,18 +16,9 @@ var (
ErrListenerClosed = errors.New("listener closed")
)

type acceptedConn struct {
remote routing.Addr
net.Conn
}

func (c *acceptedConn) Addr() net.Addr {
return c.remote
}

type Listener struct {
addr routing.Addr
conns chan *acceptedConn
conns chan *clientConn
stopListening func(port routing.Port) error
logger *logging.Logger
lm *listenersManager
Expand All @@ -39,7 +29,7 @@ func NewListener(addr routing.Addr, lm *listenersManager, procID ProcID,
stopListening func(port routing.Port) error, l *logging.Logger) *Listener {
return &Listener{
addr: addr,
conns: make(chan *acceptedConn, listenerBufSize),
conns: make(chan *clientConn, listenerBufSize),
lm: lm,
stopListening: stopListening,
logger: l,
Expand Down Expand Up @@ -83,6 +73,6 @@ func (l *Listener) Addr() net.Addr {
return l.addr
}

func (l *Listener) addConn(conn *acceptedConn) {
func (l *Listener) addConn(conn *clientConn) {
l.conns <- conn
}
2 changes: 1 addition & 1 deletion pkg/app2/listeners_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (lm *listenersManager) addConn(localPort routing.Port, remote routing.Addr,
lm.mx.RUnlock()
return ErrNoListenerOnPort
}
lm.listeners[localPort].addConn(&acceptedConn{
lm.listeners[localPort].addConn(&clientConn{
remote: remote,
Conn: conn,
})
Expand Down
18 changes: 9 additions & 9 deletions pkg/app2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/skycoin/dmsg/cipher"
)

type clientConn struct {
type serverConn struct {
procID ProcID
conn net.Conn
session *yamux.Session
Expand All @@ -34,7 +34,7 @@ type clientConn struct {
type Server struct {
PK cipher.PubKey
dmsgC *dmsg.Client
apps map[string]*clientConn
apps map[string]*serverConn
appsMx sync.RWMutex
logger *logging.Logger
}
Expand All @@ -43,7 +43,7 @@ func NewServer(localPK cipher.PubKey, dmsgC *dmsg.Client, l *logging.Logger) *Se
return &Server{
PK: localPK,
dmsgC: dmsgC,
apps: make(map[string]*clientConn),
apps: make(map[string]*serverConn),
logger: l,
}
}
Expand All @@ -70,7 +70,7 @@ func (s *Server) Serve(sockAddr string) error {
return errors.Wrap(err, "error creating yamux session")
}

s.apps[conn.RemoteAddr().String()] = &clientConn{
s.apps[conn.RemoteAddr().String()] = &serverConn{
session: session,
conn: conn,
lm: newListenersManager(),
Expand All @@ -83,7 +83,7 @@ func (s *Server) Serve(sockAddr string) error {
}
}

func (s *Server) serveClient(conn *clientConn) error {
func (s *Server) serveClient(conn *serverConn) error {
for {
stream, err := conn.session.Accept()
if err != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *Server) serveClient(conn *clientConn) error {
}
}

func (s *Server) serveStream(stream net.Conn, conn *clientConn) error {
func (s *Server) serveStream(stream net.Conn, conn *serverConn) error {
for {
hsFrame, err := readHSFrame(stream)
if err != nil {
Expand Down Expand Up @@ -209,7 +209,7 @@ func (s *Server) forwardOverDMSG(stream net.Conn, tp *dmsg.Transport) error {
return nil
}

func (c *clientConn) reserveListener(port routing.Port) error {
func (c *serverConn) reserveListener(port routing.Port) error {
c.dmsgListenersMx.Lock()
if _, ok := c.dmsgListeners[port]; ok {
c.dmsgListenersMx.Unlock()
Expand All @@ -220,7 +220,7 @@ func (c *clientConn) reserveListener(port routing.Port) error {
return nil
}

func (c *clientConn) addListener(port routing.Port, l *dmsg.Listener) error {
func (c *serverConn) addListener(port routing.Port, l *dmsg.Listener) error {
c.dmsgListenersMx.Lock()
if lis, ok := c.dmsgListeners[port]; ok && lis != nil {
c.dmsgListenersMx.Unlock()
Expand All @@ -232,7 +232,7 @@ func (c *clientConn) addListener(port routing.Port, l *dmsg.Listener) error {
return nil
}

func (c *clientConn) acceptDMSG(l *dmsg.Listener) error {
func (c *serverConn) acceptDMSG(l *dmsg.Listener) error {
for {
stream, err := c.session.Open()
if err != nil {
Expand Down

0 comments on commit 8dc1513

Please sign in to comment.