defined PeerId, RouteId, ConnId types and made relevant changes

This commit is contained in:
hyung-hwan 2024-12-09 22:41:23 +09:00
parent d043fd730b
commit 2d15f0d2a4
7 changed files with 99 additions and 93 deletions

View File

@ -36,11 +36,11 @@ type json_in_client_route struct {
} }
type json_out_client_conn_id struct { type json_out_client_conn_id struct {
Id uint32 `json:"id"` Id ConnId `json:"id"`
} }
type json_out_client_conn struct { 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 ReqServerAddrs []string `json:"req-server-addrs"` // server addresses requested. may include a domain name
CurrentServerIndex int `json:"current-server-index"` CurrentServerIndex int `json:"current-server-index"`
ServerAddr string `json:"server-addr"` // actual server address 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 { type json_out_client_route_id struct {
Id uint32 `json:"id"` Id RouteId `json:"id"`
} }
type json_out_client_route struct { type json_out_client_route struct {
Id uint32 `json:"id"` Id RouteId `json:"id"`
ClientPeerAddr string `json:"client-peer-addr"` ClientPeerAddr string `json:"client-peer-addr"`
ServerPeerListenAddr string `json:"server-peer-listen-addr"` ServerPeerListenAddr string `json:"server-peer-listen-addr"`
ServerPeerNet string `json:"server-peer-net"` ServerPeerNet string `json:"server-peer-net"`
@ -61,7 +61,7 @@ type json_out_client_route struct {
} }
type json_out_client_peer struct { type json_out_client_peer struct {
Id uint32 `json:"id"` Id PeerId `json:"id"`
ClientPeerAddr string `json:"client-peer-addr"` ClientPeerAddr string `json:"client-peer-addr"`
ClientLocalAddr string `json:"client-local-addr"` ClientLocalAddr string `json:"client-local-addr"`
ServerPeerAddr string `json:"server-peer-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 goto done
} }
cts = c.FindClientConnById(uint32(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 goto done
} }
cts = c.FindClientConnById(uint32(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 goto done
} }
cts = c.FindClientConnById(uint32(conn_nid)) cts = c.FindClientConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
r = cts.FindClientRouteById(uint32(route_nid)) r = cts.FindClientRouteById(RouteId(route_nid))
if r == nil { if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 goto done
} }
r = c.FindClientRouteById(uint32(conn_nid), uint32(route_nid)) r = c.FindClientRouteById(ConnId(conn_nid), RouteId(route_nid))
if r == nil { if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 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 { if p == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } if err = je.Encode(json_errmsg{Text: "non-existent connection/route/peer id - " + conn_id + "/" + route_id + "/" + peer_id}); err != nil { goto oops }

View File

@ -5,7 +5,7 @@ import "io"
import "net" import "net"
import "sync" 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 var cpc ClientPeerConn
cpc.route = r cpc.route = r

View File

@ -21,10 +21,10 @@ import "google.golang.org/grpc/status"
type PacketStreamClient grpc.BidiStreamingClient[Packet, Packet] type PacketStreamClient grpc.BidiStreamingClient[Packet, Packet]
type ClientConnMap = map[uint32]*ClientConn type ClientConnMap = map[ConnId]*ClientConn
type ClientPeerConnMap = map[uint32]*ClientPeerConn type ClientRouteMap = map[RouteId]*ClientRoute
type ClientRouteMap = map[uint32]*ClientRoute type ClientPeerConnMap = map[PeerId]*ClientPeerConn
type ClientPeerCancelFuncMap = map[uint32]context.CancelFunc type ClientPeerCancelFuncMap = map[PeerId]context.CancelFunc
// -------------------------------------------------------------------- // --------------------------------------------------------------------
type ClientConfig struct { type ClientConfig struct {
@ -35,7 +35,7 @@ type ClientConfig struct {
} }
type ClientConfigActive struct { type ClientConfigActive struct {
Id uint32 Id ConnId
Index int Index int
ClientConfig ClientConfig
} }
@ -74,7 +74,7 @@ type Client struct {
type ClientConn struct { type ClientConn struct {
cli *Client cli *Client
cfg ClientConfigActive cfg ClientConfigActive
id uint32 id ConnId
sid string // id rendered in string sid string // id rendered in string
local_addr string local_addr string
@ -96,7 +96,7 @@ type ClientConn struct {
type ClientRoute struct { type ClientRoute struct {
cts *ClientConn cts *ClientConn
id uint32 id RouteId
peer_addr string peer_addr string
server_peer_listen_addr *net.TCPAddr server_peer_listen_addr *net.TCPAddr
server_peer_net string server_peer_net string
@ -113,7 +113,7 @@ type ClientRoute struct {
type ClientPeerConn struct { type ClientPeerConn struct {
route *ClientRoute route *ClientRoute
conn_id uint32 conn_id PeerId
conn *net.TCPConn conn *net.TCPConn
pts_laddr string // server-local addreess of the server-side peer 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 var r ClientRoute
r.cts = cts r.cts = cts
@ -164,7 +164,7 @@ func NewClientRoute(cts *ClientConn, id uint32, client_peer_addr string, server_
return &r 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 var ptc *ClientPeerConn
r.ptc_mtx.Lock() 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 c *ClientPeerConn
var ok bool 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 err error
var conn net.Conn var conn net.Conn
var real_conn *net.TCPConn var real_conn *net.TCPConn
@ -394,7 +394,7 @@ func (r *ClientRoute) DisconnectFromPeer(ptc *ClientPeerConn) error {
return nil 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 var err error
switch event_type { 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) { func (cts *ClientConn) AddNewClientRoute(addr string, server_peer_net string, proto ROUTE_PROTO) (*ClientRoute, error) {
var r *ClientRoute var r *ClientRoute
var id uint32 var id RouteId
var ok bool var ok bool
cts.route_mtx.Lock() cts.route_mtx.Lock()
id = rand.Uint32() id = RouteId(rand.Uint32())
for { for {
_, ok = cts.route_map[id] _, ok = cts.route_map[id]
if !ok { break } if !ok { break }
@ -639,7 +639,7 @@ func (cts *ClientConn) RemoveClientRoute(route *ClientRoute) error {
return nil return nil
} }
func (cts *ClientConn) RemoveClientRouteById(route_id uint32) error { func (cts *ClientConn) RemoveClientRouteById(route_id RouteId) error {
var r *ClientRoute var r *ClientRoute
var ok bool var ok bool
@ -659,7 +659,7 @@ func (cts *ClientConn) RemoveClientRouteById(route_id uint32) error {
return nil return nil
} }
func (cts *ClientConn) FindClientRouteById(route_id uint32) *ClientRoute { func (cts *ClientConn) FindClientRouteById(route_id RouteId) *ClientRoute {
var r *ClientRoute var r *ClientRoute
var ok bool var ok bool
@ -846,7 +846,7 @@ start_over:
var ok bool var ok bool
x, ok = pkt.U.(*Packet_Route) x, ok = pkt.U.(*Packet_Route)
if ok { 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 { if err != nil {
cts.cli.log.Write(cts.sid, LOG_ERROR, cts.cli.log.Write(cts.sid, LOG_ERROR,
"Failed to handle route_started event(%d,%s) from %s - %s", "Failed to handle route_started event(%d,%s) from %s - %s",
@ -865,7 +865,7 @@ start_over:
var ok bool var ok bool
x, ok = pkt.U.(*Packet_Route) x, ok = pkt.U.(*Packet_Route)
if ok { 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 { if err != nil {
cts.cli.log.Write(cts.sid, LOG_ERROR, cts.cli.log.Write(cts.sid, LOG_ERROR,
"Failed to handle route_stopped event(%d,%s) from %s - %s", "Failed to handle route_stopped event(%d,%s) from %s - %s",
@ -885,7 +885,7 @@ start_over:
var ok bool var ok bool
x, ok = pkt.U.(*Packet_Peer) x, ok = pkt.U.(*Packet_Peer)
if ok { 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 { if err != nil {
cts.cli.log.Write(cts.sid, LOG_ERROR, cts.cli.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_started event from %s for peer(%d,%d,%s,%s) - %s", "Failed to handle peer_started event from %s for peer(%d,%d,%s,%s) - %s",
@ -908,7 +908,7 @@ start_over:
var ok bool var ok bool
x, ok = pkt.U.(*Packet_Peer) x, ok = pkt.U.(*Packet_Peer)
if ok { 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 { if err != nil {
cts.cli.log.Write(cts.sid, LOG_ERROR, cts.cli.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_stopped event from %s for peer(%d,%d,%s,%s) - %s", "Failed to handle peer_stopped event from %s for peer(%d,%d,%s,%s) - %s",
@ -927,7 +927,7 @@ start_over:
var ok bool var ok bool
x, ok = pkt.U.(*Packet_Peer) x, ok = pkt.U.(*Packet_Peer)
if ok { 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 { if err != nil {
cts.cli.log.Write(cts.sid, LOG_ERROR, cts.cli.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_eof event from %s for peer(%d,%d,%s,%s) - %s", "Failed to handle peer_eof event from %s for peer(%d,%d,%s,%s) - %s",
@ -947,7 +947,7 @@ start_over:
var ok bool var ok bool
x, ok = pkt.U.(*Packet_Data) x, ok = pkt.U.(*Packet_Data)
if ok { 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 { if err != nil {
cts.cli.log.Write(cts.sid, LOG_ERROR, cts.cli.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_data event from %s for peer(%d,%d) - %s", "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 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 r *ClientRoute
var ok bool 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) { func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) {
var cts *ClientConn var cts *ClientConn
var ok bool var ok bool
var id uint32 var 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")
@ -1083,7 +1083,8 @@ func (c *Client) AddNewClientConn(cfg *ClientConfig) (*ClientConn, error) {
c.cts_mtx.Lock() c.cts_mtx.Lock()
id = rand.Uint32() //id = rand.Uint32()
id = ConnId(monotonic_time() / 1000)
for { for {
_, ok = c.cts_map[id] _, ok = c.cts_map[id]
if !ok { break } if !ok { break }
@ -1154,7 +1155,7 @@ func (c *Client) RemoveClientConn(cts *ClientConn) error {
return nil return nil
} }
func (c *Client) RemoveClientConnById(conn_id uint32) error { func (c *Client) RemoveClientConnById(conn_id ConnId) error {
var cts *ClientConn var cts *ClientConn
var ok bool var ok bool
@ -1177,7 +1178,7 @@ func (c *Client) RemoveClientConnById(conn_id uint32) error {
return nil return nil
} }
func (c *Client) FindClientConnById(id uint32) *ClientConn { func (c *Client) FindClientConnById(id ConnId) *ClientConn {
var cts *ClientConn var cts *ClientConn
var ok bool var ok bool
@ -1192,7 +1193,7 @@ func (c *Client) FindClientConnById(id uint32) *ClientConn {
return cts 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 cts *ClientConn
var ok bool var ok bool
@ -1207,7 +1208,7 @@ func (c *Client) FindClientRouteById(conn_id uint32, route_id uint32) *ClientRou
return cts.FindClientRouteById(route_id) 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 cts *ClientConn
var r *ClientRoute var r *ClientRoute
var ok bool var ok bool

View File

@ -1,54 +1,58 @@
package hodu 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{ return &Packet{
Kind: PACKET_KIND_ROUTE_START, 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{ return &Packet{
Kind: PACKET_KIND_ROUTE_STOP, 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 // the connection from a peer to the server has been established
return &Packet{Kind: PACKET_KIND_ROUTE_STARTED, 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 // the connection from a peer to the server has been established
return &Packet{Kind: PACKET_KIND_ROUTE_STOPPED, 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 // the connection from a peer to the server has been established
return &Packet{Kind: PACKET_KIND_PEER_STARTED, 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, 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, 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, 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, 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}}}
} }

View File

@ -6,14 +6,14 @@ import "runtime"
import "strconv" import "strconv"
type json_out_server_conn struct { type json_out_server_conn struct {
Id uint32 `json:"id"` Id ConnId `json:"id"`
ServerAddr string `json:"server-addr"` ServerAddr string `json:"server-addr"`
ClientAddr string `json:"client-addr"` ClientAddr string `json:"client-addr"`
Routes []json_out_server_route `json:"routes"` Routes []json_out_server_route `json:"routes"`
} }
type json_out_server_route struct { type json_out_server_route struct {
Id uint32 `json:"id"` Id RouteId `json:"id"`
ClientPeerAddr string `json:"client-peer-addr"` ClientPeerAddr string `json:"client-peer-addr"`
ServerPeerListenAddr string `json:"server-peer-listen-addr"` ServerPeerListenAddr string `json:"server-peer-listen-addr"`
ServerPeerNet string `json:"server-peer-net"` 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 goto done
} }
cts = s.FindServerConnById(uint32(conn_nid)) cts = s.FindServerConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 goto done
} }
cts = s.FindServerConnById(uint32(conn_nid)) cts = s.FindServerConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 goto done
} }
cts = s.FindServerConnById(uint32(conn_nid)) cts = s.FindServerConnById(ConnId(conn_nid))
if cts == nil { if cts == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } if err = je.Encode(json_errmsg{Text: "non-existent connection id - " + conn_id}); err != nil { goto oops }
goto done goto done
} }
r = cts.FindServerRouteById(uint32(route_nid)) r = cts.FindServerRouteById(RouteId(route_nid))
if r == nil { if r == nil {
status_code = http.StatusNotFound; w.WriteHeader(status_code) 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 } 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 { switch req.Method {
case http.MethodGet: case http.MethodGet:
status_code = http.StatusOK; w.WriteHeader(status_code) 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, Id: r.id,
ClientPeerAddr: r.ptc_addr, ClientPeerAddr: r.ptc_addr,
ServerPeerListenAddr: r.svc_addr.String(), ServerPeerListenAddr: r.svc_addr.String(),

View File

@ -9,7 +9,7 @@ import "time"
type ServerPeerConn struct { type ServerPeerConn struct {
route *ServerRoute route *ServerRoute
conn_id uint32 conn_id PeerId
cts *ClientConn cts *ClientConn
conn *net.TCPConn conn *net.TCPConn
@ -22,7 +22,7 @@ type ServerPeerConn struct {
client_peer_eof atomic.Bool 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 var spc ServerPeerConn
spc.route = r spc.route = r

View File

@ -24,9 +24,10 @@ const PTS_LIMIT int = 16384
const CTS_LIMIT int = 16384 const CTS_LIMIT int = 16384
type ServerConnMapByAddr = map[net.Addr]*ServerConn type ServerConnMapByAddr = map[net.Addr]*ServerConn
type ServerConnMap = map[uint32]*ServerConn type ServerConnMap = map[ConnId]*ServerConn
type ServerPeerConnMap = map[uint32]*ServerPeerConn type ServerRouteMap = map[RouteId]*ServerRoute
type ServerRouteMap = map[uint32]*ServerRoute type ServerPeerConnMap = map[PeerId]*ServerPeerConn
type Server struct { type Server struct {
ctx context.Context ctx context.Context
@ -71,7 +72,7 @@ type Server struct {
// client connect to the server, the server accept it, and makes a tunnel request // client connect to the server, the server accept it, and makes a tunnel request
type ServerConn struct { type ServerConn struct {
svr *Server svr *Server
id uint32 id ConnId
sid string // for logging sid string // for logging
remote_addr net.Addr // client address that created this structure remote_addr net.Addr // client address that created this structure
@ -95,12 +96,12 @@ type ServerRoute struct {
svc_proto ROUTE_PROTO svc_proto ROUTE_PROTO
ptc_addr string ptc_addr string
id uint32 id RouteId
pts_mtx sync.Mutex pts_mtx sync.Mutex
pts_map ServerPeerConnMap pts_map ServerPeerConnMap
pts_limit int pts_limit int
pts_last_id uint32 pts_last_id PeerId
pts_wg sync.WaitGroup pts_wg sync.WaitGroup
stop_req atomic.Bool 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 r ServerRoute
var l *net.TCPListener var l *net.TCPListener
var svcaddr *net.TCPAddr 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) { func (r *ServerRoute) AddNewServerPeerConn(c *net.TCPConn) (*ServerPeerConn, error) {
var pts *ServerPeerConn var pts *ServerPeerConn
var ok bool var ok bool
var start_id uint32 var start_id PeerId
r.pts_mtx.Lock() r.pts_mtx.Lock()
defer r.pts_mtx.Unlock() 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 spc *ServerPeerConn
var ok bool 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 l *net.TCPListener
var err error var err error
var svcaddr *net.TCPAddr 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 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 r *ServerRoute
var err error var err error
@ -383,7 +384,7 @@ func (cts *ServerConn) RemoveServerRoute(route *ServerRoute) error {
return nil return nil
} }
func (cts *ServerConn) RemoveServerRouteById(route_id uint32) (*ServerRoute, error) { func (cts *ServerConn) RemoveServerRouteById(route_id RouteId) (*ServerRoute, error) {
var r *ServerRoute var r *ServerRoute
var ok bool var ok bool
@ -401,7 +402,7 @@ func (cts *ServerConn) RemoveServerRouteById(route_id uint32) (*ServerRoute, err
return r, nil return r, nil
} }
func (cts *ServerConn) FindServerRouteById(route_id uint32) *ServerRoute { func (cts *ServerConn) FindServerRouteById(route_id RouteId) *ServerRoute {
var r *ServerRoute var r *ServerRoute
var ok bool 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 r *ServerRoute
var ok bool var ok bool
@ -467,13 +468,13 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) {
if ok { if ok {
var r *ServerRoute 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 { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to add route(%d,%s) for %s - %s", "Failed to add route(%d,%s) for %s - %s",
x.Route.RouteId, x.Route.TargetAddrStr, cts.remote_addr, err.Error()) 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 { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to send route_stopped event(%d,%s,%v,%s) to client %s - %s", "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 { if ok {
var r *ServerRoute var r *ServerRoute
r, err = cts.RemoveServerRouteById(x.Route.RouteId) r, err = cts.RemoveServerRouteById(RouteId(x.Route.RouteId))
if err != nil { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to delete route(%d,%s) for client %s - %s", "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 var ok bool
x, ok = pkt.U.(*Packet_Peer) x, ok = pkt.U.(*Packet_Peer)
if ok { 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 { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_started event from %s for peer(%d,%d,%s,%s) - %s", "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 var ok bool
x, ok = pkt.U.(*Packet_Peer) x, ok = pkt.U.(*Packet_Peer)
if ok { 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 { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_aborted event from %s for peer(%d,%d,%s,%s) - %s", "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 var ok bool
x, ok = pkt.U.(*Packet_Peer) x, ok = pkt.U.(*Packet_Peer)
if ok { 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 { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_stopped event from %s for peer(%d,%d,%s,%s) - %s", "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 var ok bool
x, ok = pkt.U.(*Packet_Data) x, ok = pkt.U.(*Packet_Data)
if ok { 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 { if err != nil {
cts.svr.log.Write(cts.sid, LOG_ERROR, cts.svr.log.Write(cts.sid, LOG_ERROR,
"Failed to handle peer_data event from %s for peer(%d,%d) - %s", "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) { func (s *Server) AddNewServerConn(remote_addr *net.Addr, local_addr *net.Addr, pss Hodu_PacketStreamServer) (*ServerConn, error) {
var cts ServerConn var cts ServerConn
var id uint32 var id ConnId
var ok bool var ok bool
cts.svr = s 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) return nil, fmt.Errorf("too many connections - %d", s.cts_limit)
} }
id = uint32(monotonic_time())
//id = rand.Uint32() //id = rand.Uint32()
id = ConnId(monotonic_time()/ 1000)
for { for {
_, ok = s.cts_map[id] _, ok = s.cts_map[id]
if !ok { break } if !ok { break }
@ -1177,7 +1178,7 @@ func (s *Server) RemoveServerConnByAddr(addr net.Addr) error {
return nil return nil
} }
func (s *Server) FindServerConnById(id uint32) *ServerConn { func (s *Server) FindServerConnById(id ConnId) *ServerConn {
var cts *ServerConn var cts *ServerConn
var ok bool var ok bool