@@ -40,7 +40,7 @@ public void fail(String reason) {
4040 channel .close ();
4141 }
4242
43- public static enum Stage {
43+ public enum Stage {
4444 AWAIT_HEADER ,
4545 AWAIT_DATA
4646 }
@@ -98,13 +98,20 @@ public void run() {
9898 public HashMap <Long , ProxyPingEvent > proxyEventMap = new HashMap <>();
9999
100100 public void sendPacket (PacketOut packet ) {
101- ByteBuf buf = channel .alloc ().buffer ();
102- packet .writeTo (buf );
103- ByteBuf header = channel .alloc ().buffer ();
104- header .writeInt (buf .writerIndex ());
105- header .writeInt (packet .getPacketId ());
106- channel .writeAndFlush (header );
107- channel .writeAndFlush (buf );
101+ try {
102+ ByteBuf buf = channel .alloc ().buffer ();
103+ packet .writeTo (buf );
104+ ByteBuf header = channel .alloc ().buffer ();
105+ header .writeInt (buf .writerIndex ());
106+ header .writeInt (packet .getPacketId ());
107+ channel .writeAndFlush (header );
108+ channel .writeAndFlush (buf );
109+ }
110+ catch (Throwable ex ) {
111+ DepenizenBungee .instance .getLogger ().severe ("Connection '" + connectionName + "' had error sending packet..." );
112+ ex .printStackTrace ();
113+ fail ("Internal exception" );
114+ }
108115 }
109116
110117 public void reallocateBuf (ChannelHandlerContext ctx ) {
@@ -141,46 +148,53 @@ public void handlerRemoved(ChannelHandlerContext ctx) {
141148
142149 @ Override
143150 public void channelRead (ChannelHandlerContext ctx , Object msg ) {
144- ByteBuf m = (ByteBuf ) msg ;
145- packetBuffer .writeBytes (m );
146- m .release ();
147- while (true ) {
148- if (currentStage == Stage .AWAIT_HEADER ) {
149- if (packetBuffer .readableBytes () < 8 ) {
150- return ;
151- }
152- waitingLength = packetBuffer .readInt ();
153- packetId = packetBuffer .readInt ();
154- currentStage = Stage .AWAIT_DATA ;
155- if (thisServer == null && packetId != MyInfoPacketIn .PACKET_ID ) {
156- fail ("Invalid FIRST packet id (must be MyInfoPacket): " + packetId + ", data length thus far = " + packetBuffer .readableBytes ());
157- return ;
158- }
159- if (!DepenizenBungee .instance .packets .containsKey (packetId )) {
160- fail ("Invalid packet id: " + packetId );
161- return ;
162- }
163- }
164- else if (currentStage == Stage .AWAIT_DATA ) {
165- if (packetBuffer .readableBytes () < waitingLength ) {
166- return ;
151+ try {
152+ ByteBuf m = (ByteBuf ) msg ;
153+ packetBuffer .writeBytes (m );
154+ m .release ();
155+ while (true ) {
156+ if (currentStage == Stage .AWAIT_HEADER ) {
157+ if (packetBuffer .readableBytes () < 8 ) {
158+ return ;
159+ }
160+ waitingLength = packetBuffer .readInt ();
161+ packetId = packetBuffer .readInt ();
162+ currentStage = Stage .AWAIT_DATA ;
163+ if (thisServer == null && packetId != MyInfoPacketIn .PACKET_ID ) {
164+ fail ("Invalid FIRST packet id (must be MyInfoPacket): " + packetId + ", data length thus far = " + packetBuffer .readableBytes ());
165+ return ;
166+ }
167+ if (!DepenizenBungee .instance .packets .containsKey (packetId )) {
168+ fail ("Invalid packet id: " + packetId );
169+ return ;
170+ }
167171 }
168- try {
169- lastPacketReceived = System .currentTimeMillis ();
170- PacketIn packet = DepenizenBungee .instance .packets .get (packetId );
171- packet .process (this , packetBuffer );
172- currentStage = Stage .AWAIT_HEADER ;
173- reallocateBuf (ctx );
172+ else if (currentStage == Stage .AWAIT_DATA ) {
173+ if (packetBuffer .readableBytes () < waitingLength ) {
174+ return ;
175+ }
176+ try {
177+ lastPacketReceived = System .currentTimeMillis ();
178+ PacketIn packet = DepenizenBungee .instance .packets .get (packetId );
179+ packet .process (this , packetBuffer );
180+ currentStage = Stage .AWAIT_HEADER ;
181+ reallocateBuf (ctx );
182+ }
183+ catch (Throwable ex ) {
184+ ex .printStackTrace ();
185+ fail ("Internal exception." );
186+ return ;
187+ }
174188 }
175- catch (Throwable ex ) {
176- ex .printStackTrace ();
177- fail ("Internal exception." );
189+ else {
178190 return ;
179191 }
180192 }
181- else {
182- return ;
183- }
193+ }
194+ catch (Throwable ex ) {
195+ DepenizenBungee .instance .getLogger ().severe ("Connection '" + connectionName + "' had exception reading input packets..." );
196+ ex .printStackTrace ();
197+ fail ("Internal exception" );
184198 }
185199 }
186200}
0 commit comments