1.只留下BlockingQueueCache

2.为了避免循环引用,重新整理package结构
This commit is contained in:
dmy@lab 2016-01-11 21:29:48 +08:00
parent b284b401dc
commit 8599c87f0c
7 changed files with 57 additions and 79 deletions

32
cache/queue.go vendored
View File

@ -9,33 +9,7 @@ type QueueCache struct { //FIFO
queue chan *Cache
}
type BlockingQueueCache QueueCache
type UnblockingQueueCache QueueCache
func NewUnblockingQueueCache(quN int) *UnblockingQueueCache {
t := &UnblockingQueueCache{
queue: make(chan *Cache, quN),
}
return t
}
func (this *UnblockingQueueCache) Put(bits []byte, l int) {
this.queue <- &Cache{
cache: bits,
l: l,
}
}
func (this *UnblockingQueueCache) Get() ([]byte, int) {
var t *Cache
// select {
t = <-this.queue
// default:
// return make([]byte, 1024*4), 1024 * 4
// }
return t.cache, t.l
}
type BlockingQueueCache QueueCache
func NewBlockingQueueCache(quN int) *BlockingQueueCache {
t := &BlockingQueueCache{
@ -52,6 +26,6 @@ func (this *BlockingQueueCache) Put(bits []byte, l int) {
}
func (this *BlockingQueueCache) Get() ([]byte, int) {
t := <-this.queue
t := <-this.queue
return t.cache, t.l
}
}

37
cache/queue_test.go vendored
View File

@ -1,25 +1,28 @@
package cache
import(
"testing"
import (
"fmt"
"testing"
)
func GetAndPut(name string,get *BlockingQueueCache,put *BlockingQueueCache){
for{
r,_:=get.Get()
fmt.Printf("%s Get\n",name)
put.Put(r,1)
fmt.Printf("%s Put\n",name)
func GetAndPut(name string, get *BlockingQueueCache, put *BlockingQueueCache) {
r, _ := get.Get()
fmt.Printf("%s Get\n", name)
put.Put(r, 1)
fmt.Printf("%s Put\n", name)
}
}
func TestAsynchronousCache(t *testing.T) {
a := NewBlockingQueueCache(1)
b := NewBlockingQueueCache(1)
a.Put(make([]byte, 1), 1)
go func() {
for {
GetAndPut("A", a, b)
}
}()
for i := 0; i < 1000; i++ {
GetAndPut("B", b, a)
}
func TestAsynchronousCache(t *testing.T){
a:=NewBlockingQueueCache(1)
b:=NewBlockingQueueCache(1)
a.Put(make([]byte,1),1)
go GetAndPut("A",a,b)
GetAndPut("B",b,a)
}
}

View File

@ -1,4 +1,4 @@
package protocol
package constant
type Direction int

View File

@ -1,9 +1,10 @@
package protocol
package model
import (
// "fmt"
"github.com/TransX/cache"
"github.com/TransX/constant"
"github.com/TransX/log"
"github.com/TransX/tscipher"
"net"
@ -14,12 +15,12 @@ type Tunnel struct {
id string
src net.Conn
dest net.Conn
cipherDirection Direction
cipherDirection constant.Direction
regChan chan interface{}
unregChan chan interface{}
}
func NewTunnel(id string, src, dest net.Conn, cipherDirection Direction) *Tunnel {
func NewTunnel(id string, src, dest net.Conn, cipherDirection constant.Direction) *Tunnel {
return &Tunnel{
id: id,
src: src,
@ -28,7 +29,7 @@ func NewTunnel(id string, src, dest net.Conn, cipherDirection Direction) *Tunnel
}
}
func (this *Tunnel) GetID(id string) string {
func (this *Tunnel) GetID() string {
return this.id
}
@ -54,7 +55,7 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
id := this.id
// cache := make([]byte, 1024*4) //4kB
//构建Carrier
queCache := cache.NewUnblockingQueueCache(1)
queCache := cache.NewBlockingQueueCache(1)
for i := 0; i < 1; i++ {
queCache.Put(make([]byte, 1024*4), 0)
}
@ -88,7 +89,7 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
var err error
for {
rTimer := time.Now() //receive timer
if cipherDirection != RECEIVE {
if cipherDirection != constant.RECEIVE {
revCarrier.Cipher = nil
n, err = tscipher.RowReceiveData(revCarrier)
} else {
@ -119,7 +120,7 @@ func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
}
}
}()
if cipherDirection != SEND {
if cipherDirection != constant.SEND {
sendCarrier.Cipher = nil
}
for {

View File

@ -3,7 +3,9 @@ package protocol
import (
"errors"
"fmt"
"github.com/TransX/constant"
"github.com/TransX/log"
"github.com/TransX/model"
"github.com/TransX/stat"
"github.com/TransX/utils"
"net"
@ -77,11 +79,11 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
//tunnel model : [ -->>server ---- client -->> ](this is a tunnel)
if clientOrServer == "client" {
sendID := utils.TunnelID()
ntSend := NewTunnel(sendID, listenerConn, destConn, SEND)
ntSend := model.NewTunnel(sendID, listenerConn, destConn, constant.SEND)
ntSend.SetRegChan(tunMng.GetRegChan())
ntSend.SetUnRegChan(tunMng.GetUnregChan())
receiveID := utils.TunnelID()
ntRev := NewTunnel(receiveID, destConn, listenerConn, RECEIVE)
ntRev := model.NewTunnel(receiveID, destConn, listenerConn, constant.RECEIVE)
ntRev.SetRegChan(tunMng.GetRegChan())
ntRev.SetUnRegChan(tunMng.GetUnregChan())
printModelDetail(sendID, receiveID)
@ -90,11 +92,11 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
}
if clientOrServer == "server" {
receiveID := utils.TunnelID()
ntRev := NewTunnel(receiveID, listenerConn, destConn, RECEIVE)
ntRev := model.NewTunnel(receiveID, listenerConn, destConn, constant.RECEIVE)
ntRev.SetRegChan(tunMng.GetRegChan())
ntRev.SetUnRegChan(tunMng.GetUnregChan())
sendID := utils.TunnelID()
ntSend := NewTunnel(sendID, destConn, listenerConn, SEND)
ntSend := model.NewTunnel(sendID, destConn, listenerConn, constant.SEND)
ntSend.SetRegChan(tunMng.GetRegChan())
ntSend.SetUnRegChan(tunMng.GetUnregChan())
printModelDetail(sendID, receiveID)

View File

@ -4,6 +4,7 @@ import (
"container/list"
"fmt"
"github.com/TransX/log"
"github.com/TransX/model"
"sync"
)
@ -40,12 +41,25 @@ func (this *TunnelStatusManager) unregister(t interface{}) {
this.mux.Lock()
defer this.mux.Unlock()
l := this.tunnelList
log.Debug("%d tunnels before remove.", this.tunnelList.Len())
n := 0
nl := new(list.List)
for e := l.Front(); e != nil; e = e.Next() {
if e == t {
l.Remove(e)
break
if e.Value != t {
// log.Debug("Found")
// l.Remove(e)
// break
n++
nl.PushBack(e.Value)
}
}
if n == this.tunnelList.Len() {
m := t.(*model.Tunnel)
log.Debug("check tunntel %s", m.GetID())
}
this.tunnelList = *nl
log.Debug("%d tunnels after remove.", this.tunnelList.Len())
}

View File

@ -39,13 +39,13 @@ type Cipher interface {
type Carrier struct {
Conn net.Conn
Cipher Cipher
Cache *cache.UnblockingQueueCache
Cache *cache.BlockingQueueCache
Msg *cache.BlockingQueueCache
AttachedTunnelID string
receiveBuff []byte
}
func NewCarrier(conn net.Conn, cipher Cipher, queCache *cache.UnblockingQueueCache, msg *cache.BlockingQueueCache, id string) *Carrier {
func NewCarrier(conn net.Conn, cipher Cipher, queCache *cache.BlockingQueueCache, msg *cache.BlockingQueueCache, id string) *Carrier {
t := new(Carrier)
t.Conn = conn
t.Cipher = cipher
@ -137,8 +137,6 @@ 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")
}
@ -147,7 +145,6 @@ func SendData(carrier *Carrier) (n int, err error) {
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
@ -157,35 +154,24 @@ 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
}
func ReceiveData(carrier *Carrier) (n int, err error) {
// defer func() {
// if r := recover(); r != nil {
// log.Error("ReceiveData err %s", r)
// }
// }()
// log.Debug("id %s wrapedPackage := carrier.GetReceiveBuff()", carrier.AttachedTunnelID)
wrapedPackage := carrier.GetReceiveBuff() //make([]byte, 0, cap(carrier.Cache))
var packageData []byte
var _rest []byte
cache, _ := carrier.Cache.Get()
log.Info("id %s get Cache", carrier.AttachedTunnelID)
for {
//首先检查这个是不是完整的包,是就返回好了,免得被阻塞
data, rest, err := UnwrapPackage(wrapedPackage)
@ -226,7 +212,5 @@ 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
}