From 13faadabcd76b03228050f74fad6881872f896ed Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 7 Jan 2025 23:59:39 +0900 Subject: [PATCH] updated to assign connection id and route id from 1, using 0 as an automatic assigned marker updated the client-side api to accept a route id for route creation --- README.md | 3 ++- client-ctl.go | 2 ++ client.go | 64 ++++++++++++++++++++++++++++++++++++--------------- server.go | 39 ++++++++++++++++++++----------- 4 files changed, 75 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 88ccdcf..01b659a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## normal operation -- ./hodu server --rcp-on=0.0.0.0:9999 --ctl-on=0.0.0.0:8888 --pxy-on=0.0.0.0:9998 --wpx-on=0.0.0.0:9997 +- ./hodu server --rpc-on=0.0.0.0:9999 --ctl-on=0.0.0.0:8888 --pxy-on=0.0.0.0:9998 --wpx-on=0.0.0.0:9997 - ./hodu client --rpc-to=127.0.0.1:9999 --ctl-on=127.0.0.1:7777 192.168.1.130:8000 ## server.json @@ -25,6 +25,7 @@ ``` { + "id": 0, "client-peer-addr": "192.168.1.104:22", "client-peer-name": "Star gate", "server-peer-option": "tcp4 ssh", diff --git a/client-ctl.go b/client-ctl.go index 227611f..22bb96f 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -33,6 +33,7 @@ type json_in_client_conn struct { } type json_in_client_route struct { + Id RouteId `json:"id"` // 0 for auto-assignement. ClientPeerAddr string `json:"client-peer-addr"` ClientPeerName string `json:"client-peer-name"` ServerPeerOption string `json:"server-peer-option"` @@ -426,6 +427,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r } rc = &ClientRouteConfig{ + Id: jcr.Id, PeerAddr: jcr.ClientPeerAddr, PeerName: jcr.ClientPeerName, Option: server_peer_option, diff --git a/client.go b/client.go index 2e1de98..9df211c 100644 --- a/client.go +++ b/client.go @@ -28,6 +28,7 @@ type ClientPeerCancelFuncMap = map[PeerId]context.CancelFunc // -------------------------------------------------------------------- type ClientRouteConfig struct { + Id RouteId // requested id to be assigned. 0 for automatic assignment PeerAddr string PeerName string Option RouteOption @@ -638,7 +639,7 @@ func NewClientConn(c *Client, cfg *ClientConfig) *ClientConn { cts.cli = c cts.route_map = make(ClientRouteMap) - cts.route_next_id = 0 + cts.route_next_id = 1 cts.cfg.ClientConfig = *cfg cts.stop_req.Store(false) cts.stop_chan = make(chan bool, 8) @@ -651,25 +652,43 @@ func NewClientConn(c *Client, cfg *ClientConfig) *ClientConn { func (cts *ClientConn) AddNewClientRoute(rc *ClientRouteConfig) (*ClientRoute, error) { var r *ClientRoute - var start_id RouteId + var assigned_id RouteId cts.route_mtx.Lock() - //start_id = RouteId(rand.Uint64()) - start_id = cts.route_next_id - for { - var ok bool - _, ok = cts.route_map[cts.route_next_id] - if !ok { break } - cts.route_next_id++ - if cts.route_next_id == start_id { - cts.route_mtx.Unlock() - return nil, fmt.Errorf("unable to assign id") + if rc.Id <= 0 { + // perform automatic assignemnt + var start_id RouteId + + //start_id = RouteId(rand.Uint64()) + start_id = cts.route_next_id + for { + var ok bool + _, ok = cts.route_map[cts.route_next_id] + if !ok { + assigned_id = cts.route_next_id + cts.route_next_id++ + if cts.route_next_id == 0 { cts.route_next_id++ } + break + } + cts.route_next_id++ + if cts.route_next_id == 0 { cts.route_next_id++ } // skip 0 as it is a marker for auto-assignment + if cts.route_next_id == start_id { + cts.route_mtx.Unlock() + return nil, fmt.Errorf("unable to assign id") + } } + } else { + var ok bool + _, ok = cts.route_map[rc.Id] + if ok { + cts.route_mtx.Unlock() + return nil, fmt.Errorf("id(%d) unavailable", rc.Id) + } + assigned_id = rc.Id } - r = NewClientRoute(cts, cts.route_next_id, rc.PeerAddr, rc.PeerName, rc.ServiceAddr, rc.ServiceNet, rc.Option, rc.Lifetime) + r = NewClientRoute(cts, assigned_id, rc.PeerAddr, rc.PeerName, rc.ServiceAddr, rc.ServiceNet, rc.Option, rc.Lifetime) cts.route_map[r.id] = r - cts.route_next_id++ cts.cli.stats.routes.Add(1) cts.route_mtx.Unlock() @@ -872,7 +891,7 @@ start_over: } cts.s_seed = *s_seed cts.c_seed = c_seed - cts.route_next_id = 0 // reset this whenever a new connection is made. the number of routes must be zero. + cts.route_next_id = 1 // reset this whenever a new connection is made. the number of routes must be zero. cts.cli.log.Write(cts.sid, LOG_INFO, "Got seed from server[%d] %s - ver=%#x", cts.cfg.Index, cts.cfg.ServerAddrs[cts.cfg.Index], cts.s_seed.Version) @@ -1165,7 +1184,7 @@ func NewClient(ctx context.Context, logger Logger, ctl_addrs []string, ctl_prefi c.ptc_tmout = peer_conn_tmout c.ptc_limit = peer_max c.cts_limit = rpc_max - c.cts_next_id = 0 + c.cts_next_id = 1 c.cts_map = make(ClientConnMap) c.stop_req.Store(false) c.stop_chan = make(chan bool, 8) @@ -1215,6 +1234,7 @@ func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) { var cts *ClientConn var ok bool var start_id ConnId + var assigned_id ConnId if len(cfg.ServerAddrs) <= 0 { return nil, fmt.Errorf("no server rpc address specified") @@ -1234,18 +1254,24 @@ func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) { start_id = c.cts_next_id for { _, ok = c.cts_map[c.cts_next_id] - if !ok { break } + if !ok { + assigned_id = c.cts_next_id + c.cts_next_id++ + if c.cts_next_id == 0 { c.cts_next_id++ } + break + } c.cts_next_id++ + if c.cts_next_id == 0 { c.cts_next_id++ } if c.cts_next_id == start_id { c.cts_mtx.Lock() return nil, fmt.Errorf("unable to assign id") } } - cts.id = c.cts_next_id + + cts.id = assigned_id cts.sid = fmt.Sprintf("%d", cts.id) // id in string used for logging c.cts_map[cts.id] = cts - c.cts_next_id++ c.stats.conns.Add(1) c.cts_mtx.Unlock() diff --git a/server.go b/server.go index 747d23a..8a33d10 100644 --- a/server.go +++ b/server.go @@ -212,7 +212,7 @@ func NewServerRoute(cts *ServerConn, id RouteId, option RouteOption, ptc_addr st r.PtcName = ptc_name r.pts_limit = PTS_LIMIT r.pts_map = make(ServerPeerConnMap) - r.pts_next_id = 0 + r.pts_next_id = 1 r.stop_req.Store(false) return &r, nil @@ -222,6 +222,7 @@ func (r *ServerRoute) AddNewServerPeerConn(c *net.TCPConn) (*ServerPeerConn, err var pts *ServerPeerConn var ok bool var start_id PeerId + var assigned_id PeerId r.pts_mtx.Lock() defer r.pts_mtx.Unlock() @@ -234,18 +235,21 @@ func (r *ServerRoute) AddNewServerPeerConn(c *net.TCPConn) (*ServerPeerConn, err for { _, ok = r.pts_map[r.pts_next_id] if !ok { + assigned_id = r.pts_next_id + r.pts_next_id++ + if r.pts_next_id == 0 { r.pts_next_id++ } break } r.pts_next_id++ + if r.pts_next_id == 0 { r.pts_next_id++ } if r.pts_next_id == start_id { // unlikely to happen but it cycled through the whole range. return nil, fmt.Errorf("failed to assign peer-to-server connection id") } } - pts = NewServerPeerConn(r, c, r.pts_next_id) + pts = NewServerPeerConn(r, c, assigned_id) r.pts_map[pts.conn_id] = pts - r.pts_next_id++ r.Cts.svr.stats.peers.Add(1) return pts, nil @@ -837,8 +841,11 @@ func (cc *ConnCatcher) HandleConn(ctx context.Context, cs stats.ConnStats) { cc.server.log.Write("", LOG_INFO, "Client connected - %s", addr) case *stats.ConnEnd: - cc.server.log.Write("", LOG_INFO, "Client disconnected - %s", addr) - cc.server.RemoveServerConnByAddr(p.Addr) + var cts *ServerConn + var log_id string + cts, _ = cc.server.RemoveServerConnByAddr(p.Addr) + if cts != nil { log_id = cts.sid } + cc.server.log.Write(log_id, LOG_INFO, "Client disconnected - %s", addr) } } @@ -984,7 +991,7 @@ func NewServer(ctx context.Context, logger Logger, ctl_addrs []string, rpc_addrs s.ext_svcs = make([]Service, 0, 1) s.pts_limit = peer_max s.cts_limit = rpc_max - s.cts_next_id = 0 + s.cts_next_id = 1 s.cts_map = make(ServerConnMap) s.cts_map_by_addr = make(ServerConnMapByAddr) s.svc_port_map = make(ServerSvcPortMap) @@ -1359,6 +1366,7 @@ func (s *Server) ReqStop() { func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, pss Hodu_PacketStreamServer) (*ServerConn, error) { var cts ServerConn var start_id ConnId + var assigned_id ConnId var ok bool cts.svr = s @@ -1382,14 +1390,20 @@ func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, p start_id = s.cts_next_id for { _, ok = s.cts_map[s.cts_next_id] - if !ok { break } + if !ok { + assigned_id = s.cts_next_id + s.cts_next_id++; + if s.cts_next_id == 0 { s.cts_next_id++ } + break + } s.cts_next_id++ + if s.cts_next_id == 0 { s.cts_next_id++ } if s.cts_next_id == start_id { s.cts_mtx.Unlock() return nil, fmt.Errorf("unable to assign id") } } - cts.Id = s.cts_next_id + cts.Id = assigned_id cts.sid = fmt.Sprintf("%d", cts.Id) // id in string used for logging _, ok = s.cts_map_by_addr[cts.RemoteAddr] @@ -1399,11 +1413,10 @@ func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, p } s.cts_map_by_addr[cts.RemoteAddr] = &cts s.cts_map[cts.Id] = &cts; - s.cts_next_id++; s.stats.conns.Store(int64(len(s.cts_map))) s.cts_mtx.Unlock() - s.log.Write("", LOG_DEBUG, "Added client connection from %s", cts.RemoteAddr.String()) + s.log.Write(cts.sid, LOG_DEBUG, "Added client connection from %s", cts.RemoteAddr.String()) return &cts, nil } @@ -1439,7 +1452,7 @@ func (s *Server) RemoveServerConn(cts *ServerConn) error { return nil } -func (s *Server) RemoveServerConnByAddr(addr net.Addr) error { +func (s *Server) RemoveServerConnByAddr(addr net.Addr) (*ServerConn, error) { var cts *ServerConn var ok bool @@ -1448,7 +1461,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error { cts, ok = s.cts_map_by_addr[addr] if !ok { s.cts_mtx.Unlock() - return fmt.Errorf("non-existent connection address - %s", addr.String()) + return nil, fmt.Errorf("non-existent connection address - %s", addr.String()) } delete(s.cts_map, cts.Id) delete(s.cts_map_by_addr, cts.RemoteAddr) @@ -1456,7 +1469,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error { s.cts_mtx.Unlock() cts.ReqStop() - return nil + return cts, nil } func (s *Server) FindServerConnById(id ConnId) *ServerConn {