From 9a18b5323da02f0113d35f94d8708a1816cc0d62 Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Sun, 17 Jan 2016 22:34:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E8=AF=BBjson=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json | 4 ++++ init.go | 16 ++++++++++++++++ main_test.go | 1 - model/tunnel.go | 28 +++++++--------------------- 4 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 config.json create mode 100644 init.go 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 {