示例#1
0
func DefaultClientCode(addr string, hand func(code string, v interface{}) error) func(code string, v interface{}) error {

	ro := route.NewRoute(cfg.Whole.Apps)
	cod := ro.Code()
	recod := cod.MakeReCodeMap()
	base.INFO(recod)
	c := DefaulClient(addr, func(m1 byte, m2 byte, m3 byte, b []byte) error {
		c1, c2, c3, err := cod.Map(m1, m2, m3)
		var code string
		if err != nil {
			code = fmt.Sprintf("None.%d.%d.%d", m1, m2, m3)
		} else {
			code = c1 + "." + c2 + "." + c3
		}
		es := base.NewEncodeBytes(b)
		var r interface{}
		es.DeJson(&r)
		return hand(code, r)
	})
	return func(code string, v interface{}) error {
		m1, m2, m3, err := recod.Map(code)
		if err != nil {
			base.ERR(err)
			return err
		}
		es := base.EnJson(v)
		return c(m1, m2, m3, es.Bytes())
	}
}
示例#2
0
func DefaulAgent() *agent.Agent {
	ro := route.NewRoute(cfg.Whole.Apps)
	code := ro.Code()
	recode := code.MakeReCodeMap()
	dj := base.EnJson(DefCfg{
		CodeMaps: code,
		Agents:   cfg.Whole.Agents,
	}).Bytes()
	base.INFO(recode)
	ag := agent.NewAgent(1024, func(user *agent.User, msg []byte) (err error) {

		//defer base.PanicErr(&err)
		var reply agent.Response
		user.SetDeadline(time.Now().Add(time.Second * 60 * 60))
		if msg[0] == 255 && msg[1] == 255 && msg[2] == 255 && msg[3] == 255 {
			return user.WriteMsg(append(msg[:4], dj...))
		}

		err = ro.CallCode(msg[1], msg[2], msg[3], agent.Request{
			Session: user.Session,
			Request: base.NewEncodeBytes(msg[4:]),
			Head:    msg[:4],
		}, &reply)
		//base.INFO(string(reply.Session.Rooms.Bytes()))
		return reply.Hand(user, msg[:4])
	}, func(user *agent.User) {
		for _, v := range *code {
			sess := &user.Session
			ro.Call(v.Name, "Leave", "User", sess, nil)
		}
	})
	return ag
}