Exemplo n.º 1
0
//其他客户端连接TransferServer处理
func connectTransferServer(session *link.Session, protoMsg systemProto.ProtoMsg) {
	rev_msg := protoMsg.Body.(*systemProto.System_ConnectTransferServerC2S)

	serverName := rev_msg.GetServerName()
	INFO(serverName + " Connect TransferServer")

	useServerName := strings.Split(serverName, "[")[0]
	sessions, exists := servers[useServerName]
	if !exists {
		sessions = make([]*link.Session, 0, 10)
	}
	sessions = append(sessions, session)
	servers[useServerName] = sessions

	//GameServer可以有多个
	if useServerName == "GameServer" {
		addr := strings.Split(session.Conn().RemoteAddr().String(), ":")
		addrIp := addr[0]
		addrPort, _ := strconv.Atoi(addr[1])
		gameConsistent.Add(hashs.NewNode(len(sessions), addrIp, addrPort, serverName, 1))
	}

	send_msg := systemProto.MarshalProtoMsg(&systemProto.System_ConnectTransferServerS2C{})
	systemProto.Send(send_msg, session)

	startDealReceiveMsgC2S(session)
}
//其他客户端连接TransferServer处理
func connectTransferServer(session *link.Session, protoMsg protos.ProtoMsg) {
	rev_msg := protoMsg.Body.(*systemProto.System_ConnectTransferServerC2S)

	serverName := rev_msg.GetServerName()
	serverID := rev_msg.GetServerID()

	useServerName := strings.Split(serverName, "[")[0]
	serverList, exists := servers[useServerName]
	if !exists {
		serverList = make([]Server, 0, 10)
	}
	server := Server{
		session:     session,
		serverID:    serverID,
		serverIndex: len(serverList),
	}
	serverList = append(serverList, server)
	servers[useServerName] = serverList

	//服务器断开连接处理
	session.AddCloseCallback(session, func() {
		serverList = append(serverList[:server.serverIndex], serverList[server.serverIndex+1:]...)
		servers[useServerName] = serverList
		ERR(serverName + " Disconnect At " + global.ServerName)
	})

	//GameServer可以有多个
	if useServerName == "GameServer" {
		addr := strings.Split(session.Conn().RemoteAddr().String(), ":")
		addrIp := addr[0]
		addrPort, _ := strconv.Atoi(addr[1])
		gameNode := hashs.NewNode(server.serverIndex, addrIp, addrPort, serverName, 1)
		gameConsistent.Add(gameNode)

		//GameServer断开连接处理
		session.AddCloseCallback(session, func() {
			//移除此Node
			gameConsistent.Remove(gameNode)
			//将此Node的所有用户断开连接
			for clientSessionID, gameNodeIndex := range gameUserSessions {
				if server.serverIndex == gameNodeIndex {
					clientSession := global.GetSession(clientSessionID)
					if clientSession != nil {
						clientSession.Close()
					}
				}
			}
		})
	}

	//发送连接成功消息
	send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectTransferServerS2C{})
	protos.Send(session, send_msg)
}