aboutsummaryrefslogtreecommitdiff
path: root/btclient.go
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2017-06-18 05:53:42 +0000
committerFelix Hanley <felix@userspace.com.au>2017-06-18 05:53:42 +0000
commit30fc93f11537789824eeb24bde212a57e43ecb01 (patch)
treee83e685519c704adb97e34ea1bdef9ab6b8d484f /btclient.go
parent3e3fa4064fcaf43cd87ddbfc457097bacb697fc5 (diff)
downloaddhtsearch-30fc93f11537789824eeb24bde212a57e43ecb01.tar.gz
dhtsearch-30fc93f11537789824eeb24bde212a57e43ecb01.tar.bz2
Add user configuration and more exported variables
Diffstat (limited to 'btclient.go')
-rw-r--r--btclient.go13
1 files changed, 7 insertions, 6 deletions
diff --git a/btclient.go b/btclient.go
index 35e8451..f3d32eb 100644
--- a/btclient.go
+++ b/btclient.go
@@ -48,7 +48,6 @@ type peer struct {
}
type btClient struct {
- debug bool
peersIn <-chan peer
torrentsOut chan<- Torrent
workerTokens chan struct{}
@@ -58,7 +57,7 @@ func newBTClient(r <-chan peer, t chan<- Torrent) *btClient {
return &btClient{
peersIn: r,
torrentsOut: t,
- workerTokens: make(chan struct{}, 256),
+ workerTokens: make(chan struct{}, Config.Advanced.MaxBtWorkers),
}
}
@@ -71,9 +70,11 @@ func (bt *btClient) run(done <-chan struct{}) error {
return
case p = <-bt.peersIn:
bt.workerTokens <- struct{}{}
+ btWorkers.Add(1)
go func(p peer) {
defer func() {
+ btWorkers.Add(-1)
<-bt.workerTokens
}()
@@ -81,7 +82,7 @@ func (bt *btClient) run(done <-chan struct{}) error {
return
}
- if bt.debug {
+ if Config.Debug {
fmt.Printf("Fetching metadata for %x\n", p.id)
}
bt.fetchMetadata(p)
@@ -104,7 +105,7 @@ func (bt *btClient) isDone(pieces [][]byte) bool {
// read reads size-length bytes from conn to data.
func read(conn *net.TCPConn, size int, data *bytes.Buffer) error {
- conn.SetReadDeadline(time.Now().Add(time.Second * 15))
+ conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(Config.Advanced.TcpTimeout)))
n, err := io.CopyN(data, conn, int64(size))
if err != nil || n != int64(size) {
@@ -142,7 +143,7 @@ func sendMessage(conn *net.TCPConn, data []byte) error {
buffer := bytes.NewBuffer(nil)
binary.Write(buffer, binary.BigEndian, length)
- conn.SetWriteDeadline(time.Now().Add(time.Second * 10))
+ conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(Config.Advanced.TcpTimeout)))
b, err := conn.Write(append(buffer.Bytes(), data...))
btBytesOut.Add(int64(b))
return err
@@ -155,7 +156,7 @@ func sendHandshake(conn *net.TCPConn, infoHash, peerID []byte) error {
copy(data[28:48], infoHash)
copy(data[48:], peerID)
- conn.SetWriteDeadline(time.Now().Add(time.Second * 10))
+ conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(Config.Advanced.TcpTimeout)))
b, err := conn.Write(data)
btBytesOut.Add(int64(b))
return err