Expand streams a little bit

This commit is contained in:
md_5 2013-07-05 19:17:25 +10:00
parent 9424bdedca
commit db4abfe486
5 changed files with 94 additions and 68 deletions

View File

@ -0,0 +1,38 @@
package net.md_5.bungee.protocol;
import io.netty.buffer.ByteBuf;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class MinecraftInput
{
private final ByteBuf buf;
public byte readByte()
{
return buf.readByte();
}
public short readUnisgnedByte()
{
return buf.readUnsignedByte();
}
public int readInt()
{
return buf.readInt();
}
public String readString()
{
short len = buf.readShort();
char[] c = new char[ len ];
for ( int i = 0; i < c.length; i++ )
{
c[i] = buf.readChar();
}
return new String( c );
}
}

View File

@ -0,0 +1,50 @@
package net.md_5.bungee.protocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.Arrays;
public class MinecraftOutput
{
private final ByteBuf buf;
public MinecraftOutput()
{
buf = Unpooled.buffer();
}
public byte[] toArray()
{
if ( buf.hasArray() )
{
return Arrays.copyOfRange( buf.array(), buf.arrayOffset(), buf.arrayOffset() + buf.writerIndex() );
} else
{
byte[] b = new byte[ buf.writerIndex() ];
buf.readBytes( b );
return b;
}
}
public MinecraftOutput writeByte(byte b)
{
buf.writeByte( b );
return this;
}
public void writeInt(int i)
{
buf.writeInt( i );
}
public void writeString(String s)
{
char[] cc = s.toCharArray();
buf.writeShort( cc.length );
for ( char c : cc )
{
buf.writeChar( c );
}
}
}

View File

@ -1,62 +0,0 @@
package net.md_5.bungee.protocol;
import io.netty.buffer.ByteBuf;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class MinecraftStream
{
private final ByteBuf buf;
public byte readByte()
{
return buf.readByte();
}
public MinecraftStream writeByte(byte b)
{
buf.writeByte( b );
return this;
}
/*========================================================================*/
public short readUnisgnedByte()
{
return buf.readUnsignedByte();
}
/*========================================================================*/
public int readInt()
{
return buf.readInt();
}
public void writeInt(int i)
{
buf.writeInt( i );
}
/*========================================================================*/
public String readString()
{
short len = buf.readShort();
char[] c = new char[ len ];
for ( int i = 0; i < c.length; i++ )
{
c[i] = buf.readChar();
}
return new String( c );
}
public void writeString(String s)
{
char[] cc = s.toCharArray();
buf.writeShort( cc.length );
for ( char c : cc )
{
buf.writeChar( c );
}
}
}

View File

@ -8,7 +8,8 @@ import java.io.DataInputStream;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import net.md_5.bungee.protocol.MinecraftStream; import net.md_5.bungee.protocol.MinecraftInput;
import net.md_5.bungee.protocol.MinecraftOutput;
@Getter @Getter
@ToString @ToString
@ -56,8 +57,8 @@ public class PacketFAPluginMessage extends DefinedPacket
return new DataInputStream( new ByteArrayInputStream( data ) ); return new DataInputStream( new ByteArrayInputStream( data ) );
} }
public MinecraftStream getMCStream() public MinecraftInput getMCStream()
{ {
return new MinecraftStream( Unpooled.wrappedBuffer( data ) ); return new MinecraftInput( Unpooled.wrappedBuffer( data ) );
} }
} }

View File

@ -2,7 +2,6 @@ package net.md_5.bungee.connection;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.netty.util.concurrent.ScheduledFuture; import io.netty.util.concurrent.ScheduledFuture;
import java.io.DataInput;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -39,7 +38,7 @@ import net.md_5.bungee.netty.PacketDecoder;
import net.md_5.bungee.netty.PacketHandler; import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.protocol.Forge; import net.md_5.bungee.protocol.Forge;
import net.md_5.bungee.protocol.MinecraftStream; import net.md_5.bungee.protocol.MinecraftInput;
import net.md_5.bungee.protocol.Vanilla; import net.md_5.bungee.protocol.Vanilla;
import net.md_5.bungee.protocol.packet.DefinedPacket; import net.md_5.bungee.protocol.packet.DefinedPacket;
import net.md_5.bungee.protocol.packet.Packet1Login; import net.md_5.bungee.protocol.packet.Packet1Login;
@ -109,7 +108,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{ {
if ( pingFuture.cancel( false ) ) if ( pingFuture.cancel( false ) )
{ {
MinecraftStream in = pluginMessage.getMCStream(); MinecraftInput in = pluginMessage.getMCStream();
version = in.readByte(); version = in.readByte();
String connectHost = in.readString(); String connectHost = in.readString();
int connectPort = in.readInt(); int connectPort = in.readInt();