-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package p2p | ||
|
||
import ( | ||
"magnetflow/client" | ||
"magnetflow/message" | ||
"magnetflow/peers" | ||
) | ||
|
||
const MaxBloackSize = 1 << 14 | ||
|
||
const MaxBackLog = 5 | ||
|
||
type Torrent struct { | ||
Peers []peers.Peer | ||
PeerID [20]byte | ||
InfoHash [20]byte | ||
PieceHashes [][20]byte | ||
PieceLength int | ||
Length int | ||
Name string | ||
} | ||
|
||
type pieceWork struct { | ||
index int | ||
hash [20]byte | ||
length int | ||
} | ||
|
||
type pieceResult struct { | ||
index int | ||
buf []byte | ||
} | ||
|
||
type pieceProgress struct { | ||
index int | ||
client *client.Client | ||
buf []byte | ||
downloaded int | ||
requested int | ||
backlog int | ||
} | ||
|
||
func (state *pieceProgress) readMessage() error { | ||
msg, err := state.client.Read() | ||
if err != nil { | ||
return err | ||
} | ||
if msg == nil { | ||
return nil | ||
} | ||
switch msg.ID { | ||
case message.MsgUnchoke: | ||
state.client.Choked = false | ||
case message.MsgChoke: | ||
state.client.Choked = true | ||
case message.MsgHave: | ||
index, err := message.ParseHave(msg) | ||
if err != nil { | ||
return err | ||
} | ||
state.client.Bitfield.SetPiece(index) | ||
case message.MsgPiece: | ||
n, err := message.ParsePiece(state.index, state.buf, msg) | ||
if err != nil { | ||
return err | ||
} | ||
state.downloaded += n | ||
state.backlog-- | ||
} | ||
return nil | ||
} |