1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00

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.
This commit is contained in:
SquidDev 2019-01-05 18:50:47 +00:00
parent 34d43d8273
commit 744bba300e
6 changed files with 53 additions and 67 deletions

View File

@ -888,6 +888,18 @@ public static ITurtleUpgrade getTurtleUpgrade( ItemStack stack )
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 )
{

View File

@ -59,8 +59,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
switch( method )
{
case 0:
// read
case 0: // read
checkOpen();
try
{
@ -134,8 +133,7 @@ else if( count == 0 && m_seekable != null )
{
return null;
}
case 1:
// readAll
case 1: // readAll
checkOpen();
try
{
@ -163,9 +161,8 @@ else if( count == 0 && m_seekable != null )
{
return null;
}
case 2:
case 2: // readLine
{
// readLine
checkOpen();
boolean withTrailing = optBoolean( args, 0, false );
try
@ -199,12 +196,10 @@ else if( count == 0 && m_seekable != null )
return null;
}
}
case 3:
//close
case 3: // close
close();
return null;
case 4:
// seek
case 4: // seek
checkOpen();
return handleSeek( m_seekable, args );
default:

View File

@ -53,8 +53,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
switch( method )
{
case 0:
// write
case 0: // write
checkOpen();
try
{
@ -82,8 +81,7 @@ else if( args.length > 0 && args[0] instanceof String )
{
throw new LuaException( e.getMessage() );
}
case 1:
// flush
case 1: // flush
checkOpen();
try
{
@ -96,12 +94,10 @@ else if( args.length > 0 && args[0] instanceof String )
{
return null;
}
case 2:
//close
case 2: // close
close();
return null;
case 3:
// seek
case 3: // seek
checkOpen();
return handleSeek( m_seekable, args );
default:

View File

@ -47,8 +47,8 @@ public String[] getMethodNames()
return new String[] {
"readLine",
"readAll",
"close",
"read",
"close",
};
}
@ -57,9 +57,8 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
switch( method )
{
case 0:
case 0: // readLine
{
// readLine
checkOpen();
boolean withTrailing = optBoolean( args, 0, false );
try
@ -81,8 +80,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
return null;
}
}
case 1:
// readAll
case 1: // readAll
checkOpen();
try
{
@ -103,12 +101,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
return null;
}
case 2:
// close
close();
return null;
case 3:
// read
case 2: // read
checkOpen();
try
{
@ -158,6 +151,9 @@ else if( count <= BUFFER_SIZE )
{
return null;
}
case 3: // close
close();
return null;
default:
return null;
}

View File

@ -52,19 +52,10 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
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 Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
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 Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
throw new LuaException( e.getMessage() );
}
}
case 2:
// flush
case 2: // flush
checkOpen();
try
{
@ -111,8 +92,7 @@ public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
return null;
}
case 3:
// close
case 3: // close
close();
return null;
default:

View File

@ -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 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 @@ private void close( boolean remove )
}
@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 void channelActive( ChannelHandlerContext ctx ) throws Exception
@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 void channelRead0( ChannelHandlerContext ctx, Object msg ) throws Excepti
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 @@ else if( frame instanceof BinaryWebSocketFrame )
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 Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull O
{
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() : "";