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());
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations