diff --git a/framemgr.go b/framemgr.go index e7dcf60..8ecae8f 100644 --- a/framemgr.go +++ b/framemgr.go @@ -225,14 +225,8 @@ func (fm *FrameMgr) processRecvList(tmpreq map[int32]int, tmpack map[int32]int, func (fm *FrameMgr) addToRecvWin(rf *Frame) bool { - begin := fm.recvid - end := fm.recvid + fm.windowsize - id := (int)(rf.Id) - if id < begin { - id += FRAME_MAX_ID - } - if id > end || id < begin { - loggo.Debug("recv frame not in range %d %d %d", begin, end, id) + if !fm.isIdInRange((int)(rf.Id), FRAME_MAX_ID) { + loggo.Debug("recv frame not in range %d %d", rf.Id, fm.recvid) return false } @@ -247,7 +241,7 @@ func (fm *FrameMgr) addToRecvWin(rf *Frame) bool { for e := fm.recvwin.Front(); e != nil; e = e.Next() { f := e.Value.(*Frame) loggo.Debug("start insert recv win %d %d %d", fm.recvid, rf.Id, f.Id) - if fm.compareId(rf, f) < 0 { + if fm.compareId((int)(rf.Id), (int)(f.Id)) < 0 { fm.recvwin.InsertBefore(rf, e) loggo.Debug("insert recv win %d %d before %d", rf.Id, len(rf.Data), f.Id) return true @@ -259,10 +253,8 @@ func (fm *FrameMgr) addToRecvWin(rf *Frame) bool { return true } -func (fm *FrameMgr) compareId(lf *Frame, rf *Frame) int { +func (fm *FrameMgr) compareId(l int, r int) int { - l := (int)(lf.Id) - r := (int)(rf.Id) if l < fm.recvid { l += FRAME_MAX_ID } @@ -310,7 +302,7 @@ func (fm *FrameMgr) combineWindowToRecvBuffer() { reqtmp := make(map[int]int) e := fm.recvwin.Front() id = fm.recvid - for len(reqtmp) < fm.windowsize && e != nil { + for len(reqtmp) < fm.windowsize && len(reqtmp)*4 < FRAME_MAX_SIZE/2 && e != nil { f := e.Value.(*Frame) loggo.Debug("start add req id %d %d %d", fm.recvid, f.Id, id) if f.Id != (int32)(id) { @@ -392,3 +384,18 @@ func (fm *FrameMgr) processPong(f *Frame) { loggo.Debug("recv pong %d %d", rtt, fm.rttms) } } + +func (fm *FrameMgr) isIdInRange(id int, maxid int) bool { + begin := fm.recvid + end := fm.recvid + fm.windowsize + if end >= maxid { + if id >= 0 && id < end-maxid { + return true + } + end = maxid + } + if id >= begin && id < end { + return true + } + return false +} diff --git a/pingtunnel_test.go b/pingtunnel_test.go index ff86c96..732e9e6 100644 --- a/pingtunnel_test.go +++ b/pingtunnel_test.go @@ -25,30 +25,54 @@ func Test0001(t *testing.T) { fmt.Println("my1 = ", my1) fm := FrameMgr{} - fm.recvid = 0 + fm.recvid = 4 fm.windowsize = 100 lr := &Frame{} rr := &Frame{} lr.Id = 1 - rr.Id = 2 - fmt.Println("fm.compareId(lr, rr) = ", fm.compareId(lr, rr)) + rr.Id = 4 + fmt.Println("fm.compareId(lr, rr) = ", fm.compareId((int)(lr.Id), (int)(rr.Id))) lr.Id = 99 rr.Id = 8 - fmt.Println("fm.compareId(lr, rr) = ", fm.compareId(lr, rr)) + fmt.Println("fm.compareId(lr, rr) = ", fm.compareId((int)(lr.Id), (int)(rr.Id))) fm.recvid = 9000 lr.Id = 9998 rr.Id = 9999 - fmt.Println("fm.compareId(lr, rr) = ", fm.compareId(lr, rr)) + fmt.Println("fm.compareId(lr, rr) = ", fm.compareId((int)(lr.Id), (int)(rr.Id))) fm.recvid = 9000 lr.Id = 9998 rr.Id = 8 - fmt.Println("fm.compareId(lr, rr) = ", fm.compareId(lr, rr)) + fmt.Println("fm.compareId(lr, rr) = ", fm.compareId((int)(lr.Id), (int)(rr.Id))) fm.recvid = 0 lr.Id = 9998 rr.Id = 8 - fmt.Println("fm.compareId(lr, rr) = ", fm.compareId(lr, rr)) + fmt.Println("fm.compareId(lr, rr) = ", fm.compareId((int)(lr.Id), (int)(rr.Id))) + + fm.recvid = 0 + fm.windowsize = 5 + fmt.Println("fm.isIdInRange = ", fm.isIdInRange(4, 10)) + + fm.recvid = 0 + fm.windowsize = 5 + fmt.Println("fm.isIdInRange = ", fm.isIdInRange(5, 10)) + + fm.recvid = 4 + fm.windowsize = 5 + fmt.Println("fm.isIdInRange = ", fm.isIdInRange(1, 10)) + + fm.recvid = 7 + fm.windowsize = 5 + fmt.Println("fm.isIdInRange = ", fm.isIdInRange(1, 10)) + + fm.recvid = 7 + fm.windowsize = 5 + fmt.Println("fm.isIdInRange = ", fm.isIdInRange(2, 10)) + + fm.recvid = 7 + fm.windowsize = 5 + fmt.Println("fm.isIdInRange = ", fm.isIdInRange(9, 10)) }