Skip to content

Commit 2942fc9

Browse files
author
magiclu550
committed
[commit] something...
1 parent 4b5785f commit 2942fc9

23 files changed

+174
-8555
lines changed

.idea/libraries/Maven__io_netty_netty_all_5_0_0_Alpha1.xml

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

JPLS/JPLS.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</content>
2626
<orderEntry type="inheritedJdk" />
2727
<orderEntry type="sourceFolder" forTests="false" />
28+
<orderEntry type="library" name="Maven: io.netty:netty-all:5.0.0.Alpha1" level="project" />
2829
<orderEntry type="library" name="Maven: cn.jsmod2:jsmod2-iapi:1.0.3" level="project" />
2930
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
3031
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />

JPLS/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112

113113

114114
<dependencies>
115+
<dependency>
116+
<groupId>io.netty</groupId>
117+
<artifactId>netty-all</artifactId>
118+
<version>5.0.0.Alpha1</version>
119+
</dependency>
115120
<dependency>
116121
<groupId>cn.jsmod2</groupId>
117122
<artifactId>jsmod2-iapi</artifactId>

JPLS/src/main/java/cn/jsmod2/Register.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public void registerPacket(){
183183

184184
public static final String CONSOLE_LOG = "multi-admin.log";
185185

186+
public static final String CLIENT_PORT = "client.port";
187+
188+
public static final String START_NETTY_SERVER = "client.server.start";
189+
186190
@RegisterMethod
187191
public void registerSuccessInfo(){
188192
successInfo.add("start.finish");
@@ -195,6 +199,8 @@ public void registerServerProperties(){
195199
serverProperties.put(FileSystem.SMOD2_LOG_INTERVAL,"2000");
196200
serverProperties.put(DownloadPluginCommand.MIRROR,"");
197201
serverProperties.put(CONSOLE_LOG,"");
202+
serverProperties.put(START_NETTY_SERVER,"true");
203+
serverProperties.put(CLIENT_PORT,"20020");
198204
}
199205

200206

JPLS/src/main/java/cn/jsmod2/core/Server.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import cn.jsmod2.core.utils.Result;
2929
import cn.jsmod2.core.utils.Utils;
3030
import cn.jsmod2.core.schedule.Scheduler;
31+
import cn.jsmod2.panel.NettyServer;
3132
import org.fusesource.jansi.Ansi;
3233
import jline.console.ConsoleReader;
3334

@@ -174,8 +175,11 @@ public Server(GameServer gServer,boolean useUDP) {
174175

175176
try {
176177
this.chooseLangOrStart();
177-
scheduler.executeRunnable(()->Utils.TryCatch(this::startConsoleCommand));
178178

179+
scheduler.executeRunnable(()->Utils.TryCatch(this::startConsoleCommand));
180+
if(Boolean.parseBoolean(serverProp.getProperty(Register.START_NETTY_SERVER,"true"))) {
181+
scheduler.executeRunnable(new NettyServer());
182+
}
179183
ServerSocket socket = new ServerSocket(20003);
180184

181185
socket.accept();

JPLS/src/main/java/cn/jsmod2/core/log/ServerLogger.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
public class ServerLogger implements ILogger{
4747
private ConsoleOutputStream consoleOutputStream = new ConsoleOutputStream();
48-
@Deprecated
48+
4949
private BlockingQueue<String> queue = new LinkedBlockingQueue<>();
5050

5151
private static ServerLogger log;
@@ -156,7 +156,7 @@ public void error(String message, String prefix, String suffix) {
156156
consoleOutputStream.write(getSimpleMessage(msg));
157157
} catch (IOException ignored) {
158158
}
159-
// queue.offer(msg);
159+
queue.offer(msg);
160160
logger.error(msg);
161161
}
162162

@@ -167,7 +167,7 @@ public void info(String message, String prefix, String suffix) {
167167
consoleOutputStream.write(getSimpleMessage(msg));
168168
} catch (IOException ignored) {
169169
}
170-
// queue.offer(msg);
170+
queue.offer(msg);
171171
logger.info(msg);
172172
}
173173

@@ -177,16 +177,15 @@ public void warn(String message, String prefix, String suffix) {
177177
try {
178178
consoleOutputStream.write(getSimpleMessage(msg));
179179
} catch (IOException ignored) {
180-
}
181-
// queue.offer(msg);
180+
}queue.offer(msg);
182181
logger.warn(msg);
183182
}
184183

185184
public static ServerLogger getLogger() {
186185
return log;
187186
}
188187

189-
@Deprecated
188+
190189
public BlockingQueue<String> getQueue() {
191190
return queue;
192191
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package cn.jsmod2.panel;
2+
3+
4+
import cn.jsmod2.Register;
5+
import cn.jsmod2.core.Console;
6+
import cn.jsmod2.core.Server;
7+
import cn.jsmod2.core.log.ServerLogger;
8+
import cn.jsmod2.core.utils.Utils;
9+
import com.alibaba.fastjson.JSON;
10+
import io.netty.bootstrap.ServerBootstrap;
11+
import io.netty.channel.ChannelFuture;
12+
import io.netty.channel.ChannelHandlerAdapter;
13+
import io.netty.channel.ChannelHandlerContext;
14+
import io.netty.channel.ChannelInitializer;
15+
import io.netty.channel.ChannelOption;
16+
import io.netty.channel.nio.NioEventLoopGroup;
17+
import io.netty.channel.socket.SocketChannel;
18+
import io.netty.channel.socket.nio.NioServerSocketChannel;
19+
import io.netty.handler.codec.string.StringDecoder;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
25+
class ServerHandler extends ChannelHandlerAdapter {
26+
27+
@Override
28+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
29+
String value = (String) msg;
30+
Map<String,String> map = JSON.parseObject(value, HashMap.class);
31+
String type = map.get("type");
32+
Handler handler = NettyServer.getHandlers().get(type);
33+
Response response = handler.handle(map);
34+
if(response != null){
35+
ctx.writeAndFlush(JSON.toJSONString(new HashMap<String,String>(){
36+
{
37+
put("value",response.value);
38+
put("message",response.message);
39+
put("status",response.status);
40+
}
41+
}));
42+
}else{
43+
ctx.writeAndFlush(JSON.toJSONString(new HashMap<String,String>(){
44+
{
45+
put("value","null");
46+
put("message","null");
47+
put("status","201");
48+
}
49+
}));
50+
}
51+
ctx.flush();
52+
// 必须要调用Flush方法才能放到通道里去发送。
53+
}
54+
}
55+
56+
class Response{
57+
public String value;
58+
public String message;
59+
public String status;
60+
61+
public Response(String value, String message, String status) {
62+
this.value = value;
63+
this.message = message;
64+
this.status = status;
65+
}
66+
}
67+
68+
interface Handler{
69+
Response handle(Map<String,String> map);
70+
}
71+
72+
class LogHandler implements Handler{
73+
@Override
74+
public Response handle(Map<String, String> map) {
75+
String str = ServerLogger.getLogger().getQueue().peek();
76+
if(str == null){
77+
return new Response("null","null","201");
78+
}
79+
return new Response(str,str,"200");
80+
}
81+
}
82+
83+
class CommandHandler implements Handler{
84+
@Override
85+
public Response handle(Map<String, String> map) {
86+
String command = map.get("command");
87+
Console.getConsole().runConsoleCommandWithEmerald(command);
88+
return null;
89+
}
90+
}
91+
92+
public class NettyServer implements Runnable{
93+
94+
private static Map<String,Handler> handlers = new HashMap<>();
95+
96+
static {
97+
handlers.put("log",new LogHandler());
98+
handlers.put("command",new CommandHandler());
99+
}
100+
101+
public static Map<String, Handler> getHandlers() {
102+
return handlers;
103+
}
104+
105+
public void run() {
106+
try {
107+
ServerLogger.getLogger().multiInfo(getClass(),"Starting control server...","","");
108+
// 1.创建两个线程池,一个负责接受客户端,一个进行传输
109+
NioEventLoopGroup pGroup = new NioEventLoopGroup();
110+
NioEventLoopGroup cGroup = new NioEventLoopGroup();
111+
// 2.创建辅助类
112+
ServerBootstrap serverBootstrap = new ServerBootstrap();
113+
serverBootstrap.group(pGroup, cGroup).channel(NioServerSocketChannel.class)
114+
// 3.设置缓冲区大小
115+
.option(ChannelOption.SO_BACKLOG, 1024)
116+
// 4.设置发送缓冲区大小
117+
.option(ChannelOption.SO_SNDBUF, 1024)
118+
// 5.配置handler
119+
.childHandler(new ChannelInitializer<SocketChannel>() {
120+
@Override
121+
protected void initChannel(SocketChannel sc) throws Exception {
122+
// 让返回的是字符串类型 设置返回的结果是String类型
123+
sc.pipeline().addLast(new StringDecoder());
124+
sc.pipeline().addLast(new ServerHandler());
125+
}
126+
});
127+
// 启动netty
128+
ChannelFuture cFuture = serverBootstrap.bind(Integer.parseInt(Server.getSender().getServer().serverProp.getProperty(Register.CLIENT_PORT,"20020"))).sync();
129+
// 关闭
130+
cFuture.channel().closeFuture().sync();
131+
pGroup.shutdownGracefully();
132+
cGroup.shutdownGracefully();
133+
} catch (Exception e) {
134+
Utils.printException(e);
135+
}
136+
137+
}
138+
}

JPLS/src/main/resources/static/403.html

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)