1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Small refactor of downloadState

Renames the struct to pieceProgress and makes readMessage/readMessages
methods of the struct instead of arguments, to make it a little bit
clearer that they modify the struct in-place.
This commit is contained in:
Jesse Li
2020-01-02 10:47:00 -05:00
parent d4f6d1265d
commit 22fc59dae3

View File

@@ -39,7 +39,7 @@ type pieceResult struct {
buf []byte buf []byte
} }
type downloadState struct { type pieceProgress struct {
index int index int
client *client client *client
buf []byte buf []byte
@@ -48,7 +48,7 @@ type downloadState struct {
backlog int backlog int
} }
func readMessage(state *downloadState) error { func (state *pieceProgress) readMessage() error {
msg, err := state.client.read() // this call blocks msg, err := state.client.read() // this call blocks
if err != nil { if err != nil {
return err return err
@@ -80,13 +80,13 @@ func readMessage(state *downloadState) error {
return nil return nil
} }
func readMessages(state *downloadState) error { func (state *pieceProgress) readMessages() error {
err := readMessage(state) err := state.readMessage()
if err != nil { if err != nil {
return err return err
} }
for state.client.hasNext() { for state.client.hasNext() {
err := readMessage(state) err := state.readMessage()
if err != nil { if err != nil {
return err return err
} }
@@ -95,7 +95,7 @@ func readMessages(state *downloadState) error {
} }
func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) { func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
state := downloadState{ state := pieceProgress{
index: pw.index, index: pw.index,
client: c, client: c,
buf: make([]byte, pw.length), buf: make([]byte, pw.length),
@@ -126,7 +126,7 @@ func attemptDownloadPiece(c *client, pw *pieceWork) ([]byte, error) {
} }
// Wait until we receive at least one message, and consume them // Wait until we receive at least one message, and consume them
err := readMessages(&state) err := state.readMessages()
if err != nil { if err != nil {
return nil, err return nil, err
} }