use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroup method handleReceivedMessage.
private void handleReceivedMessage(EzyMessage message) {
try {
EzyMessageHeader header = message.getHeader();
if (header.isRawBytes()) {
boolean sessionStreamingEnable = session.isStreamingEnable();
if (!streamingEnable || !sessionStreamingEnable) {
return;
}
byte[] rawBytes = message.getContent();
EzySocketStream stream = new EzySimpleSocketStream(session, rawBytes);
streamQueue.add(stream);
} else {
Object data = decodeMessage(message);
int dataSize = message.getByteCount();
handleReceivedData(data, dataSize);
}
} catch (Exception e) {
fireExceptionCaught(e);
} finally {
executeAddReadBytes(message.getByteCount());
}
}
use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandler method handleReceivedUdpPacket.
protected void handleReceivedUdpPacket(EzyUdpReceivedPacket packet) {
try {
EzyMessage message = EzyMessageReaders.bytesToMessage(packet.getBytes());
if (message == null) {
return;
}
InetSocketAddress udpAddress = packet.getAddress();
Object socketChannel = handlerGroupManager.getSocketChannel(udpAddress);
if (socketChannel != null) {
SocketAddress tcpAddress = (socketChannel instanceof SocketChannel) ? ((SocketChannel) socketChannel).getRemoteAddress() : ((Session) socketChannel).getRemoteAddress();
if (isOneClient(tcpAddress, udpAddress)) {
socketDataReceiver.udpReceive(socketChannel, message);
}
} else {
EzyMessageHeader header = message.getHeader();
if (header.isUdpHandshake()) {
handleUdpHandshake(packet.getChannel(), udpAddress, message);
}
}
} catch (Exception e) {
logger.warn("handle received udp package: {} error: {}({})", packet, e.getClass().getName(), e.getMessage());
}
}
use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method handleReceivedMessageHeaderIsNotRawBytes.
@Test
public void handleReceivedMessageHeaderIsNotRawBytes() throws Exception {
// given
EzySimpleNioHandlerGroup sut = newHandlerGroup();
EzyMessage message = mock(EzyMessage.class);
EzyMessageHeader header = mock(EzyMessageHeader.class);
when(message.getHeader()).thenReturn(header);
// when
MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
// then
verify(header, times(1)).isRawBytes();
EzySimpleMessageDataDecoder decoder = FieldUtil.getFieldValue(sut, "decoder");
EzyByteToObjectDecoder d = FieldUtil.getFieldValue(decoder, "decoder");
EzySession session = FieldUtil.getFieldValue(sut, "session");
verify(d, times(1)).decode(message, session.getSessionKey());
}
use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleUdpHandshakeTokenSizeGreaterThan512.
@Test
public void handleUdpHandshakeTokenSizeGreaterThan512() {
// given
EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
DatagramChannel channel = mock(DatagramChannel.class);
InetSocketAddress address = new InetSocketAddress(3005);
EzyMessage message = mock(EzyMessage.class);
String sessionToken = RandomUtil.randomAlphabetString(514);
long sessionId = 12345L;
int tokenSize = sessionToken.length();
int messageSize = 0;
// sessionIdSize
messageSize += 8;
// tokenLengthSize
messageSize += 2;
// messageSize
messageSize += tokenSize;
ByteBuffer buffer = ByteBuffer.allocate(messageSize);
buffer.putLong(sessionId);
buffer.putShort((short) tokenSize);
buffer.put(sessionToken.getBytes());
buffer.flip();
byte[] bytes = EzyByteBuffers.getBytes(buffer);
when(message.getContent()).thenReturn(bytes);
// when
MethodInvoker.create().object(sut).method("handleUdpHandshake").param(DatagramChannel.class, channel).param(InetSocketAddress.class, address).param(EzyMessage.class, message).call();
// then
verify(message, times(1)).getContent();
sut.destroy();
}
use of com.tvd12.ezyfox.codec.EzyMessage in project ezyfox-server by youngmonkeys.
the class EzySocketDataReceiverTest method udpReceiveButExceptionTest.
@Test
public void udpReceiveButExceptionTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).threadPoolSize(1).build();
SocketChannel channel = mock(SocketChannel.class);
EzyMessage message = mock(EzyMessage.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
doThrow(new RuntimeException()).when(handlerGroup).fireMessageReceived(message);
when(handlerGroupManager.getHandlerGroup(channel)).thenReturn(handlerGroup);
// when
sut.udpReceive(channel, message);
Thread.sleep(120);
// then
verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
verify(handlerGroup, times(1)).fireMessageReceived(message);
sut.destroy();
}
Aggregations