Search in sources :

Example 1 with EzyNioHandlerGroup

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.

the class EzyNioSocketAcceptor method doAcceptConnection.

private void doAcceptConnection(SocketChannel clientChannel) throws Exception {
    clientChannel.configureBlocking(false);
    clientChannel.socket().setTcpNoDelay(tcpNoDelay);
    EzyChannel channel = new EzyNioSocketChannel(clientChannel);
    EzyNioHandlerGroup handlerGroup = handlerGroupManager.newHandlerGroup(channel, EzyConnectionType.SOCKET);
    EzyNioSession session = handlerGroup.getSession();
    SelectionKey selectionKey = clientChannel.register(readSelector, SelectionKey.OP_READ);
    session.setProperty(EzyNioSession.SELECTION_KEY, selectionKey);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)

Example 2 with EzyNioHandlerGroup

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.

the class EzySocketDataReceiver method processReadBytes.

private void processReadBytes(SocketChannel channel, ByteBuffer buffer) throws Exception {
    buffer.flip();
    byte[] binary = new byte[buffer.limit()];
    buffer.get(binary);
    EzyNioHandlerGroup handlerGroup = handlerGroupManager.getHandlerGroup(channel);
    if (handlerGroup != null) {
        handlerGroup.fireBytesReceived(binary);
    }
}
Also used : EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup)

Example 3 with EzyNioHandlerGroup

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.

the class EzyNioSocketAcceptorTest method acceptConnectionTest.

@Test
public void acceptConnectionTest() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
    Selector readSelector = Selector.open();
    EzyNioSocketAcceptor sut = new EzyNioSocketAcceptor();
    sut.setReadSelector(readSelector);
    SocketChannel clientChannel = SocketChannel.open();
    when(handlerGroupManager.newHandlerGroup(any(), any())).thenReturn(handlerGroup);
    sut.setHandlerGroupManager(handlerGroupManager);
    EzyNioSession session = mock(EzyNioSession.class);
    when(handlerGroup.getSession()).thenReturn(session);
    // when
    MethodInvoker.create().object(sut).method("acceptConnection").param(SocketChannel.class, clientChannel).call();
    // then
    verify(handlerGroupManager, times(1)).newHandlerGroup(any(), any());
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Selector(java.nio.channels.Selector) AbstractSelector(java.nio.channels.spi.AbstractSelector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 4 with EzyNioHandlerGroup

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup 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();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) Test(org.testng.annotations.Test)

Example 5 with EzyNioHandlerGroup

use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.

the class EzySocketDataReceiverTest method udpReceiveTest.

@Test
public void udpReceiveTest() 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);
    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();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) Test(org.testng.annotations.Test)

Aggregations

EzyNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup)6 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)4 Test (org.testng.annotations.Test)4 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)3 SocketChannel (java.nio.channels.SocketChannel)3 EzyMessage (com.tvd12.ezyfox.codec.EzyMessage)2 EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)2 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)2 EzySimpleSession (com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession)1 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)1 EzyNioSocketAcceptor (com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor)1 EzySimpleUdpReceivedPacket (com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket)1 EzyUdpReceivedPacket (com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket)1 BaseTest (com.tvd12.test.base.BaseTest)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 AbstractSelector (java.nio.channels.spi.AbstractSelector)1