PeerConn.post becomes PeerConn.write
This commit is contained in:
parent
56e2a8a3a6
commit
9c9ba1aeac
12
client.go
12
client.go
|
@ -975,7 +975,7 @@ const localClientReqq = 1 << 5
|
||||||
// See the order given in Transmission's tr_peerMsgsNew.
|
// See the order given in Transmission's tr_peerMsgsNew.
|
||||||
func (cl *Client) sendInitialMessages(conn *PeerConn, torrent *Torrent) {
|
func (cl *Client) sendInitialMessages(conn *PeerConn, torrent *Torrent) {
|
||||||
if conn.PeerExtensionBytes.SupportsExtended() && cl.config.Extensions.SupportsExtended() {
|
if conn.PeerExtensionBytes.SupportsExtended() && cl.config.Extensions.SupportsExtended() {
|
||||||
conn.post(pp.Message{
|
conn.write(pp.Message{
|
||||||
Type: pp.Extended,
|
Type: pp.Extended,
|
||||||
ExtendedID: pp.HandshakeExtendedID,
|
ExtendedID: pp.HandshakeExtendedID,
|
||||||
ExtendedPayload: func() []byte {
|
ExtendedPayload: func() []byte {
|
||||||
|
@ -1004,11 +1004,11 @@ func (cl *Client) sendInitialMessages(conn *PeerConn, torrent *Torrent) {
|
||||||
func() {
|
func() {
|
||||||
if conn.fastEnabled() {
|
if conn.fastEnabled() {
|
||||||
if torrent.haveAllPieces() {
|
if torrent.haveAllPieces() {
|
||||||
conn.post(pp.Message{Type: pp.HaveAll})
|
conn.write(pp.Message{Type: pp.HaveAll})
|
||||||
conn.sentHaves.AddRange(0, bitmap.BitIndex(conn.t.NumPieces()))
|
conn.sentHaves.AddRange(0, bitmap.BitIndex(conn.t.NumPieces()))
|
||||||
return
|
return
|
||||||
} else if !torrent.haveAnyPieces() {
|
} else if !torrent.haveAnyPieces() {
|
||||||
conn.post(pp.Message{Type: pp.HaveNone})
|
conn.write(pp.Message{Type: pp.HaveNone})
|
||||||
conn.sentHaves.Clear()
|
conn.sentHaves.Clear()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1016,7 +1016,7 @@ func (cl *Client) sendInitialMessages(conn *PeerConn, torrent *Torrent) {
|
||||||
conn.postBitfield()
|
conn.postBitfield()
|
||||||
}()
|
}()
|
||||||
if conn.PeerExtensionBytes.SupportsDHT() && cl.config.Extensions.SupportsDHT() && cl.haveDhtServer() {
|
if conn.PeerExtensionBytes.SupportsDHT() && cl.config.Extensions.SupportsDHT() && cl.haveDhtServer() {
|
||||||
conn.post(pp.Message{
|
conn.write(pp.Message{
|
||||||
Type: pp.Port,
|
Type: pp.Port,
|
||||||
Port: cl.dhtPort(),
|
Port: cl.dhtPort(),
|
||||||
})
|
})
|
||||||
|
@ -1074,12 +1074,12 @@ func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *PeerCon
|
||||||
return err
|
return err
|
||||||
case pp.RequestMetadataExtensionMsgType:
|
case pp.RequestMetadataExtensionMsgType:
|
||||||
if !t.haveMetadataPiece(piece) {
|
if !t.haveMetadataPiece(piece) {
|
||||||
c.post(t.newMetadataExtensionMessage(c, pp.RejectMetadataExtensionMsgType, d["piece"], nil))
|
c.write(t.newMetadataExtensionMessage(c, pp.RejectMetadataExtensionMsgType, d["piece"], nil))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
start := (1 << 14) * piece
|
start := (1 << 14) * piece
|
||||||
c.logger.WithDefaultLevel(log.Debug).Printf("sending metadata piece %d", piece)
|
c.logger.WithDefaultLevel(log.Debug).Printf("sending metadata piece %d", piece)
|
||||||
c.post(t.newMetadataExtensionMessage(c, pp.DataMetadataExtensionMsgType, piece, t.metadataBytes[start:start+t.metadataPieceSize(piece)]))
|
c.write(t.newMetadataExtensionMessage(c, pp.DataMetadataExtensionMsgType, piece, t.metadataBytes[start:start+t.metadataPieceSize(piece)]))
|
||||||
return nil
|
return nil
|
||||||
case pp.RejectMetadataExtensionMsgType:
|
case pp.RejectMetadataExtensionMsgType:
|
||||||
return nil
|
return nil
|
||||||
|
|
30
peerconn.go
30
peerconn.go
|
@ -404,27 +404,17 @@ func (cn *Peer) peerHasPiece(piece pieceIndex) bool {
|
||||||
// https://github.com/pion/datachannel/issues/59 is fixed.
|
// https://github.com/pion/datachannel/issues/59 is fixed.
|
||||||
const writeBufferHighWaterLen = 1 << 15
|
const writeBufferHighWaterLen = 1 << 15
|
||||||
|
|
||||||
// Writes a message into the write buffer. Returns whether it's okay to keep writing. Posting is
|
// Writes a message into the write buffer. Returns whether it's okay to keep writing. Writing is
|
||||||
// done asynchronously, so it may be that we're not able to honour backpressure from this method. It
|
// done asynchronously, so it may be that we're not able to honour backpressure from this method.
|
||||||
// might be possible to merge this with PeerConn.write down the track? They seem to be very similar.
|
func (cn *PeerConn) write(msg pp.Message) bool {
|
||||||
func (cn *PeerConn) post(msg pp.Message) bool {
|
torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
|
||||||
torrent.Add(fmt.Sprintf("messages posted of type %s", msg.Type.String()), 1)
|
|
||||||
// We don't need to track bytes here because a connection.w Writer wrapper takes care of that
|
// We don't need to track bytes here because a connection.w Writer wrapper takes care of that
|
||||||
// (although there's some delay between us recording the message, and the connection writer
|
// (although there's some delay between us recording the message, and the connection writer
|
||||||
// flushing it out.).
|
// flushing it out.).
|
||||||
cn.writeBuffer.Write(msg.MustMarshalBinary())
|
cn.writeBuffer.Write(msg.MustMarshalBinary())
|
||||||
// Last I checked only Piece messages affect stats, and we don't post those.
|
// Last I checked only Piece messages affect stats, and we don't write those.
|
||||||
cn.wroteMsg(&msg)
|
cn.wroteMsg(&msg)
|
||||||
cn.tickleWriter()
|
cn.tickleWriter()
|
||||||
return cn.writeBuffer.Len() < writeBufferHighWaterLen
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true if there's room to write more.
|
|
||||||
func (cn *PeerConn) write(msg pp.Message) bool {
|
|
||||||
cn.wroteMsg(&msg)
|
|
||||||
cn.writeBuffer.Write(msg.MustMarshalBinary())
|
|
||||||
torrent.Add(fmt.Sprintf("messages filled of type %s", msg.Type.String()), 1)
|
|
||||||
cn.tickleWriter()
|
|
||||||
return !cn.writeBufferFull()
|
return !cn.writeBufferFull()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,7 +431,7 @@ func (cn *PeerConn) requestMetadataPiece(index int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cn.logger.WithDefaultLevel(log.Debug).Printf("requesting metadata piece %d", index)
|
cn.logger.WithDefaultLevel(log.Debug).Printf("requesting metadata piece %d", index)
|
||||||
cn.post(pp.Message{
|
cn.write(pp.Message{
|
||||||
Type: pp.Extended,
|
Type: pp.Extended,
|
||||||
ExtendedID: eID,
|
ExtendedID: eID,
|
||||||
ExtendedPayload: func() []byte {
|
ExtendedPayload: func() []byte {
|
||||||
|
@ -689,7 +679,7 @@ func (cn *PeerConn) have(piece pieceIndex) {
|
||||||
if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
|
if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cn.post(pp.Message{
|
cn.write(pp.Message{
|
||||||
Type: pp.Have,
|
Type: pp.Have,
|
||||||
Index: pp.Integer(piece),
|
Index: pp.Integer(piece),
|
||||||
})
|
})
|
||||||
|
@ -703,7 +693,7 @@ func (cn *PeerConn) postBitfield() {
|
||||||
if !cn.t.haveAnyPieces() {
|
if !cn.t.haveAnyPieces() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cn.post(pp.Message{
|
cn.write(pp.Message{
|
||||||
Type: pp.Bitfield,
|
Type: pp.Bitfield,
|
||||||
Bitfield: cn.t.bitfield(),
|
Bitfield: cn.t.bitfield(),
|
||||||
})
|
})
|
||||||
|
@ -946,7 +936,7 @@ func (c *PeerConn) reject(r Request) {
|
||||||
if !c.fastEnabled() {
|
if !c.fastEnabled() {
|
||||||
panic("fast not enabled")
|
panic("fast not enabled")
|
||||||
}
|
}
|
||||||
c.post(r.ToMsg(pp.Reject))
|
c.write(r.ToMsg(pp.Reject))
|
||||||
delete(c.peerRequests, r)
|
delete(c.peerRequests, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1028,7 +1018,7 @@ func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
|
||||||
if c.choking {
|
if c.choking {
|
||||||
c.logger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
|
c.logger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
|
||||||
}
|
}
|
||||||
c.choke(c.post)
|
c.choke(c.write)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readPeerRequestData(r Request, c *PeerConn) ([]byte, error) {
|
func readPeerRequestData(r Request, c *PeerConn) ([]byte, error) {
|
||||||
|
|
Loading…
Reference in New Issue