use of io.netty.buffer.ByteBufInputStream in project Railcraft by Railcraft.
the class EntityLocomotive method readSpawnData.
@Override
public void readSpawnData(ByteBuf data) {
try (ByteBufInputStream byteBufStream = new ByteBufInputStream(data);
DataInputStream byteStream = new DataInputStream(byteBufStream)) {
String name = byteStream.readUTF();
if (!name.isEmpty())
setCustomNameTag(name);
model = byteStream.readUTF();
} catch (IOException ignored) {
}
}
use of io.netty.buffer.ByteBufInputStream in project drill by apache.
the class UserServerRequestHandler method handle.
@Override
public void handle(BitToUserConnection connection, int rpcType, ByteBuf pBody, ByteBuf dBody, ResponseSender responseSender) throws RpcException {
switch(rpcType) {
case RpcType.RUN_QUERY_VALUE:
logger.debug("Received query to run. Returning query handle.");
try {
final RunQuery query = RunQuery.PARSER.parseFrom(new ByteBufInputStream(pBody));
final QueryId queryId = worker.submitWork(connection, query);
responseSender.send(new Response(RpcType.QUERY_HANDLE, queryId));
break;
} catch (InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding RunQuery body.", e);
}
case RpcType.CANCEL_QUERY_VALUE:
try {
final QueryId queryId = QueryId.PARSER.parseFrom(new ByteBufInputStream(pBody));
final Ack ack = worker.cancelQuery(queryId);
responseSender.send(new Response(RpcType.ACK, ack));
break;
} catch (InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding QueryId body.", e);
}
case RpcType.RESUME_PAUSED_QUERY_VALUE:
try {
final QueryId queryId = QueryId.PARSER.parseFrom(new ByteBufInputStream(pBody));
final Ack ack = worker.resumeQuery(queryId);
responseSender.send(new Response(RpcType.ACK, ack));
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding QueryId body.", e);
}
case RpcType.GET_QUERY_PLAN_FRAGMENTS_VALUE:
try {
final GetQueryPlanFragments req = GetQueryPlanFragments.PARSER.parseFrom(new ByteBufInputStream(pBody));
responseSender.send(new Response(RpcType.QUERY_PLAN_FRAGMENTS, worker.getQueryPlan(connection, req)));
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding GetQueryPlanFragments body.", e);
}
case RpcType.GET_CATALOGS_VALUE:
try {
final GetCatalogsReq req = GetCatalogsReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
worker.submitCatalogMetadataWork(connection.getSession(), req, responseSender);
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding GetCatalogsReq body.", e);
}
case RpcType.GET_SCHEMAS_VALUE:
try {
final GetSchemasReq req = GetSchemasReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
worker.submitSchemasMetadataWork(connection.getSession(), req, responseSender);
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding GetSchemasReq body.", e);
}
case RpcType.GET_TABLES_VALUE:
try {
final GetTablesReq req = GetTablesReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
worker.submitTablesMetadataWork(connection.getSession(), req, responseSender);
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding GetTablesReq body.", e);
}
case RpcType.GET_COLUMNS_VALUE:
try {
final GetColumnsReq req = GetColumnsReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
worker.submitColumnsMetadataWork(connection.getSession(), req, responseSender);
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding GetColumnsReq body.", e);
}
case RpcType.CREATE_PREPARED_STATEMENT_VALUE:
try {
final CreatePreparedStatementReq req = CreatePreparedStatementReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
worker.submitPreparedStatementWork(connection, req, responseSender);
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding CreatePreparedStatementReq body.", e);
}
case RpcType.GET_SERVER_META_VALUE:
try {
final GetServerMetaReq req = GetServerMetaReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
worker.submitServerMetadataWork(connection.getSession(), req, responseSender);
break;
} catch (final InvalidProtocolBufferException e) {
throw new RpcException("Failure while decoding CreatePreparedStatementReq body.", e);
}
default:
throw new UnsupportedOperationException(String.format("UserServerRequestHandler received rpc of unknown type. Type was %d.", rpcType));
}
}
use of io.netty.buffer.ByteBufInputStream in project Railcraft by Railcraft.
the class PacketHandler method onPacketData.
private void onPacketData(ByteBuf byteBuf, EntityPlayerMP player, @Nullable IThreadListener listener) {
RailcraftInputStream data = new RailcraftInputStream(new ByteBufInputStream(byteBuf));
try {
RailcraftPacket pkt;
byte packetID = data.readByte();
if (packetID < 0)
return;
// System.out.println("Packet Received: " + packetID);
PacketType type = packetTypes[packetID];
switch(type) {
case GUI_RETURN:
pkt = new PacketGuiReturn(player);
break;
case TILE_EXTRA_DATA:
pkt = new PacketTileExtraData();
break;
case TILE_REQUEST:
pkt = new PacketTileRequest(player);
break;
case GUI_INTEGER:
pkt = new PacketGuiInteger();
break;
case GUI_STRING:
pkt = new PacketGuiString();
break;
case GUI_WIDGET:
pkt = new PacketGuiWidget();
break;
case EFFECT:
pkt = new PacketEffect();
break;
case CONTROLLER_UPDATE:
case RECEIVER_UPDATE:
case SIGNAL_UPDATE:
pkt = new PacketPairUpdate(type);
break;
case CONTROLLER_REQUEST:
case RECEIVER_REQUEST:
case SIGNAL_REQUEST:
pkt = new PacketPairRequest(player, type);
break;
case ITEM_NBT_HAND:
pkt = new PacketItemNBT.CurrentItem(player);
break;
case ITEM_NBT_TILE:
pkt = new PacketItemNBT.RoutableTile(player);
break;
case KEY_PRESS:
pkt = new PacketKeyPress(player);
break;
case GOLDEN_TICKET_GUI:
pkt = new PacketTicketGui();
break;
case LOGBOOK_GUI:
pkt = new PacketLogbook();
break;
case SHUNTING_AURA:
pkt = new PacketShuntingAura();
break;
case MOVING_SOUND:
pkt = new PacketMovingSound();
break;
case STOP_RECORD:
pkt = new PacketStopRecord();
break;
case ENTITY_SYNC:
pkt = new PacketEntitySync();
break;
default:
return;
}
readPacket(pkt, data, listener);
} catch (IOException e) {
Game.log().throwable("Exception in PacketHandler.onPacketData", e);
}
}
use of io.netty.buffer.ByteBufInputStream in project drill by apache.
the class RpcDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
if (!ctx.channel().isOpen()) {
return;
}
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Inbound rpc message received.");
}
// now, we know the entire message is in the buffer and the buffer is constrained to this message. Additionally,
// this process should avoid reading beyond the end of this buffer so we inform the ByteBufInputStream to throw an
// exception if be go beyond readable bytes (as opposed to blocking).
final ByteBufInputStream is = new ByteBufInputStream(buffer, buffer.readableBytes());
// read the rpc header, saved in delimited format.
checkTag(is, RpcEncoder.HEADER_TAG);
final RpcHeader header = RpcHeader.parseDelimitedFrom(is);
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug(" post header read index {}", buffer.readerIndex());
}
// read the protobuf body into a buffer.
checkTag(is, RpcEncoder.PROTOBUF_BODY_TAG);
final int pBodyLength = readRawVarint32(is);
final ByteBuf pBody = buffer.slice(buffer.readerIndex(), pBodyLength);
buffer.skipBytes(pBodyLength);
pBody.retain(1);
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Read protobuf body of length {} into buffer {}.", pBodyLength, pBody);
}
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("post protobufbody read index {}", buffer.readerIndex());
}
ByteBuf dBody = null;
int dBodyLength = 0;
// read the data body.
if (buffer.readableBytes() > 0) {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Reading raw body, buffer has {} bytes available, is available {}.", buffer.readableBytes(), is.available());
}
checkTag(is, RpcEncoder.RAW_BODY_TAG);
dBodyLength = readRawVarint32(is);
if (buffer.readableBytes() != dBodyLength) {
throw new CorruptedFrameException(String.format("Expected to receive a raw body of %d bytes but received a buffer with %d bytes.", dBodyLength, buffer.readableBytes()));
}
dBody = buffer.slice();
dBody.retain(1);
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Read raw body of {}", dBody);
}
} else {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("No need to read raw body, no readable bytes left.");
}
}
// return the rpc message.
InboundRpcMessage m = new InboundRpcMessage(header.getMode(), header.getRpcType(), header.getCoordinationId(), pBody, dBody);
// move the reader index forward so the next rpc call won't try to work with it.
buffer.skipBytes(dBodyLength);
messageCounter.incrementAndGet();
if (RpcConstants.SOME_DEBUGGING) {
logger.debug("Inbound Rpc Message Decoded {}.", m);
}
out.add(m);
}
use of io.netty.buffer.ByteBufInputStream in project ArsMagica2 by Mithion.
the class AMPacketProcessorClient method onPacketData.
@SubscribeEvent
public void onPacketData(ClientCustomPacketEvent event) {
ByteBufInputStream bbis = new ByteBufInputStream(event.packet.payload());
byte packetID = -1;
try {
if (event.packet.getTarget() != Side.CLIENT) {
return;
}
// constant details all packets share: ID, player, and remaining data
packetID = bbis.readByte();
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
byte[] remaining = new byte[bbis.available()];
bbis.readFully(remaining);
switch(packetID) {
case AMPacketIDs.SPELL_CAST:
handleSpellCast(remaining);
break;
case AMPacketIDs.MAGIC_LEVEL_UP:
handleMagicLevelUpResponse(remaining);
break;
case AMPacketIDs.PARTICLE_SPAWN_SPECIAL:
AMCore.instance.proxy.particleManager.handleClientPacketData(Minecraft.getMinecraft().theWorld, remaining);
break;
case AMPacketIDs.PLAYER_VELOCITY_ADD:
handleVelocityAdd(remaining);
break;
case AMPacketIDs.FLASH_ARMOR_PIECE:
handleFlashArmor(remaining);
break;
case AMPacketIDs.REMOVE_BUFF_EFFECT:
handleRemoveBuffEffect(remaining);
break;
case AMPacketIDs.SYNC_EXTENDED_PROPS:
handleSyncProps(remaining);
break;
case AMPacketIDs.SYNC_BETA_PARTICLES:
handleSyncBetaParticles(remaining);
break;
case AMPacketIDs.REQUEST_BETA_PARTICLES:
handleRequestBetaParticles(remaining);
break;
case AMPacketIDs.SYNC_AIR_CHANGE:
handleSyncAir(remaining);
break;
case AMPacketIDs.SYNC_AFFINITY_DATA:
handleSyncAffinity(remaining);
break;
case AMPacketIDs.SYNC_SPELL_KNOWLEDGE:
handleSyncSpellKnowledge(remaining);
break;
case AMPacketIDs.ENTITY_ACTION_UPDATE:
handleEntityActionUpdate(remaining, player);
break;
case AMPacketIDs.PLAYER_LOGIN_DATA:
handlePlayerLoginData(remaining, player);
break;
case AMPacketIDs.NBT_DUMP:
handleNBTDump(player);
break;
case AMPacketIDs.SET_MAG_WORK_REC:
handleSetMagicicansWorkbenchRecipe(player);
break;
case AMPacketIDs.COMPENDIUM_UNLOCK:
handleCompendiumUnlock(remaining);
break;
case AMPacketIDs.SYNC_WORLD_NAME:
handleSyncWorldName(remaining);
break;
case AMPacketIDs.STAR_FALL:
handleStarFall(remaining);
break;
case AMPacketIDs.HIDDEN_COMPONENT_UNLOCK:
Minecraft.getMinecraft().guiAchievement.func_146256_a(ArcaneCompendium.componentUnlock);
break;
case AMPacketIDs.SPELL_APPLY_EFFECT:
handleSpellApplyEffect(remaining);
break;
case AMPacketIDs.HECATE_DEATH:
handleHecateDeath(remaining);
break;
case AMPacketIDs.REQUEST_PWR_PATHS:
handleRcvPowerPaths(remaining);
break;
case AMPacketIDs.CAPABILITY_CHANGE:
handleCapabilityChange(remaining);
break;
case AMPacketIDs.CRAFTING_ALTAR_DATA:
handleCraftingAltarData(remaining);
break;
case AMPacketIDs.LECTERN_DATA:
handleLecternData(remaining);
break;
case AMPacketIDs.CALEFACTOR_DATA:
handleCalefactorData(remaining);
break;
case AMPacketIDs.OBELISK_DATA:
handleObeliskData(remaining);
break;
}
} catch (Throwable t) {
LogHelper.error("Client Packet Failed to Handle!");
LogHelper.error("Packet Type: " + packetID);
t.printStackTrace();
} finally {
try {
if (bbis != null)
bbis.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Aggregations