func (app *app) Check() { app.lastcheck = time.Now() heartbeat := time.NewTicker(5 * time.Second) updatelist := time.NewTicker(time.Minute) //每分钟同步一下app列表 for !app.Shutdown { select { case <-heartbeat.C: //send heartbeat pkg if time.Now().Sub(app.lastcheck).Seconds() > TIMEOUTSECS { app.conn.Close() return } data, err := util.CreateMsg(nil, []byte{}, share.M_HEARTBEAT) if err != nil { log.LogFatalf(err) } app.conn.Write(data) case <-updatelist.C: app.SendList() case <-app.exit: return } } heartbeat.Stop() updatelist.Stop() }
func CreateRegisterAgent(id string, nobalance bool) (data []byte, err error) { reg := &RegisterAgent{id, nobalance} out, e := EncodeMsg(reg) if e != nil { err = e return } return util.CreateMsg(nil, out, M_REGISTER_AGENT) }
func CreateAppMsg(typ string, reqid string, appid string, appuid int32, args string, callapp int32) (data []byte, err error) { create := &CreateApp{typ, reqid, appid, appuid, args, callapp} out, e := EncodeMsg(create) if e != nil { err = e return } return util.CreateMsg(nil, out, M_CREATEAPP) }
func CreateReadyMsg(id int32) (data []byte, err error) { ready := &AppReady{id} out, e := EncodeMsg(ready) if e != nil { err = e return } return util.CreateMsg(nil, out, M_READY) }
func CreateMustAppReadyMsg() (data []byte, err error) { ready := &MustAppReady{} out, e := EncodeMsg(ready) if e != nil { err = e return } return util.CreateMsg(nil, out, M_MUSTAPPREADY) }
func CreateAppBakMsg(reqid string, appid int32, res string) (data []byte, err error) { create := &CreateAppBak{reqid, appid, res} out, e := EncodeMsg(create) if e != nil { err = e return } return util.CreateMsg(nil, out, M_CREATEAPP_BAK) }
func CreateForwardMsg(appid int32, msg []byte) (data []byte, err error) { forward := &SendAppMsg{appid, msg} out, e := EncodeMsg(forward) if e != nil { err = e return } return util.CreateMsg(nil, out, M_FORWARD_MSG) }
func CreateRemoveServerMsg(id int32) (data []byte, err error) { rs := &RemoveApp{id} out, e := EncodeMsg(rs) if e != nil { err = e return } data, err = util.CreateMsg(nil, out, M_REMOVE_SERVER) return }
func CreateAddServerMsg(typ string, id int32, name string, host string, port int, clienthost string, clientport int, ready bool, enableglobaldata bool) (data []byte, err error) { as := &AddApp{typ, id, name, host, port, clienthost, clientport, ready, enableglobaldata} out, e := EncodeMsg(as) if e != nil { err = e return } return util.CreateMsg(nil, out, M_ADD_SERVER) }
func CreateServerListMsg(slist []AddApp) (data []byte, err error) { si := &AppInfo{slist} out, e := EncodeMsg(si) if e != nil { err = e return } return util.CreateMsg(nil, out, M_SERVER_LIST) }
func CreateRegisterAppMsg(typ string, id int32, name string, host string, port int, clienthost string, clientport int, enableglobaldata bool) (data []byte, err error) { si := &RegisterApp{typ, id, name, host, port, clienthost, clientport, enableglobaldata} out, e := EncodeMsg(si) if e != nil { err = e return } return util.CreateMsg(nil, out, M_REGISTER_APP) }
func (mp *master_peer) Handle(id uint16, msgbody []byte) error { switch id { case share.M_ADD_SERVER: var as share.AddApp if err := share.DecodeMsg(msgbody, &as); err != nil { return err } AddApp(as.Type, as.Id, as.Name, as.Host, as.Port, as.ClientHost, as.ClientPort, as.Ready, as.EnableGlobalData) case share.M_REMOVE_SERVER: var rs share.RemoveApp if err := share.DecodeMsg(msgbody, &rs); err != nil { return err } RemoveAppById(rs.Id) case share.M_SERVER_LIST: var sl share.AppInfo if err := share.DecodeMsg(msgbody, &sl); err != nil { return err } for _, a := range sl.Apps { AddApp(a.Type, a.Id, a.Name, a.Host, a.Port, a.ClientHost, a.ClientPort, a.Ready, a.EnableGlobalData) } case share.M_HEARTBEAT: data, err := util.CreateMsg(nil, []byte{}, share.M_HEARTBEAT) if err != nil { log.LogFatalf(err) } core.noder.Send(data) case share.M_READY: var ready share.AppReady if err := share.DecodeMsg(msgbody, &ready); err != nil { return err } app := GetAppById(ready.Id) if app != nil { app.SetReady(true) } else { log.LogFatalf("app not found") } case share.M_MUSTAPPREADY: { log.LogMessage("must app ready") core.MustReady() } case share.M_SHUTDOWN: core.Closing = true close(core.exitChannel) return nil } core.Emitter.Push(MASERTINMSG, map[string]interface{}{"msg": MasterMsg{id, msgbody}}, false) return nil }
func (p *peer) Ready() error { out, err := util.CreateMsg(nil, []byte{}, share.M_READY) if err != nil { log.LogFatalf(err) } if _, err := p.rwc.Write(out); err != nil { return err } return nil }
func CreateMessage(servicemethod string, pb proto.Message) ([]byte, error) { data, err := proto.Marshal(pb) if err != nil { return nil, err } rpc := &c2s.Rpc{} rpc.Node = proto.String(".") rpc.Servicemethod = proto.String(servicemethod) rpc.Data = data data2, err := proto.Marshal(rpc) if err != nil { return nil, err } return util.CreateMsg(nil, data2, share.C2S_RPC) }
func (app *app) Close() { if !app.Shutdown { data, err := util.CreateMsg(nil, []byte{}, share.M_SHUTDOWN) if err != nil { log.LogFatalf(err) } _, err = app.conn.Write(data) if err != nil { log.LogInfo(app.id, " closed") } else { log.LogInfo(app.id, " send shutdown") } app.Shutdown = true } }
func (al *AgentList) CloseAll() { al.Lock() defer al.Unlock() data, err := util.CreateMsg(nil, []byte{}, share.M_SHUTDOWN) if err != nil { log.LogFatalf(err) } for k, agent := range al.agents { _, err = agent.rwc.Write(data) if err != nil { log.LogInfo(k, " closed") } else { log.LogInfo(k, " send shutdown") } } }
//处理服务器向客户端的调用,对消息进行封装转成客户端的协议 func (s *S2CHelper) Call(src rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { request := &share.S2CMsg{} reader := NewMessageReader(msg) if err := reader.ReadObject(request); err != nil { log.LogError(err) return 0, nil } out, err := util.CreateMsg(s.sendbuf, request.Data, share.S2C_RPC) if err != nil { log.LogError(err) return 0, nil } err = s.call(src, request.To, request.Method, out) if err != nil { log.LogError(err) } return 0, nil }
func (hd *handler) Handle(conn net.Conn) { log.LogInfo("new client: ", conn.RemoteAddr()) if !App.MustAppReady { log.LogError("must app not ready") conn.Close() return } r := &s2c.Rpc{} r.Sender = proto.String(App.Name) h, p, err := GetLogin() if err != nil { e := &s2c.Error{} e.ErrorNo = proto.Int32(share.ERROR_NOLOGIN) b, err := proto.Marshal(e) if err != nil { conn.Close() log.LogFatalf(err) } r.Servicemethod = proto.String("Login.Error") r.Data = b data, err := proto.Marshal(r) if err != nil { log.LogFatalf(err) return } out, _ := util.CreateMsg(nil, data, share.S2C_RPC) _, err = conn.Write(out) if err != nil { conn.Close() log.LogError(err) return } } else { l := &s2c.Login{} l.Host = proto.String(h) l.Port = proto.Int32(int32(p)) b, err := proto.Marshal(l) if err != nil { conn.Close() log.LogFatalf(err) } log.LogInfo("client choose login:"******":", p) r.Servicemethod = proto.String("Login.LoginInfo") r.Data = b data, err := proto.Marshal(r) if err != nil { log.LogFatalf(err) return } out, _ := util.CreateMsg(nil, data, share.S2C_RPC) _, err = conn.Write(out) if err != nil { conn.Close() log.LogError(err) return } } conn.SetReadDeadline(time.Now().Add(time.Second * 10)) conn.Read(hd.ignore[:]) log.LogMessage("client close: ", conn.RemoteAddr()) conn.Close() }