Java Code Examples for org.apache.mina.filter.codec.ProtocolEncoderOutput#flush()

The following examples show how to use org.apache.mina.filter.codec.ProtocolEncoderOutput#flush() . 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: MassProtocolEncoder.java    From game-server with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * 编码,格式:数据长度|数据部分
 */
@Override
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out)
        throws Exception {
    if (getOverScheduledWriteBytesHandler() != null && session.getScheduledWriteMessages() > getMaxScheduledWriteMessages() && getOverScheduledWriteBytesHandler().test(session)) {
        return;
    }
    IoBuffer buf = null;
    if (obj instanceof MassMessage) {
        buf = MsgUtil.toIobuffer((MassMessage) obj);
    } else {
        log.warn("未知的数据类型");
        return;
    }
    if (buf != null && session.isConnected()) {
        buf.rewind();
        out.write(buf);
        out.flush();
    }
}
 
Example 2
Source File: MinaCodecAdapter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
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.removeChannelIfDisconnected(session);
    }
    out.write(ByteBuffer.wrap(buffer.toByteBuffer()));
    out.flush();
}
 
Example 3
Source File: MinaCodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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 4
Source File: MinaCodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
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 5
Source File: MinaCodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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 6
Source File: MinaCodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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 7
Source File: ClientProtocolEncoder.java    From game-server with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) throws Exception {
	if (getOverScheduledWriteBytesHandler() != null
			&& session.getScheduledWriteMessages() > getMaxScheduledWriteMessages()
			&& getOverScheduledWriteBytesHandler().test(session)) {
		LOGGER.warn("{}消息{}大于最大累积{}",MsgUtil.getIp(session), session.getScheduledWriteMessages(),getMaxScheduledWriteMessages());
		return;
	}

	IoBuffer buf = null;
	if (obj instanceof Message) {
		buf = MsgUtil.toGameClientIobuffer((Message) obj);
	} else if (obj instanceof byte[]) {
		byte[] data = (byte[]) obj; // 消息ID(4字节)+protobuf
		buf = IoBuffer.allocate(data.length + 6);
		// 消息长度
		byte[] lengthBytes = IntUtil.short2Bytes((short) (data.length + 4), ByteOrder.LITTLE_ENDIAN);
		buf.put(lengthBytes);

		// 消息ID ,将顺序改变为前端客户端顺序
		byte[] idBytes = new byte[4];
		idBytes[0] = data[3];
		idBytes[1] = data[2];
		idBytes[2] = data[1];
		idBytes[3] = data[0];
		buf.put(idBytes);
		// protobuf长度
		int protobufLength = data.length - 4; // 移除消息ID长度
		buf.put(IntUtil.writeIntToBytesLittleEnding(protobufLength));
		// 数据
		buf.put(data, 4, protobufLength);

	}

	if (buf != null && session.isConnected()) {
		buf.rewind();
		out.write(buf);
		out.flush();
	}
}
 
Example 8
Source File: CougarProtocolEncoder.java    From cougar with Apache License 2.0 4 votes vote down vote up
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();
}