From d4e7e8e501fb49d40a9b79cd00ac813271279129 Mon Sep 17 00:00:00 2001 From: lwch Date: Fri, 13 Aug 2021 17:42:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F=E5=90=8D?= =?UTF-8?q?=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/client/client/client.go | 36 ++++++++++++++++---------------- code/client/client/send.go | 2 +- code/client/client/tunnel.go | 38 +++++++++++++++++----------------- code/server/handler/client.go | 34 +++++++++++++++--------------- code/server/handler/handler.go | 24 ++++++++++----------- 5 files changed, 67 insertions(+), 67 deletions(-) diff --git a/code/client/client/client.go b/code/client/client/client.go index 392507d..a116934 100644 --- a/code/client/client/client.go +++ b/code/client/client/client.go @@ -16,17 +16,17 @@ import ( type Client struct { sync.RWMutex - cfg *global.Configure - conn *network.Conn - tunnels map[string]*tunnel + cfg *global.Configure + conn *network.Conn + links map[string]*link } // New create client func New(cfg *global.Configure, conn *network.Conn) *Client { return &Client{ - cfg: cfg, - conn: conn, - tunnels: make(map[string]*tunnel), + cfg: cfg, + conn: conn, + links: make(map[string]*link), } } @@ -97,18 +97,18 @@ func (c *Client) handleTcpTunnel(ctx context.Context, t global.Tunnel) { id, err := runtime.UUID(16, "0123456789abcdef") if err != nil { conn.Close() - logging.Error("generate tunnel id failed, err=%v", err) + logging.Error("generate link id failed, err=%v", err) continue } - tn := newTunnel(id, t.Name, t.Target, c, conn) - c.sendConnect(tn.id, t) + link := newLink(id, t.Name, t.Target, c, conn) + c.sendConnect(link.id, t) c.Lock() - c.tunnels[tn.id] = tn + c.links[link.id] = link c.Unlock() - go tn.loop(ctx) + go link.loop(ctx) } } @@ -140,12 +140,12 @@ func (c *Client) handleConnect(ctx context.Context, from, to string, req *networ c.connectError(to, req.GetId(), err.Error()) return } - tn := newTunnel(req.GetId(), req.GetName(), from, c, conn) + link := newLink(req.GetId(), req.GetName(), from, c, conn) c.Lock() - c.tunnels[tn.id] = tn + c.links[link.id] = link c.Unlock() c.connectOK(to, req.GetId()) - go tn.loop(ctx) + go link.loop(ctx) } // handleDisconnect handle disconnect message from remote, this means remote connection is closed @@ -153,14 +153,14 @@ func (c *Client) handleDisconnect(data *network.Disconnect) { id := data.GetId() c.RLock() - tn := c.tunnels[id] + tn := c.links[id] c.RUnlock() if tn != nil { tn.close() c.Lock() - delete(c.tunnels, id) + delete(c.links, id) c.Unlock() } } @@ -169,10 +169,10 @@ func (c *Client) handleDisconnect(data *network.Disconnect) { func (c *Client) handleData(data *network.Data) { id := data.GetCid() c.RLock() - tn := c.tunnels[id] + tn := c.links[id] c.RUnlock() if tn == nil { - logging.Error("tunnel %s not found", id) + logging.Error("link %s not found", id) return } tn.write(data.GetData()) diff --git a/code/client/client/send.go b/code/client/client/send.go index d7767a5..c2e4628 100644 --- a/code/client/client/send.go +++ b/code/client/client/send.go @@ -95,7 +95,7 @@ func (c *Client) disconnect(id, to string) { } c.conn.WriteMessage(&msg, time.Second) c.Lock() - delete(c.tunnels, id) + delete(c.links, id) c.Unlock() } diff --git a/code/client/client/tunnel.go b/code/client/client/tunnel.go index c4cb5c7..c4f8b3d 100644 --- a/code/client/client/tunnel.go +++ b/code/client/client/tunnel.go @@ -9,17 +9,17 @@ import ( "github.com/lwch/logging" ) -type tunnel struct { +type link struct { cli *Client - id string - name string - target string + id string // link id + name string // tunnel name + target string // remote client id c net.Conn } -func newTunnel(id, name, target string, cli *Client, conn net.Conn) *tunnel { - logging.Info("create tunnel %s: %s", name, id) - return &tunnel{ +func newLink(id, name, target string, cli *Client, conn net.Conn) *link { + logging.Info("create link %s: %s", name, id) + return &link{ cli: cli, id: id, name: name, @@ -28,16 +28,16 @@ func newTunnel(id, name, target string, cli *Client, conn net.Conn) *tunnel { } } -func (tn *tunnel) close() { - logging.Info("disconnect tunnel %s: %s", tn.name, tn.id) - err := tn.c.Close() +func (l *link) close() { + logging.Info("disconnect tunnel %s on link %s", l.name, l.id) + err := l.c.Close() if err == nil { - tn.cli.disconnect(tn.id, tn.target) + l.cli.disconnect(l.id, l.target) } } -func (tn *tunnel) loop(ctx context.Context) { - defer tn.close() +func (l *link) loop(ctx context.Context) { + defer l.close() buf := make([]byte, 32*1024) for { select { @@ -45,20 +45,20 @@ func (tn *tunnel) loop(ctx context.Context) { return default: } - n, err := tn.c.Read(buf) + n, err := l.c.Read(buf) if err != nil { return } if n == 0 { continue } - logging.Debug("tunnel %s read from local %d bytes", tn.name, n) - tn.cli.send(tn.id, tn.target, buf[:n]) + logging.Debug("link %s on tunnel %s read from local %d bytes", l.id, l.name, n) + l.cli.send(l.id, l.target, buf[:n]) } } -func (tn *tunnel) write(data []byte) error { - logging.Debug("tunnel %s write from remote %d bytes", tn.name, len(data)) - _, err := io.Copy(tn.c, bytes.NewReader(data)) +func (l *link) write(data []byte) error { + logging.Debug("link %s on tunnel %s write from remote %d bytes", l.id, l.name, len(data)) + _, err := io.Copy(l.c, bytes.NewReader(data)) return err } diff --git a/code/server/handler/client.go b/code/server/handler/client.go index 57ab000..7a2d956 100644 --- a/code/server/handler/client.go +++ b/code/server/handler/client.go @@ -11,18 +11,18 @@ import ( type client struct { sync.RWMutex - parent *Handler - id string - c *network.Conn - tunnels map[string]struct{} + parent *Handler + id string + c *network.Conn + links map[string]struct{} // link id => struct{} } func newClient(parent *Handler, id string, conn *network.Conn) *client { return &client{ - parent: parent, - id: id, - c: conn, - tunnels: make(map[string]struct{}), + parent: parent, + id: id, + c: conn, + links: make(map[string]struct{}), } } @@ -44,23 +44,23 @@ func (c *client) writeMessage(msg *network.Msg) error { return c.c.WriteMessage(msg, time.Second) } -func (c *client) addTunnel(id string) { +func (c *client) addLink(id string) { c.Lock() - c.tunnels[id] = struct{}{} + c.links[id] = struct{}{} c.Unlock() } -func (c *client) removeTunnel(id string) { +func (c *client) removeLink(id string) { c.Lock() - delete(c.tunnels, id) + delete(c.links, id) c.Unlock() } -func (c *client) getTunnels() []string { - ret := make([]string, 0, len(c.tunnels)) +func (c *client) getLinks() []string { + ret := make([]string, 0, len(c.links)) c.RLock() - for tn := range c.tunnels { - ret = append(ret, tn) + for link := range c.links { + ret = append(ret, link) } c.RUnlock() return ret @@ -78,6 +78,6 @@ func (c *client) close(id string) { } c.c.WriteMessage(&msg, time.Second) c.Lock() - delete(c.tunnels, id) + delete(c.links, id) c.Unlock() } diff --git a/code/server/handler/handler.go b/code/server/handler/handler.go index f8c2ef9..c06f456 100644 --- a/code/server/handler/handler.go +++ b/code/server/handler/handler.go @@ -15,7 +15,7 @@ type Handler struct { sync.RWMutex cfg *global.Configure clients map[string]*client // client id => client - tunnels map[string][2]*client // tunnel id => endpoints + links map[string][2]*client // link id => endpoints } // New create handler @@ -23,7 +23,7 @@ func New(cfg *global.Configure) *Handler { return &Handler{ cfg: cfg, clients: make(map[string]*client), - tunnels: make(map[string][2]*client), + links: make(map[string][2]*client), } } @@ -112,33 +112,33 @@ func (h *Handler) msgHook(msg *network.Msg) { id := msg.GetCrep().GetId() var pair [2]*client if fromCli != nil { - fromCli.addTunnel(id) + fromCli.addLink(id) pair[0] = fromCli } if toCli != nil { - toCli.addTunnel(id) + toCli.addLink(id) pair[1] = toCli } h.Lock() - h.tunnels[id] = pair + h.links[id] = pair h.Unlock() } case network.Msg_disconnect: if fromCli != nil { - fromCli.removeTunnel(msg.GetXDisconnect().GetId()) + fromCli.removeLink(msg.GetXDisconnect().GetId()) } if toCli != nil { - toCli.removeTunnel(msg.GetXDisconnect().GetId()) + toCli.removeLink(msg.GetXDisconnect().GetId()) } } } -// closeAll close all tunnel from client +// closeAll close all links from client func (h *Handler) closeAll(cli *client) { - tunnels := cli.getTunnels() - for _, t := range tunnels { + links := cli.getLinks() + for _, t := range links { h.RLock() - pair := h.tunnels[t] + pair := h.links[t] h.RUnlock() if pair[0] != nil { pair[0].close(t) @@ -147,7 +147,7 @@ func (h *Handler) closeAll(cli *client) { pair[1].close(t) } h.Lock() - delete(h.tunnels, t) + delete(h.links, t) h.Unlock() } h.Lock()