refactored websocket codec to single class

This commit is contained in:
Dank074 2020-11-15 15:42:02 -06:00
parent 99596ca354
commit f9d94197d5
7 changed files with 23 additions and 50 deletions

View File

@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.krews.nitro</groupId> <groupId>org.krews.plugin.nitro</groupId>
<artifactId>NitroWebsockets</artifactId> <artifactId>NitroWebsockets</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@ -1,29 +0,0 @@
package com.krews.plugin.nitro.websockets.codec;
import com.eu.habbo.messages.ServerMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.util.IllegalReferenceCountException;
import java.io.IOException;
import java.util.List;
public class HabboFrameToWebSocketFrameEncoder extends MessageToMessageEncoder<ServerMessage> {
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, ServerMessage message, List<Object> out) throws Exception {
try {
BinaryWebSocketFrame frame = new BinaryWebSocketFrame(message.get());
try {
out.add(frame.retain());
} finally {
// Release copied buffer.
frame.release();
}
} catch (IllegalReferenceCountException e) {
throw new IOException(String.format("IllegalReferenceCountException happened for ServerMessage with packet id %d.", message.getHeader()), e);
}
}
}

View File

@ -1,4 +1,4 @@
package com.krews.plugin.nitro; package org.krews.plugin.nitro;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo; import com.eu.habbo.habbohotel.users.Habbo;
@ -6,12 +6,12 @@ import com.eu.habbo.plugin.EventHandler;
import com.eu.habbo.plugin.EventListener; import com.eu.habbo.plugin.EventListener;
import com.eu.habbo.plugin.HabboPlugin; import com.eu.habbo.plugin.HabboPlugin;
import com.eu.habbo.plugin.events.emulator.EmulatorLoadedEvent; import com.eu.habbo.plugin.events.emulator.EmulatorLoadedEvent;
import com.krews.plugin.nitro.websockets.NetworkChannelInitializer; import org.krews.plugin.nitro.websockets.NetworkChannelInitializer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class main extends HabboPlugin implements EventListener { public class main extends HabboPlugin implements EventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(Emulator.class); private static final Logger LOGGER = LoggerFactory.getLogger(main.class);
public void onEnable() throws Exception { public void onEnable() throws Exception {
Emulator.getPluginManager().registerEvents(this, this); Emulator.getPluginManager().registerEvents(this, this);
@ -29,7 +29,7 @@ public class main extends HabboPlugin implements EventListener {
} }
@EventHandler @EventHandler
public void onEmulatorLoadedEvent (EmulatorLoadedEvent e) { public void onEmulatorLoadedEvent (EmulatorLoadedEvent e) throws InterruptedException {
//add missing db entry //add missing db entry
Emulator.getConfig().register("websockets.whitelist", "localhost"); Emulator.getConfig().register("websockets.whitelist", "localhost");
Emulator.getConfig().register("ws.nitro.host", "0.0.0.0"); Emulator.getConfig().register("ws.nitro.host", "0.0.0.0");
@ -37,7 +37,7 @@ public class main extends HabboPlugin implements EventListener {
Emulator.getGameServer().getServerBootstrap().childHandler(new NetworkChannelInitializer()); Emulator.getGameServer().getServerBootstrap().childHandler(new NetworkChannelInitializer());
Emulator.getGameServer().getServerBootstrap().bind(Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0"), Emulator.getConfig().getInt("ws.nitro.port", 2096)).syncUninterruptibly(); Emulator.getGameServer().getServerBootstrap().bind(Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0"), Emulator.getConfig().getInt("ws.nitro.port", 2096)).sync();
LOGGER.info("OFFICIAL PLUGIN - Nitro Websockets has started!"); LOGGER.info("OFFICIAL PLUGIN - Nitro Websockets has started!");
LOGGER.info("Nitro Websockets Listening on " + Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0") + ":" + Emulator.getConfig().getInt("ws.nitro.port", 2096)); LOGGER.info("Nitro Websockets Listening on " + Emulator.getConfig().getValue("ws.nitro.host", "0.0.0.0") + ":" + Emulator.getConfig().getInt("ws.nitro.port", 2096));

View File

@ -1,11 +1,11 @@
package com.krews.plugin.nitro.websockets; package org.krews.plugin.nitro.websockets;
import com.eu.habbo.messages.PacketManager; import com.eu.habbo.messages.PacketManager;
import com.eu.habbo.networking.gameserver.decoders.*; import com.eu.habbo.networking.gameserver.decoders.*;
import com.eu.habbo.networking.gameserver.encoders.GameServerMessageEncoder; import com.eu.habbo.networking.gameserver.encoders.GameServerMessageEncoder;
import com.eu.habbo.networking.gameserver.encoders.GameServerMessageLogger; import com.eu.habbo.networking.gameserver.encoders.GameServerMessageLogger;
import com.eu.habbo.networking.gameserver.handlers.IdleTimeoutHandler; import com.eu.habbo.networking.gameserver.handlers.IdleTimeoutHandler;
import com.krews.plugin.nitro.websockets.handlers.MessageInterceptorHandler; import org.krews.plugin.nitro.websockets.handlers.MessageInterceptorHandler;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LoggingHandler; import io.netty.handler.logging.LoggingHandler;

View File

@ -1,18 +1,22 @@
package com.krews.plugin.nitro.websockets.codec; package org.krews.plugin.nitro.websockets.codec;
import com.eu.habbo.Emulator; import com.eu.habbo.Emulator;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageCodec;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.*;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List; import java.util.List;
public class WebSocketFrameToHabboFrameDecoder extends MessageToMessageDecoder<WebSocketFrame> { public class WebSocketCodec extends MessageToMessageCodec<WebSocketFrame, ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
out.add(new BinaryWebSocketFrame(in).retain());
}
@Override @Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame in, List<Object> out) { protected void decode(ChannelHandlerContext ctx, WebSocketFrame in, List<Object> out) {
out.add(in.content().retain()); out.add(in.content().retain());

View File

@ -1,7 +1,6 @@
package com.krews.plugin.nitro.websockets.handlers; package org.krews.plugin.nitro.websockets.handlers;
import com.krews.plugin.nitro.websockets.codec.HabboFrameToWebSocketFrameEncoder; import org.krews.plugin.nitro.websockets.codec.WebSocketCodec;
import com.krews.plugin.nitro.websockets.codec.WebSocketFrameToHabboFrameDecoder;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
@ -17,11 +16,10 @@ public class MessageInterceptorHandler extends ByteToMessageDecoder {
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if(in.toString(CharsetUtil.UTF_8).startsWith("GET")) { if(in.toString(CharsetUtil.UTF_8).startsWith("GET")) {
// this is a websocket upgrade request, so add the appropriate decoders/encoders // this is a websocket upgrade request, so add the appropriate decoders/encoders
ctx.pipeline().addAfter("messageInterceptor", "websocketHandler", new WebSocketFrameToHabboFrameDecoder()); ctx.pipeline().addAfter("messageInterceptor", "websocketCodec", new WebSocketCodec());
ctx.pipeline().addAfter("messageInterceptor", "protocolHandler", new WebSocketServerProtocolHandler("/", true)); ctx.pipeline().addAfter("messageInterceptor", "protocolHandler", new WebSocketServerProtocolHandler("/", true));
ctx.pipeline().addAfter("messageInterceptor", "objectAggregator", new HttpObjectAggregator(65536)); ctx.pipeline().addAfter("messageInterceptor", "objectAggregator", new HttpObjectAggregator(65536));
ctx.pipeline().addAfter("messageInterceptor", "httpCodec", new HttpServerCodec()); ctx.pipeline().addAfter("messageInterceptor", "httpCodec", new HttpServerCodec());
ctx.pipeline().replace("messageEncoder", "websocketFrameEncoder", new HabboFrameToWebSocketFrameEncoder());
} }
// Remove ourselves // Remove ourselves
ctx.pipeline().remove(this); ctx.pipeline().remove(this);

View File

@ -1,5 +1,5 @@
{ {
"main" : "com.krews.plugin.nitro.main", "main" : "org.krews.plugin.nitro.main",
"name" : "Nitro Websockets", "name" : "Nitro Websockets",
"author" : "Krews" "author" : "Krews"
} }