Created
November 13, 2015 21:21
-
-
Save SharpMan/894e8f9606649c5d493e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package koh.protocol.client.codec; | |
import java.util.Date; | |
import koh.protocol.MessageQueue; | |
import koh.protocol.client.Message; | |
import koh.protocol.messages.handshake.ProtocolRequired; | |
import org.apache.mina.core.buffer.CachedBufferAllocator; | |
import org.apache.mina.core.buffer.IoBuffer; | |
import org.apache.mina.core.buffer.IoBufferAllocator; | |
import org.apache.mina.core.session.IoSession; | |
import org.apache.mina.filter.codec.ProtocolEncoderOutput; | |
/** | |
* | |
* @author Neo-Craft | |
*/ | |
public class ProtocolEncoder implements org.apache.mina.filter.codec.ProtocolEncoder { | |
private static final int DEFAULT_CAPACITY = 512; | |
private static final int BIT_RIGHT_SHIFT_LEN_PACKET_ID = 2; | |
public static int subComputeStaticHeader(int messageId, int typeLen) { | |
return messageId << BIT_RIGHT_SHIFT_LEN_PACKET_ID | typeLen; | |
} | |
/** | |
* Alleos optimization : make header static | |
* | |
*/ | |
public static void writeHeader(IoBuffer buf, Message message, int len) { | |
buf.putShort((short) subComputeStaticHeader(message.getMessageId(), 3)); | |
buf.put((byte) (len >> 16 & 255)); | |
buf.putShort((short) (len & 65535)); | |
} | |
@Override | |
public void encode(IoSession session, Object input, ProtocolEncoderOutput output) throws Exception { | |
if (input instanceof Message) { | |
this.encodeMessage(session, (Message) input, output); | |
} else if (input instanceof MessageQueue) { | |
this.encodeQueue(session, (MessageQueue) input, output); | |
} else { | |
throw new IllegalArgumentException("I can only encode Message and MessageQueue types"); | |
} | |
} | |
private void encodeMessage(IoSession session, Message message, ProtocolEncoderOutput output) { | |
IoBuffer msgBuffer = IoBuffer.allocate(DEFAULT_CAPACITY) | |
.setAutoExpand(true); | |
writeHeader(msgBuffer, message, 0); | |
message.serialize(msgBuffer); | |
int endOffset = msgBuffer.position(); | |
writeHeader(msgBuffer.position(0), message, endOffset - 5); | |
msgBuffer.position(endOffset).flip(); | |
output.write(msgBuffer); | |
} | |
private void encodeQueue(IoSession session, MessageQueue queue, ProtocolEncoderOutput output) { | |
int initialCapacity = Math.max((queue.size() / 10), 1) * DEFAULT_CAPACITY; | |
IoBuffer msgBuffer = IoBuffer.allocate(initialCapacity) | |
.setAutoExpand(true); | |
for(Message message : queue.get()) { | |
int initialOffset = msgBuffer.position(); | |
writeHeader(msgBuffer, message, 0); | |
message.serialize(msgBuffer); | |
int endOffset = msgBuffer.position(); | |
writeHeader(msgBuffer.position(initialOffset), message, endOffset - 5 - initialOffset); | |
msgBuffer.position(endOffset); | |
} | |
msgBuffer.flip(); | |
output.write(msgBuffer); | |
} | |
@Override | |
public void dispose(IoSession session) throws Exception { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment