mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-21 02:40:24 +08:00
fix(p2p): handle error on parsing endpoint
This commit is contained in:
@@ -12,7 +12,7 @@ var (
|
|||||||
ErrEndpointTypeMistatch = errors.New("endpoint type mismatch")
|
ErrEndpointTypeMistatch = errors.New("endpoint type mismatch")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Initializer func(endpoint string, configs ...any) EndPoint
|
type Initializer func(endpoint string, configs ...any) (EndPoint, error)
|
||||||
|
|
||||||
var factory syncx.Map[string, Initializer]
|
var factory syncx.Map[string, Initializer]
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ func NewEndPoint(network, endpoint string, configs ...any) (EndPoint, error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("network " + network + " not found")
|
return nil, errors.New("network " + network + " not found")
|
||||||
}
|
}
|
||||||
return initializer(endpoint, configs...), nil
|
return initializer(endpoint, configs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Conn interface {
|
type Conn interface {
|
||||||
|
|||||||
@@ -14,25 +14,27 @@ type Config struct {
|
|||||||
ReceiveChannelSize int
|
ReceiveChannelSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEndpoint(endpoint string, configs ...any) p2p.EndPoint {
|
func NewEndpoint(endpoint string, configs ...any) (p2p.EndPoint, error) {
|
||||||
return newEndpoint(endpoint, configs...)
|
return newEndpoint(endpoint, configs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newEndpoint(endpoint string, configs ...any) *EndPoint {
|
func newEndpoint(endpoint string, configs ...any) (*EndPoint, error) {
|
||||||
var cfg *Config
|
var cfg *Config
|
||||||
if len(configs) == 0 || configs[0] == nil {
|
if len(configs) == 0 || configs[0] == nil {
|
||||||
cfg = &Config{}
|
cfg = &Config{}
|
||||||
} else {
|
} else {
|
||||||
cfg = configs[0].(*Config)
|
cfg = configs[0].(*Config)
|
||||||
}
|
}
|
||||||
|
addr, err := netip.ParseAddrPort(endpoint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &EndPoint{
|
return &EndPoint{
|
||||||
addr: net.TCPAddrFromAddrPort(
|
addr: net.TCPAddrFromAddrPort(addr),
|
||||||
netip.MustParseAddrPort(endpoint),
|
|
||||||
),
|
|
||||||
dialtimeout: cfg.DialTimeout,
|
dialtimeout: cfg.DialTimeout,
|
||||||
peerstimeout: cfg.PeersTimeout,
|
peerstimeout: cfg.PeersTimeout,
|
||||||
recvchansize: cfg.ReceiveChannelSize,
|
recvchansize: cfg.ReceiveChannelSize,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ func (conn *Conn) accept() {
|
|||||||
logrus.Info("[tcp] re-listen on", conn.addr)
|
logrus.Info("[tcp] re-listen on", conn.addr)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ep := newEndpoint(tcpconn.RemoteAddr().String(), &Config{
|
ep, _ := newEndpoint(tcpconn.RemoteAddr().String(), &Config{
|
||||||
DialTimeout: conn.addr.dialtimeout,
|
DialTimeout: conn.addr.dialtimeout,
|
||||||
PeersTimeout: conn.addr.peerstimeout,
|
PeersTimeout: conn.addr.peerstimeout,
|
||||||
ReceiveChannelSize: conn.addr.recvchansize,
|
ReceiveChannelSize: conn.addr.recvchansize,
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import (
|
|||||||
"github.com/fumiama/WireGold/gold/p2p"
|
"github.com/fumiama/WireGold/gold/p2p"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewEndpoint(endpoint string, _ ...any) p2p.EndPoint {
|
func NewEndpoint(endpoint string, _ ...any) (p2p.EndPoint, error) {
|
||||||
return (*EndPoint)(net.UDPAddrFromAddrPort(
|
addr, err := netip.ParseAddrPort(endpoint)
|
||||||
netip.MustParseAddrPort(endpoint),
|
if err != nil {
|
||||||
))
|
return nil, err
|
||||||
|
}
|
||||||
|
return (*EndPoint)(net.UDPAddrFromAddrPort(addr)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ func (conn *Conn) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) LocalAddr() p2p.EndPoint {
|
func (conn *Conn) LocalAddr() p2p.EndPoint {
|
||||||
return NewEndpoint((*net.UDPConn)(conn).LocalAddr().String())
|
ep, _ := NewEndpoint((*net.UDPConn)(conn).LocalAddr().String())
|
||||||
|
return ep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
|
func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user