Exemple #1
0
func (c *Client) Start() {
	log.LogMessage("client start:", c.id)
	conn, err := net.Dial("tcp", "172.30.9.101:5391")
	if err != nil {
		log.LogError(err)
		return
	}

	c.buf = make([]byte, 1024*16)

	mid, data, err := util.ReadPkg(conn, c.buf)
	if err != nil {
		log.LogError(err)
		log.LogError("quit client ", c.id)
		return
	}

	l := &s2c.Login{}
	err = ParseProto(mid, data, l)
	if err != nil {
		log.LogError(err)
		log.LogError("quit client ", c.id)
		return
	}

	conn.Close()
	c.Login(l.GetHost(), l.GetPort())
}
Exemple #2
0
func (app *app) Loop() {
	app.exit = make(chan int)
	go app.Check()
	buffer := make([]byte, 2048)
	for {
		id, body, err := util.ReadPkg(app.conn, buffer)
		if err != nil {
			break
		}

		if err := app.Handle(id, body); err != nil {
			log.LogError(err)
			break
		}
	}
	RemoveApp(app.id)
}
Exemple #3
0
func (c *Client) EnterGame(conn io.ReadWriteCloser) {
	log.LogMessage("client entergame:", c.id)
	co := &s2c.CreateObject{}
	mid, data, err := util.ReadPkg(conn, c.buf)
	if err != nil {
		log.LogError(err)
		log.LogError("quit client ", c.id)
		conn.Close()
		return
	}
	err = ParseProto(mid, data, co)
	if err != nil {
		log.LogError(err)
		conn.Close()
		return
	}

	time.Sleep(time.Second * 60)
}
Exemple #4
0
func (a *Agent) readloop() {
	buffer := make([]byte, 2048)
	for !a.quit {
		id, msg, err := util.ReadPkg(a.rwc, buffer)
		if err != nil {
			if err != io.EOF && !strings.Contains(err.Error(), "use of closed network connection") {
				log.LogError(err)
			}
			break
		}

		if err := a.Handle(id, msg); err != nil {
			log.LogError(err)
			break
		}
	}

	log.LogInfo("agent pipe quit")
	a.Close()
}
Exemple #5
0
func (c *Client) Login(h string, p int32) {
	log.LogMessage("client login:"******"tcp", fmt.Sprintf("%s:%d", h, p))
	if err != nil {
		log.LogError(err)
		return
	}

	l := &c2s.Loginuser{}
	l.User = proto.String(fmt.Sprintf("test%d", c.id))
	l.Password = proto.String("123")

	c.user = *l.User
	data, err := CreateMessage("Account.Login", l)
	if err != nil {
		log.LogError(err)
		conn.Close()
		return
	}

	conn.Write(data)
	mid, data, err := util.ReadPkg(conn, c.buf)
	if err != nil {
		log.LogError(err)
		log.LogError("quit client ", c.id)
		conn.Close()
		return
	}

	ls := &s2c.Loginsucceed{}
	err = ParseProto(mid, data, ls)
	if err != nil {
		log.LogError(err)
		log.LogError("quit client ", c.id)
		conn.Close()
		return
	}

	conn.Close()
	c.LoginBase(ls.GetHost(), ls.GetPort(), ls.GetKey())
}
Exemple #6
0
func (a *AgentNode) readloop() {
	buffer := make([]byte, 2048)
	for !a.quit {
		id, msg, err := util.ReadPkg(a.rwc, buffer)
		if err != nil {
			if err != io.EOF && !strings.Contains(err.Error(), "use of closed network connection") {
				log.LogError(err)
			}
			break
		}

		if err := a.HandleMsg(id, msg); err != nil {
			log.LogError(err)
			break
		}
	}

	log.LogInfo("agent node closed")
	a.Close()
	context.agentlist.RemoveAgent(a.id)
}
Exemple #7
0
func (tcp *tcp_server) Handle(clientconn net.Conn) {

	buff := make([]byte, 2048)
	id, msgbody, err := util.ReadPkg(clientconn, buff)
	if err != nil {
		log.LogError(err)
		clientconn.Close()
		return
	}

	switch id {
	case share.M_REGISTER_APP:
		var reg share.RegisterApp
		if err := share.DecodeMsg(msgbody, &reg); err != nil {
			clientconn.Close()
			log.LogFatalf(err)
		}
		app := &app{typ: reg.Type, id: reg.Id, name: reg.Name, conn: clientconn, host: reg.Host, port: reg.Port, clienthost: reg.ClientHost, clientport: reg.ClientPort, enableglobaldata: reg.EnableGlobalData}
		log.LogMessage(app.id, ":", app.conn.RemoteAddr().String())
		app.SendList()
		AddApp(app)
		app.Loop()
	case share.M_REGISTER_AGENT:
		var reg share.RegisterAgent
		if err := share.DecodeMsg(msgbody, &reg); err != nil {
			clientconn.Close()
			log.LogFatalf(err)
		}

		agent := &AgentNode{id: reg.AgentId, nobalance: reg.NoBalance}
		agent.Handle(clientconn)
		context.agentlist.AddAgent(agent)
		log.LogMessage("agent add:", reg.AgentId)
	default:
		log.LogError("first message must reg app")
		return
	}

}
Exemple #8
0
func (c *ClientCodec) ReadRequest(maxrc uint16) (*rpc.Message, error) {
	for {
		id, data, err := util.ReadPkg(c.rwc, c.cachebuf)
		if err != nil {
			return nil, err
		}
		switch id {
		case share.C2S_PING:
			c.node.Ping()
			break
		case share.C2S_RPC:
			msg := rpc.NewMessage(len(data))
			ar := NewHeadWriter(msg)
			ar.Write(uint64(0))
			ar.Write(c.node.MailBox.Uid)
			ar.Write("C2SC2SHelper.Call")
			msg.Header = msg.Header[:ar.Len()]
			msg.Body = append(msg.Body, data...)
			return msg, nil
		}
	}
}
Exemple #9
0
func (p *peer) readloop() {
	buffer := make([]byte, 2048)
	for !core.quit {
		id, msg, err := util.ReadPkg(p.rwc, buffer)
		if err != nil {
			if err != io.EOF && !strings.Contains(err.Error(), "use of closed network connection") {
				log.LogError(err)
			}
			break
		}

		if p.h != nil {
			if err := p.h.Handle(id, msg); err != nil {
				log.LogError(err)
				break
			}
		}
	}

	log.LogInfo("peer node closed")
	p.Close()
}
Exemple #10
0
func (c *Client) LoginBase(h string, p int32, key int32) {
	log.LogMessage("client loginbase:", c.id)
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", h, p))
	if err != nil {
		log.LogError(err)
		return
	}

	base := &c2s.Enterbase{}
	base.User = proto.String(c.user)
	base.Key = proto.Int32(key)

	data, err := CreateMessage("Account.Login", base)
	if err != nil {
		log.LogError(err)
		conn.Close()
		return
	}

	conn.Write(data)

	for {
		mid, data, err := util.ReadPkg(conn, c.buf)
		if err != nil {
			log.LogError(err)
			log.LogError("quit client ", c.id)
			conn.Close()
			return
		}

		roleinfo := &s2c.RoleInfo{}
		err = ParseProto(mid, data, roleinfo)
		if err != nil {
			log.LogError(err)
			conn.Close()
			return
		}

		if len(roleinfo.UserInfo) > 0 {
			s := &c2s.Selectuser{}
			s.Rolename = roleinfo.UserInfo[0].Name
			s.Roleindex = roleinfo.UserInfo[0].Index
			data, err := CreateMessage("Account.SelectUser", s)
			if err != nil {
				log.LogError(err)
				conn.Close()
				return
			}

			conn.Write(data)
			break
		} else {
			create := &c2s.Create{}
			create.Name = proto.String(c.user)
			create.Index = proto.Int32(1)
			create.Sex = proto.Int32(1)
			create.Roleid = proto.Int32(1)
			data, err := CreateMessage("Account.CreatePlayer", create)
			if err != nil {
				log.LogError(err)
				conn.Close()
				return
			}
			conn.Write(data)
		}
	}

	c.EnterGame(conn)
}