mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-27 14:20:27 +08:00
feat(p2p): add ip
This commit is contained in:
32
gold/p2p/ip/init.go
Normal file
32
gold/p2p/ip/init.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package ip
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
)
|
||||
|
||||
func NewEndpoint(endpoint string, configs ...any) (p2p.EndPoint, error) {
|
||||
addr, err := netip.ParseAddr(endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ptcl := uint(0x04) // IPIP
|
||||
return &EndPoint{
|
||||
addr: &net.IPAddr{
|
||||
IP: addr.AsSlice(),
|
||||
Zone: addr.Zone(),
|
||||
},
|
||||
ptcl: ptcl,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
name := helper.FolderName()
|
||||
_, hasexist := p2p.Register(name, NewEndpoint)
|
||||
if hasexist {
|
||||
panic("network " + name + " has been registered")
|
||||
}
|
||||
}
|
||||
79
gold/p2p/ip/ip.go
Normal file
79
gold/p2p/ip/ip.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package ip
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
)
|
||||
|
||||
type EndPoint struct {
|
||||
addr *net.IPAddr
|
||||
ptcl uint
|
||||
}
|
||||
|
||||
func (ep *EndPoint) String() string {
|
||||
return ep.addr.String()
|
||||
}
|
||||
|
||||
func (ep *EndPoint) Network() string {
|
||||
return ep.addr.Network()
|
||||
}
|
||||
|
||||
func (ep *EndPoint) Euqal(ep2 p2p.EndPoint) bool {
|
||||
if ep == nil || ep2 == nil {
|
||||
return ep == nil && ep2 == nil
|
||||
}
|
||||
ipep2, ok := ep2.(*EndPoint)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
ipep1 := ep
|
||||
return ipep1.addr.IP.Equal(ipep2.addr.IP) &&
|
||||
ipep1.addr.Zone == ipep2.addr.Zone
|
||||
}
|
||||
|
||||
func (ep *EndPoint) Listen() (p2p.Conn, error) {
|
||||
conn, err := net.ListenIP(
|
||||
"ip:"+strconv.Itoa(int(ep.ptcl)),
|
||||
ep.addr,
|
||||
)
|
||||
return &Conn{
|
||||
ep: ep,
|
||||
conn: conn,
|
||||
}, err
|
||||
}
|
||||
|
||||
type Conn struct {
|
||||
ep *EndPoint
|
||||
conn *net.IPConn
|
||||
}
|
||||
|
||||
func (conn *Conn) Close() error {
|
||||
return conn.conn.Close()
|
||||
}
|
||||
|
||||
func (conn *Conn) String() string {
|
||||
return conn.conn.LocalAddr().String()
|
||||
}
|
||||
|
||||
func (conn *Conn) LocalAddr() p2p.EndPoint {
|
||||
ep, _ := NewEndpoint(conn.conn.LocalAddr().String())
|
||||
return ep
|
||||
}
|
||||
|
||||
func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
|
||||
n, addr, err := conn.conn.ReadFromIP(b)
|
||||
return n, &EndPoint{
|
||||
addr: addr,
|
||||
ptcl: conn.ep.ptcl,
|
||||
}, err
|
||||
}
|
||||
|
||||
func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (int, error) {
|
||||
ipep, ok := ep.(*EndPoint)
|
||||
if !ok {
|
||||
return 0, p2p.ErrEndpointTypeMistatch
|
||||
}
|
||||
return conn.conn.WriteToIP(b, ipep.addr)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -38,8 +39,9 @@ func newEndpoint(endpoint string, configs ...any) (*EndPoint, error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
_, hasexist := p2p.Register("tcp", NewEndpoint)
|
||||
name := helper.FolderName()
|
||||
_, hasexist := p2p.Register(name, NewEndpoint)
|
||||
if hasexist {
|
||||
panic("network tcp has been registered")
|
||||
panic("network " + name + " has been registered")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net/netip"
|
||||
|
||||
"github.com/fumiama/WireGold/gold/p2p"
|
||||
"github.com/fumiama/WireGold/helper"
|
||||
)
|
||||
|
||||
func NewEndpoint(endpoint string, _ ...any) (p2p.EndPoint, error) {
|
||||
@@ -16,8 +17,9 @@ func NewEndpoint(endpoint string, _ ...any) (p2p.EndPoint, error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
_, hasexist := p2p.Register("udp", NewEndpoint)
|
||||
name := helper.FolderName()
|
||||
_, hasexist := p2p.Register(name, NewEndpoint)
|
||||
if hasexist {
|
||||
panic("network udp has been registered")
|
||||
panic("network " + name + " has been registered")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user