2021-05-20 10:28:23 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2021-05-24 07:36:39 +00:00
|
|
|
"github.com/anacrolix/chansync"
|
2021-05-20 10:28:23 +00:00
|
|
|
"github.com/anacrolix/log"
|
|
|
|
"github.com/anacrolix/sync"
|
|
|
|
|
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
2022-06-25 13:16:58 +00:00
|
|
|
func (pc *PeerConn) initMessageWriter() {
|
2021-05-20 10:28:23 +00:00
|
|
|
w := &pc.messageWriter
|
|
|
|
*w = peerConnMsgWriter{
|
|
|
|
fillWriteBuffer: func() {
|
|
|
|
pc.locker().Lock()
|
|
|
|
defer pc.locker().Unlock()
|
|
|
|
if pc.closed.IsSet() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pc.fillWriteBuffer()
|
|
|
|
},
|
|
|
|
closed: &pc.closed,
|
|
|
|
logger: pc.logger,
|
|
|
|
w: pc.w,
|
|
|
|
keepAlive: func() bool {
|
2022-07-13 10:04:03 +00:00
|
|
|
pc.locker().RLock()
|
|
|
|
defer pc.locker().RUnlock()
|
2021-05-20 10:28:23 +00:00
|
|
|
return pc.useful()
|
|
|
|
},
|
|
|
|
writeBuffer: new(bytes.Buffer),
|
|
|
|
}
|
2022-06-25 13:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *PeerConn) startMessageWriter() {
|
|
|
|
pc.initMessageWriter()
|
|
|
|
go pc.messageWriterRunner()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *PeerConn) messageWriterRunner() {
|
|
|
|
defer pc.locker().Unlock()
|
|
|
|
defer pc.close()
|
|
|
|
defer pc.locker().Lock()
|
|
|
|
pc.messageWriter.run(pc.t.cl.config.KeepAliveTimeout)
|
2021-05-20 10:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type peerConnMsgWriter struct {
|
|
|
|
// Must not be called with the local mutex held, as it will call back into the write method.
|
|
|
|
fillWriteBuffer func()
|
|
|
|
closed *chansync.SetOnce
|
|
|
|
logger log.Logger
|
|
|
|
w io.Writer
|
|
|
|
keepAlive func() bool
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
writeCond chansync.BroadcastCond
|
|
|
|
// Pointer so we can swap with the "front buffer".
|
|
|
|
writeBuffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Routine that writes to the peer. Some of what to write is buffered by
|
|
|
|
// activity elsewhere in the Client, and some is determined locally when the
|
|
|
|
// connection is writable.
|
|
|
|
func (cn *peerConnMsgWriter) run(keepAliveTimeout time.Duration) {
|
2021-08-18 06:51:30 +00:00
|
|
|
lastWrite := time.Now()
|
2021-08-22 10:05:53 +00:00
|
|
|
keepAliveTimer := time.NewTimer(keepAliveTimeout)
|
2021-05-20 10:28:23 +00:00
|
|
|
frontBuf := new(bytes.Buffer)
|
|
|
|
for {
|
|
|
|
if cn.closed.IsSet() {
|
|
|
|
return
|
|
|
|
}
|
2021-08-18 06:51:30 +00:00
|
|
|
cn.fillWriteBuffer()
|
|
|
|
keepAlive := cn.keepAlive()
|
|
|
|
cn.mu.Lock()
|
2021-08-18 06:23:04 +00:00
|
|
|
if cn.writeBuffer.Len() == 0 && time.Since(lastWrite) >= keepAliveTimeout && keepAlive {
|
2021-05-20 10:28:23 +00:00
|
|
|
cn.writeBuffer.Write(pp.Message{Keepalive: true}.MustMarshalBinary())
|
|
|
|
torrent.Add("written keepalives", 1)
|
|
|
|
}
|
|
|
|
if cn.writeBuffer.Len() == 0 {
|
|
|
|
writeCond := cn.writeCond.Signaled()
|
|
|
|
cn.mu.Unlock()
|
|
|
|
select {
|
|
|
|
case <-cn.closed.Done():
|
|
|
|
case <-writeCond:
|
2021-08-22 10:05:53 +00:00
|
|
|
case <-keepAliveTimer.C:
|
2021-05-20 10:28:23 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Flip the buffers.
|
|
|
|
frontBuf, cn.writeBuffer = cn.writeBuffer, frontBuf
|
|
|
|
cn.mu.Unlock()
|
2022-06-14 04:07:45 +00:00
|
|
|
if frontBuf.Len() == 0 {
|
|
|
|
panic("expected non-empty front buffer")
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
for frontBuf.Len() != 0 {
|
|
|
|
// Limit write size for WebRTC. See https://github.com/pion/datachannel/issues/59.
|
|
|
|
next := frontBuf.Next(1<<16 - 1)
|
|
|
|
var n int
|
|
|
|
n, err = cn.w.Write(next)
|
|
|
|
if err == nil && n != len(next) {
|
|
|
|
panic("expected full write")
|
|
|
|
}
|
2022-12-25 08:23:07 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2021-05-20 10:28:23 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
cn.logger.WithDefaultLevel(log.Debug).Printf("error writing: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-14 04:07:45 +00:00
|
|
|
lastWrite = time.Now()
|
|
|
|
keepAliveTimer.Reset(keepAliveTimeout)
|
2021-05-20 10:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *peerConnMsgWriter) write(msg pp.Message) bool {
|
|
|
|
cn.mu.Lock()
|
|
|
|
defer cn.mu.Unlock()
|
|
|
|
cn.writeBuffer.Write(msg.MustMarshalBinary())
|
|
|
|
cn.writeCond.Broadcast()
|
|
|
|
return !cn.writeBufferFull()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *peerConnMsgWriter) writeBufferFull() bool {
|
|
|
|
return cn.writeBuffer.Len() >= writeBufferHighWaterLen
|
|
|
|
}
|