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
This commit is contained in:
parent
b8bd8f4d4c
commit
13faadabcd
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
## normal operation
|
## 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
|
- ./hodu client --rpc-to=127.0.0.1:9999 --ctl-on=127.0.0.1:7777 192.168.1.130:8000
|
||||||
|
|
||||||
## server.json
|
## server.json
|
||||||
@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
|
"id": 0,
|
||||||
"client-peer-addr": "192.168.1.104:22",
|
"client-peer-addr": "192.168.1.104:22",
|
||||||
"client-peer-name": "Star gate",
|
"client-peer-name": "Star gate",
|
||||||
"server-peer-option": "tcp4 ssh",
|
"server-peer-option": "tcp4 ssh",
|
||||||
|
@ -33,6 +33,7 @@ type json_in_client_conn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type json_in_client_route struct {
|
type json_in_client_route struct {
|
||||||
|
Id RouteId `json:"id"` // 0 for auto-assignement.
|
||||||
ClientPeerAddr string `json:"client-peer-addr"`
|
ClientPeerAddr string `json:"client-peer-addr"`
|
||||||
ClientPeerName string `json:"client-peer-name"`
|
ClientPeerName string `json:"client-peer-name"`
|
||||||
ServerPeerOption string `json:"server-peer-option"`
|
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{
|
rc = &ClientRouteConfig{
|
||||||
|
Id: jcr.Id,
|
||||||
PeerAddr: jcr.ClientPeerAddr,
|
PeerAddr: jcr.ClientPeerAddr,
|
||||||
PeerName: jcr.ClientPeerName,
|
PeerName: jcr.ClientPeerName,
|
||||||
Option: server_peer_option,
|
Option: server_peer_option,
|
||||||
|
46
client.go
46
client.go
@ -28,6 +28,7 @@ type ClientPeerCancelFuncMap = map[PeerId]context.CancelFunc
|
|||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
type ClientRouteConfig struct {
|
type ClientRouteConfig struct {
|
||||||
|
Id RouteId // requested id to be assigned. 0 for automatic assignment
|
||||||
PeerAddr string
|
PeerAddr string
|
||||||
PeerName string
|
PeerName string
|
||||||
Option RouteOption
|
Option RouteOption
|
||||||
@ -638,7 +639,7 @@ func NewClientConn(c *Client, cfg *ClientConfig) *ClientConn {
|
|||||||
|
|
||||||
cts.cli = c
|
cts.cli = c
|
||||||
cts.route_map = make(ClientRouteMap)
|
cts.route_map = make(ClientRouteMap)
|
||||||
cts.route_next_id = 0
|
cts.route_next_id = 1
|
||||||
cts.cfg.ClientConfig = *cfg
|
cts.cfg.ClientConfig = *cfg
|
||||||
cts.stop_req.Store(false)
|
cts.stop_req.Store(false)
|
||||||
cts.stop_chan = make(chan bool, 8)
|
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) {
|
func (cts *ClientConn) AddNewClientRoute(rc *ClientRouteConfig) (*ClientRoute, error) {
|
||||||
var r *ClientRoute
|
var r *ClientRoute
|
||||||
var start_id RouteId
|
var assigned_id RouteId
|
||||||
|
|
||||||
cts.route_mtx.Lock()
|
cts.route_mtx.Lock()
|
||||||
|
if rc.Id <= 0 {
|
||||||
|
// perform automatic assignemnt
|
||||||
|
var start_id RouteId
|
||||||
|
|
||||||
//start_id = RouteId(rand.Uint64())
|
//start_id = RouteId(rand.Uint64())
|
||||||
start_id = cts.route_next_id
|
start_id = cts.route_next_id
|
||||||
for {
|
for {
|
||||||
var ok bool
|
var ok bool
|
||||||
_, ok = cts.route_map[cts.route_next_id]
|
_, ok = cts.route_map[cts.route_next_id]
|
||||||
if !ok { break }
|
if !ok {
|
||||||
|
assigned_id = cts.route_next_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 {
|
if cts.route_next_id == start_id {
|
||||||
cts.route_mtx.Unlock()
|
cts.route_mtx.Unlock()
|
||||||
return nil, fmt.Errorf("unable to assign id")
|
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_map[r.id] = r
|
||||||
cts.route_next_id++
|
|
||||||
cts.cli.stats.routes.Add(1)
|
cts.cli.stats.routes.Add(1)
|
||||||
cts.route_mtx.Unlock()
|
cts.route_mtx.Unlock()
|
||||||
|
|
||||||
@ -872,7 +891,7 @@ start_over:
|
|||||||
}
|
}
|
||||||
cts.s_seed = *s_seed
|
cts.s_seed = *s_seed
|
||||||
cts.c_seed = c_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)
|
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_tmout = peer_conn_tmout
|
||||||
c.ptc_limit = peer_max
|
c.ptc_limit = peer_max
|
||||||
c.cts_limit = rpc_max
|
c.cts_limit = rpc_max
|
||||||
c.cts_next_id = 0
|
c.cts_next_id = 1
|
||||||
c.cts_map = make(ClientConnMap)
|
c.cts_map = make(ClientConnMap)
|
||||||
c.stop_req.Store(false)
|
c.stop_req.Store(false)
|
||||||
c.stop_chan = make(chan bool, 8)
|
c.stop_chan = make(chan bool, 8)
|
||||||
@ -1215,6 +1234,7 @@ func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) {
|
|||||||
var cts *ClientConn
|
var cts *ClientConn
|
||||||
var ok bool
|
var ok bool
|
||||||
var start_id ConnId
|
var start_id ConnId
|
||||||
|
var assigned_id ConnId
|
||||||
|
|
||||||
if len(cfg.ServerAddrs) <= 0 {
|
if len(cfg.ServerAddrs) <= 0 {
|
||||||
return nil, fmt.Errorf("no server rpc address specified")
|
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
|
start_id = c.cts_next_id
|
||||||
for {
|
for {
|
||||||
_, ok = c.cts_map[c.cts_next_id]
|
_, ok = c.cts_map[c.cts_next_id]
|
||||||
if !ok { break }
|
if !ok {
|
||||||
|
assigned_id = c.cts_next_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 {
|
if c.cts_next_id == start_id {
|
||||||
c.cts_mtx.Lock()
|
c.cts_mtx.Lock()
|
||||||
return nil, fmt.Errorf("unable to assign id")
|
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
|
cts.sid = fmt.Sprintf("%d", cts.id) // id in string used for logging
|
||||||
|
|
||||||
c.cts_map[cts.id] = cts
|
c.cts_map[cts.id] = cts
|
||||||
c.cts_next_id++
|
|
||||||
c.stats.conns.Add(1)
|
c.stats.conns.Add(1)
|
||||||
c.cts_mtx.Unlock()
|
c.cts_mtx.Unlock()
|
||||||
|
|
||||||
|
39
server.go
39
server.go
@ -212,7 +212,7 @@ func NewServerRoute(cts *ServerConn, id RouteId, option RouteOption, ptc_addr st
|
|||||||
r.PtcName = ptc_name
|
r.PtcName = ptc_name
|
||||||
r.pts_limit = PTS_LIMIT
|
r.pts_limit = PTS_LIMIT
|
||||||
r.pts_map = make(ServerPeerConnMap)
|
r.pts_map = make(ServerPeerConnMap)
|
||||||
r.pts_next_id = 0
|
r.pts_next_id = 1
|
||||||
r.stop_req.Store(false)
|
r.stop_req.Store(false)
|
||||||
|
|
||||||
return &r, nil
|
return &r, nil
|
||||||
@ -222,6 +222,7 @@ func (r *ServerRoute) AddNewServerPeerConn(c *net.TCPConn) (*ServerPeerConn, err
|
|||||||
var pts *ServerPeerConn
|
var pts *ServerPeerConn
|
||||||
var ok bool
|
var ok bool
|
||||||
var start_id PeerId
|
var start_id PeerId
|
||||||
|
var assigned_id PeerId
|
||||||
|
|
||||||
r.pts_mtx.Lock()
|
r.pts_mtx.Lock()
|
||||||
defer r.pts_mtx.Unlock()
|
defer r.pts_mtx.Unlock()
|
||||||
@ -234,18 +235,21 @@ func (r *ServerRoute) AddNewServerPeerConn(c *net.TCPConn) (*ServerPeerConn, err
|
|||||||
for {
|
for {
|
||||||
_, ok = r.pts_map[r.pts_next_id]
|
_, ok = r.pts_map[r.pts_next_id]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
assigned_id = r.pts_next_id
|
||||||
|
r.pts_next_id++
|
||||||
|
if r.pts_next_id == 0 { r.pts_next_id++ }
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
r.pts_next_id++
|
r.pts_next_id++
|
||||||
|
if r.pts_next_id == 0 { r.pts_next_id++ }
|
||||||
if r.pts_next_id == start_id {
|
if r.pts_next_id == start_id {
|
||||||
// unlikely to happen but it cycled through the whole range.
|
// unlikely to happen but it cycled through the whole range.
|
||||||
return nil, fmt.Errorf("failed to assign peer-to-server connection id")
|
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_map[pts.conn_id] = pts
|
||||||
r.pts_next_id++
|
|
||||||
r.Cts.svr.stats.peers.Add(1)
|
r.Cts.svr.stats.peers.Add(1)
|
||||||
|
|
||||||
return pts, nil
|
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)
|
cc.server.log.Write("", LOG_INFO, "Client connected - %s", addr)
|
||||||
|
|
||||||
case *stats.ConnEnd:
|
case *stats.ConnEnd:
|
||||||
cc.server.log.Write("", LOG_INFO, "Client disconnected - %s", addr)
|
var cts *ServerConn
|
||||||
cc.server.RemoveServerConnByAddr(p.Addr)
|
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.ext_svcs = make([]Service, 0, 1)
|
||||||
s.pts_limit = peer_max
|
s.pts_limit = peer_max
|
||||||
s.cts_limit = rpc_max
|
s.cts_limit = rpc_max
|
||||||
s.cts_next_id = 0
|
s.cts_next_id = 1
|
||||||
s.cts_map = make(ServerConnMap)
|
s.cts_map = make(ServerConnMap)
|
||||||
s.cts_map_by_addr = make(ServerConnMapByAddr)
|
s.cts_map_by_addr = make(ServerConnMapByAddr)
|
||||||
s.svc_port_map = make(ServerSvcPortMap)
|
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) {
|
func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, pss Hodu_PacketStreamServer) (*ServerConn, error) {
|
||||||
var cts ServerConn
|
var cts ServerConn
|
||||||
var start_id ConnId
|
var start_id ConnId
|
||||||
|
var assigned_id ConnId
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
cts.svr = s
|
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
|
start_id = s.cts_next_id
|
||||||
for {
|
for {
|
||||||
_, ok = s.cts_map[s.cts_next_id]
|
_, 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++
|
s.cts_next_id++
|
||||||
|
if s.cts_next_id == 0 { s.cts_next_id++ }
|
||||||
if s.cts_next_id == start_id {
|
if s.cts_next_id == start_id {
|
||||||
s.cts_mtx.Unlock()
|
s.cts_mtx.Unlock()
|
||||||
return nil, fmt.Errorf("unable to assign id")
|
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
|
cts.sid = fmt.Sprintf("%d", cts.Id) // id in string used for logging
|
||||||
|
|
||||||
_, ok = s.cts_map_by_addr[cts.RemoteAddr]
|
_, 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_by_addr[cts.RemoteAddr] = &cts
|
||||||
s.cts_map[cts.Id] = &cts;
|
s.cts_map[cts.Id] = &cts;
|
||||||
s.cts_next_id++;
|
|
||||||
s.stats.conns.Store(int64(len(s.cts_map)))
|
s.stats.conns.Store(int64(len(s.cts_map)))
|
||||||
s.cts_mtx.Unlock()
|
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
|
return &cts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1439,7 +1452,7 @@ func (s *Server) RemoveServerConn(cts *ServerConn) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) RemoveServerConnByAddr(addr net.Addr) error {
|
func (s *Server) RemoveServerConnByAddr(addr net.Addr) (*ServerConn, error) {
|
||||||
var cts *ServerConn
|
var cts *ServerConn
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
@ -1448,7 +1461,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error {
|
|||||||
cts, ok = s.cts_map_by_addr[addr]
|
cts, ok = s.cts_map_by_addr[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
s.cts_mtx.Unlock()
|
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, cts.Id)
|
||||||
delete(s.cts_map_by_addr, cts.RemoteAddr)
|
delete(s.cts_map_by_addr, cts.RemoteAddr)
|
||||||
@ -1456,7 +1469,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error {
|
|||||||
s.cts_mtx.Unlock()
|
s.cts_mtx.Unlock()
|
||||||
|
|
||||||
cts.ReqStop()
|
cts.ReqStop()
|
||||||
return nil
|
return cts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) FindServerConnById(id ConnId) *ServerConn {
|
func (s *Server) FindServerConnById(id ConnId) *ServerConn {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user