Skip to content

Commit

Permalink
调整: 循环计时器更换为更加稳定的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Jul 17, 2017
1 parent 1862047 commit 67dd10a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 58 deletions.
2 changes: 1 addition & 1 deletion example/classicrecv/classicrecv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func server() {
_, send := peer.HandlerList()

// 在原有流程中, 插入固定消息回调
recvList := socket.BuildRecvHandler(new(RecvMessageHandler), peer)
recvList := socket.BuildRecvHandler(cellnet.NewBranchHandler(new(RecvMessageHandler), peer))

peer.SetHandlerList(recvList, send)

Expand Down
13 changes: 8 additions & 5 deletions example/timer/timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,30 @@ func TestAfterTimer(t *testing.T) {
signal.WaitAndExpect("2 sec after not done", 2)
}

func TestTickerTimer(t *testing.T) {
func TestLoopTimer(t *testing.T) {

signal := util.NewSignalTester(t)
signal.SetTimeout(60 * time.Second)

queue := cellnet.NewEventQueue()

// 启动消息循环
queue.StartLoop()

var count int
timer.Tick(queue, time.Millisecond*100, func(stopper timer.TickStopper) {

// 启动计时循环
timer.NewLoop(queue, time.Millisecond*100, func(ctx *timer.Loop) {

log.Debugln("tick 100 ms", count)

count++

if count >= 10 {
signal.Done(1)
stopper.Stop()
ctx.Stop()
}

})
}, nil).Start()

signal.WaitAndExpect("100ms * 10 times ticker not done", 1)
}
Expand Down
90 changes: 90 additions & 0 deletions timer/loop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package timer

import (
"github.com/davyxu/cellnet"
"time"
)

type Loop struct {
Context interface{}
Duration time.Duration
userCallback func(*Loop)

running bool

Queue cellnet.EventQueue
}

func (self *Loop) Running() bool {
return self.running
}

func (self *Loop) Start() bool {

if self.running {
return false
}

self.running = true

self.rawPost()

return true
}

func (self *Loop) rawPost() {

if self.Duration == 0 {
panic("seconds can be zero in loop")
}

After(self.Queue, self.Duration, func() {
tick(self, false)
})
}

func (self *Loop) NextLoop() {

self.Queue.Post(func() {
tick(self, true)
})
}

func (self *Loop) Stop() {

self.running = false
}

func (self *Loop) Notify() {
self.userCallback(self)
}

func tick(ctx interface{}, nextLoop bool) {

loop := ctx.(*Loop)

loop.Notify()

if !loop.running {
return
}

// 不等待, 直接跳到下一个循环

if !nextLoop {
loop.rawPost()
}

}

func NewLoop(q cellnet.EventQueue, duration time.Duration, callback func(*Loop), context interface{}) *Loop {

self := &Loop{
Context: context,
Duration: duration,
userCallback: callback,
Queue: q,
}

return self
}
51 changes: 0 additions & 51 deletions timer/ticker.go

This file was deleted.

2 changes: 1 addition & 1 deletion websocket/textpkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package websocket

import "bytes"

// 格式
// 格式: 消息名\n+json文本

func parsePacket(pkt []byte) (msgName string, data []byte) {

Expand Down

0 comments on commit 67dd10a

Please sign in to comment.