Search in sources :

Example 1 with EzyMessageHeader

use of com.tvd12.ezyfox.codec.EzyMessageHeader 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());
    }
}
Also used : EzySimpleSocketStream(com.tvd12.ezyfoxserver.socket.EzySimpleSocketStream) EzySocketStream(com.tvd12.ezyfoxserver.socket.EzySocketStream)

Example 2 with EzyMessageHeader

use of com.tvd12.ezyfox.codec.EzyMessageHeader 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());
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyMessageHeader(com.tvd12.ezyfox.codec.EzyMessageHeader) InetSocketAddress(java.net.InetSocketAddress) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with EzyMessageHeader

use of com.tvd12.ezyfox.codec.EzyMessageHeader 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());
}
Also used : EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 4 with EzyMessageHeader

use of com.tvd12.ezyfox.codec.EzyMessageHeader in project ezyfox-server by youngmonkeys.

the class EzySimpleNioHandlerGroupTest method handleReceivedMessageStreamingNotEnable.

@Test
public void handleReceivedMessageStreamingNotEnable() throws Exception {
    // given
    EzySimpleNioHandlerGroup sut = newHandlerGroup(false);
    EzyMessage message = mock(EzyMessage.class);
    EzyMessageHeader header = mock(EzyMessageHeader.class);
    when(header.isRawBytes()).thenReturn(true);
    when(message.getHeader()).thenReturn(header);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
    // then
    verify(header, times(1)).isRawBytes();
}
Also used : EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 5 with EzyMessageHeader

use of com.tvd12.ezyfox.codec.EzyMessageHeader in project ezyfox-server by youngmonkeys.

the class EzySimpleNioHandlerGroupTest method handleReceivedMessageSessionStreamingNotEnable.

@Test
public void handleReceivedMessageSessionStreamingNotEnable() throws Exception {
    // given
    EzySimpleNioHandlerGroup sut = newHandlerGroup();
    EzyMessage message = mock(EzyMessage.class);
    EzyMessageHeader header = mock(EzyMessageHeader.class);
    when(header.isRawBytes()).thenReturn(true);
    when(message.getHeader()).thenReturn(header);
    EzySession session = FieldUtil.getFieldValue(sut, "session");
    when(session.isStreamingEnable()).thenReturn(false);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedMessage").param(EzyMessage.class, message).call();
    // then
    verify(header, times(1)).isRawBytes();
    verify(session, times(1)).isStreamingEnable();
}
Also used : EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

EzySimpleNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup)3 BaseTest (com.tvd12.test.base.BaseTest)3 Test (org.testng.annotations.Test)3 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)2 EzyMessage (com.tvd12.ezyfox.codec.EzyMessage)1 EzyMessageHeader (com.tvd12.ezyfox.codec.EzyMessageHeader)1 EzySimpleSocketStream (com.tvd12.ezyfoxserver.socket.EzySimpleSocketStream)1 EzySocketStream (com.tvd12.ezyfoxserver.socket.EzySocketStream)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 SocketChannel (java.nio.channels.SocketChannel)1