From 2d15f0d2a4758b0906e79b96bab0c2a6b7cbd061 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 9 Dec 2024 22:41:23 +0900 Subject: [PATCH] defined PeerId, RouteId, ConnId types and made relevant changes --- client-ctl.go | 22 +++++++++--------- client-peer.go | 2 +- client.go | 61 +++++++++++++++++++++++++------------------------- packet.go | 40 ++++++++++++++++++--------------- server-ctl.go | 14 ++++++------ server-peer.go | 4 ++-- server.go | 49 ++++++++++++++++++++-------------------- 7 files changed, 99 insertions(+), 93 deletions(-) diff --git a/client-ctl.go b/client-ctl.go index 9595968..9776161 100644 --- a/client-ctl.go +++ b/client-ctl.go @@ -36,11 +36,11 @@ type json_in_client_route struct { } type json_out_client_conn_id struct { - Id uint32 `json:"id"` + Id ConnId `json:"id"` } type json_out_client_conn struct { - Id uint32 `json:"id"` + Id ConnId `json:"id"` ReqServerAddrs []string `json:"req-server-addrs"` // server addresses requested. may include a domain name CurrentServerIndex int `json:"current-server-index"` ServerAddr string `json:"server-addr"` // actual server address @@ -49,11 +49,11 @@ type json_out_client_conn struct { } type json_out_client_route_id struct { - Id uint32 `json:"id"` + Id RouteId `json:"id"` } type json_out_client_route struct { - Id uint32 `json:"id"` + Id RouteId `json:"id"` ClientPeerAddr string `json:"client-peer-addr"` ServerPeerListenAddr string `json:"server-peer-listen-addr"` ServerPeerNet string `json:"server-peer-net"` @@ -61,7 +61,7 @@ type json_out_client_route struct { } type json_out_client_peer struct { - Id uint32 `json:"id"` + Id PeerId `json:"id"` ClientPeerAddr string `json:"client-peer-addr"` ClientLocalAddr string `json:"client-local-addr"` ServerPeerAddr string `json:"server-peer-addr"` @@ -249,7 +249,7 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt goto done } - cts = c.FindClientConnById(uint32(conn_nid)) + cts = c.FindClientConnById(ConnId(conn_nid)) if cts == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } @@ -333,7 +333,7 @@ func (ctl *client_ctl_client_conns_id_routes) ServeHTTP(w http.ResponseWriter, r goto done } - cts = c.FindClientConnById(uint32(conn_nid)) + cts = c.FindClientConnById(ConnId(conn_nid)) if cts == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } @@ -437,14 +437,14 @@ func (ctl *client_ctl_client_conns_id_routes_id) ServeHTTP(w http.ResponseWriter goto done } - cts = c.FindClientConnById(uint32(conn_nid)) + cts = c.FindClientConnById(ConnId(conn_nid)) if cts == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } goto done } - r = cts.FindClientRouteById(uint32(route_nid)) + r = cts.FindClientRouteById(RouteId(route_nid)) if r == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent route id - " + conn_id}); err != nil { goto oops } @@ -518,7 +518,7 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers) ServeHTTP(w http.Response goto done } - r = c.FindClientRouteById(uint32(conn_nid), uint32(route_nid)) + r = c.FindClientRouteById(ConnId(conn_nid), RouteId(route_nid)) if r == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection/route id - " + conn_id + "/" + route_id}); err != nil { goto oops } @@ -608,7 +608,7 @@ func (ctl *client_ctl_client_conns_id_routes_id_peers_id) ServeHTTP(w http.Respo goto done } - p = c.FindClientPeerConnById(uint32(conn_nid), uint32(route_nid), uint32(peer_nid)) + p = c.FindClientPeerConnById(ConnId(conn_nid), RouteId(route_nid), PeerId(peer_nid)) if p == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection/route/peer id - " + conn_id + "/" + route_id + "/" + peer_id}); err != nil { goto oops } diff --git a/client-peer.go b/client-peer.go index 80b0c24..efb49b7 100644 --- a/client-peer.go +++ b/client-peer.go @@ -5,7 +5,7 @@ import "io" import "net" import "sync" -func NewClientPeerConn(r *ClientRoute, c *net.TCPConn, id uint32, pts_raddr string, pts_laddr string) *ClientPeerConn { +func NewClientPeerConn(r *ClientRoute, c *net.TCPConn, id PeerId, pts_raddr string, pts_laddr string) *ClientPeerConn { var cpc ClientPeerConn cpc.route = r diff --git a/client.go b/client.go index 6d79e0f..531a227 100644 --- a/client.go +++ b/client.go @@ -21,10 +21,10 @@ import "google.golang.org/grpc/status" type PacketStreamClient grpc.BidiStreamingClient[Packet, Packet] -type ClientConnMap = map[uint32]*ClientConn -type ClientPeerConnMap = map[uint32]*ClientPeerConn -type ClientRouteMap = map[uint32]*ClientRoute -type ClientPeerCancelFuncMap = map[uint32]context.CancelFunc +type ClientConnMap = map[ConnId]*ClientConn +type ClientRouteMap = map[RouteId]*ClientRoute +type ClientPeerConnMap = map[PeerId]*ClientPeerConn +type ClientPeerCancelFuncMap = map[PeerId]context.CancelFunc // -------------------------------------------------------------------- type ClientConfig struct { @@ -35,7 +35,7 @@ type ClientConfig struct { } type ClientConfigActive struct { - Id uint32 + Id ConnId Index int ClientConfig } @@ -74,7 +74,7 @@ type Client struct { type ClientConn struct { cli *Client cfg ClientConfigActive - id uint32 + id ConnId sid string // id rendered in string local_addr string @@ -96,7 +96,7 @@ type ClientConn struct { type ClientRoute struct { cts *ClientConn - id uint32 + id RouteId peer_addr string server_peer_listen_addr *net.TCPAddr server_peer_net string @@ -113,7 +113,7 @@ type ClientRoute struct { type ClientPeerConn struct { route *ClientRoute - conn_id uint32 + conn_id PeerId conn *net.TCPConn pts_laddr string // server-local addreess of the server-side peer @@ -148,7 +148,7 @@ func (g *GuardedPacketStreamClient) Context() context.Context { }*/ // -------------------------------------------------------------------- -func NewClientRoute(cts *ClientConn, id uint32, client_peer_addr string, server_peer_net string, server_peer_proto ROUTE_PROTO) *ClientRoute { +func NewClientRoute(cts *ClientConn, id RouteId, client_peer_addr string, server_peer_net string, server_peer_proto ROUTE_PROTO) *ClientRoute { var r ClientRoute r.cts = cts @@ -164,7 +164,7 @@ func NewClientRoute(cts *ClientConn, id uint32, client_peer_addr string, server_ return &r } -func (r *ClientRoute) AddNewClientPeerConn(c *net.TCPConn, pts_id uint32, pts_raddr string, pts_laddr string) (*ClientPeerConn, error) { +func (r *ClientRoute) AddNewClientPeerConn(c *net.TCPConn, pts_id PeerId, pts_raddr string, pts_laddr string) (*ClientPeerConn, error) { var ptc *ClientPeerConn r.ptc_mtx.Lock() @@ -224,7 +224,7 @@ func (r *ClientRoute) ReqStopAllClientPeerConns() { } } -func (r *ClientRoute) FindClientPeerConnById(conn_id uint32) *ClientPeerConn { +func (r *ClientRoute) FindClientPeerConnById(conn_id PeerId) *ClientPeerConn { var c *ClientPeerConn var ok bool @@ -294,7 +294,7 @@ func (r *ClientRoute) ReqStop() { } } -func (r *ClientRoute) ConnectToPeer(pts_id uint32, pts_raddr string, pts_laddr string, wg *sync.WaitGroup) { +func (r *ClientRoute) ConnectToPeer(pts_id PeerId, pts_raddr string, pts_laddr string, wg *sync.WaitGroup) { var err error var conn net.Conn var real_conn *net.TCPConn @@ -394,7 +394,7 @@ func (r *ClientRoute) DisconnectFromPeer(ptc *ClientPeerConn) error { return nil } -func (r *ClientRoute) ReportEvent(pts_id uint32, event_type PACKET_KIND, event_data interface{}) error { +func (r *ClientRoute) ReportEvent(pts_id PeerId, event_type PACKET_KIND, event_data interface{}) error { var err error switch event_type { @@ -562,12 +562,12 @@ func NewClientConn(c *Client, cfg *ClientConfig) *ClientConn { func (cts *ClientConn) AddNewClientRoute(addr string, server_peer_net string, proto ROUTE_PROTO) (*ClientRoute, error) { var r *ClientRoute - var id uint32 + var id RouteId var ok bool cts.route_mtx.Lock() - id = rand.Uint32() + id = RouteId(rand.Uint32()) for { _, ok = cts.route_map[id] if !ok { break } @@ -639,7 +639,7 @@ func (cts *ClientConn) RemoveClientRoute(route *ClientRoute) error { return nil } -func (cts *ClientConn) RemoveClientRouteById(route_id uint32) error { +func (cts *ClientConn) RemoveClientRouteById(route_id RouteId) error { var r *ClientRoute var ok bool @@ -659,7 +659,7 @@ func (cts *ClientConn) RemoveClientRouteById(route_id uint32) error { return nil } -func (cts *ClientConn) FindClientRouteById(route_id uint32) *ClientRoute { +func (cts *ClientConn) FindClientRouteById(route_id RouteId) *ClientRoute { var r *ClientRoute var ok bool @@ -846,7 +846,7 @@ start_over: var ok bool x, ok = pkt.U.(*Packet_Route) if ok { - err = cts.ReportEvent(x.Route.RouteId, 0, pkt.Kind, x.Route) + err = cts.ReportEvent(RouteId(x.Route.RouteId), 0, pkt.Kind, x.Route) if err != nil { cts.cli.log.Write(cts.sid, LOG_ERROR, "Failed to handle route_started event(%d,%s) from %s - %s", @@ -865,7 +865,7 @@ start_over: var ok bool x, ok = pkt.U.(*Packet_Route) if ok { - err = cts.ReportEvent(x.Route.RouteId, 0, pkt.Kind, x.Route) + err = cts.ReportEvent(RouteId(x.Route.RouteId), 0, pkt.Kind, x.Route) if err != nil { cts.cli.log.Write(cts.sid, LOG_ERROR, "Failed to handle route_stopped event(%d,%s) from %s - %s", @@ -885,7 +885,7 @@ start_over: var ok bool x, ok = pkt.U.(*Packet_Peer) if ok { - err = cts.ReportEvent(x.Peer.RouteId, x.Peer.PeerId, PACKET_KIND_PEER_STARTED, x.Peer) + err = cts.ReportEvent(RouteId(x.Peer.RouteId), PeerId(x.Peer.PeerId), PACKET_KIND_PEER_STARTED, x.Peer) if err != nil { cts.cli.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_started event from %s for peer(%d,%d,%s,%s) - %s", @@ -908,7 +908,7 @@ start_over: var ok bool x, ok = pkt.U.(*Packet_Peer) if ok { - err = cts.ReportEvent(x.Peer.RouteId, x.Peer.PeerId, PACKET_KIND_PEER_STOPPED, x.Peer) + err = cts.ReportEvent(RouteId(x.Peer.RouteId), PeerId(x.Peer.PeerId), PACKET_KIND_PEER_STOPPED, x.Peer) if err != nil { cts.cli.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_stopped event from %s for peer(%d,%d,%s,%s) - %s", @@ -927,7 +927,7 @@ start_over: var ok bool x, ok = pkt.U.(*Packet_Peer) if ok { - err = cts.ReportEvent(x.Peer.RouteId, x.Peer.PeerId, PACKET_KIND_PEER_EOF, x.Peer) + err = cts.ReportEvent(RouteId(x.Peer.RouteId), PeerId(x.Peer.PeerId), PACKET_KIND_PEER_EOF, x.Peer) if err != nil { cts.cli.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_eof event from %s for peer(%d,%d,%s,%s) - %s", @@ -947,7 +947,7 @@ start_over: var ok bool x, ok = pkt.U.(*Packet_Data) if ok { - err = cts.ReportEvent(x.Data.RouteId, x.Data.PeerId, PACKET_KIND_PEER_DATA, x.Data.Data) + err = cts.ReportEvent(RouteId(x.Data.RouteId), PeerId(x.Data.PeerId), PACKET_KIND_PEER_DATA, x.Data.Data) if err != nil { cts.cli.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_data event from %s for peer(%d,%d) - %s", @@ -995,7 +995,7 @@ reconnect_to_server: goto start_over // and reconnect } -func (cts *ClientConn) ReportEvent(route_id uint32, pts_id uint32, event_type PACKET_KIND, event_data interface{}) error { +func (cts *ClientConn) ReportEvent(route_id RouteId, pts_id PeerId, event_type PACKET_KIND, event_data interface{}) error { var r *ClientRoute var ok bool @@ -1073,7 +1073,7 @@ func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctl_prefi func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) { var cts *ClientConn var ok bool - var id uint32 + var id ConnId if len(cfg.ServerAddrs) <= 0 { return nil, fmt.Errorf("no server rpc address specified") @@ -1083,7 +1083,8 @@ func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) { c.cts_mtx.Lock() - id = rand.Uint32() + //id = rand.Uint32() + id = ConnId(monotonic_time() / 1000) for { _, ok = c.cts_map[id] if !ok { break } @@ -1154,7 +1155,7 @@ func (c *Client) RemoveClientConn(cts *ClientConn) error { return nil } -func (c *Client) RemoveClientConnById(conn_id uint32) error { +func (c *Client) RemoveClientConnById(conn_id ConnId) error { var cts *ClientConn var ok bool @@ -1177,7 +1178,7 @@ func (c *Client) RemoveClientConnById(conn_id uint32) error { return nil } -func (c *Client) FindClientConnById(id uint32) *ClientConn { +func (c *Client) FindClientConnById(id ConnId) *ClientConn { var cts *ClientConn var ok bool @@ -1192,7 +1193,7 @@ func (c *Client) FindClientConnById(id uint32) *ClientConn { return cts } -func (c *Client) FindClientRouteById(conn_id uint32, route_id uint32) *ClientRoute { +func (c *Client) FindClientRouteById(conn_id ConnId, route_id RouteId) *ClientRoute { var cts *ClientConn var ok bool @@ -1207,7 +1208,7 @@ func (c *Client) FindClientRouteById(conn_id uint32, route_id uint32) *ClientRou return cts.FindClientRouteById(route_id) } -func (c *Client) FindClientPeerConnById(conn_id uint32, route_id uint32, peer_id uint32) *ClientPeerConn { +func (c *Client) FindClientPeerConnById(conn_id ConnId, route_id RouteId, peer_id PeerId) *ClientPeerConn { var cts *ClientConn var r *ClientRoute var ok bool diff --git a/packet.go b/packet.go index 7d331e7..94dc260 100644 --- a/packet.go +++ b/packet.go @@ -1,54 +1,58 @@ package hodu -func MakeRouteStartPacket(route_id uint32, proto ROUTE_PROTO, addr string, svcnet string) *Packet { +type ConnId uint64 +type RouteId uint32 +type PeerId uint32 + +func MakeRouteStartPacket(route_id RouteId, proto ROUTE_PROTO, addr string, svcnet string) *Packet { return &Packet{ Kind: PACKET_KIND_ROUTE_START, - U: &Packet_Route{Route: &RouteDesc{RouteId: route_id, ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} + U: &Packet_Route{Route: &RouteDesc{RouteId: uint32(route_id), ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} } -func MakeRouteStopPacket(route_id uint32, proto ROUTE_PROTO, addr string, svcnet string) *Packet { +func MakeRouteStopPacket(route_id RouteId, proto ROUTE_PROTO, addr string, svcnet string) *Packet { return &Packet{ Kind: PACKET_KIND_ROUTE_STOP, - U: &Packet_Route{Route: &RouteDesc{RouteId: route_id, ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} + U: &Packet_Route{Route: &RouteDesc{RouteId: uint32(route_id), ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} } -func MakeRouteStartedPacket(route_id uint32, proto ROUTE_PROTO, addr string, svcnet string) *Packet { +func MakeRouteStartedPacket(route_id RouteId, proto ROUTE_PROTO, addr string, svcnet string) *Packet { // the connection from a peer to the server has been established return &Packet{Kind: PACKET_KIND_ROUTE_STARTED, - U: &Packet_Route{Route: &RouteDesc{RouteId: route_id, ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} + U: &Packet_Route{Route: &RouteDesc{RouteId: uint32(route_id), ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} } -func MakeRouteStoppedPacket(route_id uint32, proto ROUTE_PROTO, addr string, svcnet string) *Packet { +func MakeRouteStoppedPacket(route_id RouteId, proto ROUTE_PROTO, addr string, svcnet string) *Packet { // the connection from a peer to the server has been established return &Packet{Kind: PACKET_KIND_ROUTE_STOPPED, - U: &Packet_Route{Route: &RouteDesc{RouteId: route_id, ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} + U: &Packet_Route{Route: &RouteDesc{RouteId: uint32(route_id), ServiceProto: proto, TargetAddrStr: addr, ServiceNetStr: svcnet}}} } -func MakePeerStartedPacket(route_id uint32, peer_id uint32, remote_addr string, local_addr string) *Packet { +func MakePeerStartedPacket(route_id RouteId, peer_id PeerId, remote_addr string, local_addr string) *Packet { // the connection from a peer to the server has been established return &Packet{Kind: PACKET_KIND_PEER_STARTED, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id, RemoteAddrStr: remote_addr, LocalAddrStr: local_addr}}, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: uint32(route_id), PeerId: uint32(peer_id), RemoteAddrStr: remote_addr, LocalAddrStr: local_addr}}, } } -func MakePeerStoppedPacket(route_id uint32, peer_id uint32, remote_addr string, local_addr string) *Packet { +func MakePeerStoppedPacket(route_id RouteId, peer_id PeerId, remote_addr string, local_addr string) *Packet { return &Packet{Kind: PACKET_KIND_PEER_STOPPED, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id, RemoteAddrStr: remote_addr, LocalAddrStr: local_addr}}, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: uint32(route_id), PeerId: uint32(peer_id), RemoteAddrStr: remote_addr, LocalAddrStr: local_addr}}, } } -func MakePeerAbortedPacket(route_id uint32, peer_id uint32, remote_addr string, local_addr string) *Packet { +func MakePeerAbortedPacket(route_id RouteId, peer_id PeerId, remote_addr string, local_addr string) *Packet { return &Packet{Kind: PACKET_KIND_PEER_ABORTED, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id, RemoteAddrStr: remote_addr, LocalAddrStr: local_addr}}, + U: &Packet_Peer{Peer: &PeerDesc{RouteId: uint32(route_id), PeerId: uint32(peer_id), RemoteAddrStr: remote_addr, LocalAddrStr: local_addr}}, } } -func MakePeerEofPacket(route_id uint32, peer_id uint32) *Packet { +func MakePeerEofPacket(route_id RouteId, peer_id PeerId) *Packet { return &Packet{Kind: PACKET_KIND_PEER_EOF, - U: &Packet_Peer{Peer: &PeerDesc{RouteId: route_id, PeerId: peer_id}}} + U: &Packet_Peer{Peer: &PeerDesc{RouteId: uint32(route_id), PeerId: uint32(peer_id)}}} } -func MakePeerDataPacket(route_id uint32, peer_id uint32, data []byte) *Packet { +func MakePeerDataPacket(route_id RouteId, peer_id PeerId, data []byte) *Packet { return &Packet{Kind: PACKET_KIND_PEER_DATA, - U: &Packet_Data{Data: &PeerData{RouteId: route_id, PeerId: peer_id, Data: data}}} + U: &Packet_Data{Data: &PeerData{RouteId: uint32(route_id), PeerId: uint32(peer_id), Data: data}}} } diff --git a/server-ctl.go b/server-ctl.go index f802b67..3c35a3d 100644 --- a/server-ctl.go +++ b/server-ctl.go @@ -6,14 +6,14 @@ import "runtime" import "strconv" type json_out_server_conn struct { - Id uint32 `json:"id"` + Id ConnId `json:"id"` ServerAddr string `json:"server-addr"` ClientAddr string `json:"client-addr"` Routes []json_out_server_route `json:"routes"` } type json_out_server_route struct { - Id uint32 `json:"id"` + Id RouteId `json:"id"` ClientPeerAddr string `json:"client-peer-addr"` ServerPeerListenAddr string `json:"server-peer-listen-addr"` ServerPeerNet string `json:"server-peer-net"` @@ -157,7 +157,7 @@ func (ctl *server_ctl_server_conns_id) ServeHTTP(w http.ResponseWriter, req *htt goto done } - cts = s.FindServerConnById(uint32(conn_nid)) + cts = s.FindServerConnById(ConnId(conn_nid)) if cts == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } @@ -238,7 +238,7 @@ func (ctl *server_ctl_server_conns_id_routes) ServeHTTP(w http.ResponseWriter, r goto done } - cts = s.FindServerConnById(uint32(conn_nid)) + cts = s.FindServerConnById(ConnId(conn_nid)) if cts == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } @@ -322,14 +322,14 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter goto done } - cts = s.FindServerConnById(uint32(conn_nid)) + cts = s.FindServerConnById(ConnId(conn_nid)) if cts == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops } goto done } - r = cts.FindServerRouteById(uint32(route_nid)) + r = cts.FindServerRouteById(RouteId(route_nid)) if r == nil { status_code = http.StatusNotFound; w.WriteHeader(status_code) if err = je.Encode(json_errmsg{Text: "non-existent route id - " + conn_id}); err != nil { goto oops } @@ -339,7 +339,7 @@ func (ctl *server_ctl_server_conns_id_routes_id) ServeHTTP(w http.ResponseWriter switch req.Method { case http.MethodGet: status_code = http.StatusOK; w.WriteHeader(status_code) - err = je.Encode(json_out_client_route{ + err = je.Encode(json_out_server_route{ Id: r.id, ClientPeerAddr: r.ptc_addr, ServerPeerListenAddr: r.svc_addr.String(), diff --git a/server-peer.go b/server-peer.go index 4b95181..d210f06 100644 --- a/server-peer.go +++ b/server-peer.go @@ -9,7 +9,7 @@ import "time" type ServerPeerConn struct { route *ServerRoute - conn_id uint32 + conn_id PeerId cts *ClientConn conn *net.TCPConn @@ -22,7 +22,7 @@ type ServerPeerConn struct { client_peer_eof atomic.Bool } -func NewServerPeerConn(r *ServerRoute, c *net.TCPConn, id uint32) *ServerPeerConn { +func NewServerPeerConn(r *ServerRoute, c *net.TCPConn, id PeerId) *ServerPeerConn { var spc ServerPeerConn spc.route = r diff --git a/server.go b/server.go index 0ac47e8..b91699d 100644 --- a/server.go +++ b/server.go @@ -24,9 +24,10 @@ const PTS_LIMIT int = 16384 const CTS_LIMIT int = 16384 type ServerConnMapByAddr = map[net.Addr]*ServerConn -type ServerConnMap = map[uint32]*ServerConn -type ServerPeerConnMap = map[uint32]*ServerPeerConn -type ServerRouteMap = map[uint32]*ServerRoute +type ServerConnMap = map[ConnId]*ServerConn +type ServerRouteMap = map[RouteId]*ServerRoute +type ServerPeerConnMap = map[PeerId]*ServerPeerConn + type Server struct { ctx context.Context @@ -71,7 +72,7 @@ type Server struct { // client connect to the server, the server accept it, and makes a tunnel request type ServerConn struct { svr *Server - id uint32 + id ConnId sid string // for logging remote_addr net.Addr // client address that created this structure @@ -95,12 +96,12 @@ type ServerRoute struct { svc_proto ROUTE_PROTO ptc_addr string - id uint32 + id RouteId pts_mtx sync.Mutex pts_map ServerPeerConnMap pts_limit int - pts_last_id uint32 + pts_last_id PeerId pts_wg sync.WaitGroup stop_req atomic.Bool } @@ -134,7 +135,7 @@ func (g *GuardedPacketStreamServer) Context() context.Context { // ------------------------------------ -func NewServerRoute(cts *ServerConn, id uint32, proto ROUTE_PROTO, ptc_addr string, svc_permitted_net string) (*ServerRoute, error) { +func NewServerRoute(cts *ServerConn, id RouteId, proto ROUTE_PROTO, ptc_addr string, svc_permitted_net string) (*ServerRoute, error) { var r ServerRoute var l *net.TCPListener var svcaddr *net.TCPAddr @@ -180,7 +181,7 @@ func NewServerRoute(cts *ServerConn, id uint32, proto ROUTE_PROTO, ptc_addr stri func (r *ServerRoute) AddNewServerPeerConn(c *net.TCPConn) (*ServerPeerConn, error) { var pts *ServerPeerConn var ok bool - var start_id uint32 + var start_id PeerId r.pts_mtx.Lock() defer r.pts_mtx.Unlock() @@ -277,7 +278,7 @@ func (r *ServerRoute) ReqStop() { } } -func (r *ServerRoute) ReportEvent(pts_id uint32, event_type PACKET_KIND, event_data interface{}) error { +func (r *ServerRoute) ReportEvent(pts_id PeerId, event_type PACKET_KIND, event_data interface{}) error { var spc *ServerPeerConn var ok bool @@ -293,7 +294,7 @@ func (r *ServerRoute) ReportEvent(pts_id uint32, event_type PACKET_KIND, event_d } // ------------------------------------ -func (cts *ServerConn) make_route_listener(id uint32, proto ROUTE_PROTO) (*net.TCPListener, *net.TCPAddr, error) { +func (cts *ServerConn) make_route_listener(id RouteId, proto ROUTE_PROTO) (*net.TCPListener, *net.TCPAddr, error) { var l *net.TCPListener var err error var svcaddr *net.TCPAddr @@ -338,7 +339,7 @@ func (cts *ServerConn) make_route_listener(id uint32, proto ROUTE_PROTO) (*net.T return nil, nil, err } -func (cts *ServerConn) AddNewServerRoute(route_id uint32, proto ROUTE_PROTO, ptc_addr string, svc_permitted_net string) (*ServerRoute, error) { +func (cts *ServerConn) AddNewServerRoute(route_id RouteId, proto ROUTE_PROTO, ptc_addr string, svc_permitted_net string) (*ServerRoute, error) { var r *ServerRoute var err error @@ -383,7 +384,7 @@ func (cts *ServerConn) RemoveServerRoute(route *ServerRoute) error { return nil } -func (cts *ServerConn) RemoveServerRouteById(route_id uint32) (*ServerRoute, error) { +func (cts *ServerConn) RemoveServerRouteById(route_id RouteId) (*ServerRoute, error) { var r *ServerRoute var ok bool @@ -401,7 +402,7 @@ func (cts *ServerConn) RemoveServerRouteById(route_id uint32) (*ServerRoute, err return r, nil } -func (cts *ServerConn) FindServerRouteById(route_id uint32) *ServerRoute { +func (cts *ServerConn) FindServerRouteById(route_id RouteId) *ServerRoute { var r *ServerRoute var ok bool @@ -427,7 +428,7 @@ func (cts *ServerConn) ReqStopAllServerRoutes() { } } -func (cts *ServerConn) ReportEvent(route_id uint32, pts_id uint32, event_type PACKET_KIND, event_data interface{}) error { +func (cts *ServerConn) ReportEvent(route_id RouteId, pts_id PeerId, event_type PACKET_KIND, event_data interface{}) error { var r *ServerRoute var ok bool @@ -467,13 +468,13 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) { if ok { var r *ServerRoute - r, err = cts.AddNewServerRoute(x.Route.RouteId, x.Route.ServiceProto, x.Route.TargetAddrStr, x.Route.ServiceNetStr) + r, err = cts.AddNewServerRoute(RouteId(x.Route.RouteId), x.Route.ServiceProto, x.Route.TargetAddrStr, x.Route.ServiceNetStr) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to add route(%d,%s) for %s - %s", x.Route.RouteId, x.Route.TargetAddrStr, cts.remote_addr, err.Error()) - err = cts.pss.Send(MakeRouteStoppedPacket(x.Route.RouteId, x.Route.ServiceProto, x.Route.TargetAddrStr, x.Route.ServiceNetStr)) + err = cts.pss.Send(MakeRouteStoppedPacket(RouteId(x.Route.RouteId), x.Route.ServiceProto, x.Route.TargetAddrStr, x.Route.ServiceNetStr)) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to send route_stopped event(%d,%s,%v,%s) to client %s - %s", @@ -510,7 +511,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) { if ok { var r *ServerRoute - r, err = cts.RemoveServerRouteById(x.Route.RouteId) + r, err = cts.RemoveServerRouteById(RouteId(x.Route.RouteId)) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to delete route(%d,%s) for client %s - %s", @@ -538,7 +539,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) { var ok bool x, ok = pkt.U.(*Packet_Peer) if ok { - err = cts.ReportEvent(x.Peer.RouteId, x.Peer.PeerId, PACKET_KIND_PEER_STARTED, x.Peer) + err = cts.ReportEvent(RouteId(x.Peer.RouteId), PeerId(x.Peer.PeerId), PACKET_KIND_PEER_STARTED, x.Peer) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_started event from %s for peer(%d,%d,%s,%s) - %s", @@ -558,7 +559,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) { var ok bool x, ok = pkt.U.(*Packet_Peer) if ok { - err = cts.ReportEvent(x.Peer.RouteId, x.Peer.PeerId, PACKET_KIND_PEER_ABORTED, x.Peer) + err = cts.ReportEvent(RouteId(x.Peer.RouteId), PeerId(x.Peer.PeerId), PACKET_KIND_PEER_ABORTED, x.Peer) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_aborted event from %s for peer(%d,%d,%s,%s) - %s", @@ -579,7 +580,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) { var ok bool x, ok = pkt.U.(*Packet_Peer) if ok { - err = cts.ReportEvent(x.Peer.RouteId, x.Peer.PeerId, PACKET_KIND_PEER_STOPPED, x.Peer) + err = cts.ReportEvent(RouteId(x.Peer.RouteId), PeerId(x.Peer.PeerId), PACKET_KIND_PEER_STOPPED, x.Peer) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_stopped event from %s for peer(%d,%d,%s,%s) - %s", @@ -600,7 +601,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) { var ok bool x, ok = pkt.U.(*Packet_Data) if ok { - err = cts.ReportEvent(x.Data.RouteId, x.Data.PeerId, PACKET_KIND_PEER_DATA, x.Data.Data) + err = cts.ReportEvent(RouteId(x.Data.RouteId), PeerId(x.Data.PeerId), PACKET_KIND_PEER_DATA, x.Data.Data) if err != nil { cts.svr.log.Write(cts.sid, LOG_ERROR, "Failed to handle peer_data event from %s for peer(%d,%d) - %s", @@ -1081,7 +1082,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 id uint32 + var id ConnId var ok bool cts.svr = s @@ -1100,8 +1101,8 @@ func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, p return nil, fmt.Errorf("too many connections - %d", s.cts_limit) } - id = uint32(monotonic_time()) //id = rand.Uint32() + id = ConnId(monotonic_time()/ 1000) for { _, ok = s.cts_map[id] if !ok { break } @@ -1177,7 +1178,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error { return nil } -func (s *Server) FindServerConnById(id uint32) *ServerConn { +func (s *Server) FindServerConnById(id ConnId) *ServerConn { var cts *ServerConn var ok bool