use a net.PacketConn instead of a net.UDPConn in Server and Session

This commit is contained in:
Marten Seemann
2017-02-15 18:59:15 +07:00
parent 7fe2a37c76
commit 5b42675da2
9 changed files with 205 additions and 104 deletions

39
conn.go Normal file
View File

@@ -0,0 +1,39 @@
package quic
import (
"net"
"sync"
)
type connection interface {
write([]byte) error
setCurrentRemoteAddr(net.Addr)
RemoteAddr() net.Addr
}
type conn struct {
mutex sync.RWMutex
pconn net.PacketConn
currentAddr net.Addr
}
var _ connection = &conn{}
func (c *conn) write(p []byte) error {
_, err := c.pconn.WriteTo(p, c.currentAddr)
return err
}
func (c *conn) setCurrentRemoteAddr(addr net.Addr) {
c.mutex.Lock()
c.currentAddr = addr
c.mutex.Unlock()
}
func (c *conn) RemoteAddr() net.Addr {
c.mutex.RLock()
addr := c.currentAddr
c.mutex.RUnlock()
return addr
}