mirror of
https://github.com/cmz0228/hysteria-dev.git
synced 2025-06-25 05:50:05 +00:00
chore: improve udphop conn
This commit is contained in:
parent
5efd0f8f44
commit
81a98e1e5a
@ -40,6 +40,7 @@ type udpPacket struct {
|
|||||||
Buf []byte
|
Buf []byte
|
||||||
N int
|
N int
|
||||||
Addr net.Addr
|
Addr net.Addr
|
||||||
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDPHopPacketConn(addr *UDPHopAddr, hopInterval time.Duration) (net.PacketConn, error) {
|
func NewUDPHopPacketConn(addr *UDPHopAddr, hopInterval time.Duration) (net.PacketConn, error) {
|
||||||
@ -81,10 +82,13 @@ func (u *udpHopPacketConn) recvLoop(conn net.PacketConn) {
|
|||||||
buf := u.bufPool.Get().([]byte)
|
buf := u.bufPool.Get().([]byte)
|
||||||
n, addr, err := conn.ReadFrom(buf)
|
n, addr, err := conn.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
u.bufPool.Put(buf)
|
||||||
|
u.recvQueue <- &udpPacket{nil, 0, nil, err}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case u.recvQueue <- &udpPacket{buf, n, addr}:
|
case u.recvQueue <- &udpPacket{buf, n, addr, nil}:
|
||||||
|
// Packet successfully queued
|
||||||
default:
|
default:
|
||||||
// Queue is full, drop the packet
|
// Queue is full, drop the packet
|
||||||
u.bufPool.Put(buf)
|
u.bufPool.Put(buf)
|
||||||
@ -145,6 +149,9 @@ func (u *udpHopPacketConn) ReadFrom(b []byte) (n int, addr net.Addr, err error)
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case p := <-u.recvQueue:
|
case p := <-u.recvQueue:
|
||||||
|
if p.Err != nil {
|
||||||
|
return 0, nil, p.Err
|
||||||
|
}
|
||||||
// Currently we do not check whether the packet is from
|
// Currently we do not check whether the packet is from
|
||||||
// the server or not due to performance reasons.
|
// the server or not due to performance reasons.
|
||||||
n := copy(b, p.Buf[:p.N])
|
n := copy(b, p.Buf[:p.N])
|
||||||
@ -193,18 +200,30 @@ func (u *udpHopPacketConn) LocalAddr() net.Addr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *udpHopPacketConn) SetDeadline(t time.Time) error {
|
func (u *udpHopPacketConn) SetDeadline(t time.Time) error {
|
||||||
// Not implemented
|
u.connMutex.RLock()
|
||||||
return nil
|
defer u.connMutex.RUnlock()
|
||||||
|
if u.prevConn != nil {
|
||||||
|
_ = u.prevConn.SetDeadline(t)
|
||||||
|
}
|
||||||
|
return u.currentConn.SetDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *udpHopPacketConn) SetReadDeadline(t time.Time) error {
|
func (u *udpHopPacketConn) SetReadDeadline(t time.Time) error {
|
||||||
// Not implemented
|
u.connMutex.RLock()
|
||||||
return nil
|
defer u.connMutex.RUnlock()
|
||||||
|
if u.prevConn != nil {
|
||||||
|
_ = u.prevConn.SetReadDeadline(t)
|
||||||
|
}
|
||||||
|
return u.currentConn.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *udpHopPacketConn) SetWriteDeadline(t time.Time) error {
|
func (u *udpHopPacketConn) SetWriteDeadline(t time.Time) error {
|
||||||
// Not implemented
|
u.connMutex.RLock()
|
||||||
return nil
|
defer u.connMutex.RUnlock()
|
||||||
|
if u.prevConn != nil {
|
||||||
|
_ = u.prevConn.SetWriteDeadline(t)
|
||||||
|
}
|
||||||
|
return u.currentConn.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UDP-specific methods below
|
// UDP-specific methods below
|
||||||
|
Loading…
x
Reference in New Issue
Block a user