func TestLoginMsg(t *testing.T) { loginReq := &Login_Req{} loginReq.Packet = packet.NewEmptyPacket() loginReq.CmdID = 0x01 loginReq.UserName = "******" loginReq.PWD = "1234" loginReq.e.a = 10 loginReq.e.b = 20 loginReq.eList = append(loginReq.eList, loginReq.e) loginReq.eList = append(loginReq.eList, loginReq.e) buf := loginReq.EncodePacket(0) fmt.Println("login=>", loginReq) fmt.Println("buf =>", buf.GetData()) pack := packet.Packing(buf.GetData()) fmt.Println("cbuf=>", pack.GetClientFromRawData()) loginReq2 := &Login_Req{} loginReq2.Packet = packet.Packing(buf.GetData()) fmt.Println("packing=>", loginReq2.Packet) loginReq2.DecodePacket() fmt.Println("login2 =>", loginReq2) }
func (rule *LsForwardRule) FowardToClient(cmdID uint16, pack *packet.Packet, conn netio.ConnInf) { clientID := pack.ID var client *ProxyClient if cmdID <= protocol_login.CMD_C_LOGIN_VALID_RESP { client = ClientManagerInst.FindClientByConnID(clientID) } else { client = ClientManagerInst.FindClientByUserID(clientID) } if client == nil { ProxyLog.Error("LS msg can't find physical conn,conn_id = ", clientID, " cmd_id = ", cmdID) return } switch cmdID { case protocol_proxy.CMD_C_P_USER_OFFLINE_REQ: client.GetPhysicalLink().Close() case protocol_login.CMD_C_LOGIN_VALID_RESP: if pack.ErrCode == 0 { loginValid := &protocol_login.LoginValidResp{} loginValid.Packet = pack if loginValid.DecodePacket() { client.GetPhysicalLink().SetBsStatus(BS_STATUS_AUTHED) client.GetPhysicalLink().SetUID(loginValid.UserId) oldClient := ClientManagerInst.ChangeClientFromConnIDtoUserID(client) if oldClient != nil && oldClient.GetPhysicalLink() != nil { oldClient.GetPhysicalLink().Close() } } else { loginValid.Packet = packet.NewEmptyPacket() loginValid.ErrCode = PROXY_FORWARD_DECODE_ERR loginValid.CmdID = protocol_login.CMD_C_LOGIN_VALID_RESP buf := loginValid.EncodePacket(128) client.GetPhysicalLink().Write(buf.GetData()) } } else { client.GetPhysicalLink().Write(pack.GetClientFromRawData()) } default: client.GetPhysicalLink().Write(pack.GetClientFromRawData()) } }
//包内通用函数 func errorResponse(cmdId uint16, errCode uint16, userId uint32, conn netio.ConnInf) { if conn == nil { ProxyLog.Error("send error msg error,physical link is nil,req-cmd-id = ", cmdId) return } resp := &protocol_comm.ServerErrorNt{} resp.Packet = packet.NewEmptyPacket() resp.CmdID = protocol_comm.CMD_S_C_ERROR_NT resp.ID = userId resp.FSID = NODE_TYPE_PROXY resp.ReqCmdID = cmdId resp.ErrCode = errCode buf := resp.EncodePacket(64) conn.Write(buf.GetData()) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU() - 1) //创建N个客户端 clientList := make(map[int]*MyClient) for i := 0; i < 3000; i++ { go func() { c1 := NewMyClient() c1.SetPackDispatcher(netio.NewDefaultPackDispatcher()) c1.SetPackParser(netio.NewDefaultParser()) c1.SetServerAddress("192.168.15.26") c1.SetServerPort(12000) c1.GetPackDispatcher().AddPackEventListener(strconv.Itoa(i+1), c1) err := c1.Connect() //启动客户端,看乒乓能不能打起来,哈哈! if err != nil { fmt.Println("client error,", err) } clientList[i+1] = c1 login := protocol_login.Login_Req{} login.Packet = packet.NewEmptyPacket() login.CmdID = 0x01 login.UserName = "******" login.PWD = "1q2323" buf := login.EncodePacket(512) time.Sleep(20 * time.Millisecond) //fmt.Println("client send data :", buf.GetData()) for { time.Sleep(time.Millisecond * 500) c1.Write(buf.GetData()) } }() } console.CheckInput() }
//收到来自各服务节点的消息 func (n *NodeService) HandleMsg(cmdID uint16, pack *packet.Packet, conn netio.ConnInf) { if conn == nil { panic("NodeService.HandleMsg error,conn is nil ") } switch conn.GetBsStatus() { case 0, BS_STATUS_OPENED: //需要先判断节点是已经通过了验证 if protocol_proxy.CMD_S_P_REG_REQ == cmdID { node := NewServerNode() var regReq protocol_proxy.NodeRegReq regReq.Packet = pack if !regReq.DecodePacket() { ProxyLog.Error("invalid CMD_S_S_REG_REQ,Packet decode failed") conn.Close() } else { node.NodeType = regReq.NodeType node.GameId = regReq.GameId node.GameAreaId = regReq.GameAreaId node.GameServerId = regReq.GameServerId node.GameServerName = regReq.GameServerName node.GameServerDesc = regReq.GameServerDesc node.Ip = conn.GetRemoteIp() nRetCode := NodeManagerInst.RegNodeConnection(node) if 0 == nRetCode { node.SetPhysicalLink(conn) conn.SetBsStatus(BS_STATUS_AUTHED) } else { ProxyLog.Info("server node register failed, ip = ", conn.GetRemoteIp(), " NodeType = ", node.NodeType, " IP = ", node.Ip, " errcode = ", nRetCode) } resp := protocol_proxy.NodeRegResp{} resp.Packet = packet.NewEmptyPacket() resp.Code = uint16(nRetCode) resp.CmdID = protocol_proxy.CMD_S_P_REG_RESP buf := resp.EncodePacket(256) conn.Write(buf.GetData()) } } else { //不合法的请求,将其关闭 ProxyLog.Error("invalid request ,cmdid = ", cmdID, " before server node is authed.") conn.Close() } case BS_STATUS_AUTHED: //请进入转发模式 if pack.FSID == NODE_TYPE_LS { lsForwarderInst.FowardToClient(cmdID, pack, conn) } else if pack.FSID == NODE_TYPE_HS { hsForwarderInst.FowardToClient(cmdID, pack, conn) } else if pack.FSID == NODE_TYPE_GS { gsForwarderInst.FowardToClient(cmdID, pack, conn) } else { ProxyLog.Error("unknow server node type :", pack.FSID) } //fowardrule::: TODO default: ProxyLog.Error("unknown server status : ", conn.GetBsStatus()) } }