Skip to content

Commit 158a924

Browse files
committed
fan in WriteBuffers
1 parent 7c104a5 commit 158a924

File tree

1 file changed

+1
-62
lines changed

1 file changed

+1
-62
lines changed

sess.go

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -241,68 +241,7 @@ func (s *UDPSession) Read(b []byte) (n int, err error) {
241241
}
242242

243243
// Write implements net.Conn
244-
func (s *UDPSession) Write(b []byte) (n int, err error) {
245-
for {
246-
s.mu.Lock()
247-
if s.isClosed {
248-
s.mu.Unlock()
249-
return 0, errors.New(errBrokenPipe)
250-
}
251-
252-
// controls how much data will be sent to kcp core
253-
// to prevent the memory from exhuasting
254-
if s.kcp.WaitSnd() < int(s.kcp.snd_wnd) {
255-
n = len(b)
256-
for {
257-
if len(b) <= int(s.kcp.mss) {
258-
s.kcp.Send(b)
259-
break
260-
} else {
261-
s.kcp.Send(b[:s.kcp.mss])
262-
b = b[s.kcp.mss:]
263-
}
264-
}
265-
266-
// flush immediately if the queue is full
267-
if s.kcp.WaitSnd() >= int(s.kcp.snd_wnd) || !s.writeDelay {
268-
s.kcp.flush(false)
269-
}
270-
s.mu.Unlock()
271-
atomic.AddUint64(&DefaultSnmp.BytesSent, uint64(n))
272-
return n, nil
273-
}
274-
275-
// deadline for current writing operation
276-
var timeout *time.Timer
277-
var c <-chan time.Time
278-
if !s.wd.IsZero() {
279-
if time.Now().After(s.wd) {
280-
s.mu.Unlock()
281-
return 0, errTimeout{}
282-
}
283-
delay := s.wd.Sub(time.Now())
284-
timeout = time.NewTimer(delay)
285-
c = timeout.C
286-
}
287-
s.mu.Unlock()
288-
289-
// wait for write event or timeout
290-
select {
291-
case <-s.chWriteEvent:
292-
case <-c:
293-
case <-s.die:
294-
case err = <-s.chWriteError:
295-
if timeout != nil {
296-
timeout.Stop()
297-
}
298-
return n, err
299-
}
300-
301-
if timeout != nil {
302-
timeout.Stop()
303-
}
304-
}
305-
}
244+
func (s *UDPSession) Write(b []byte) (n int, err error) { return s.WriteBuffers([][]byte{b}) }
306245

307246
// WriteBuffers write a vector of byte slices to the underlying connection
308247
func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) {

0 commit comments

Comments
 (0)