This commit is contained in:
dmy@lab
2015-11-23 13:50:34 +08:00
parent 67a00e7902
commit 825d0f2501
2 changed files with 127 additions and 46 deletions

View File

@@ -11,6 +11,38 @@ import (
"strings"
)
type Cache struct {
cache []byte
l int //length, not capability
}
type QueueCache struct { //FIFO
queue chan *Cache
}
func NewQueueCache(quN int) *QueueCache {
t := &QueueCache{
queue: make(chan *Cache, quN),
}
return t
}
func (this *QueueCache) Put(bits []byte, l int) {
// fmt.Printf("len of chan Put %d\n", len(this.queue))
this.queue <- &Cache{
cache: bits,
l: l,
}
// fmt.Printf("QueueCache put %d\n", len(this.queue))
}
func (this *QueueCache) Get() ([]byte, int) {
// fmt.Printf("len of chan Get %d\n", len(this.queue))
t := <-this.queue
// fmt.Printf("QueueCache got %d\n", len(this.queue))
return t.cache, t.l
}
var StartMark = []byte("#2v!") //should be constant
var EndMark = []byte("_=1z") //should be constant
@@ -36,20 +68,21 @@ type Cipher interface {
}
type Carrier struct {
Conn net.Conn
Cipher Cipher
Cache []byte
Conn net.Conn
Cipher Cipher
// Cache []byte
Cache *QueueCache
AttachedTunnelID string
receiveBuff []byte
}
func NewCarrier(conn net.Conn, cipher Cipher, cache []byte, id string) *Carrier {
func NewCarrier(conn net.Conn, cipher Cipher, queCache *QueueCache, id string) *Carrier {
t := new(Carrier)
t.Conn = conn
t.Cipher = cipher
t.Cache = cache
t.Cache = queCache
t.AttachedTunnelID = id
t.receiveBuff = make([]byte, 0, len(cache))
t.receiveBuff = make([]byte, 0, 1024*4)
return t
}
@@ -139,15 +172,17 @@ func UnwrapPackage(pacakge []byte) (data []byte, rest []byte, err error) {
}
func SendData(carrier *Carrier, nByte int) (n int, err error) {
if len(carrier.Cache) < nByte {
func SendData(carrier *Carrier) (n int, err error) {
cache, nByte := carrier.Cache.Get()
// fmt.Printf("id %s get cache\n", carrier.AttachedTunnelID)
if len(cache) < nByte {
log.Panic("Cache of send is too small")
}
if carrier.Cipher == nil {
n, err = carrier.Conn.Write(carrier.Cache[:nByte])
n, err = carrier.Conn.Write(cache[:nByte])
return
}
encrypedByte, err := carrier.Cipher.Encrypt(carrier.Cache[:nByte])
encrypedByte, err := carrier.Cipher.Encrypt(cache[:nByte])
if err != nil {
n = 0
return
@@ -156,16 +191,18 @@ func SendData(carrier *Carrier, nByte int) (n int, err error) {
wraped := WrapPackage(encrypedByte[:nByte])
n, err = carrier.Conn.Write(wraped)
log.Debug("Ready to write id %s, 18 byte %s", carrier.AttachedTunnelID, string(wraped[:18]))
copy(carrier.Cache, encrypedByte[:nByte]) // in case of debugging
// copy(carrier.Cache, encrypedByte[:nByte]) // in case of debugging
return
}
func RowReceiveData(carrier *Carrier) (n int, err error) {
n, err = carrier.Conn.Read(carrier.Cache)
cache := make([]byte, 1024*4)
n, err = carrier.Conn.Read(cache)
if err != nil {
n = 0
}
carrier.Cache.Put(cache, n)
// fmt.Printf("id %s put cache\n", carrier.AttachedTunnelID)
return
}
@@ -179,6 +216,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
wrapedPackage := carrier.GetReceiveBuff() //make([]byte, 0, cap(carrier.Cache))
var packageData []byte
var _rest []byte
cache := make([]byte, 1024*4)
for {
//首先检查这个是不是完整的包,是就返回好了,免得被阻塞
data, rest, err := UnwrapPackage(wrapedPackage)
@@ -202,13 +240,13 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
//如果读到的数据不够一个完整的包
log.Debug("id %s to read wrapedPackage %d", carrier.AttachedTunnelID, len(wrapedPackage))
if len(wrapedPackage) > 0 {
n, err = carrier.Conn.Read(carrier.Cache)
n, err = carrier.Conn.Read(cache)
if err != nil {
log.Error("ERROR %s", err)
}
log.Debug("id %s to Conn.Read %d", carrier.AttachedTunnelID, n)
} else {
n, err = io.ReadAtLeast(carrier.Conn, carrier.Cache, 18)
n, err = io.ReadAtLeast(carrier.Conn, cache, 18)
log.Debug("id %s to ReadAtLeast", carrier.AttachedTunnelID)
}
@@ -216,7 +254,7 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
n = 0
return n, err
}
wrapedPackage = append(wrapedPackage, carrier.Cache[:n]...)
wrapedPackage = append(wrapedPackage, cache[:n]...)
log.Debug("id %s length of conn %d", carrier.AttachedTunnelID, n)
log.Debug("id %s first 18 %s from %s", carrier.AttachedTunnelID, string(wrapedPackage[:18]), carrier.Conn.RemoteAddr().String())
@@ -233,6 +271,8 @@ func ReceiveData(carrier *Carrier) (n int, err error) {
return
}
n = len(decrypted)
copy(carrier.Cache, decrypted)
// copy(cache, decrypted)
carrier.Cache.Put(decrypted, n)
// fmt.Printf("id %s put cache\n", carrier.AttachedTunnelID)
return
}