From 744bba300ea285ed9f7defaf9feea384acb6d057 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Sat, 5 Jan 2019 18:50:47 +0000 Subject: [PATCH] A couple of enhancements to websockets - Provide whether a message was binary or text in websocket_message and handle.receive(). (Fixes #96) - Provide an optional reason and status code within the websocket_close event. Off topic, but also cleanup the file handles a little. --- .../dan200/computercraft/ComputerCraft.java | 12 +++++++ .../apis/handles/BinaryReadableHandle.java | 15 +++------ .../apis/handles/BinaryWritableHandle.java | 12 +++---- .../apis/handles/EncodedReadableHandle.java | 18 ++++------- .../apis/handles/EncodedWritableHandle.java | 32 ++++--------------- .../core/apis/http/WebsocketConnection.java | 31 +++++++++++------- 6 files changed, 53 insertions(+), 67 deletions(-) diff --git a/src/main/java/dan200/computercraft/ComputerCraft.java b/src/main/java/dan200/computercraft/ComputerCraft.java index 92504b0a2..c7a96425d 100644 --- a/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/src/main/java/dan200/computercraft/ComputerCraft.java @@ -888,6 +888,18 @@ public class ComputerCraft return TurtleUpgrades.get( stack ); } + @Deprecated + public static IPocketUpgrade getPocketUpgrade( String id ) + { + return dan200.computercraft.shared.PocketUpgrades.get( id ); + } + + @Deprecated + public static ITurtleUpgrade getTurtleUpgrade( String id ) + { + return TurtleUpgrades.get( id ); + } + @Deprecated public static IPeripheral getPeripheralAt( World world, BlockPos pos, EnumFacing side ) { diff --git a/src/main/java/dan200/computercraft/core/apis/handles/BinaryReadableHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/BinaryReadableHandle.java index 1c27e5457..7d0a8627e 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/BinaryReadableHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/BinaryReadableHandle.java @@ -59,8 +59,7 @@ public class BinaryReadableHandle extends HandleGeneric { switch( method ) { - case 0: - // read + case 0: // read checkOpen(); try { @@ -134,8 +133,7 @@ public class BinaryReadableHandle extends HandleGeneric { return null; } - case 1: - // readAll + case 1: // readAll checkOpen(); try { @@ -163,9 +161,8 @@ public class BinaryReadableHandle extends HandleGeneric { return null; } - case 2: + case 2: // readLine { - // readLine checkOpen(); boolean withTrailing = optBoolean( args, 0, false ); try @@ -199,12 +196,10 @@ public class BinaryReadableHandle extends HandleGeneric return null; } } - case 3: - //close + case 3: // close close(); return null; - case 4: - // seek + case 4: // seek checkOpen(); return handleSeek( m_seekable, args ); default: diff --git a/src/main/java/dan200/computercraft/core/apis/handles/BinaryWritableHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/BinaryWritableHandle.java index be5388708..50ce2d55e 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/BinaryWritableHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/BinaryWritableHandle.java @@ -53,8 +53,7 @@ public class BinaryWritableHandle extends HandleGeneric { switch( method ) { - case 0: - // write + case 0: // write checkOpen(); try { @@ -82,8 +81,7 @@ public class BinaryWritableHandle extends HandleGeneric { throw new LuaException( e.getMessage() ); } - case 1: - // flush + case 1: // flush checkOpen(); try { @@ -96,12 +94,10 @@ public class BinaryWritableHandle extends HandleGeneric { return null; } - case 2: - //close + case 2: // close close(); return null; - case 3: - // seek + case 3: // seek checkOpen(); return handleSeek( m_seekable, args ); default: diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedReadableHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedReadableHandle.java index 409ec46cb..0ac670109 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedReadableHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedReadableHandle.java @@ -47,8 +47,8 @@ public class EncodedReadableHandle extends HandleGeneric return new String[] { "readLine", "readAll", - "close", "read", + "close", }; } @@ -57,9 +57,8 @@ public class EncodedReadableHandle extends HandleGeneric { switch( method ) { - case 0: + case 0: // readLine { - // readLine checkOpen(); boolean withTrailing = optBoolean( args, 0, false ); try @@ -81,8 +80,7 @@ public class EncodedReadableHandle extends HandleGeneric return null; } } - case 1: - // readAll + case 1: // readAll checkOpen(); try { @@ -103,12 +101,7 @@ public class EncodedReadableHandle extends HandleGeneric { return null; } - case 2: - // close - close(); - return null; - case 3: - // read + case 2: // read checkOpen(); try { @@ -158,6 +151,9 @@ public class EncodedReadableHandle extends HandleGeneric { return null; } + case 3: // close + close(); + return null; default: return null; } diff --git a/src/main/java/dan200/computercraft/core/apis/handles/EncodedWritableHandle.java b/src/main/java/dan200/computercraft/core/apis/handles/EncodedWritableHandle.java index 8be118442..16a5fb13a 100644 --- a/src/main/java/dan200/computercraft/core/apis/handles/EncodedWritableHandle.java +++ b/src/main/java/dan200/computercraft/core/apis/handles/EncodedWritableHandle.java @@ -52,19 +52,10 @@ public class EncodedWritableHandle extends HandleGeneric { switch( method ) { - case 0: + case 0: // write { - // write checkOpen(); - String text; - if( args.length > 0 && args[0] != null ) - { - text = args[0].toString(); - } - else - { - text = ""; - } + String text = args.length > 0 && args[0] != null ? args[0].toString() : ""; try { m_writer.write( text, 0, text.length() ); @@ -75,19 +66,10 @@ public class EncodedWritableHandle extends HandleGeneric throw new LuaException( e.getMessage() ); } } - case 1: + case 1: // writeLine { - // writeLine checkOpen(); - String text; - if( args.length > 0 && args[0] != null ) - { - text = args[0].toString(); - } - else - { - text = ""; - } + String text = args.length > 0 && args[0] != null ? args[0].toString() : ""; try { m_writer.write( text, 0, text.length() ); @@ -99,8 +81,7 @@ public class EncodedWritableHandle extends HandleGeneric throw new LuaException( e.getMessage() ); } } - case 2: - // flush + case 2: // flush checkOpen(); try { @@ -111,8 +92,7 @@ public class EncodedWritableHandle extends HandleGeneric { return null; } - case 3: - // close + case 3: // close close(); return null; default: diff --git a/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java b/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java index 990242e94..bb783ab1a 100644 --- a/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java +++ b/src/main/java/dan200/computercraft/core/apis/http/WebsocketConnection.java @@ -7,6 +7,7 @@ package dan200.computercraft.core.apis.http; import com.google.common.base.Objects; +import com.google.common.base.Strings; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaObject; import dan200.computercraft.api.lua.LuaException; @@ -26,7 +27,7 @@ import io.netty.util.CharsetUtil; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.Closeable; -import java.io.IOException; +import java.util.Arrays; import static dan200.computercraft.core.apis.ArgumentHelper.optBoolean; @@ -68,15 +69,20 @@ public class WebsocketConnection extends SimpleChannelInboundHandler imp } @Override - public void close() throws IOException + public void close() { close( false ); } - private void onClosed() + private void onClosed( int status, String reason ) { close( true ); - computer.queueEvent( CLOSE_EVENT, new Object[] { url } ); + + computer.queueEvent( CLOSE_EVENT, new Object[] { + url, + Strings.isNullOrEmpty( reason ) ? null : reason, + status < 0 ? null : status, + } ); } @Override @@ -96,12 +102,12 @@ public class WebsocketConnection extends SimpleChannelInboundHandler imp @Override public void channelInactive( ChannelHandlerContext ctx ) throws Exception { - onClosed(); + onClosed( -1, "Websocket is inactive" ); super.channelInactive( ctx ); } @Override - public void channelRead0( ChannelHandlerContext ctx, Object msg ) throws Exception + public void channelRead0( ChannelHandlerContext ctx, Object msg ) { Channel ch = ctx.channel(); if( !handshaker.isHandshakeComplete() ) @@ -123,7 +129,7 @@ public class WebsocketConnection extends SimpleChannelInboundHandler imp String data = ((TextWebSocketFrame) frame).text(); computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, data.length() ); - computer.queueEvent( MESSAGE_EVENT, new Object[] { url, data } ); + computer.queueEvent( MESSAGE_EVENT, new Object[] { url, data, false } ); } else if( frame instanceof BinaryWebSocketFrame ) { @@ -132,12 +138,13 @@ public class WebsocketConnection extends SimpleChannelInboundHandler imp data.readBytes( converted ); computer.addTrackingChange( TrackingField.WEBSOCKET_INCOMING, converted.length ); - computer.queueEvent( MESSAGE_EVENT, new Object[] { url, converted } ); + computer.queueEvent( MESSAGE_EVENT, new Object[] { url, converted, true } ); } else if( frame instanceof CloseWebSocketFrame ) { + CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame; ch.close(); - onClosed(); + onClosed( closeFrame.statusCode(), closeFrame.reasonText() ); } } @@ -164,17 +171,17 @@ public class WebsocketConnection extends SimpleChannelInboundHandler imp { switch( method ) { - case 0: + case 0: // receive while( true ) { checkOpen(); Object[] event = context.pullEvent( MESSAGE_EVENT ); if( event.length >= 3 && Objects.equal( event[1], url ) ) { - return new Object[] { event[2] }; + return Arrays.copyOfRange( event, 2, event.length ); } } - case 1: + case 1: // send { checkOpen(); String text = arguments.length > 0 && arguments[0] != null ? arguments[0].toString() : "";