mirror of
https://github.com/xtaci/kcp-go.git
synced 2024-04-21 12:32:15 +00:00
extend newUDPSession with remote FEC parameters
This commit is contained in:
@@ -126,7 +126,7 @@ type (
|
||||
)
|
||||
|
||||
// newUDPSession create a new udp session for client or server
|
||||
func newUDPSession(conv uint32, dataShards, parityShards int, l *Listener, conn net.PacketConn, ownConn bool, remote net.Addr, block BlockCrypt) *UDPSession {
|
||||
func newUDPSession(conv uint32, dataShards, parityShards int, remoteDataShards, remoteParityShards int, l *Listener, conn net.PacketConn, ownConn bool, remote net.Addr, block BlockCrypt) *UDPSession {
|
||||
sess := new(UDPSession)
|
||||
sess.die = make(chan struct{})
|
||||
sess.nonce = new(nonceAES128)
|
||||
@@ -155,7 +155,7 @@ func newUDPSession(conv uint32, dataShards, parityShards int, l *Listener, conn
|
||||
}
|
||||
|
||||
// FEC codec initialization
|
||||
sess.fecDecoder = newFECDecoder(rxFECMulti*(dataShards+parityShards), dataShards, parityShards)
|
||||
sess.fecDecoder = newFECDecoder(rxFECMulti*(remoteDataShards+remoteParityShards), remoteDataShards, remoteParityShards)
|
||||
if sess.block != nil {
|
||||
sess.fecEncoder = newFECEncoder(dataShards, parityShards, cryptHeaderSize)
|
||||
} else {
|
||||
@@ -768,12 +768,14 @@ func (s *UDPSession) kcpInput(data []byte) {
|
||||
type (
|
||||
// Listener defines a server which will be waiting to accept incoming connections
|
||||
Listener struct {
|
||||
block BlockCrypt // block encryption
|
||||
dataShards int // FEC data shard
|
||||
parityShards int // FEC parity shard
|
||||
fecDecoder *fecDecoder // FEC mock initialization
|
||||
conn net.PacketConn // the underlying packet connection
|
||||
ownConn bool // true if we created conn internally, false if provided by caller
|
||||
block BlockCrypt // block encryption
|
||||
dataShards int // FEC data shard
|
||||
parityShards int // FEC parity shard
|
||||
remoteDataShards int // FEC remote data shard
|
||||
remoteParityShards int // FEC remote parity shard
|
||||
fecDecoder *fecDecoder // FEC mock initialization
|
||||
conn net.PacketConn // the underlying packet connection
|
||||
ownConn bool // true if we created conn internally, false if provided by caller
|
||||
|
||||
sessions map[string]*UDPSession // all sessions accepted by this Listener
|
||||
sessionLock sync.Mutex
|
||||
@@ -841,7 +843,7 @@ func (l *Listener) packetInput(data []byte, addr net.Addr) {
|
||||
|
||||
if s == nil && convValid { // new session
|
||||
if len(l.chAccepts) < cap(l.chAccepts) { // do not let the new sessions overwhelm accept queue
|
||||
s := newUDPSession(conv, l.dataShards, l.parityShards, l, l.conn, false, addr, l.block)
|
||||
s := newUDPSession(conv, l.dataShards, l.parityShards, l.remoteDataShards, l.remoteParityShards, l, l.conn, false, addr, l.block)
|
||||
s.kcpInput(data)
|
||||
l.sessionLock.Lock()
|
||||
l.sessions[addr.String()] = s
|
||||
@@ -1001,15 +1003,15 @@ func ListenWithOptions(laddr string, block BlockCrypt, dataShards, parityShards
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return serveConn(block, dataShards, parityShards, conn, true)
|
||||
return serveConn(block, dataShards, parityShards, dataShards, parityShards, conn, true)
|
||||
}
|
||||
|
||||
// ServeConn serves KCP protocol for a single packet connection.
|
||||
func ServeConn(block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*Listener, error) {
|
||||
return serveConn(block, dataShards, parityShards, conn, false)
|
||||
return serveConn(block, dataShards, parityShards, dataShards, parityShards, conn, false)
|
||||
}
|
||||
|
||||
func serveConn(block BlockCrypt, dataShards, parityShards int, conn net.PacketConn, ownConn bool) (*Listener, error) {
|
||||
func serveConn(block BlockCrypt, dataShards, parityShards int, remoteDataShards, remoteParityShards int, conn net.PacketConn, ownConn bool) (*Listener, error) {
|
||||
l := new(Listener)
|
||||
l.conn = conn
|
||||
l.ownConn = ownConn
|
||||
@@ -1019,8 +1021,10 @@ func serveConn(block BlockCrypt, dataShards, parityShards int, conn net.PacketCo
|
||||
l.die = make(chan struct{})
|
||||
l.dataShards = dataShards
|
||||
l.parityShards = parityShards
|
||||
l.remoteDataShards = remoteDataShards
|
||||
l.remoteParityShards = remoteParityShards
|
||||
l.block = block
|
||||
l.fecDecoder = newFECDecoder(rxFECMulti*(dataShards+parityShards), dataShards, parityShards)
|
||||
l.fecDecoder = newFECDecoder(rxFECMulti*(remoteDataShards+remoteParityShards), remoteDataShards, remoteParityShards)
|
||||
l.chSocketReadError = make(chan struct{})
|
||||
|
||||
// calculate header size
|
||||
@@ -1063,12 +1067,12 @@ func DialWithOptions(raddr string, block BlockCrypt, dataShards, parityShards in
|
||||
|
||||
var convid uint32
|
||||
binary.Read(rand.Reader, binary.LittleEndian, &convid)
|
||||
return newUDPSession(convid, dataShards, parityShards, nil, conn, true, udpaddr, block), nil
|
||||
return newUDPSession(convid, dataShards, parityShards, dataShards, parityShards, nil, conn, true, udpaddr, block), nil
|
||||
}
|
||||
|
||||
// NewConn3 establishes a session and talks KCP protocol over a packet connection.
|
||||
func NewConn3(convid uint32, raddr net.Addr, block BlockCrypt, dataShards, parityShards int, conn net.PacketConn) (*UDPSession, error) {
|
||||
return newUDPSession(convid, dataShards, parityShards, nil, conn, false, raddr, block), nil
|
||||
return newUDPSession(convid, dataShards, parityShards, dataShards, parityShards, nil, conn, false, raddr, block), nil
|
||||
}
|
||||
|
||||
// NewConn2 establishes a session and talks KCP protocol over a packet connection.
|
||||
|
||||
Reference in New Issue
Block a user