Skip to content

Commit

Permalink
Can not receive UDP request when a client has TCP IP and UDP IP are n…
Browse files Browse the repository at this point in the history
…ot same (#104)

* Can not receive UDP request when a client has TCP IP and UDP IP are not same

* update unit test
  • Loading branch information
tvd12 committed Sep 28, 2022
1 parent 2f9502c commit e7431f5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.tvd12.ezyfox.io.EzyBytes;
import com.tvd12.ezyfox.io.EzyInts;
import com.tvd12.ezyfox.io.EzyLongs;
import com.tvd12.ezyfox.net.EzySocketAddresses;
import com.tvd12.ezyfox.util.EzyDestroyable;
import com.tvd12.ezyfox.util.EzyLoggable;
import com.tvd12.ezyfoxserver.api.EzyResponseApi;
Expand All @@ -23,12 +22,10 @@
import com.tvd12.ezyfoxserver.wrapper.EzySessionManager;
import com.tvd12.ezyfoxserver.wrapper.EzySessionManagerAware;
import lombok.Setter;
import org.eclipse.jetty.websocket.api.Session;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -76,12 +73,7 @@ protected void handleReceivedUdpPacket(EzyUdpReceivedPacket packet) {
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);
}
socketDataReceiver.udpReceive(socketChannel, message);
} else {
EzyMessageHeader header = message.getHeader();
if (header.isUdpHandshake()) {
Expand Down Expand Up @@ -148,12 +140,6 @@ protected void response(EzySession recipient, int responseCode) throws Exception
logger.debug("response udp handshake to: {}, code: {}", recipient, responseCode);
}

protected boolean isOneClient(SocketAddress tcpAddress, SocketAddress udpAddress) {
String tcpHost = EzySocketAddresses.getHost(tcpAddress);
String udpHost = EzySocketAddresses.getHost(udpAddress);
return tcpHost.equals(udpHost);
}

@Override
public void destroy() {
this.executorService.shutdownNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ public void handleReceivedUdpPacketTest() throws Exception {

// 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();
}
Expand Down Expand Up @@ -380,7 +379,6 @@ public void handleReceivedUdpPacketWithSessionTest() {

// 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();
}
Expand All @@ -405,6 +403,9 @@ public void handleReceivedUdpPacketIsNotOneClientTest() throws Exception {
when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
sut.setHandlerGroupManager(handlerGroupManager);

EzySocketDataReceiver socketDataReceiver = mock(EzySocketDataReceiver.class);
sut.setSocketDataReceiver(socketDataReceiver);

// when
MethodInvoker.create()
.object(sut)
Expand All @@ -414,7 +415,7 @@ public void handleReceivedUdpPacketIsNotOneClientTest() throws Exception {

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

Expand Down

0 comments on commit e7431f5

Please sign in to comment.