重构,用多态模式写数据发送和接收的代码。速度快了,可能和减少了判断有关。

This commit is contained in:
dmy@lab
2016-01-31 12:32:41 +08:00
parent dee06848fb
commit bdfcbbbda8
7 changed files with 288 additions and 108 deletions

View File

@@ -3,13 +3,15 @@ package model
import (
// "fmt"
"net"
"time"
"github.com/TransX/cache"
"github.com/TransX/communicator"
"github.com/TransX/constant"
"github.com/TransX/log"
"github.com/TransX/tscipher"
"github.com/spf13/viper"
"net"
"time"
)
type Tunnel struct {
@@ -95,14 +97,18 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
defer this.onError()
var n int
var err error
var receiver communicator.Receiver
if cipherDirection != constant.RECEIVE {
// revCarrier.Cipher = nil
// n, err = tscipher.RowReceiveData(revCarrier)
receiver = communicator.NewDirectReceiver(revCarrier)
} else {
receiver = communicator.NewNormalReceiver(revCarrier)
}
for {
rTimer := time.Now() //receive timer
if cipherDirection != constant.RECEIVE {
revCarrier.Cipher = nil
n, err = tscipher.RowReceiveData(revCarrier)
} else {
n, err = tscipher.ReceiveData(revCarrier)
}
n, err = receiver.Receive()
log.Info("id %s time to receive %d", id, time.Since(rTimer)/1000)
if err != nil {
log.Panic("Read panic. Tunnel id: %s. Remote Add: %s Local: %s. Err:%s", id, src.RemoteAddr().String(), src.LocalAddr().String(), err.Error())
@@ -115,13 +121,17 @@ func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
dest := this.dest
cipherDirection := this.cipherDirection
id := this.id
var sender communicator.Sender
defer this.onError()
if cipherDirection != constant.SEND {
sendCarrier.Cipher = nil
// sendCarrier.Cipher = nil
sender = communicator.NewDirectSender(sendCarrier)
} else {
sender = communicator.NewNormalSender(sendCarrier)
}
for {
sTimer := time.Now() //send timer
n, err := tscipher.SendData(sendCarrier)
n, err := sender.Send()
log.Info("id %s time to send %d", id, time.Since(sTimer)/1000)
if err != nil {
log.Panic("Write panic. ID: %s, Err: %s, Remote Add: %s", id, err, dest.RemoteAddr().String())