How to use addTimer method of conn Package

Best Gauge code snippet using conn.addTimer

user_item.go

Source:user_item.go Github

copy

Full Screen

1package game2import (3 "encoding/json"4 "log"5 "net"6 "sync"7 "sync/atomic"8 "time"9 "github.com/panshiqu/framework/define"10 "github.com/panshiqu/framework/network"11 "github.com/panshiqu/framework/utils"12)13// UserItem 用户14type UserItem struct {15 id int // 编号16 name string // 名称17 icon int // 图标18 level int // 等级19 gender int // 性别20 phone string // 手机21 score int64 // 分数22 cacheScore int64 // 缓存分数23 diamond int64 // 钻石24 cacheDiamond int64 // 缓存钻石25 robot bool // 机器人26 conn net.Conn // 网络连接(暂不为它考虑加锁)27 status int32 // 状态28 chairID int // 椅子编号29 tableFrame *TableFrame // 桌子框架30 mutex sync.Mutex // 财富加锁31}32// UserID 用户编号33func (u *UserItem) UserID() int {34 return u.id35}36// UserName 用户名称37func (u *UserItem) UserName() string {38 return u.name39}40// UserIcon 用户图标41func (u *UserItem) UserIcon() int {42 return u.icon43}44// UserLevel 用户等级45func (u *UserItem) UserLevel() int {46 return u.level47}48// UserGender 用户性别49func (u *UserItem) UserGender() int {50 return u.gender51}52// BindPhone 用户手机53func (u *UserItem) BindPhone() string {54 return u.phone55}56// UserScore 用户分数57func (u *UserItem) UserScore() int64 {58 u.mutex.Lock()59 defer u.mutex.Unlock()60 return u.score + u.cacheScore61}62// CacheScore 缓存分数63func (u *UserItem) CacheScore() int64 {64 u.mutex.Lock()65 defer u.mutex.Unlock()66 return u.cacheScore67}68// UserDiamond 用户钻石69func (u *UserItem) UserDiamond() int64 {70 u.mutex.Lock()71 defer u.mutex.Unlock()72 return u.diamond + u.cacheDiamond73}74// CacheDiamond 缓存钻石75func (u *UserItem) CacheDiamond() int64 {76 u.mutex.Lock()77 defer u.mutex.Unlock()78 return u.cacheDiamond79}80// IsRobot 是否机器人81func (u *UserItem) IsRobot() bool {82 return u.robot83}84// SetConn 设置网络连接85func (u *UserItem) SetConn(v net.Conn) {86 u.conn = v87}88// UserStatus 用户状态89func (u *UserItem) UserStatus() int {90 return int(atomic.LoadInt32(&u.status))91}92// SetUserStatus 设置用户状态93func (u *UserItem) SetUserStatus(v int) {94 atomic.StoreInt32(&u.status, int32(v))95 if tableFrame := u.TableFrame(); tableFrame != nil {96 notifyStatus := &define.NotifyStatus{97 ChairID: u.ChairID(),98 UserStatus: u.UserStatus(),99 }100 tableFrame.SendTableJSONMessage(define.GameCommon, define.GameNotifyStatus, notifyStatus)101 }102}103// ChairID 椅子编号104func (u *UserItem) ChairID() int {105 u.mutex.Lock()106 defer u.mutex.Unlock()107 return u.chairID108}109// SetChairID 设置椅子编号110func (u *UserItem) SetChairID(v int) {111 u.mutex.Lock()112 u.chairID = v113 u.mutex.Unlock()114}115// TableID 桌子编号116func (u *UserItem) TableID() int {117 if tableFrame := u.TableFrame(); tableFrame != nil {118 return tableFrame.TableID()119 }120 return define.InvalidTable121}122// TableFrame 桌子框架123func (u *UserItem) TableFrame() *TableFrame {124 u.mutex.Lock()125 defer u.mutex.Unlock()126 return u.tableFrame127}128// SetTableFrame 设置桌子框架129func (u *UserItem) SetTableFrame(v *TableFrame) {130 u.mutex.Lock()131 u.tableFrame = v132 u.mutex.Unlock()133}134// TableUserInfo 桌子用户信息135func (u *UserItem) TableUserInfo() *define.NotifySitDown {136 return &define.NotifySitDown{137 UserInfo: define.UserInfo{138 UserID: u.id,139 UserName: u.name,140 UserIcon: u.icon,141 UserLevel: u.level,142 UserGender: u.gender,143 UserScore: u.UserScore(),144 UserDiamond: u.UserDiamond(),145 },146 TableID: u.TableID(),147 ChairID: u.ChairID(),148 UserStatus: u.UserStatus(),149 }150}151// WriteScore 写入分数152func (u *UserItem) WriteScore(varScore int64, changeType int) error {153 return utils.Wrap(u.WriteTreasure(varScore, 0, changeType))154}155// WriteDiamond 写入钻石156func (u *UserItem) WriteDiamond(varDiamond int64, changeType int) error {157 return utils.Wrap(u.WriteTreasure(0, varDiamond, changeType))158}159// WriteTreasure 写入财富160func (u *UserItem) WriteTreasure(varScore int64, varDiamond int64, changeType int) (err error) {161 defer func() {162 if err == nil {163 if tableFrame := u.TableFrame(); tableFrame != nil {164 notifyTreasure := &define.NotifyTreasure{165 UserID: u.id,166 VarScore: varScore,167 VarDiamond: varDiamond,168 ChangeType: changeType,169 }170 tableFrame.SendTableJSONMessage(define.GameCommon, define.GameNotifyTreasure, notifyTreasure)171 }172 }173 }()174 u.mutex.Lock()175 defer u.mutex.Unlock()176 // 分数不足177 if u.score+u.cacheScore+varScore < 0 {178 return define.ErrNotEnoughScore179 }180 // 钻石不足181 if u.diamond+u.cacheDiamond+varDiamond < 0 {182 return define.ErrNotEnoughDiamond183 }184 // 缓存输赢185 if changeType == define.ChangeTypeWinLose {186 u.cacheScore += varScore187 u.cacheDiamond += varDiamond188 return nil189 }190 // 写入数据库191 if err := u.WriteToDB(varScore, varDiamond, changeType); err != nil {192 return utils.Wrap(err)193 }194 // 更新财富195 u.score += varScore196 u.diamond += varDiamond197 return nil198}199// WriteToDB 写入数据库200func (u *UserItem) WriteToDB(varScore int64, varDiamond int64, changeType int) error {201 if varScore == 0 && varDiamond == 0 {202 return nil203 }204 notifyTreasure := &define.NotifyTreasure{205 UserID: u.id,206 VarScore: varScore,207 VarDiamond: varDiamond,208 ChangeType: changeType,209 }210 return utils.Wrap(rpc.JSONCall(define.DBCommon, define.DBChangeTreasure, notifyTreasure, nil))211}212// SendMessage 发送消息213func (u *UserItem) SendMessage(mcmd uint16, scmd uint16, data []byte) {214 if err := network.SendMessage(u.conn, mcmd, scmd, data); err != nil {215 log.Println("UserItem SendMessage", err)216 }217}218// SendJSONMessage 发送消息219func (u *UserItem) SendJSONMessage(mcmd uint16, scmd uint16, js interface{}) {220 if data, err := json.Marshal(js); err == nil {221 u.SendMessage(mcmd, scmd, data)222 }223}224// AddTimer 添加定时器225func (u *UserItem) AddTimer(id int, duration time.Duration, parameter interface{}, persistence bool) {226 if id >= 0 && id < define.TimerPerUser {227 sins.Add(u.TableID()*define.TimerPerTable+define.TimerPerTable+u.ChairID()*define.TimerPerUser+define.TimerPerUser+id, duration, parameter, persistence)228 }229}230// RunAfter 添加定时器231func (u *UserItem) RunAfter(id int, duration time.Duration, parameter interface{}) {232 u.AddTimer(id, duration, parameter, false)233}234// RunAlways 添加定时器235func (u *UserItem) RunAlways(id int, duration time.Duration, parameter interface{}) {236 u.AddTimer(id, duration, parameter, true)237}238// RemoveTimer 移除定时器239func (u *UserItem) RemoveTimer(id int) {240 if id >= 0 && id < define.TimerPerUser {241 sins.Remove(u.TableID()*define.TimerPerTable + define.TimerPerTable + u.ChairID()*define.TimerPerUser + define.TimerPerUser + id)242 }243}244// SurplusDuration 定时器剩余时间245func (u *UserItem) SurplusDuration(id int) time.Duration {246 if id >= 0 && id < define.TimerPerUser {247 return sins.Surplus(u.TableID()*define.TimerPerTable + define.TimerPerTable + u.ChairID()*define.TimerPerUser + define.TimerPerUser + id)248 }249 return 0250}251// OnTimer 定时器252func (u *UserItem) OnTimer(id int, parameter interface{}) error {253 return nil254}...

