1. 把tunnelid函数放到其他地方

2.在创建tunnel的时候显示每个tunnel属于哪个model
3.增加了queuecache的测试
This commit is contained in:
dmy@lab
2016-01-11 00:00:30 +08:00
parent 421c18687e
commit b284b401dc
6 changed files with 90 additions and 48 deletions

View File

@@ -37,9 +37,8 @@ type Cipher interface {
}
type Carrier struct {
Conn net.Conn
Cipher Cipher
// Cache []byte
Conn net.Conn
Cipher Cipher
Cache *cache.UnblockingQueueCache
Msg *cache.BlockingQueueCache
AttachedTunnelID string
@@ -138,13 +137,17 @@ func UnwrapPackage(pacakge []byte) (data []byte, rest []byte, err error) {
func SendData(carrier *Carrier) (n int, err error) {
msg, nByte := carrier.Msg.Get()
id := carrier.AttachedTunnelID
log.Info("id %s Get Msg", id)
if len(msg) < nByte {
log.Panic("Cache of send is too small")
}
if carrier.Cipher == nil {
n, err = carrier.Conn.Write(msg[:nByte])
carrier.Cache.Put(make([]byte, 1024*4), 1024*4)
return
}
log.Info("id %s AAAAAAAaaa", id)
encrypedByte, err := carrier.Cipher.Encrypt(msg[:nByte])
if err != nil {
n = 0
@@ -154,16 +157,20 @@ func SendData(carrier *Carrier) (n int, err error) {
wraped := WrapPackage(encrypedByte[:nByte])
n, err = carrier.Conn.Write(wraped)
carrier.Cache.Put(make([]byte, 1024*4), 1024*4)
log.Info("id %s give back cache", id)
return
}
func RowReceiveData(carrier *Carrier) (n int, err error) {
cache, _ := carrier.Cache.Get()
log.Info("id %s get Cache", carrier.AttachedTunnelID)
n, err = carrier.Conn.Read(cache)
if err != nil {
n = 0
}
carrier.Msg.Put(cache, n)
id := carrier.AttachedTunnelID
log.Info("id %s put Msg", id)
return
}
@@ -178,6 +185,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
var packageData []byte
var _rest []byte
cache, _ := carrier.Cache.Get()
log.Info("id %s get Cache", carrier.AttachedTunnelID)
for {
//首先检查这个是不是完整的包,是就返回好了,免得被阻塞
data, rest, err := UnwrapPackage(wrapedPackage)
@@ -218,5 +226,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
}
n = len(decrypted)
carrier.Msg.Put(decrypted, n)
id := carrier.AttachedTunnelID
log.Info("id %s put Msg", id)
return
}