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