Skip to content

Commit

Permalink
less latency; close shadowsocks#93
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Apr 18, 2014
1 parent 0c789ea commit 7ec1cb1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
46 changes: 19 additions & 27 deletions lib/shadowsocks/local.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion lib/shadowsocks/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 17 additions & 23 deletions src/local.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres
stage = 0
headerLength = 0
remote = null
cachedPieces = []
addrLen = 0
remoteAddr = null
remotePort = null
Expand All @@ -92,7 +91,7 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres
if stage is 5
# pipe sockets
data = encryptor.encrypt data
connection.pause() unless remote.write(data)
connection.pause() unless remote.write(data)
return
if stage is 0
tempBuf = new Buffer(2)
Expand Down Expand Up @@ -165,20 +164,6 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres
[aServer, aPort] = getServer()
utils.info "connecting #{aServer}:#{aPort}"
remote = net.connect(aPort, aServer, ->
if not encryptor
remote.destroy() if remote
return
addrToSendBuf = new Buffer(addrToSend, "binary")
addrToSendBuf = encryptor.encrypt addrToSendBuf
remote.write addrToSendBuf
i = 0

while i < cachedPieces.length
piece = cachedPieces[i]
piece = encryptor.encrypt piece
remote.write piece
i++
cachedPieces = null # save memory
stage = 5
utils.debug "stage = 5"
)
Expand Down Expand Up @@ -218,23 +203,32 @@ createServer = (serverAddr, serverPort, port, key, method, timeout, local_addres
utils.debug "remote on timeout"
remote.destroy() if remote
connection.destroy() if connection


addrToSendBuf = new Buffer(addrToSend, "binary")
addrToSendBuf = encryptor.encrypt addrToSendBuf
# remote.setNoDelay false
remote.write addrToSendBuf

if data.length > headerLength
buf = new Buffer(data.length - headerLength)
data.copy buf, 0, headerLength
cachedPieces.push buf
buf = null
piece = encryptor.encrypt buf
remote.write piece
stage = 4
utils.debug "stage = 4"
catch e
# may encounter index out of range
utils.error e
connection.destroy() if connection
remote.destroy() if remote
else cachedPieces.push data if stage is 4
# remote server not connected
# cache received buffers
# make sure no data is lost
clean()
else if stage is 4
if not remote?
connection.destroy() if connection
return
data = encryptor.encrypt data
# remote.setNoDelay true
connection.pause() unless remote.write(data)

connection.on "end", ->
utils.debug "connection on end"
Expand Down
5 changes: 4 additions & 1 deletion src/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ exports.config = (level) ->

exports.log = (level, msg)->
if level >= _logging_level
util.log msg
if level >= exports.DEBUG
util.log(new Date().getMilliseconds() + 'ms ' + msg)
else
util.log msg

exports.debug = (msg)->
exports.log exports.DEBUG, msg
Expand Down
6 changes: 6 additions & 0 deletions test/benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

for i in {1..10} ; do
echo $i
curl --socks5-hostname 127.0.0.1:$1 http://www.google.com/ >/dev/null
done

0 comments on commit 7ec1cb1

Please sign in to comment.