xtaci / kcp-go

A Crypto-Secure Reliable-UDP Library for golang with FEC
MIT License
4.09k stars 733 forks source link

新增:服务端主动对外发动链接方法 #268

Closed back0893 closed 4 months ago

back0893 commented 4 months ago
// 新增Listener主动发起请求
func (l *Listener) NewConn(raddr string) (*UDPSession, error) {
    addr, err := net.ResolveUDPAddr("udp", raddr)
    if err != nil {
        return nil, errors.WithStack(err)
    }
    return l.NewConn2(addr)
}
func (l *Listener) NewConn2(addr net.Addr) (*UDPSession, error) {
    var convid uint32
    binary.Read(rand.Reader, binary.LittleEndian, &convid)
    s := newUDPSession(convid, l.dataShards, l.parityShards, l, l.conn, false, addr, l.block)
    l.sessionLock.Lock()
    defer l.sessionLock.Unlock()
    l.sessions[addr.String()] = s
    return s, nil
}

是否可以在master中添加上主动发送请求的方法 方便在开发p2p时,既可以作为服务端,也可以主动发起请求,作为客户端