mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-04 12:47:36 +03:00
identify connections by their local addr when adding to the multiplexer
This commit is contained in:
parent
d689f9a392
commit
81be522bf3
4 changed files with 33 additions and 6 deletions
|
@ -25,6 +25,7 @@ type mockPacketConn struct {
|
||||||
|
|
||||||
func newMockPacketConn() *mockPacketConn {
|
func newMockPacketConn() *mockPacketConn {
|
||||||
return &mockPacketConn{
|
return &mockPacketConn{
|
||||||
|
addr: &net.UDPAddr{IP: net.IPv6zero, Port: 0x42},
|
||||||
dataToRead: make(chan []byte, 1000),
|
dataToRead: make(chan []byte, 1000),
|
||||||
dataWritten: make(chan mockPacketConnWrite, 1000),
|
dataWritten: make(chan mockPacketConnWrite, 1000),
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ type connManager struct {
|
||||||
type connMultiplexer struct {
|
type connMultiplexer struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
|
||||||
conns map[net.PacketConn]connManager
|
conns map[string] /* LocalAddr().String() */ connManager
|
||||||
newPacketHandlerManager func(net.PacketConn, int, []byte, utils.Logger) packetHandlerManager // so it can be replaced in the tests
|
newPacketHandlerManager func(net.PacketConn, int, []byte, utils.Logger) packetHandlerManager // so it can be replaced in the tests
|
||||||
|
|
||||||
logger utils.Logger
|
logger utils.Logger
|
||||||
|
@ -41,7 +41,7 @@ var _ multiplexer = &connMultiplexer{}
|
||||||
func getMultiplexer() multiplexer {
|
func getMultiplexer() multiplexer {
|
||||||
connMuxerOnce.Do(func() {
|
connMuxerOnce.Do(func() {
|
||||||
connMuxer = &connMultiplexer{
|
connMuxer = &connMultiplexer{
|
||||||
conns: make(map[net.PacketConn]connManager),
|
conns: make(map[string]connManager),
|
||||||
logger: utils.DefaultLogger.WithPrefix("muxer"),
|
logger: utils.DefaultLogger.WithPrefix("muxer"),
|
||||||
newPacketHandlerManager: newPacketHandlerMap,
|
newPacketHandlerManager: newPacketHandlerMap,
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,8 @@ func (m *connMultiplexer) AddConn(
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
|
|
||||||
p, ok := m.conns[c]
|
laddr := c.LocalAddr().String()
|
||||||
|
p, ok := m.conns[laddr]
|
||||||
if !ok {
|
if !ok {
|
||||||
manager := m.newPacketHandlerManager(c, connIDLen, statelessResetKey, m.logger)
|
manager := m.newPacketHandlerManager(c, connIDLen, statelessResetKey, m.logger)
|
||||||
p = connManager{
|
p = connManager{
|
||||||
|
@ -65,7 +66,7 @@ func (m *connMultiplexer) AddConn(
|
||||||
statelessResetKey: statelessResetKey,
|
statelessResetKey: statelessResetKey,
|
||||||
manager: manager,
|
manager: manager,
|
||||||
}
|
}
|
||||||
m.conns[c] = p
|
m.conns[laddr] = p
|
||||||
}
|
}
|
||||||
if p.connIDLen != connIDLen {
|
if p.connIDLen != connIDLen {
|
||||||
return nil, fmt.Errorf("cannot use %d byte connection IDs on a connection that is already using %d byte connction IDs", connIDLen, p.connIDLen)
|
return nil, fmt.Errorf("cannot use %d byte connection IDs on a connection that is already using %d byte connction IDs", connIDLen, p.connIDLen)
|
||||||
|
@ -80,10 +81,11 @@ func (m *connMultiplexer) RemoveConn(c net.PacketConn) error {
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
|
|
||||||
if _, ok := m.conns[c]; !ok {
|
laddr := c.LocalAddr().String()
|
||||||
|
if _, ok := m.conns[laddr]; !ok {
|
||||||
return fmt.Errorf("cannote remove connection, connection is unknown")
|
return fmt.Errorf("cannote remove connection, connection is unknown")
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(m.conns, c)
|
delete(m.conns, laddr)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
package quic
|
package quic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type testConn struct {
|
||||||
|
counter int
|
||||||
|
net.PacketConn
|
||||||
|
}
|
||||||
|
|
||||||
var _ = Describe("Client Multiplexer", func() {
|
var _ = Describe("Client Multiplexer", func() {
|
||||||
It("adds a new packet conn ", func() {
|
It("adds a new packet conn ", func() {
|
||||||
conn := newMockPacketConn()
|
conn := newMockPacketConn()
|
||||||
|
@ -12,6 +19,18 @@ var _ = Describe("Client Multiplexer", func() {
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("recognizes when the same connection is added twice", func() {
|
||||||
|
pconn := newMockPacketConn()
|
||||||
|
pconn.addr = &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 4321}
|
||||||
|
conn := testConn{PacketConn: pconn}
|
||||||
|
_, err := getMultiplexer().AddConn(conn, 8, nil)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
conn.counter++
|
||||||
|
_, err = getMultiplexer().AddConn(conn, 8, nil)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Expect(getMultiplexer().(*connMultiplexer).conns).To(HaveLen(1))
|
||||||
|
})
|
||||||
|
|
||||||
It("errors when adding an existing conn with a different connection ID length", func() {
|
It("errors when adding an existing conn with a different connection ID length", func() {
|
||||||
conn := newMockPacketConn()
|
conn := newMockPacketConn()
|
||||||
_, err := getMultiplexer().AddConn(conn, 5, nil)
|
_, err := getMultiplexer().AddConn(conn, 5, nil)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package quic
|
package quic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
@ -17,6 +19,9 @@ var mockCtrl *gomock.Controller
|
||||||
|
|
||||||
var _ = BeforeEach(func() {
|
var _ = BeforeEach(func() {
|
||||||
mockCtrl = gomock.NewController(GinkgoT())
|
mockCtrl = gomock.NewController(GinkgoT())
|
||||||
|
|
||||||
|
// reset the sync.Once
|
||||||
|
connMuxerOnce = *new(sync.Once)
|
||||||
})
|
})
|
||||||
|
|
||||||
var _ = AfterEach(func() {
|
var _ = AfterEach(func() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue