mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
Merge pull request #702 from schollz:schollz/issue599
chore: improve efficiency and remove extraneous reads
This commit is contained in:
commit
f6633cbac9
1 changed files with 52 additions and 48 deletions
100
src/croc/croc.go
100
src/croc/croc.go
|
@ -1982,62 +1982,66 @@ func (c *Client) sendData(i int) {
|
||||||
curi := float64(0)
|
curi := float64(0)
|
||||||
for {
|
for {
|
||||||
// Read file
|
// Read file
|
||||||
data := make([]byte, models.TCP_BUFFER_SIZE/2)
|
var n int
|
||||||
// log.Debugf("%d trying to read", i)
|
var errRead error
|
||||||
n, errRead := c.fread.ReadAt(data, readingPos)
|
|
||||||
// log.Debugf("%d read %d bytes", i, n)
|
|
||||||
readingPos += int64(n)
|
|
||||||
if c.limiter != nil {
|
|
||||||
r := c.limiter.ReserveN(time.Now(), n)
|
|
||||||
log.Debugf("Limiting Upload for %d", r.Delay())
|
|
||||||
time.Sleep(r.Delay())
|
|
||||||
}
|
|
||||||
|
|
||||||
if math.Mod(curi, float64(len(c.Options.RelayPorts))) == float64(i) {
|
if math.Mod(curi, float64(len(c.Options.RelayPorts))) == float64(i) {
|
||||||
// check to see if this is a chunk that the recipient wants
|
data := make([]byte, models.TCP_BUFFER_SIZE/2)
|
||||||
usableChunk := true
|
n, errRead = c.fread.ReadAt(data, readingPos)
|
||||||
c.mutex.Lock()
|
if c.limiter != nil {
|
||||||
if len(c.chunkMap) != 0 {
|
r := c.limiter.ReserveN(time.Now(), n)
|
||||||
if _, ok := c.chunkMap[pos]; !ok {
|
log.Debugf("Limiting Upload for %d", r.Delay())
|
||||||
usableChunk = false
|
time.Sleep(r.Delay())
|
||||||
} else {
|
|
||||||
delete(c.chunkMap, pos)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
c.mutex.Unlock()
|
if n > 0 {
|
||||||
if usableChunk {
|
// check to see if this is a chunk that the recipient wants
|
||||||
// log.Debugf("sending chunk %d", pos)
|
usableChunk := true
|
||||||
posByte := make([]byte, 8)
|
c.mutex.Lock()
|
||||||
binary.LittleEndian.PutUint64(posByte, pos)
|
if len(c.chunkMap) != 0 {
|
||||||
var err error
|
if _, ok := c.chunkMap[pos]; !ok {
|
||||||
var dataToSend []byte
|
usableChunk = false
|
||||||
if c.Options.NoCompress {
|
} else {
|
||||||
dataToSend, err = crypt.Encrypt(
|
delete(c.chunkMap, pos)
|
||||||
append(posByte, data[:n]...),
|
}
|
||||||
c.Key,
|
}
|
||||||
)
|
c.mutex.Unlock()
|
||||||
} else {
|
if usableChunk {
|
||||||
dataToSend, err = crypt.Encrypt(
|
// log.Debugf("sending chunk %d", pos)
|
||||||
compress.Compress(
|
posByte := make([]byte, 8)
|
||||||
|
binary.LittleEndian.PutUint64(posByte, pos)
|
||||||
|
var err error
|
||||||
|
var dataToSend []byte
|
||||||
|
if c.Options.NoCompress {
|
||||||
|
dataToSend, err = crypt.Encrypt(
|
||||||
append(posByte, data[:n]...),
|
append(posByte, data[:n]...),
|
||||||
),
|
c.Key,
|
||||||
c.Key,
|
)
|
||||||
)
|
} else {
|
||||||
}
|
dataToSend, err = crypt.Encrypt(
|
||||||
if err != nil {
|
compress.Compress(
|
||||||
panic(err)
|
append(posByte, data[:n]...),
|
||||||
}
|
),
|
||||||
|
c.Key,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
err = c.conn[i+1].Send(dataToSend)
|
err = c.conn[i+1].Send(dataToSend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
}
|
||||||
|
c.bar.Add(n)
|
||||||
|
c.TotalSent += int64(n)
|
||||||
|
// time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
c.bar.Add(n)
|
|
||||||
c.TotalSent += int64(n)
|
|
||||||
// time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
n = models.TCP_BUFFER_SIZE / 2
|
||||||
|
}
|
||||||
|
readingPos += int64(n)
|
||||||
curi++
|
curi++
|
||||||
pos += uint64(n)
|
pos += uint64(n)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue