Skip to content

Commit 097b5a3

Browse files
author
Evan Hu
committed
app gateway
1 parent 3d4c8db commit 097b5a3

File tree

83 files changed

+60205
-952
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+60205
-952
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
with Licensor regarding such Contributions.
137137

138138
6. Trademarks. This License does not grant permission to use the trade
139-
names, trademarks, service marks, or product names of the Licensor,
139+
names, trademarks, gameService marks, or product names of the Licensor,
140140
except as required for reasonable and customary use in describing the
141141
origin of the Work and reproducing the content of the NOTICE file.
142142

game-gate/pom.xml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>game-project</artifactId>
7+
<groupId>info.xiaomo</groupId>
8+
<version>2019.1</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>game-gate</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>info.xiaomo</groupId>
22+
<artifactId>gameCore</artifactId>
23+
<version>2.0.7</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>info.xiaomo</groupId>
27+
<artifactId>game-protocol</artifactId>
28+
<version>2019.1</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package info.xiaomo.server.gate;
2+
3+
import java.io.File;
4+
import info.xiaomo.core.common.utils.FileUtil;
5+
import info.xiaomo.core.persist.redis.jedis.JedisClusterConfig;
6+
import info.xiaomo.core.persist.redis.jedis.JedisManager;
7+
import info.xiaomo.core.script.ScriptManager;
8+
import info.xiaomo.server.gate.manager.MongoManager;
9+
import info.xiaomo.server.gate.server.GateServer;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public final class AppGate {
14+
15+
private static final Logger LOGGER = LoggerFactory.getLogger(AppGate.class);
16+
17+
private static String configPath;
18+
static JedisManager redisManager;
19+
private static GateServer gateServer;
20+
21+
private AppGate() {}
22+
23+
public static void main(String[] args) {
24+
initConfigPath();
25+
26+
// redis
27+
JedisClusterConfig jedisClusterConfig =
28+
FileUtil.getConfigXML(configPath, "jedisClusterConfig.xml", JedisClusterConfig.class);
29+
if (jedisClusterConfig == null) {
30+
LOGGER.error("redis配置{}未找到", configPath);
31+
System.exit(1);
32+
}
33+
redisManager = new JedisManager(jedisClusterConfig);
34+
35+
// 创建mongodb连接
36+
MongoManager.getInstance().createConnect(configPath);
37+
38+
// 加载脚本
39+
ScriptManager.getInstance().init(null);
40+
41+
// 通信服务
42+
gateServer = new GateServer();
43+
new Thread(gateServer).start();
44+
}
45+
46+
private static void initConfigPath() {
47+
File file = new File(System.getProperty("user.dir"));
48+
if ("target".equals(file.getName())) {
49+
configPath = file.getPath() + File.separatorChar + "config";
50+
} else {
51+
configPath = file.getPath() + File.separatorChar + "target" + File.separatorChar + "config";
52+
}
53+
LOGGER.info("配置路径为:" + configPath);
54+
}
55+
56+
public static String getConfigPath() {
57+
return configPath;
58+
}
59+
60+
public static GateServer getHallServer() {
61+
return gateServer;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package info.xiaomo.server.gate.manager;
7+
8+
import info.xiaomo.core.persist.mongo.AbsMongoManager;
9+
import info.xiaomo.core.persist.mongo.dao.HallInfoDao;
10+
import info.xiaomo.core.persist.mongo.dao.RoleDao;
11+
import info.xiaomo.core.persist.mongo.dao.UserDao;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* mongodb
17+
*
18+
*
19+
*
20+
*/
21+
public class MongoManager extends AbsMongoManager {
22+
23+
private static final Logger LOGGER = LoggerFactory.getLogger(MongoManager.class);
24+
private static final MongoManager INSTANCE_MANAGER = new MongoManager();
25+
26+
private MongoManager() {
27+
28+
}
29+
30+
public static MongoManager getInstance() {
31+
return INSTANCE_MANAGER;
32+
}
33+
34+
@Override
35+
protected void initDao() {
36+
HallInfoDao.getDB(INSTANCE_MANAGER);
37+
UserDao.getDB(INSTANCE_MANAGER);
38+
RoleDao.getDB(INSTANCE_MANAGER);
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package info.xiaomo.server.gate.manager;
2+
3+
import info.xiaomo.core.server.ServerInfo;
4+
import info.xiaomo.core.server.ServerState;
5+
import info.xiaomo.core.server.ServerType;
6+
import info.xiaomo.server.gate.struct.UserSession;
7+
import info.xiaomo.server.protocol.ServerMessage;
8+
import info.xiaomo.server.protocol.system.SystemMessage.SystemErroCode;
9+
import info.xiaomo.server.protocol.system.SystemMessage.SystemErrorResponse;
10+
import java.util.Map;
11+
import java.util.Optional;
12+
import java.util.concurrent.ConcurrentHashMap;
13+
import io.netty.util.internal.StringUtil;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
/**
18+
* 服务器管理类
19+
*
20+
*
21+
* 2017年6月30日 下午5:49:38
22+
*/
23+
public class ServerManager {
24+
private static final Logger LOGGER = LoggerFactory.getLogger(ServerManager.class);
25+
private static volatile ServerManager serverManager;
26+
27+
/** 服务器列表 */
28+
private final Map<ServerType, Map<Integer, ServerInfo>> serverMap = new ConcurrentHashMap<>();
29+
30+
private ServerManager() {
31+
32+
}
33+
34+
public static ServerManager getInstance() {
35+
if (serverManager == null) {
36+
synchronized (ServerManager.class) {
37+
if (serverManager == null) {
38+
serverManager = new ServerManager();
39+
}
40+
}
41+
}
42+
return serverManager;
43+
}
44+
45+
/**
46+
* 获取游戏信息
47+
*
48+
* @param serverType
49+
* @param serverId
50+
* @return
51+
*/
52+
public ServerInfo getGameServerInfo(ServerType serverType, int serverId) {
53+
if (serverMap.containsKey(serverType)) {
54+
return serverMap.get(serverType).get(serverId);
55+
}
56+
return null;
57+
}
58+
59+
/**
60+
* 更新服务器信息
61+
*
62+
* @param info
63+
*/
64+
public void updateServerInfo(ServerMessage.ServerInfo info) {
65+
if (info.getType() < 100 && info.getType() != ServerType.HALL.ordinal()) { // 游戏服从101开始定义
66+
// 大厅服需要更新
67+
return;
68+
}
69+
ServerType serverType = ServerType.valueof(info.getType());
70+
ServerInfo server = getGameServerInfo(serverType, info.getId());
71+
if (server == null) {
72+
server = new ServerInfo();
73+
server.setId(info.getId());
74+
}
75+
server.setIp(info.getIp());
76+
server.setPort(info.getPort());
77+
server.setOnline(info.getOnline());
78+
server.setMaxUserCount(info.getMaxUserCount());
79+
server.setName(info.getName());
80+
server.setHttpPort(server.getHttpPort());
81+
server.setState(info.getState());
82+
server.setType(info.getType());
83+
server.setWwwip(info.getWwwip());
84+
85+
if (!serverMap.containsKey(serverType)) {
86+
serverMap.put(serverType, new ConcurrentHashMap<>());
87+
}
88+
89+
serverMap.get(serverType).put(info.getId(), server);
90+
// LOGGER.warn("游戏服务器{}注册更新到网关服务器", server.toString());
91+
}
92+
93+
/**
94+
* 获取空闲的游戏服务器
95+
*
96+
* @param serverType
97+
* @return
98+
*/
99+
public ServerInfo getIdleGameServer(ServerType serverType, UserSession userSession) {
100+
Map<Integer, ServerInfo> map = serverMap.get(serverType);
101+
if (map == null || map.size() == 0) {
102+
return null;
103+
}
104+
Optional<ServerInfo> findFirst = map.values().stream()
105+
.filter(server -> StringUtil.isNullOrEmpty(userSession.getVersion())
106+
|| userSession.getVersion().equals(server.getVersion())) // 版本号
107+
.filter(server -> server.getState() == ServerState.NORMAL.ordinal()) // 状态
108+
.sorted((s1, s2) -> s1.getOnline() - s2.getOnline()).findFirst();
109+
return findFirst.orElse(null);
110+
}
111+
112+
/**
113+
* 构建错误信息
114+
*
115+
*
116+
* 2017年7月21日 上午10:37:49
117+
* @param erroCode
118+
* @param msg
119+
* @return
120+
*/
121+
public SystemErrorResponse buildSystemErrorResponse(SystemErroCode erroCode, String msg) {
122+
SystemErrorResponse.Builder builder = SystemErrorResponse.newBuilder();
123+
builder.setErrorCode(erroCode);
124+
if (msg != null) {
125+
builder.setMsg(msg);
126+
}
127+
return builder.build();
128+
}
129+
130+
}

0 commit comments

Comments
 (0)