1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-04 23:40:26 +08:00
Files
WireGold/gold/p2p/define.go
2026-04-11 15:02:45 +08:00

45 lines
962 B
Go

package p2p
import (
"errors"
"fmt"
"io"
"github.com/RomiChan/syncx"
)
var (
ErrEndpointTypeMistatch = errors.New("endpoint type mismatch")
)
type Initializer func(endpoint string, configs ...any) (EndPoint, error)
var factory syncx.Map[string, Initializer]
func Register(network string, initializer Initializer) (actual Initializer, hasexist bool) {
return factory.LoadOrStore(network, initializer)
}
type EndPoint interface {
fmt.Stringer
Network() string
Equal(EndPoint) bool
Listen() (Conn, error)
}
func NewEndPoint(network, endpoint string, configs ...any) (EndPoint, error) {
initializer, ok := factory.Load(network)
if !ok {
return nil, errors.New("network " + network + " not found")
}
return initializer(endpoint, configs...)
}
type Conn interface {
io.Closer
fmt.Stringer // basically, the local address string
LocalAddr() EndPoint
ReadFromPeer([]byte) (int, EndPoint, error)
WriteToPeer([]byte, EndPoint) (int, error)
}