func (u *User) Register(args *User_RegisterArgs, reply *Reply) error { if args.Psw == "" { reply.Code = com.E_AgentPasswordCannotBeNull } else { accountkey := k_account_user_ + args.Account account, _ := redis.String(u.c.Do("GET", accountkey)) // exists, _ := redis.Bool(u.c.Do("EXISTS", accountkey)) // if !exists { if account == "" { // create id u.c.Do("INCR", k_account_count) id, _ := redis.String(u.c.Do("GET", k_account_count)) // save account u.c.Do("SET", accountkey, id) u.c.Do("SADD", k_account_userlist, id) // save user userkey := k_user_ + string(id) u.c.Do("HSET", userkey, k_psw, args.Psw) u.c.Do("HSET", userkey, k_coin, 0) u.c.Do("HSET", userkey, k_nickname, "nickname") reply.Code = com.E_Success log.Debug("Register success") } else { reply.Code = com.E_AgentAccountExist log.Debug("E_AgentAccountExist") } } return nil }
func (a *Agent) Serve() (err error) { a.Enter() defer a.Exit() session := make(chan string, 5) go func(c chan string) { var buf string for { err = a.Read(&buf) if err != nil { //fixme: 通过正常流程退出 log.Debug("agent read: ", err.Error()) return } c <- buf } }(session) timeout := time.NewTimer(0) L: for { timeout.Reset(10 * time.Second) select { case cmd := <-a.out: if cmd == CtrlRemoveAgent { log.Debug("recv ctrl: CtrlRemoveAgent") break L } case msg := <-session: var resp string resp, err = a.Handle(msg) if err != nil { log.Error("agent session: ", err.Error()) break L } a.Write(resp) case msg := <-a.send: if err = a.Write(msg); err != nil { // 写错误 log.Error("client write failed: ", err.Error()) break L } case <-timeout.C: log.Debug("recv timeout") break L } } return }
func (c *RpcClient) Close() error { log.Debug("rpc client close") if err := c.Close(); err != nil { return err } return nil }
func startServer2() { server, err := NewServer() if err != nil { log.Error(err.Error()) return } defer func() { server.Close() }() serve := func(ws *websocket.Conn) { log.Debug("new agent") if err := server.Serve(fw.NewWsReadWriter(ws)); err != nil { log.Error(err.Error()) } log.Debug("agent leave") } http.Handle("/", websocket.Handler(serve)) http.ListenAndServe(cfg.AgentAddr(), nil) log.Info("server2 serve on ", cfg.AgentAddr()) }
func main() { server, err := agent.NewServer() if err != nil { panic("new server failed") } defer func() { server.Close() }() serve := func(ws *websocket.Conn) { if err := server.Serve(fw.NewWsReadWriter(ws)); err != nil { log.Error(err.Error()) } log.Infof("agent leaves, agent count=%v", server.AgentCount()) } http.Handle("/", websocket.Handler(serve)) log.Debug("agent server start on:", cfg.AgentPort) http.ListenAndServe(":"+cfg.AgentPort, nil) }
func main() { var agent *fw.Agent serve := func(ws *websocket.Conn) { // only ONE agent server allowed to connect log.Debugf("new comes, agent=%+v", agent) if agent == nil { agent = fw.NewAgent(game.NewModel(), fw.NewWsReadWriter(ws), 1) if err := agent.Serve(); err != nil { log.Error(err.Error()) } } else { log.Warning("already connected by agentsrv, cannont serve more") } // release agent = nil } http.Handle("/", websocket.Handler(serve)) log.Debug("game server start on:", cfg.GameAddr()) http.ListenAndServe(cfg.GameAddr(), nil) }
func Test3(t *testing.T) { log.Debug(tt01) log.Debug(tt02) log.Debug(tt03) log.Debug(tt04) log.Debug(tt05) log.Debug(tt06) log.Debug(tt01 >> 2) log.Debug(tt02 >> 2) log.Debug(tt03 >> 2) log.Debug(tt04 >> 2) log.Debug(tt05 >> 2) log.Debug(tt06 >> 2) log.Debug("---") log.Debug(vv02 + tt03) log.Debug(vv02 + tt04) log.Debug(vv02 + tt05) log.Debug(vv02 + tt06) }