Skip to content

Commit

Permalink
Set request buffer size to 260.
Browse files Browse the repository at this point in the history
Testing shows that usually the first read will get just the request,
no more extra data.
  • Loading branch information
cyfdecyf committed Dec 14, 2012
1 parent 35e1e06 commit 2cb7cde
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func getRequest(conn *ss.Conn) (host string, extra []byte, err error) {
// buf size should at least have the same size with the largest possible
// request size (when addrType is 3, domain name has at most 256 bytes)
// 1(addrType) + 1(lenByte) + 256(max length address) + 2(port)
buf := make([]byte, 4096, 4096) // use 4096 to read more if possible
cur := 0 // current location in buf
buf := make([]byte, 260, 260)
cur := 0 // current location in buf

// first read the complete request, may read extra bytes
var n int
for {
// hopefully, we should only need one read to get the complete request
// this read normally will read just the request, no extra data
if n, err = conn.Read(buf[cur:]); err != nil {
// debug.Println("read request error:", err)
return
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Config struct {
func ParseConfig(path string) *Config {
file, err := os.Open(path) // For read access.
if err != nil {
log.Fatalf("error opening config file %s: %v\n", file, err)
log.Fatal("error opening config file:", err)
}
defer file.Close()
data, err := ioutil.ReadAll(file)
Expand Down
12 changes: 6 additions & 6 deletions shadowsocks/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ func Pipe(src, dst net.Conn, end chan byte) {
// introducing unnecessary overhead.
buf := make([]byte, 4096)
for {
num, err := src.Read(buf)
// read may return EOF with num > 0
// should always process num > 0 bytes before handling error
if num > 0 {
if _, err = dst.Write(buf[0:num]); err != nil {
n, err := src.Read(buf)
// read may return EOF with n > 0
// should always process n > 0 bytes before handling error
if n > 0 {
if _, err = dst.Write(buf[0:n]); err != nil {
Debug.Println("write:", err)
break
}
}
if num == 0 { // num == 0 should associate with EOF
if n == 0 { // n == 0 should associate with EOF
break
}
if err != nil {
Expand Down

0 comments on commit 2cb7cde

Please sign in to comment.