Implement basic MC stream

This commit is contained in:
md_5 2013-07-05 19:06:46 +10:00
parent 52b3c6b77c
commit 9424bdedca
3 changed files with 71 additions and 11 deletions

View File

@ -0,0 +1,62 @@
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

@ -1,7 +1,6 @@
package net.md_5.bungee.protocol.packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.Unpooled;
import java.io.ByteArrayInputStream;
import java.io.DataInput;
@ -9,6 +8,7 @@ import java.io.DataInputStream;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import net.md_5.bungee.protocol.MinecraftStream;
@Getter
@ToString
@ -55,4 +55,9 @@ public class PacketFAPluginMessage extends DefinedPacket
{
return new DataInputStream( new ByteArrayInputStream( data ) );
}
public MinecraftStream getMCStream()
{
return new MinecraftStream( Unpooled.wrappedBuffer( data ) );
}
}

View File

@ -39,6 +39,7 @@ import net.md_5.bungee.netty.PacketDecoder;
import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.protocol.Forge;
import net.md_5.bungee.protocol.MinecraftStream;
import net.md_5.bungee.protocol.Vanilla;
import net.md_5.bungee.protocol.packet.DefinedPacket;
import net.md_5.bungee.protocol.packet.Packet1Login;
@ -108,17 +109,9 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{
if ( pingFuture.cancel( false ) )
{
DataInput in = pluginMessage.getStream();
MinecraftStream in = pluginMessage.getMCStream();
version = in.readByte();
//
short len = in.readShort();
char[] chars = new char[ len ];
for ( int i = 0; i < len; i++ )
{
chars[i] = in.readChar();
}
//
String connectHost = new String( chars );
String connectHost = in.readString();
int connectPort = in.readInt();
this.vHost = new InetSocketAddress( connectHost, connectPort );