diff --git a/config.json b/config.json new file mode 100644 index 0000000..38255b9 --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "cacheSize":4096, + "queueLength":10 + } \ No newline at end of file diff --git a/init.go b/init.go new file mode 100644 index 0000000..d82b21c --- /dev/null +++ b/init.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "github.com/spf13/viper" +) + +func init() { + viper.SetConfigType("json") + viper.SetConfigName("config") + viper.AddConfigPath(".") + err := viper.ReadInConfig() + if err != nil { + fmt.Println("faield to parse config file %s", err.Error()) + } +} diff --git a/main_test.go b/main_test.go index f28bdf1..3e2f04c 100644 --- a/main_test.go +++ b/main_test.go @@ -2,7 +2,6 @@ package main import ( "bufio" - // "fmt" "fmt" "github.com/TransX/log" "github.com/TransX/protocol" diff --git a/model/tunnel.go b/model/tunnel.go index 49a11dc..d59f8ac 100644 --- a/model/tunnel.go +++ b/model/tunnel.go @@ -7,6 +7,7 @@ import ( "github.com/TransX/constant" "github.com/TransX/log" "github.com/TransX/tscipher" + "github.com/spf13/viper" "net" "time" ) @@ -53,15 +54,15 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest this.regChan <- this src := this.src dest := this.dest - // cipherDirection := this.cipherDirection id := this.id - // cache := make([]byte, 1024*4) //4kB //构建Carrier - queCache := cache.NewBlockingQueueCache(1) - for i := 0; i < 1; i++ { - queCache.Put(make([]byte, 1024*4), 0) + queLength := viper.GetInt("queueLength") + queCache := cache.NewBlockingQueueCache(queLength) + cacheSize := viper.GetInt("cacheSize") + for i := 0; i < queLength; i++ { + queCache.Put(make([]byte, cacheSize), 0) } - msg := cache.NewBlockingQueueCache(1) + msg := cache.NewBlockingQueueCache(queLength) revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), queCache, msg, id) sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), queCache, msg, id) go this.receive(revCarrier) @@ -92,21 +93,6 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) { cipherDirection := this.cipherDirection id := this.id defer this.onError() - // defer func() { - // //注销 - // if !this.unregistered { // 应该不存在异步问题 - // this.unregChan <- this - // this.unregistered = true - // } - // if r := recover(); r != nil { - // if src != nil { - // src.Close() - // } - // if dest != nil { - // dest.Close() - // } - // } - // }() var n int var err error for {