use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method writeSuccessTest.
@SuppressWarnings("rawtypes")
@Test
public void writeSuccessTest() throws Exception {
EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
server.setSessionManager(sessionManager);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
when(channel.write(any(ByteBuffer.class), anyBoolean())).thenReturn(123456);
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
EzySimpleNioHandlerGroup group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzySimplePacket packet = new EzySimplePacket();
packet.setBinary(true);
packet.setData("world".getBytes());
packet.setTransportType(EzyTransportType.TCP);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
group.firePacketSend(packet, writeBuffer);
Thread.sleep(1000);
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzySimpleNioHandlerGroupTest method writeFailureTest.
@SuppressWarnings("rawtypes")
@Test
public void writeFailureTest() throws Exception {
EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
EzyStatistics statistics = new EzySimpleStatistics();
EzySimpleSettings settings = new EzySimpleSettings();
EzySimpleStreamingSetting streaming = settings.getStreaming();
streaming.setEnable(true);
EzySimpleServer server = new EzySimpleServer();
server.setSettings(settings);
server.setSessionManager(sessionManager);
EzySimpleServerContext serverContext = new EzySimpleServerContext();
serverContext.setServer(server);
serverContext.init();
EzyChannel channel = mock(EzyChannel.class);
when(channel.isConnected()).thenReturn(true);
when(channel.getConnection()).thenReturn(SocketChannel.open());
when(channel.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
SelectionKey selectionKey = mock(SelectionKey.class);
when(selectionKey.isValid()).thenReturn(true);
session.setProperty(EzyNioSession.SELECTION_KEY, selectionKey);
EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
EzySimpleNioHandlerGroup group = (EzySimpleNioHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.SOCKET).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
EzySimplePacket packet = new EzySimplePacket();
packet.setBinary(true);
packet.setData("world".getBytes());
packet.setTransportType(EzyTransportType.TCP);
ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
group.firePacketSend(packet, writeBuffer);
Thread.sleep(1000);
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel 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();
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzyWsHandler method onConnect.
@OnWebSocketConnect
public void onConnect(Session session) {
long sessionMaxIdleTime = sessionManagementSetting.getSessionMaxIdleTime();
session.setIdleTimeout(sessionMaxIdleTime);
EzyChannel channel = new EzyWsChannel(session);
handlerGroupManager.newHandlerGroup(channel, EzyConnectionType.WEBSOCKET);
}
use of com.tvd12.ezyfoxserver.socket.EzyChannel in project ezyfox-server by youngmonkeys.
the class EzyHandlerGroupManagerImpl method mapSocketChannel.
@Override
public void mapSocketChannel(SocketAddress udpAddress, EzySession session) {
if (session == null) {
return;
}
EzyChannel channel = session.getChannel();
if (channel == null) {
return;
}
Object connection = channel.getConnection();
if (connection == null) {
return;
}
if (groupsByConnection.containsKey(connection)) {
socketChannelByUdpAddress.put(udpAddress, connection);
}
logger.debug("map socket channel to: {}, socketChannelByUdpAddress.size: {}", udpAddress, socketChannelByUdpAddress.size());
}
Aggregations