Search in sources :

Example 1 with EzyUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket 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 2 with EzyUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    String sessionToken = "12345678";
    long sessionId = 12345L;
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
    byte header = 0;
    header |= 1 << 5;
    buffer.put(header);
    buffer.putShort((short) messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    EzySession session = spy(EzyAbstractSession.class);
    session.setToken(sessionToken);
    when(sessionManager.getSession(sessionId)).thenReturn(session);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzyDatagramChannelPool channelPool = new EzyDatagramChannelPool(1);
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.setSessionManager(sessionManager);
    handler.setResponseApi(responseApi);
    handler.setDatagramChannelPool(channelPool);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzyDatagramChannelPool(com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) Test(org.testng.annotations.Test)

Example 3 with EzyUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method testExceptionCase.

@Test
public void testExceptionCase() throws Exception {
    String sessionToken = "12345678";
    long sessionId = 12345L;
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
    byte header = 0;
    buffer.put(header);
    buffer.putShort((short) messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getHandlerGroup(packet.getAddress())).thenThrow(new IllegalArgumentException("test"));
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) ByteBuffer(java.nio.ByteBuffer) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Example 4 with EzyUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketTest.

@Test
public void handleReceivedUdpPacketTest() throws Exception {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    byte[] messageBytes = messageBytes();
    InetSocketAddress udpAddress = new InetSocketAddress(3005);
    InetSocketAddress tcpAddress = new InetSocketAddress(123);
    EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
    when(packet.getBytes()).thenReturn(messageBytes);
    when(packet.getAddress()).thenReturn(udpAddress);
    SocketChannel socketChannel = mock(SocketChannel.class);
    when(socketChannel.getRemoteAddress()).thenReturn(tcpAddress);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
    sut.setHandlerGroupManager(handlerGroupManager);
    EzySocketDataReceiver socketDataReceiver = mock(EzySocketDataReceiver.class);
    sut.setSocketDataReceiver(socketDataReceiver);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
    // then
    verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
    verify(socketChannel, times(1)).getRemoteAddress();
    verify(socketDataReceiver, times(1)).udpReceive(any(SocketChannel.class), any(EzyMessage.class));
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Example 5 with EzyUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketIsNotOneClientTest.

@Test
public void handleReceivedUdpPacketIsNotOneClientTest() throws Exception {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    byte[] messageBytes = messageBytes();
    InetSocketAddress udpAddress = new InetSocketAddress("client1", 3005);
    InetSocketAddress tcpAddress = new InetSocketAddress("client2", 123);
    EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
    when(packet.getBytes()).thenReturn(messageBytes);
    when(packet.getAddress()).thenReturn(udpAddress);
    SocketChannel socketChannel = mock(SocketChannel.class);
    when(socketChannel.getRemoteAddress()).thenReturn(tcpAddress);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
    sut.setHandlerGroupManager(handlerGroupManager);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
    // then
    verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
    verify(socketChannel, times(1)).getRemoteAddress();
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Aggregations

EzyUdpReceivedPacket (com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket)9 InetSocketAddress (java.net.InetSocketAddress)9 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)8 Test (org.testng.annotations.Test)8 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)6 EzySimpleUdpReceivedPacket (com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket)5 SocketChannel (java.nio.channels.SocketChannel)4 EzyMessage (com.tvd12.ezyfox.codec.EzyMessage)3 ByteBuffer (java.nio.ByteBuffer)3 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)2 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)2 EzySimpleSession (com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession)2 EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)2 EzyMessageHeader (com.tvd12.ezyfox.codec.EzyMessageHeader)1 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)1 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)1 EzyNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup)1 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)1 EzyDatagramChannelPool (com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool)1 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)1