This commit is contained in:
xtaci
2024-03-11 22:08:35 +08:00
parent 16b0610a85
commit d26364a67c
+13 -3
View File
@@ -195,6 +195,7 @@ func newUDPSession(conv uint32, dataShards, parityShards int, l *Listener, conn
// Read implements net.Conn
func (s *UDPSession) Read(b []byte) (n int, err error) {
RESET_TIMER:
var timeout *time.Timer
// deadline for current reading operation
var c <-chan time.Time
@@ -243,6 +244,10 @@ func (s *UDPSession) Read(b []byte) (n int, err error) {
// wait for read event or timeout or error
select {
case <-s.chReadEvent:
if timeout != nil {
timeout.Stop()
goto RESET_TIMER
}
case <-c:
return 0, errors.WithStack(errTimeout)
case <-s.chSocketReadError:
@@ -258,6 +263,7 @@ func (s *UDPSession) Write(b []byte) (n int, err error) { return s.WriteBuffers(
// WriteBuffers write a vector of byte slices to the underlying connection
func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) {
RESET_TIMER:
var timeout *time.Timer
var c <-chan time.Time
if !s.wd.IsZero() {
@@ -308,6 +314,10 @@ func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) {
select {
case <-s.chWriteEvent:
if timeout != nil {
timeout.Stop()
goto RESET_TIMER
}
case <-c:
return 0, errors.WithStack(errTimeout)
case <-s.chSocketWriteError:
@@ -375,9 +385,9 @@ func (s *UDPSession) RemoteAddr() net.Addr { return s.remote }
// SetDeadline sets the deadline associated with the listener. A zero time value disables the deadline.
func (s *UDPSession) SetDeadline(t time.Time) error {
s.mu.Lock()
defer s.mu.Unlock()
s.rd = t
s.wd = t
s.mu.Unlock()
s.notifyReadEvent()
s.notifyWriteEvent()
return nil
@@ -386,8 +396,8 @@ func (s *UDPSession) SetDeadline(t time.Time) error {
// SetReadDeadline implements the Conn SetReadDeadline method.
func (s *UDPSession) SetReadDeadline(t time.Time) error {
s.mu.Lock()
defer s.mu.Unlock()
s.rd = t
s.mu.Unlock()
s.notifyReadEvent()
return nil
}
@@ -395,8 +405,8 @@ func (s *UDPSession) SetReadDeadline(t time.Time) error {
// SetWriteDeadline implements the Conn SetWriteDeadline method.
func (s *UDPSession) SetWriteDeadline(t time.Time) error {
s.mu.Lock()
defer s.mu.Unlock()
s.wd = t
s.mu.Unlock()
s.notifyWriteEvent()
return nil
}