List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(Charset charset);
From source file:com.corundumstudio.socketio.handler.InPacketHandler.java
License:Apache License
@Override protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, PacketsMessage message) throws Exception { ByteBuf content = message.getContent(); ClientHead client = message.getClient(); if (log.isTraceEnabled()) { log.trace("In message: {} sessionId: {}", content.toString(CharsetUtil.UTF_8), client.getSessionId()); }//from w w w . j a v a 2s .co m while (content.isReadable()) { try { Packet packet = decoder.decodePackets(content, client); if (packet.hasAttachments() && !packet.isAttachmentsLoaded()) { return; } Namespace ns = namespacesHub.get(packet.getNsp()); if (ns == null) { if (packet.getSubType() == PacketType.CONNECT) { Packet p = new Packet(PacketType.MESSAGE); p.setSubType(PacketType.ERROR); p.setNsp(packet.getNsp()); p.setData("Invalid namespace"); client.send(p); return; } log.debug("Can't find namespace for endpoint: {}, sessionId: {} probably it was removed.", packet.getNsp(), client.getSessionId()); return; } if (packet.getSubType() == PacketType.CONNECT) { client.addNamespaceClient(ns); } NamespaceClient nClient = client.getChildClient(ns); if (nClient == null) { log.debug( "Can't find namespace client in namespace: {}, sessionId: {} probably it was disconnected.", ns.getName(), client.getSessionId()); return; } packetListener.onPacket(packet, nClient, message.getTransport()); } catch (Exception ex) { String c = content.toString(CharsetUtil.UTF_8); log.error( "Error during data processing. Client sessionId: " + client.getSessionId() + ", data: " + c, ex); throw ex; } } }
From source file:com.corundumstudio.socketio.handler.PacketHandler.java
License:Apache License
@Override protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, PacketsMessage message) throws Exception { ByteBuf content = message.getContent(); MainBaseClient client = message.getClient(); if (log.isTraceEnabled()) { log.trace("In message: {} sessionId: {}", content.toString(CharsetUtil.UTF_8), client.getSessionId()); }/* w w w. j a v a 2s.co m*/ while (content.isReadable()) { try { Packet packet = decoder.decodePackets(content, client.getSessionId()); Namespace ns = namespacesHub.get(packet.getEndpoint()); if (ns == null) { log.debug("Can't find namespace for endpoint: {}, sessionId: {} probably it was removed.", packet.getEndpoint(), client.getSessionId()); return; } if (packet.getType() == PacketType.CONNECT) { client.addChildClient(ns); } NamespaceClient nClient = (NamespaceClient) client.getChildClient(ns); if (nClient == null) { log.debug( "Can't find namespace client in namespace: {}, sessionId: {} probably it was disconnected.", ns.getName(), client.getSessionId()); return; } packetListener.onPacket(packet, nClient); } catch (Exception ex) { String c = content.toString(CharsetUtil.UTF_8); log.error( "Error during data processing. Client sessionId: " + client.getSessionId() + ", data: " + c, ex); return; } } }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private Packet decodePacket(ByteBuf buffer, UUID uuid) throws IOException { if (buffer.readableBytes() < 3) { throw new DecoderException("Can't parse " + buffer.toString(CharsetUtil.UTF_8)); }/*from w ww. j a va 2s. c o m*/ PacketType type = getType(buffer); int readerIndex = buffer.readerIndex() + 1; // 'null' to avoid unnecessary StringBuilder creation boolean hasData = false; StringBuilder messageId = null; for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) { if (messageId == null) { messageId = new StringBuilder(4); } byte msg = buffer.getByte(readerIndex); if (msg == Packet.SEPARATOR) { break; } if (msg != (byte) '+') { messageId.append((char) msg); } else { hasData = true; } } Long id = null; if (messageId != null && messageId.length() > 0) { id = Long.valueOf(messageId.toString()); } // 'null' to avoid unnecessary StringBuilder creation StringBuilder endpointBuffer = null; for (readerIndex += 1; readerIndex < buffer.readableBytes(); readerIndex++) { if (endpointBuffer == null) { endpointBuffer = new StringBuilder(); } byte msg = buffer.getByte(readerIndex); if (msg == Packet.SEPARATOR) { break; } endpointBuffer.append((char) msg); } String endpoint = Namespace.DEFAULT_NAME; if (endpointBuffer != null && endpointBuffer.length() > 0) { endpoint = endpointBuffer.toString(); } if (buffer.readableBytes() == readerIndex) { buffer.readerIndex(buffer.readableBytes()); } else { readerIndex += 1; buffer.readerIndex(readerIndex); } Packet packet = new Packet(type); packet.setEndpoint(endpoint); if (id != null) { packet.setId(id); if (hasData) { packet.setAck(Packet.ACK_DATA); } else { packet.setAck(true); } } switch (type) { case ERROR: { if (!buffer.isReadable()) { break; } String[] pieces = buffer.toString(CharsetUtil.UTF_8).split("\\+"); if (pieces.length > 0 && pieces[0].trim().length() > 0) { ErrorReason reason = ErrorReason.valueOf(Integer.valueOf(pieces[0])); packet.setReason(reason); if (pieces.length > 1) { ErrorAdvice advice = ErrorAdvice.valueOf(Integer.valueOf(pieces[1])); packet.setAdvice(advice); } } break; } case MESSAGE: { if (buffer.isReadable()) { packet.setData(buffer.toString(CharsetUtil.UTF_8)); } else { packet.setData(""); } break; } case EVENT: { ByteBufInputStream in = new ByteBufInputStream(buffer); Event event = jsonSupport.readValue(in, Event.class); packet.setName(event.getName()); if (event.getArgs() != null) { packet.setArgs(event.getArgs()); } break; } case JSON: { ByteBufInputStream in = new ByteBufInputStream(buffer); JsonObject obj = jsonSupport.readValue(in, JsonObject.class); if (obj != null) { packet.setData(obj.getObject()); } else { in.reset(); Object object = jsonSupport.readValue(in, Object.class); packet.setData(object); } break; } case CONNECT: { if (buffer.isReadable()) { packet.setQs(buffer.toString(CharsetUtil.UTF_8)); } break; } case ACK: { if (!buffer.isReadable()) { break; } boolean validFormat = true; int plusIndex = -1; for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { byte dataChar = buffer.getByte(i); if (!Character.isDigit(dataChar)) { if (dataChar == '+') { plusIndex = i; break; } else { validFormat = false; break; } } } if (!validFormat) { break; } if (plusIndex == -1) { packet.setAckId(parseLong(buffer)); break; } else { packet.setAckId(parseLong(buffer, plusIndex)); buffer.readerIndex(plusIndex + 1); ByteBufInputStream in = new ByteBufInputStream(buffer); AckCallback<?> callback = ackManager.getCallback(uuid, packet.getAckId()); AckArgs args = jsonSupport.readAckArgs(in, callback); packet.setArgs(args.getArgs()); } break; } case DISCONNECT: case HEARTBEAT: case NOOP: break; } buffer.readerIndex(buffer.readerIndex() + buffer.readableBytes()); return packet; }
From source file:com.corundumstudio.socketio.parser.Decoder.java
License:Apache License
private PacketType getType(ByteBuf buffer) { int typeId = buffer.getByte(buffer.readerIndex()) & 0xF; if (typeId >= PacketType.VALUES.length || buffer.getByte(buffer.readerIndex() + 1) != Packet.SEPARATOR) { throw new DecoderException("Can't parse " + buffer.toString(CharsetUtil.UTF_8)); }/*from w w w.j av a 2 s . co m*/ return PacketType.valueOf(typeId); }
From source file:com.corundumstudio.socketio.parser.EncoderAckPacketTest.java
License:Apache License
@Test public void testEncode() throws IOException { Packet packet = new Packet(PacketType.ACK); packet.setAckId(140L);/*from w w w . j a v a 2 s . c o m*/ ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("6:::140", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.parser.EncoderAckPacketTest.java
License:Apache License
@Test public void testEncodeWithArgs() throws IOException { Packet packet = new Packet(PacketType.ACK); packet.setAckId(12L);/*from ww w . jav a 2s . c om*/ // packet.setArgs(Arrays.<Object>asList("woot", "wa")); ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("6:::12+[\"woot\",\"wa\"]", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.parser.EncoderConnectionPacketTest.java
License:Apache License
@Test public void testEncodeDisconnection() throws IOException { Packet packet = new Packet(PacketType.DISCONNECT); packet.setNsp("/woot"); ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("0::/woot", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.parser.EncoderConnectionPacketTest.java
License:Apache License
@Test public void testEncode() throws IOException { Packet packet = new Packet(PacketType.CONNECT); packet.setNsp("/tobi"); ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("1::/tobi", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.parser.EncoderConnectionPacketTest.java
License:Apache License
@Test public void testEncodePacketWithQueryString() throws IOException { Packet packet = new Packet(PacketType.CONNECT); packet.setNsp("/test"); // packet.setQs("?test=1"); ByteBuf result = Unpooled.buffer(); // encoder.encodePacket(packet, result); Assert.assertEquals("1::/test:?test=1", result.toString(CharsetUtil.UTF_8)); }
From source file:com.corundumstudio.socketio.parser.EncoderErrorPacketTest.java
License:Apache License
@Test public void testEncode() throws IOException { Packet packet = new Packet(PacketType.ERROR); ByteBuf result = Unpooled.buffer(); encoder.encodePacket(packet, result); Assert.assertEquals("7::", result.toString(CharsetUtil.UTF_8)); }