rename the clientMultiplexer to connMultiplexer

This commit is contained in:
Marten Seemann 2018-07-04 12:49:45 +07:00
parent a654e7600a
commit 7e2adfe19d
4 changed files with 39 additions and 39 deletions

View file

@ -123,7 +123,7 @@ func dialContext(
createdPacketConn bool,
) (Session, error) {
config = populateClientConfig(config, createdPacketConn)
multiplexer := getClientMultiplexer()
multiplexer := getMultiplexer()
manager, err := multiplexer.AddConn(pconn, config.ConnectionIDLength)
if err != nil {
return nil, err
@ -484,7 +484,7 @@ func (c *client) handleVersionNegotiationPacket(hdr *wire.Header) error {
c.initialVersion = c.version
c.version = newVersion
c.generateConnectionIDs()
if err := getClientMultiplexer().AddHandler(c.pconn, c.srcConnID, c); err != nil {
if err := getMultiplexer().AddHandler(c.pconn, c.srcConnID, c); err != nil {
return err
}

View file

@ -72,15 +72,15 @@ var _ = Describe("Client", func() {
conn: &conn{pconn: packetConn, currentAddr: addr},
logger: utils.DefaultLogger,
}
getClientMultiplexer() // make the sync.Once execute
getMultiplexer() // make the sync.Once execute
// replace the clientMuxer. getClientMultiplexer will now return the MockMultiplexer
mockMultiplexer = NewMockMultiplexer(mockCtrl)
origMultiplexer = clientMuxer
clientMuxer = mockMultiplexer
origMultiplexer = connMuxer
connMuxer = mockMultiplexer
})
AfterEach(func() {
clientMuxer = origMultiplexer
connMuxer = origMultiplexer
newClientSession = originalClientSessConstructor
})

View file

@ -15,8 +15,8 @@ import (
)
var (
clientMuxerOnce sync.Once
clientMuxer multiplexer
connMuxerOnce sync.Once
connMuxer multiplexer
)
type multiplexer interface {
@ -29,9 +29,9 @@ type connManager struct {
manager packetHandlerManager
}
// The clientMultiplexer listens on multiple net.PacketConns and dispatches
// The connMultiplexer listens on multiple net.PacketConns and dispatches
// incoming packets to the session handler.
type clientMultiplexer struct {
type connMultiplexer struct {
mutex sync.Mutex
conns map[net.PacketConn]connManager
@ -40,20 +40,20 @@ type clientMultiplexer struct {
logger utils.Logger
}
var _ multiplexer = &clientMultiplexer{}
var _ multiplexer = &connMultiplexer{}
func getClientMultiplexer() multiplexer {
clientMuxerOnce.Do(func() {
clientMuxer = &clientMultiplexer{
func getMultiplexer() multiplexer {
connMuxerOnce.Do(func() {
connMuxer = &connMultiplexer{
conns: make(map[net.PacketConn]connManager),
logger: utils.DefaultLogger.WithPrefix("client muxer"),
logger: utils.DefaultLogger.WithPrefix("muxer"),
newPacketHandlerManager: newPacketHandlerMap,
}
})
return clientMuxer
return connMuxer
}
func (m *clientMultiplexer) AddConn(c net.PacketConn, connIDLen int) (packetHandlerManager, error) {
func (m *connMultiplexer) AddConn(c net.PacketConn, connIDLen int) (packetHandlerManager, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
@ -72,7 +72,7 @@ func (m *clientMultiplexer) AddConn(c net.PacketConn, connIDLen int) (packetHand
return p.manager, nil
}
func (m *clientMultiplexer) AddHandler(c net.PacketConn, connID protocol.ConnectionID, handler packetHandler) error {
func (m *connMultiplexer) AddHandler(c net.PacketConn, connID protocol.ConnectionID, handler packetHandler) error {
m.mutex.Lock()
defer m.mutex.Unlock()
@ -84,7 +84,7 @@ func (m *clientMultiplexer) AddHandler(c net.PacketConn, connID protocol.Connect
return nil
}
func (m *clientMultiplexer) listen(c net.PacketConn, p *connManager) {
func (m *connMultiplexer) listen(c net.PacketConn, p *connManager) {
for {
data := *getPacketBuffer()
data = data[:protocol.MaxReceivePacketSize]
@ -105,7 +105,7 @@ func (m *clientMultiplexer) listen(c net.PacketConn, p *connManager) {
}
}
func (m *clientMultiplexer) handlePacket(addr net.Addr, data []byte, p *connManager) error {
func (m *connMultiplexer) handlePacket(addr net.Addr, data []byte, p *connManager) error {
rcvTime := time.Now()
r := bytes.NewReader(data)
@ -114,15 +114,15 @@ func (m *clientMultiplexer) handlePacket(addr net.Addr, data []byte, p *connMana
if err != nil {
return fmt.Errorf("error parsing invariant header: %s", err)
}
client, ok := p.manager.Get(iHdr.DestConnectionID)
handler, ok := p.manager.Get(iHdr.DestConnectionID)
if !ok {
return fmt.Errorf("received a packet with an unexpected connection ID %s", iHdr.DestConnectionID)
}
if client == nil {
if handler == nil {
// Late packet for closed session
return nil
}
hdr, err := iHdr.Parse(r, protocol.PerspectiveServer, client.GetVersion())
hdr, err := iHdr.Parse(r, protocol.PerspectiveServer, handler.GetVersion())
if err != nil {
return fmt.Errorf("error parsing header: %s", err)
}
@ -137,7 +137,7 @@ func (m *clientMultiplexer) handlePacket(addr net.Addr, data []byte, p *connMana
// TODO(#1312): implement parsing of compound packets
}
client.handlePacket(&receivedPacket{
handler.handlePacket(&receivedPacket{
remoteAddr: addr,
header: hdr,
data: packetData,

View file

@ -33,8 +33,8 @@ var _ = Describe("Client Multiplexer", func() {
close(handledPacket)
})
packetHandler.EXPECT().GetVersion()
getClientMultiplexer().AddConn(conn, 8)
err := getClientMultiplexer().AddHandler(conn, connID, packetHandler)
getMultiplexer().AddConn(conn, 8)
err := getMultiplexer().AddHandler(conn, connID, packetHandler)
Expect(err).ToNot(HaveOccurred())
conn.dataToRead <- getPacket(connID)
Eventually(handledPacket).Should(BeClosed())
@ -45,15 +45,15 @@ var _ = Describe("Client Multiplexer", func() {
It("errors when adding an existing conn with a different connection ID length", func() {
conn := newMockPacketConn()
_, err := getClientMultiplexer().AddConn(conn, 5)
_, err := getMultiplexer().AddConn(conn, 5)
Expect(err).ToNot(HaveOccurred())
_, err = getClientMultiplexer().AddConn(conn, 6)
_, err = getMultiplexer().AddConn(conn, 6)
Expect(err).To(MatchError("cannot use 6 byte connection IDs on a connection that is already using 5 byte connction IDs"))
})
It("errors when adding a handler for an unknown conn", func() {
conn := newMockPacketConn()
err := getClientMultiplexer().AddHandler(conn, protocol.ConnectionID{1, 2, 3, 4}, NewMockQuicSession(mockCtrl))
err := getMultiplexer().AddHandler(conn, protocol.ConnectionID{1, 2, 3, 4}, NewMockQuicSession(mockCtrl))
Expect(err).ToNot(MatchError("unknown packet conn"))
})
@ -75,9 +75,9 @@ var _ = Describe("Client Multiplexer", func() {
close(handledPacket2)
})
packetHandler2.EXPECT().GetVersion()
getClientMultiplexer().AddConn(conn, connID1.Len())
Expect(getClientMultiplexer().AddHandler(conn, connID1, packetHandler1)).To(Succeed())
Expect(getClientMultiplexer().AddHandler(conn, connID2, packetHandler2)).To(Succeed())
getMultiplexer().AddConn(conn, connID1.Len())
Expect(getMultiplexer().AddHandler(conn, connID1, packetHandler1)).To(Succeed())
Expect(getMultiplexer().AddHandler(conn, connID2, packetHandler2)).To(Succeed())
conn.dataToRead <- getPacket(connID1)
conn.dataToRead <- getPacket(connID2)
@ -91,7 +91,7 @@ var _ = Describe("Client Multiplexer", func() {
})
It("drops unparseable packets", func() {
err := getClientMultiplexer().(*clientMultiplexer).handlePacket(nil, []byte("invalid"), &connManager{connIDLen: 8})
err := getMultiplexer().(*connMultiplexer).handlePacket(nil, []byte("invalid"), &connManager{connIDLen: 8})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error parsing invariant header:"))
})
@ -100,7 +100,7 @@ var _ = Describe("Client Multiplexer", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
manager := NewMockPacketHandlerManager(mockCtrl)
manager.EXPECT().Get(connID).Return(nil, true)
err := getClientMultiplexer().(*clientMultiplexer).handlePacket(nil, getPacket(connID), &connManager{manager: manager, connIDLen: 8})
err := getMultiplexer().(*connMultiplexer).handlePacket(nil, getPacket(connID), &connManager{manager: manager, connIDLen: 8})
Expect(err).ToNot(HaveOccurred())
})
@ -108,7 +108,7 @@ var _ = Describe("Client Multiplexer", func() {
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
manager := NewMockPacketHandlerManager(mockCtrl)
manager.EXPECT().Get(connID).Return(nil, false)
err := getClientMultiplexer().(*clientMultiplexer).handlePacket(nil, getPacket(connID), &connManager{manager: manager, connIDLen: 8})
err := getMultiplexer().(*connMultiplexer).handlePacket(nil, getPacket(connID), &connManager{manager: manager, connIDLen: 8})
Expect(err).To(MatchError("received a packet with an unexpected connection ID 0x0102030405060708"))
})
@ -130,7 +130,7 @@ var _ = Describe("Client Multiplexer", func() {
sess.EXPECT().GetVersion().Return(versionIETFFrames)
manager := NewMockPacketHandlerManager(mockCtrl)
manager.EXPECT().Get(connID).Return(sess, true)
err := getClientMultiplexer().(*clientMultiplexer).handlePacket(nil, buf.Bytes(), &connManager{manager: manager, connIDLen: 8})
err := getMultiplexer().(*connMultiplexer).handlePacket(nil, buf.Bytes(), &connManager{manager: manager, connIDLen: 8})
Expect(err).To(MatchError("packet payload (500 bytes) is smaller than the expected payload length (1000 bytes)"))
})
@ -155,7 +155,7 @@ var _ = Describe("Client Multiplexer", func() {
})
manager := NewMockPacketHandlerManager(mockCtrl)
manager.EXPECT().Get(connID).Return(sess, true)
err := getClientMultiplexer().(*clientMultiplexer).handlePacket(nil, buf.Bytes(), &connManager{manager: manager, connIDLen: 8})
err := getMultiplexer().(*connMultiplexer).handlePacket(nil, buf.Bytes(), &connManager{manager: manager, connIDLen: 8})
Expect(err).ToNot(HaveOccurred())
})
@ -167,8 +167,8 @@ var _ = Describe("Client Multiplexer", func() {
packetHandler.EXPECT().Close().Do(func() {
close(done)
})
getClientMultiplexer().AddConn(conn, 8)
Expect(getClientMultiplexer().AddHandler(conn, protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, packetHandler)).To(Succeed())
getMultiplexer().AddConn(conn, 8)
Expect(getMultiplexer().AddHandler(conn, protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, packetHandler)).To(Succeed())
Eventually(done).Should(BeClosed())
})
})