upcast net.Conn to *net.TCPConn as soon as client-side peer connection is established

This commit is contained in:
hyung-hwan 2024-11-20 00:35:58 +09:00
parent 188900c1ae
commit de85c4c754
2 changed files with 14 additions and 10 deletions

View File

@ -4,7 +4,7 @@ import "fmt"
import "net" import "net"
import "sync" 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 var cpc ClientPeerConn
cpc.route = r cpc.route = r
@ -59,12 +59,7 @@ func (cpc *ClientPeerConn) ReqStop() {
func (cpc* ClientPeerConn) CloseWrite() { func (cpc* ClientPeerConn) CloseWrite() {
if cpc.server_peer_eof.CompareAndSwap(false, true) { if cpc.server_peer_eof.CompareAndSwap(false, true) {
if cpc.conn != nil { if cpc.conn != nil {
var conn *net.TCPConn cpc.conn.CloseWrite()
var ok bool
conn, ok = cpc.conn.(*net.TCPConn)
if ok {
conn.CloseWrite()
}
} }
} }
} }

View File

@ -55,7 +55,7 @@ type Client struct {
type ClientPeerConn struct { type ClientPeerConn struct {
route *ClientRoute route *ClientRoute
conn_id uint32 conn_id uint32
conn net.Conn conn *net.TCPConn
remot_conn_id uint32 remot_conn_id uint32
addr string // peer address addr string // peer address
@ -179,10 +179,12 @@ fmt.Printf ("*** Sent stop request to Route..\n");
func (r* ClientRoute) ConnectToPeer(pts_id uint32) { func (r* ClientRoute) ConnectToPeer(pts_id uint32) {
var err error var err error
var conn net.Conn var conn net.Conn
var real_conn *net.TCPConn
var ptc *ClientPeerConn var ptc *ClientPeerConn
var d net.Dialer var d net.Dialer
var ctx context.Context var ctx context.Context
//var cancel context.CancelFunc //var cancel context.CancelFunc
var ok bool
// TODO: how to abort blocking DialTCP()? call cancellation funtion? // TODO: how to abort blocking DialTCP()? call cancellation funtion?
// TODO: make timeuot value configurable // TODO: make timeuot value configurable
@ -198,7 +200,14 @@ func (r* ClientRoute) ConnectToPeer(pts_id uint32) {
return 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 { if err != nil {
// TODO: logging // TODO: logging
// TODO: make send peer started failure mesage? // 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 ptc *ClientPeerConn
//var ok bool //var ok bool
//var start_id uint32 //var start_id uint32