Full Screen

Full Screen

wheel.go

Source:wheel.go Github

copy

Full Screen

1package main2import (3 "encoding/json"4 "fmt"5 "strconv"6 "sync"7 //"github.txt.com/zzh20/timewheel"8 "github.com/ouqiang/timewheel"9 "time"10)11// 定义心跳包,设置心跳超时时间,处理函数12//var wheelHeartbeat = timewheel.New(time.Second*1, 30, func(data interface{}) {13// c := data.(net.Conn)14// log.Printf("timeout close conn:%v", c)15// c.Close()16//})17func Put(data interface{}) {18 res := data.(DataT)19 out, _ := json.Marshal(&res)20 fmt.Printf("===%v\n", string(out))21}22type DataT struct {23 Index int24}25func main111() {26 // 初始化时间轮27 // 第一个参数为tick刻度, 即时间轮多久转动一次28 // 第二个参数为时间轮槽slot数量29 // 第三个参数为回调函数30 //tw := timewheel.New(1*time.Second, 3600, func(data interface{}) {31 // // do something32 //33 //})34 tw := timewheel.New(1*time.Second, 3600, Put)35 // 启动时间轮36 tw.Start()37 // 添加定时器38 // 第一个参数为延迟时间39 // 第二个参数为定时器唯一标识, 删除定时器需传递此参数40 // 第三个参数为用户自定义数据, 此参数将会传递给回调函数, 类型为interface{}41 conn := "hh"42 tw.AddTimer(1*time.Second, conn, DataT{Index: 105626})43 for i := 0; i < 5; i++ {44 nn := strconv.Itoa(i)45 tw.AddTimer(2*time.Second, conn+nn, DataT{Index: 100 + i})46 time.Sleep(time.Millisecond * 500)47 }48 //for i := 0; i < 5; i++ {49 // nn := strconv.Itoa(i)50 // tw.RemoveTimer(conn + nn)51 //}52 // 删除定时器, 参数为添加定时器传递的唯一标识53 //tw.RemoveTimer(conn)54 // 停止时间轮55 //tw.Stop()56 time.Sleep(time.Second * 170)57 ///select {}58}59func main() {60 var once sync.Once61 for i, v := range make([]string, 10) {62 once.Do(onces)63 fmt.Println("count:", v, "---", i)64 }65 for i := 0; i < 10; i++ {66 go func() {67 once.Do(onced)68 fmt.Println("213")69 }()70 }71 time.Sleep(4000)72}73func onces() {74 fmt.Println("onces")75}76func onced() {77 fmt.Println("onced")78}...

Full Screen

Full Screen

mytimewheel.go

Source:mytimewheel.go Github

copy

Full Screen

1package main2import (3 "fmt"4 "github.com/ouqiang/timewheel"5 "time"6)7func main() {8 // 初始化时间轮9 // 第一个参数为tick刻度, 即时间轮多久转动一次10 // 第二个参数为时间轮槽slot数量11 // 第三个参数为回调函数12 tw := timewheel.New(1*time.Second, 3600, func(data interface{}) {13 // do something14 fmt.Println(data)15 time.Sleep(time.Second * 3)16 })17 // 启动时间轮18 tw.Start()19 var conn = 020 // 添加定时器21 // 第一个参数为延迟时间22 // 第二个参数为定时器唯一标识, 删除定时器需传递此参数23 // 第三个参数为用户自定义数据, 此参数将会传递给回调函数, 类型为interface{}24 //tw.AddTimer(5 * time.Second, conn, map[string]int{"uid" : 105626})25 for i := 0; i < 3; i++ {26 tw.AddTimer(0, conn, map[string]int{"uid": i})27 }28 // 删除定时器, 参数为添加定时器传递的唯一标识29 //tw.RemoveTimer(conn)30 // 停止时间轮31 //tw.Stop()32 select {}33}...

Full Screen

Full Screen

addTimer

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 conn, err := net.Dial("tcp", "localhost:8000")4 if err != nil {5 panic(err)6 }7 fmt.Println("Connection established")8 conn.SetDeadline(time.Now().Add(10 * time.Second))9 fmt.Println("Deadline set")10 time.Sleep(15 * time.Second)11 fmt.Println("Sleep completed")12}13import (14func main() {15 conn, err := net.Dial("tcp", "localhost:8000")16 if err != nil {17 panic(err)18 }19 fmt.Println("Connection established")20 conn.SetReadDeadline(time.Now().Add(10 * time.Second))21 fmt.Println("Deadline set")22 time.Sleep(15 * time.Second)23 fmt.Println("Sleep completed")24}25import (26func main() {27 conn, err := net.Dial("tcp", "localhost:8000")28 if err != nil {29 panic(err)30 }31 fmt.Println("Connection established")32 conn.SetWriteDeadline(time.Now().Add(10 * time.Second))33 fmt.Println("Deadline set")34 time.Sleep(15 * time.Second)35 fmt.Println("Sleep completed")36}37import (38func main() {39 listener, err := net.Listen("tcp", ":8000")40 if err != nil {41 panic(err)42 }43 fmt.Println("Listening on port 8000")44 listener.SetDeadline(time.Now().Add(10 * time.Second))45 fmt.Println("Deadline set")46 time.Sleep(15 * time.Second)47 fmt.Println("Sleep completed")48}

Full Screen

Full Screen

addTimer

Using AI Code Generation

copy

Full Screen

1import (2type conn struct {3}4func (c *conn) addTimer(d time.Duration) {5 c.timers = append(c.timers, *time.NewTimer(d))6}7func main() {8 c := conn{}9 c.addTimer(1 * time.Second)10 fmt.Println("timer added, now wait for 2 seconds")11 time.Sleep(2 * time.Second)12}13time.(*Timer).Stop(0xc00000e2d0, 0x0, 0x0)14time.(*Timer).Reset(0xc00000e2d0, 0x3b9aca00, 0x0, 0x0)15main.main()

Full Screen

Full Screen

addTimer

Using AI Code Generation

copy

Full Screen

1import (2type Conn struct {3}4func (c *Conn) addTimer() {5 fmt.Println("Timer started")6 time.Sleep(2 * time.Second)7}8func main() {9 c := Conn{ch: make(chan int)}10 go c.addTimer()11 fmt.Println("Timer ended")12}13import (14func addTimer() {15 fmt.Println("Timer started")16 time.Sleep(2 * time.Second)17 fmt.Println("Timer ended")18}19func main() {20 go addTimer()21 time.Sleep(3 * time.Second)22}23import (24type Conn struct {25}26func (c *Conn) addTimer() {27 fmt.Println("Timer started")28 time.Sleep(2 * time.Second)29}30func main() {31 c := Conn{ch: make(chan int)}32 go c.addTimer()33 fmt.Println("Timer ended")34}

Full Screen

Full Screen

addTimer

Using AI Code Generation

copy

Full Screen

1import (2type conn struct {3}4func (c *conn) addTimer(t time.Duration) {5 fmt.Println("Adding timer", t)6}7func (c *conn) getTimer() time.Duration {8 fmt.Println("Getting timer")9 return time.Duration(0)10}11func main() {12 c := &conn{1}13 c.addTimer(time.Second)14 c.getTimer()15}16import (17func main() {18 c := &timer.conn{1}19 c.addTimer(time.Second)20 c.getTimer()21}22./main.go:9: c.addTimer undefined (type *timer.conn has no field or method addTimer)23./main.go:10: c.getTimer undefined (type *timer.conn has no field or method getTimer)24import (25type conn struct {26}27func (c *conn) addTimer(t time.Duration) {28 fmt.Println("Adding timer", t)29}30func (c *conn) getTimer() time.Duration {31 fmt.Println("Getting timer")32 return time.Duration(0)33}34func main() {35 c := &conn{1}36 c.addTimer(time.Second)37 c.getTimer()38}39import (40func main() {41 c := &timer.conn{1}42 c.addTimer(time.Second)43 c.getTimer()44}45./main.go:9: c.addTimer undefined (type *timer.conn has no field or method addTimer)46./main.go:10: c.getTimer undefined (type *timer.conn has no field or method getTimer

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful