Java Code Examples for org.apache.mina.filter.codec.ProtocolEncoderOutput#write()
The following examples show how to use
org.apache.mina.filter.codec.ProtocolEncoderOutput#write() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArduinoCodec.java From neoscada with Eclipse Public License 1.0 | 6 votes |
@Override public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput output ) throws Exception { final IoBuffer data = IoBuffer.allocate ( 0 ); data.order ( ByteOrder.LITTLE_ENDIAN ); data.setAutoExpand ( true ); if ( message instanceof WriteRequestMessage ) { encodeHeader ( data, (CommonMessage)message ); encodeWriteRequest ( data, (WriteRequestMessage)message ); } else if ( message instanceof CommonMessage ) { encodeHeader ( data, (CommonMessage)message ); } data.flip (); output.write ( data ); }
Example 2
Source File: WebSocketEncoder.java From red5-websocket with Apache License 2.0 | 6 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { WebSocketConnection conn = (WebSocketConnection) session.getAttribute(Constants.CONNECTION); IoBuffer resultBuffer; if (message instanceof Packet) { Packet packet = (Packet) message; // if the connection is not native / direct, add websocket encoding resultBuffer = conn.isWebConnection() ? encodeOutgoingData(packet) : packet.getData(); } else if (message instanceof HandshakeResponse) { HandshakeResponse resp = (HandshakeResponse) message; resultBuffer = resp.getResponse(); } else if (message instanceof HandshakeRequest) { HandshakeRequest req = (HandshakeRequest) message; resultBuffer = req.getRequest(); } else { throw new Exception("message not a websocket type"); } out.write(resultBuffer); }
Example 3
Source File: HttpServerEncoderImpl.java From game-server with MIT License | 6 votes |
/** {@inheritDoc} */ public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { LOG.debug("encode {}", message.getClass().getCanonicalName()); if ((message instanceof HttpResponseImpl)) { HttpResponseImpl msg = (HttpResponseImpl) message; IoBuffer buf = IoBuffer.allocate(128).setAutoExpand(true); buf.putString(msg.getStatus().line(), ENCODER); for (Map.Entry<String, String> header : msg.getHeaders().entrySet()) { buf.putString((CharSequence) header.getKey(), ENCODER); buf.putString(": ", ENCODER); buf.putString((CharSequence) header.getValue(), ENCODER); buf.put(CRLF); } if (msg.getBody() != null) { buf.putString(CONTENTLENGTH, ENCODER); buf.putString(String.valueOf(msg.getBody().length), ENCODER); buf.put(CRLF); } buf.put(CRLF); if (msg.getBody() != null) { buf.put(msg.getBody()); } buf.flip(); out.write(buf); } }
Example 4
Source File: ProtobufEncoder.java From gameserver with Apache License 2.0 | 6 votes |
/** * Check the id value and set it into context attachment. */ @Override public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { /** * If the message is already in IoBuffer format, * directly output it to client. * * TODO Maybe used to optimize SessionMessage. */ if (msg instanceof IoBuffer) { out.write(msg); } else if ( msg instanceof XinqiMessage ) { XinqiMessage message = (XinqiMessage)msg; if (message != null) { IoBuffer body = encodeXinqiMessage(message); out.write(body); } } }
Example 5
Source File: MyDataEncoder.java From QuickerAndroid with GNU General Public License v3.0 | 6 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message instanceof MessageBase) { Gson gson = new Gson(); String msgJson = gson.toJson(message); byte[] msgBytes = msgJson.getBytes("utf-8"); IoBuffer buffer = IoBuffer.allocate(msgBytes.length + 16); buffer.putInt(0xFFFFFFFF); buffer.putInt(((MessageBase) message).getMessageType()); buffer.putInt(msgBytes.length); buffer.put(msgBytes); buffer.putInt(0); buffer.flip(); out.write(buffer); } }
Example 6
Source File: PrefixedStringEncoder.java From neoscada with Eclipse Public License 1.0 | 5 votes |
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { String value = (String) message; IoBuffer buffer = IoBuffer.allocate(value.length()).setAutoExpand(true); buffer.putPrefixedString(value, prefixLength, charset.newEncoder()); if (buffer.position() > maxDataLength) { throw new IllegalArgumentException("Data length: " + buffer.position()); } buffer.flip(); out.write(buffer); }
Example 7
Source File: GameMsgEncoder.java From TestClient with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { Request value = (Request) message; out.write(value.entireMsg()); value.release(); }
Example 8
Source File: ServerProtocolEncoder.java From seed with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, String message, ProtocolEncoderOutput out) throws Exception { IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true); buffer.putString(message, Charset.forName(SeedConstants.DEFAULT_CHARSET).newEncoder()); buffer.flip(); out.write(buffer); }
Example 9
Source File: MinaProtocolEncoder.java From jforgame with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { CodecContext context = (CodecContext) session.getAttribute(MinaSessionProperties.CODEC_CONTEXT); if (context == null) { context = new CodecContext(); session.setAttribute(MinaSessionProperties.CODEC_CONTEXT, context); } IoBuffer buffer = writeMessage((Message) message); out.write(buffer); }
Example 10
Source File: MinaCodecAdapter.java From dubbox-hystrix with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnectd(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example 11
Source File: RpcResponseEncoder.java From gameserver with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { MessageLite resp = (MessageLite) message; byte[] respBytes = resp.toByteArray(); IoBuffer buf = IoBuffer.allocate(HEADER_LEN+respBytes.length); buf.putInt(respBytes.length); buf.put(respBytes); buf.flip(); out.write(buf); }
Example 12
Source File: WebMessageEncoder.java From cim with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession iosession, Object object, ProtocolEncoderOutput out) throws Exception { Transportable message = (Transportable) object; byte[] data = message.getBody(); /* * websocket的握手响应 */ if (message instanceof HandshakerResponse) { IoBuffer buff = IoBuffer.allocate(data.length).setAutoExpand(true); buff.put(data); buff.flip(); out.write(buff); return; } byte[] protobuf = new byte[data.length + 1]; protobuf[0] = message.getType(); System.arraycopy(data,0, protobuf, 1, data.length); byte[] binaryFrame = encodeDataFrame(protobuf); IoBuffer buffer = IoBuffer.allocate(binaryFrame.length); buffer.put(binaryFrame); buffer.flip(); out.write(buffer); }
Example 13
Source File: CommandEncoder.java From mina with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { BaseCommand command = (BaseCommand) message; byte[] bytes = command.toBytes(); IoBuffer buf = IoBuffer.allocate(bytes.length, false); buf.setAutoExpand(true); buf.putShort( (short) bytes.length ); buf.put(bytes); buf.flip(); out.write(buf); }
Example 14
Source File: RTMPMinaCodecFactory.java From red5-client with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get the connection from the session String sessionId = (String) session.getAttribute(RTMPConnection.RTMP_SESSION_ID); log.trace("Session id: {}", sessionId); RTMPConnection conn = (RTMPConnection) RTMPConnManager.getInstance().getConnectionBySessionId(sessionId); if (conn != null) { Red5.setConnectionLocal(conn); final Semaphore lock = conn.getEncoderLock(); try { // acquire the encoder lock lock.acquire(); // get the buffer final IoBuffer buf = message instanceof IoBuffer ? (IoBuffer) message : getEncoder().encode(message); if (buf != null) { if (log.isTraceEnabled()) { log.trace("Writing output data: {}", Hex.encodeHexString(buf.array())); } out.write(buf); } else { log.trace("Response buffer was null after encoding"); } } catch (Exception ex) { log.error("Exception during encode", ex); } finally { lock.release(); Red5.setConnectionLocal(null); } } else { log.debug("Connection is no longer available for encoding, may have been closed already"); } }
Example 15
Source File: GameMsgEncoder.java From GameServer with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { ResponseMsg value = (ResponseMsg) message; out.write(value.entireMsg()); value.release(); }
Example 16
Source File: ITCHDeflateCodec.java From sailfish-core with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { // Compression is not required for send messages out.write(message); }
Example 17
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnectd(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example 18
Source File: TestTupleEncoder.java From streamsx.topology with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { TestTuple tuple = (TestTuple) message; IoBuffer buffer = IoBuffer .allocate(4 + 4 + tuple.getTupleData().length); buffer.putInt(tuple.getTesterId()); buffer.putInt(tuple.getTupleData().length); buffer.put(tuple.getTupleData()); buffer.flip(); out.write(buffer); }
Example 19
Source File: CougarProtocolEncoder.java From cougar with Apache License 2.0 | 4 votes |
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { final ByteBuffer buffer; if (message instanceof ProtocolMessage) { ProtocolMessage pm = (ProtocolMessage) message; nioLogger.log(PROTOCOL, session, "CougarProtocolEncoder: Writing protocol message %s", pm.getProtocolMessageType()); Byte version = (Byte) session.getAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME); // go for lowest likely common denominator, since this will likely only occur for RejectMessages if (version == null) { version = CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MIN_SUPPORTED; } buffer = pm.getSerialisedForm(version); if (buffer == null) { badMessagesRequested.incrementAndGet(); throw new IllegalArgumentException("Couldn't serialise ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "]"); } switch (pm.getProtocolMessageType()) { case ACCEPT: acceptsSent.incrementAndGet(); break; case CONNECT: connectsSent.incrementAndGet(); break; case REJECT: rejectsSent.incrementAndGet(); break; case KEEP_ALIVE: keepAlivesSent.incrementAndGet(); break; case DISCONNECT: disconnectsSent.incrementAndGet(); break; case MESSAGE_REQUEST: messageRequestsSent.incrementAndGet(); nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", (((RequestMessage) pm).getPayload().length + 8)); break; case MESSAGE_RESPONSE: messageRequestsSent.incrementAndGet(); nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", ((ResponseMessage) pm).getPayload().length); break; case EVENT: eventsSent.incrementAndGet(); nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing event of length %s", ((EventMessage) pm).getPayload().length); break; case SUSPEND: suspendsSent.incrementAndGet(); break; case START_TLS_REQUEST: tlsRequestsSent.incrementAndGet(); break; case START_TLS_RESPONSE: tlsResponsesSent.incrementAndGet(); break; default: badMessagesRequested.incrementAndGet(); throw new IllegalArgumentException("Unknown ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "] received"); } } else { throw new IllegalArgumentException("Unknown message type " + message); } buffer.flip(); out.write(buffer); out.flush(); }
Example 20
Source File: MaplePacketEncoder.java From HeavenMS with GNU Affero General Public License v3.0 | 4 votes |
@Override public void encode(final IoSession session, final Object message, final ProtocolEncoderOutput out) throws Exception { final MapleClient client = (MapleClient) session.getAttribute(MapleClient.CLIENT_KEY); try { if (client.tryacquireEncoder()) { try { final MapleAESOFB send_crypto = client.getSendCrypto(); final byte[] input = (byte[]) message; if (YamlConfig.config.server.USE_DEBUG_SHOW_PACKET) { int packetLen = input.length; int pHeader = readFirstShort(input); String pHeaderStr = Integer.toHexString(pHeader).toUpperCase(); String op = lookupRecv(pHeader); String Recv = "ServerSend:" + op + " [" + pHeaderStr + "] (" + packetLen + ")\r\n"; if (packetLen <= 50000) { String RecvTo = Recv + HexTool.toString(input) + "\r\n" + HexTool.toStringFromAscii(input); System.out.println(RecvTo); if (op == null) { System.out.println("UnknownPacket:" + RecvTo); } } else { FilePrinter.print(FilePrinter.PACKET_STREAM + MapleSessionCoordinator.getSessionRemoteAddress(session) + ".txt", HexTool.toString(new byte[]{input[0], input[1]}) + " ..."); } } final byte[] unencrypted = new byte[input.length]; System.arraycopy(input, 0, unencrypted, 0, input.length); final byte[] ret = new byte[unencrypted.length + 4]; final byte[] header = send_crypto.getPacketHeader(unencrypted.length); MapleCustomEncryption.encryptData(unencrypted); send_crypto.crypt(unencrypted); System.arraycopy(header, 0, ret, 0, 4); System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length); out.write(IoBuffer.wrap(ret)); } finally { client.unlockEncoder(); } } // System.arraycopy(unencrypted, 0, ret, 4, unencrypted.length); // out.write(ByteBuffer.wrap(ret)); } catch (NullPointerException npe) { out.write(IoBuffer.wrap(((byte[]) message))); } }