mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-20 21:33:19 -06:00
Updates to yamux
This commit is contained in:
parent
23d2341811
commit
e2d9f86569
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/libp2p/go-yamux"
|
||||||
yggdrasiladmin "github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
yggdrasiladmin "github.com/yggdrasil-network/yggdrasil-go/src/admin"
|
||||||
yggdrasilconfig "github.com/yggdrasil-network/yggdrasil-go/src/config"
|
yggdrasilconfig "github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||||
yggdrasilmulticast "github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
yggdrasilmulticast "github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
||||||
|
|
@ -27,9 +28,8 @@ type Node struct {
|
||||||
log *gologme.Logger
|
log *gologme.Logger
|
||||||
listener *yggdrasil.Listener
|
listener *yggdrasil.Listener
|
||||||
dialer *yggdrasil.Dialer
|
dialer *yggdrasil.Dialer
|
||||||
conns sync.Map // string -> yggdrasil.Conn
|
|
||||||
sessions sync.Map // string -> yamux.Session
|
sessions sync.Map // string -> yamux.Session
|
||||||
incoming chan *stream
|
incoming chan *yamux.Stream
|
||||||
}
|
}
|
||||||
|
|
||||||
func Setup(instanceName string) (*Node, error) {
|
func Setup(instanceName string) (*Node, error) {
|
||||||
|
|
@ -38,7 +38,7 @@ func Setup(instanceName string) (*Node, error) {
|
||||||
admin: &yggdrasiladmin.AdminSocket{},
|
admin: &yggdrasiladmin.AdminSocket{},
|
||||||
multicast: &yggdrasilmulticast.Multicast{},
|
multicast: &yggdrasilmulticast.Multicast{},
|
||||||
log: gologme.New(os.Stdout, "YGG ", log.Flags()),
|
log: gologme.New(os.Stdout, "YGG ", log.Flags()),
|
||||||
incoming: make(chan *stream),
|
incoming: make(chan *yamux.Stream),
|
||||||
}
|
}
|
||||||
n.config.AdminListen = fmt.Sprintf("unix://./%s-yggdrasil.sock", instanceName)
|
n.config.AdminListen = fmt.Sprintf("unix://./%s-yggdrasil.sock", instanceName)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,35 +2,47 @@ package yggconn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/libp2p/go-yamux"
|
"github.com/libp2p/go-yamux"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (n *Node) yamuxConfig() *yamux.Config {
|
||||||
|
cfg := yamux.DefaultConfig()
|
||||||
|
cfg.EnableKeepAlive = true
|
||||||
|
cfg.KeepAliveInterval = time.Second
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Node) listenFromYgg() {
|
func (n *Node) listenFromYgg() {
|
||||||
for {
|
for {
|
||||||
conn, err := n.listener.Accept()
|
conn, err := n.listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("n.listener.Accept:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session, err := yamux.Server(conn, nil)
|
session, err := yamux.Server(conn, n.yamuxConfig())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("yamux.Server:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n.conns.Store(conn.RemoteAddr(), conn)
|
go n.listenFromYggConn(session)
|
||||||
n.sessions.Store(conn.RemoteAddr(), session)
|
|
||||||
go n.listenFromYggConn(session, conn)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) listenFromYggConn(session *yamux.Session, conn net.Conn) {
|
func (n *Node) listenFromYggConn(session *yamux.Session) {
|
||||||
|
n.sessions.Store(session.RemoteAddr().String(), session)
|
||||||
|
defer n.sessions.Delete(session.RemoteAddr())
|
||||||
|
|
||||||
for {
|
for {
|
||||||
st, err := session.AcceptStream()
|
st, err := session.AcceptStream()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("session.AcceptStream:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n.incoming <- &stream{st, conn}
|
n.incoming <- st
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,34 +63,24 @@ func (n *Node) Dial(network, address string) (net.Conn, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
func (n *Node) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
conn, err := n.dialer.DialContext(ctx, network, address)
|
s, ok1 := n.sessions.Load(address)
|
||||||
if err != nil {
|
session, ok2 := s.(*yamux.Session)
|
||||||
c, ok := n.conns.Load(address)
|
if !ok1 || !ok2 {
|
||||||
if !ok {
|
conn, err := n.dialer.DialContext(ctx, network, address)
|
||||||
return nil, errors.New("conn not found")
|
if err != nil {
|
||||||
|
fmt.Println("n.dialer.DialContext:", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
conn, ok = c.(net.Conn)
|
session, err = yamux.Client(conn, n.yamuxConfig())
|
||||||
if !ok {
|
if err != nil {
|
||||||
return nil, errors.New("conn type assertion error")
|
fmt.Println("yamux.Client.AcceptStream:", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
go n.listenFromYggConn(session)
|
||||||
client, cerr := yamux.Client(conn, nil)
|
|
||||||
if cerr != nil {
|
|
||||||
return nil, cerr
|
|
||||||
}
|
|
||||||
n.sessions.Store(address, client)
|
|
||||||
}
|
}
|
||||||
s, ok := n.sessions.Load(address)
|
st, err := session.OpenStream()
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("session not found")
|
|
||||||
}
|
|
||||||
session, ok := s.(*yamux.Session)
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("session type assertion error")
|
|
||||||
}
|
|
||||||
ch, err := session.OpenStream()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &stream{ch, conn}, nil
|
return st, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,17 @@
|
||||||
package yggconn
|
package yggconn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/libp2p/go-yamux"
|
"github.com/libp2p/go-yamux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stream struct {
|
type stream struct {
|
||||||
*yamux.Stream
|
*yamux.Stream
|
||||||
conn net.Conn
|
//conn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func (c *stream) LocalAddr() net.Addr {
|
func (c *stream) LocalAddr() net.Addr {
|
||||||
return c.conn.LocalAddr()
|
return c.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *stream) RemoteAddr() net.Addr {
|
func (c *stream) RemoteAddr() net.Addr {
|
||||||
|
|
@ -31,3 +29,4 @@ func (c *stream) SetReadDeadline(t time.Time) error {
|
||||||
func (c *stream) SetWriteDeadline(t time.Time) error {
|
func (c *stream) SetWriteDeadline(t time.Time) error {
|
||||||
return c.conn.SetWriteDeadline(t)
|
return c.conn.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -16,6 +16,7 @@ require (
|
||||||
github.com/libp2p/go-libp2p-kad-dht v0.5.0
|
github.com/libp2p/go-libp2p-kad-dht v0.5.0
|
||||||
github.com/libp2p/go-libp2p-pubsub v0.2.5
|
github.com/libp2p/go-libp2p-pubsub v0.2.5
|
||||||
github.com/libp2p/go-libp2p-record v0.1.2
|
github.com/libp2p/go-libp2p-record v0.1.2
|
||||||
|
github.com/libp2p/go-yamux v1.3.7
|
||||||
github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5
|
github.com/matrix-org/dugong v0.0.0-20171220115018-ea0a4690a0d5
|
||||||
github.com/matrix-org/go-http-js-libp2p v0.0.0-20200518170932-783164aeeda4
|
github.com/matrix-org/go-http-js-libp2p v0.0.0-20200518170932-783164aeeda4
|
||||||
github.com/matrix-org/go-sqlite3-js v0.0.0-20200522092705-bc8506ccbcf3
|
github.com/matrix-org/go-sqlite3-js v0.0.0-20200522092705-bc8506ccbcf3
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -356,6 +356,8 @@ github.com/libp2p/go-yamux v1.2.3 h1:xX8A36vpXb59frIzWFdEgptLMsOANMFq2K7fPRlunYI
|
||||||
github.com/libp2p/go-yamux v1.2.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow=
|
github.com/libp2p/go-yamux v1.2.3/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow=
|
||||||
github.com/libp2p/go-yamux v1.3.0 h1:FsYzT16Wq2XqUGJsBbOxoz9g+dFklvNi7jN6YFPfl7U=
|
github.com/libp2p/go-yamux v1.3.0 h1:FsYzT16Wq2XqUGJsBbOxoz9g+dFklvNi7jN6YFPfl7U=
|
||||||
github.com/libp2p/go-yamux v1.3.0/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow=
|
github.com/libp2p/go-yamux v1.3.0/go.mod h1:FGTiPvoV/3DVdgWpX+tM0OW3tsM+W5bSE3gZwqQTcow=
|
||||||
|
github.com/libp2p/go-yamux v1.3.7 h1:v40A1eSPJDIZwz2AvrV3cxpTZEGDP11QJbukmEhYyQI=
|
||||||
|
github.com/libp2p/go-yamux v1.3.7/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE=
|
||||||
github.com/lxn/walk v0.0.0-20191128110447-55ccb3a9f5c1/go.mod h1:E23UucZGqpuUANJooIbHWCufXvOcT6E7Stq81gU+CSQ=
|
github.com/lxn/walk v0.0.0-20191128110447-55ccb3a9f5c1/go.mod h1:E23UucZGqpuUANJooIbHWCufXvOcT6E7Stq81gU+CSQ=
|
||||||
github.com/lxn/win v0.0.0-20191128105842-2da648fda5b4/go.mod h1:ouWl4wViUNh8tPSIwxTVMuS014WakR1hqvBc2I0bMoA=
|
github.com/lxn/win v0.0.0-20191128105842-2da648fda5b4/go.mod h1:ouWl4wViUNh8tPSIwxTVMuS014WakR1hqvBc2I0bMoA=
|
||||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue