引入读json配置文件

This commit is contained in:
dmy@lab 2016-01-17 22:34:26 +08:00
parent 97edfb5fff
commit 9a18b5323d
4 changed files with 27 additions and 22 deletions

4
config.json Normal file
View File

@ -0,0 +1,4 @@
{
"cacheSize":4096,
"queueLength":10
}

16
init.go Normal file
View File

@ -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())
}
}

View File

@ -2,7 +2,6 @@ package main
import ( import (
"bufio" "bufio"
// "fmt"
"fmt" "fmt"
"github.com/TransX/log" "github.com/TransX/log"
"github.com/TransX/protocol" "github.com/TransX/protocol"

View File

@ -7,6 +7,7 @@ import (
"github.com/TransX/constant" "github.com/TransX/constant"
"github.com/TransX/log" "github.com/TransX/log"
"github.com/TransX/tscipher" "github.com/TransX/tscipher"
"github.com/spf13/viper"
"net" "net"
"time" "time"
) )
@ -53,15 +54,15 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
this.regChan <- this this.regChan <- this
src := this.src src := this.src
dest := this.dest dest := this.dest
// cipherDirection := this.cipherDirection
id := this.id id := this.id
// cache := make([]byte, 1024*4) //4kB
//构建Carrier //构建Carrier
queCache := cache.NewBlockingQueueCache(1) queLength := viper.GetInt("queueLength")
for i := 0; i < 1; i++ { queCache := cache.NewBlockingQueueCache(queLength)
queCache.Put(make([]byte, 1024*4), 0) 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) revCarrier := tscipher.NewCarrier(src, tscipher.NewCipher("XOR"), queCache, msg, id)
sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), queCache, msg, id) sendCarrier := tscipher.NewCarrier(dest, tscipher.NewCipher("XOR"), queCache, msg, id)
go this.receive(revCarrier) go this.receive(revCarrier)
@ -92,21 +93,6 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
cipherDirection := this.cipherDirection cipherDirection := this.cipherDirection
id := this.id id := this.id
defer this.onError() 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 n int
var err error var err error
for { for {