Skip to content

Commit ed3a77a

Browse files
committed
[V1.1]最基本的基础版本只能简单处理一些数据
1 parent d152e6f commit ed3a77a

18 files changed

Lines changed: 157 additions & 95 deletions

File tree

‎.idea/encodings.xml‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Ginx 是一个基于Java的轻量级并发服务器框架,模仿Golang中的Zi
88

99
## 一、写在前面的话
1010
为什么我要开发这款Java服务器,目前Java在服务器方面的框架已经很丰富了,但没有一个面向游戏的简单清亮的服务器,开发这个框架初衷是想做一个简单的TCP游戏服务器,可能后续会加更多功能。这个服务器是模仿Golang的一个叫Zinx的服务器,很多API与他一致。
11-
可能和Zinx不会太一致,欢迎大家一起来维护。
11+
因为作者实力有限,所以可能和Zinx不会太一致,欢迎大家一起来维护与开发。
1212

1313
## 二、版本要求
1414
### 1. Java:17

‎pom.xml‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
<groupId>org.apache.logging.log4j</groupId>
2020
<artifactId>log4j-core</artifactId>
2121
<version>${log4j2.version}</version>
22+
<scope>provided</scope>
2223
</dependency>
2324
<dependency>
2425
<groupId>org.apache.logging.log4j</groupId>
2526
<artifactId>log4j-api</artifactId>
2627
<version>${log4j2.version}</version>
28+
<scope>provided</scope>
2729
</dependency>
2830
<dependency>
2931
<groupId>org.projectlombok</groupId>
@@ -51,7 +53,7 @@
5153
</dependencies>
5254
<build>
5355
<plugins>
54-
56+
<!--打包Test出错跳过-->
5557
<plugin>
5658
<groupId>org.springframework.boot</groupId>
5759
<artifactId>spring-boot-maven-plugin</artifactId>
@@ -69,6 +71,23 @@
6971
</execution>
7072
</executions>
7173
</plugin>
74+
75+
<!-- 打包包含源码的jar -->
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-surefire-plugin</artifactId>
79+
<configuration>
80+
<testFailureIgnore>true</testFailureIgnore>
81+
</configuration>
82+
</plugin>
83+
84+
7285
</plugins>
86+
<resources>
87+
<resource>
88+
<directory>src/test/resources</directory>
89+
<filtering>true</filtering>
90+
</resource>
91+
</resources>
7392
</build>
7493
</project>

‎src/main/java/com/modulecode/net/IMsgHandle.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.modulecode.net;
22

3+
import lombok.ToString;
4+
35
import java.io.IOException;
46

