/** * Creates a new TCP frame for the specified framed data. * * @param data The data to frame. */ public TcpFrame(final ByteBuffer data) { this(MinaUtils.toByteArray(data)); }
@Override protected DecodingState finishDecode(final ByteBuffer readData, final ProtocolDecoderOutput out) throws Exception { // This copy is not ideal, but passing around ByteBuffers was // causing issues. final byte[] transactionId = MinaUtils.toByteArray(readData); m_log.debug("Read transaction id..."); if (this.m_messageLength > 0) { return new ReadBody(this.m_messageType, this.m_messageLength, transactionId); } else { m_log.debug("Handling empty body"); final StunMessage message = createMessage(this.m_messageType, transactionId, EMPTY_ATTRIBUTES); out.write(message); return null; } } }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { // This override is key because OutputStream typically calls write(byte) // for each byte here. We need to overwrite that because otherwise // we'd wrap every single byte in a TCP frame, so this takes care of // most cases. Most code will generally use the bulk write methods, // so we should be in fairly good shape. final byte[] subArray = MinaUtils.toByteArray(ByteBuffer.wrap(b, off, len)); if (m_log.isDebugEnabled()) { //final String dataString = new String(subArray, "US-ASCII"); //m_log.debug("Sending data:\n{}", dataString); m_log.debug("Data length is: "+subArray.length); m_rawBytesWritten += subArray.length; m_log.debug("Raw bytes written: {}", m_rawBytesWritten); } write(new TcpFrame(subArray)); }
buffers.add(toByteArray(buffer)); totalSent += chunkSize; buffers.add(toByteArray(buffer));
private void wrapInSendIndication(final StunMessage msg) { final StunMessageEncoder encoder = new StunMessageEncoder(); final ByteBuffer buf = encoder.encode(msg); final InetSocketAddress remoteAddress = m_mapper.get(msg); if (remoteAddress == null) { m_log.warn("No matching transaction ID for: {} in {}", msg, m_mapper); return; } final byte[] bytes = MinaUtils.toByteArray(buf); m_log.debug("Sending TCP framed data of length: {}", bytes.length); final SendIndication indication = new SendIndication(remoteAddress, bytes); final ByteBuffer indicationBuf = encoder.encode(indication); m_out.write(indicationBuf); }
private static UUID createTransactionId() { final UUID id = UUID.randomUUID(); final byte[] idBytes = id.getRawBytes(); final ByteBuffer idBuf = ByteBuffer.wrap(idBytes); // Lower the limit to make room for the magic cookie. idBuf.limit(idBytes.length - 4); final ByteBuffer newIdBuf = ByteBuffer.allocate(16); MinaUtils.putUnsignedInt(newIdBuf, MAGIC_COOKIE); newIdBuf.put(idBuf); newIdBuf.flip(); return new UUID(MinaUtils.toByteArray(newIdBuf)); }