From de85c4c754ae764681b23286e0e7473e17117161 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 20 Nov 2024 00:35:58 +0900 Subject: [PATCH] upcast net.Conn to *net.TCPConn as soon as client-side peer connection is established --- c-peer.go | 9 ++------- client.go | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/c-peer.go b/c-peer.go index 30d8097..4b407db 100644 --- a/c-peer.go +++ b/c-peer.go @@ -4,7 +4,7 @@ import "fmt" import "net" import "sync" -func NewClientPeerConn(r *ClientRoute, c net.Conn, id uint32) (*ClientPeerConn) { +func NewClientPeerConn(r *ClientRoute, c *net.TCPConn, id uint32) (*ClientPeerConn) { var cpc ClientPeerConn cpc.route = r @@ -59,12 +59,7 @@ func (cpc *ClientPeerConn) ReqStop() { func (cpc* ClientPeerConn) CloseWrite() { if cpc.server_peer_eof.CompareAndSwap(false, true) { if cpc.conn != nil { - var conn *net.TCPConn - var ok bool - conn, ok = cpc.conn.(*net.TCPConn) - if ok { - conn.CloseWrite() - } + cpc.conn.CloseWrite() } } } diff --git a/client.go b/client.go index 43c27b6..cd1def6 100644 --- a/client.go +++ b/client.go @@ -55,7 +55,7 @@ type Client struct { type ClientPeerConn struct { route *ClientRoute conn_id uint32 - conn net.Conn + conn *net.TCPConn remot_conn_id uint32 addr string // peer address @@ -179,10 +179,12 @@ fmt.Printf ("*** Sent stop request to Route..\n"); func (r* ClientRoute) ConnectToPeer(pts_id uint32) { var err error var conn net.Conn + var real_conn *net.TCPConn var ptc *ClientPeerConn var d net.Dialer var ctx context.Context //var cancel context.CancelFunc + var ok bool // TODO: how to abort blocking DialTCP()? call cancellation funtion? // TODO: make timeuot value configurable @@ -198,7 +200,14 @@ func (r* ClientRoute) ConnectToPeer(pts_id uint32) { return } - ptc, err = r.AddNewClientPeerConn(conn, pts_id) + real_conn, ok = conn.(*net.TCPConn) + if !ok { + fmt.Printf("not tcp connection - %s\n", err.Error()) + conn.Close() + return + } + + ptc, err = r.AddNewClientPeerConn(real_conn, pts_id) if err != nil { // TODO: logging // TODO: make send peer started failure mesage? @@ -641,7 +650,7 @@ func (cts *ServerConn) ReportEvent (route_id uint32, pts_id uint32, event_type P } // -------------------------------------------------------------------- -func (r *ClientRoute) AddNewClientPeerConn (c net.Conn, pts_id uint32) (*ClientPeerConn, error) { +func (r *ClientRoute) AddNewClientPeerConn (c *net.TCPConn, pts_id uint32) (*ClientPeerConn, error) { var ptc *ClientPeerConn //var ok bool //var start_id uint32