57
/**
68
* 提供worker启动、处理消息业务调用等接口
79
*/
10+
811
public interface IMsgHandle {
912
int getHandlerCount();
1013

‎src/main/java/com/modulecode/net/IRequest.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ public interface IRequest {
1414
int getMsgID(); //获取请求的消息ID
1515

1616
String getString(String charsetName) throws UnsupportedEncodingException; //获取请求消息字符串
17-
//
17+
18+
<T> T convertClass( Class<T> clazz) throws IOException;
19+
20+
<T> T convertClass( String charsetName, Class<T> clazz) throws IOException;
21+
1822
// void bindRouter(IRouter router); //绑定这次请求由哪个路由处理
1923
//
2024
// void next(); //转进到下一个处理器开始执行 但是调用此方法的函数会根据先后顺序逆序执行

‎src/main/java/com/modulecode/net/impl/Connection.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ private void startReader() throws IOException {
5454
if (this.dataPack == null) {
5555
this.dataPack = new DataPack();
5656
}
57+
5758
//从路由,找到绑定的对应 router
5859
IDataPack dataPack = this.dataPack;
5960
IMessage iMessage = dataPack.unPack(dataInputStream);

‎src/main/java/com/modulecode/net/impl/MsgHandle.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import com.modulecode.net.IMsgHandle;
44
import com.modulecode.net.IRequest;
55
import com.modulecode.net.IRouter;
6+
import lombok.ToString;
67
import org.apache.logging.log4j.LogManager;
78
import org.apache.logging.log4j.Logger;
89

910
import java.io.IOException;
1011
import java.util.HashMap;
1112

13+
@ToString
1214
public class MsgHandle implements IMsgHandle {
1315
private static final Logger logger = LogManager.getLogger(MsgHandle.class);
1416
HashMap<Integer, IRouter> routers = new HashMap<>();
@@ -22,17 +24,20 @@ public int getHandlerCount() {
2224
public void doMsgHandler(IRequest request) throws IOException {
2325
int msgID = request.getMsgID();
2426
IRouter iRouter = routers.get(msgID);
25-
iRouter.preHandle(request);
26-
iRouter.handle(request);
27-
iRouter.postHandle(request);
27+
if (iRouter != null) {
28+
iRouter.preHandle(request);
29+
iRouter.handle(request);
30+
iRouter.postHandle(request);
31+
}
2832
}
2933

3034
@Override
3135
public void addRouter(int msgID, IRouter router) {
36+
3237
IRouter iRouter = this.routers.get(msgID);
3338
if (iRouter == null) {
3439
routers.put(msgID, router);
35-
// logger.info("接口 {} 添加成功!", msgID, iRouter);
40+
logger.info("接口 {} 添加成功!", router);
3641
return;
3742
}
3843
logger.warn("接口 {} 已经被注册!", msgID);

‎src/main/java/com/modulecode/net/impl/Request.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import com.modulecode.net.IConnection;
44
import com.modulecode.net.IMessage;
55
import com.modulecode.net.IRequest;
6-
import com.modulecode.net.IRouter;
6+
import com.modulecode.utils.JacksonUtils;
77

8+
import java.io.IOException;
89
import java.io.UnsupportedEncodingException;
910

1011

@@ -39,6 +40,21 @@ public String getString(String charsetName) throws UnsupportedEncodingException
3940
return new String(this.message.getData(), charsetName);
4041
}
4142

43+
// @Override
44+
// public <T> T convertClass(Class<T> clazz) throws IOException {
45+
// return convertClass(getString("utf-8"), clazz);
46+
// }
47+
48+
@Override
49+
public <T> T convertClass(String charsetName, Class<T> clazz) throws IOException {
50+
return convertClass(getString(charsetName), clazz);
51+
}
52+
53+
@Override
54+
public <T> T convertClass(Class<T> clazz) throws IOException {
55+
return JacksonUtils.json2Bean(getString("utf-8"), clazz);
56+
}
57+
4258
//获取数据
4359

4460
}

‎src/main/java/com/modulecode/net/impl/Server.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Server(String configUrl) {
5454
@Override
5555
public void start() {
5656
int totalHandler = this.msgHandler.getHandlerCount();
57-
logger.info("[HANDLERS] Now {} register {} handlers", ginxConfig.getName(), totalHandler);
57+
logger.info("[HANDLERS] Now {} register {} handlers:{}", ginxConfig.getName(), totalHandler, msgHandler);
5858
logger.info("[START] Server name: {},listenner at IP: {}, Port {} is starting", ginxConfig.getName(), ginxConfig.getHost(), ginxConfig.getTcpPort());
5959
var cid = 0;
6060
try {

‎src/main/java/com/modulecode/utils/Global.java‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,9 @@ public Global(String host, int tcpPort, String name, String version, int maxconn
3030
}
3131

3232
public GinxConfig reloadGinxConfig(String url) {
33-
34-
java.net.URL uri = this.getClass().getResource(url);
35-
if (uri == null) {
36-
return null;
37-
}
3833
try {
39-
File file = new File(uri.toURI());
40-
FileInputStream fileInputStream = new FileInputStream(file);
41-
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
34+
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(url);
35+
DataInputStream dataInputStream = new DataInputStream(resourceAsStream);
4236
byte[] bytes = dataInputStream.readAllBytes();
4337
GinxConfig ginxConfig = JacksonUtils.json2Bean(new String(bytes, "utf-8"), GinxConfig.class);
4438
ginxConfig.setConfigUrl(url);
@@ -49,12 +43,10 @@ public GinxConfig reloadGinxConfig(String url) {
4943
throw new RuntimeException(e);
5044
} catch (IOException e) {
5145
throw new RuntimeException(e);
52-
} catch (URISyntaxException e) {
53-
throw new RuntimeException(e);
5446
}
5547
}
5648

5749
public GinxConfig reloadGinxConfig() {
58-
return this.reloadGinxConfig("/ginx.json");
50+
return this.reloadGinxConfig("ginx.json");
5951
}
6052
}

0 commit comments

Comments
 (0)