Search in sources :

Example 6 with EzyUdpReceivedPacket

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

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketWithSessionTest.

@Test
public void handleReceivedUdpPacketWithSessionTest() {
    // 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);
    Session session = mock(Session.class);
    when(session.getRemoteAddress()).thenReturn(tcpAddress);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(session);
    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(session, 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) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Session(org.eclipse.jetty.websocket.api.Session) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySimpleSession(com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession) Test(org.testng.annotations.Test)

Example 7 with EzyUdpReceivedPacket

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

the class EzySimpleNioUdpDataHandlerTest method testNotHandshakeCase.

@Test
public void testNotHandshakeCase() 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);
    EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
    when(handlerGroupManager.getHandlerGroup(packet.getAddress())).thenReturn(handlerGroup);
    EzyNioSession session = new EzySimpleSession();
    session.setToken(sessionToken);
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.getClientAddress()).thenReturn(new InetSocketAddress(12345));
    session.setChannel(channel);
    when(handlerGroup.getSession()).thenReturn(session);
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) InetSocketAddress(java.net.InetSocketAddress) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) ByteBuffer(java.nio.ByteBuffer) EzySimpleSession(com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) Test(org.testng.annotations.Test)

Example 8 with EzyUdpReceivedPacket

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

the class EzySimpleNioUdpDataHandlerTest method testInvalidMessageCase.

@Test
public void testInvalidMessageCase() throws Exception {
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    byte[] bytes = new byte[] { 1, 2, 3 };
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    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) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Example 9 with EzyUdpReceivedPacket

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

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketInvalidMessageTest.

@Test
public void handleReceivedUdpPacketInvalidMessageTest() {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    byte[] messageBytes = new byte[0];
    EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
    when(packet.getBytes()).thenReturn(messageBytes);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
    // then
    sut.destroy();
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Example 10 with EzyUdpReceivedPacket

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

the class EzyNioUdpReader method processReadBytes.

private void processReadBytes(DatagramChannel channel) throws Exception {
    buffer.clear();
    InetSocketAddress address = (InetSocketAddress) channel.receive(buffer);
    if (address == null) {
        logger.info("has no data in udp channel: {}", channel);
        return;
    }
    int byteCount = buffer.position();
    if (byteCount > 0) {
        buffer.flip();
        byte[] binary = new byte[buffer.limit()];
        buffer.get(binary);
        EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(channel, address, binary);
        udpDataHandler.fireUdpPacketReceived(packet);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket)

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