Skip to content

Commit ab64d8f

Browse files
committed
UDP receiving works in non-blocking mode until it's not cancelled.
This allows to receive any number of datagrams.
1 parent 61f9572 commit ab64d8f

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

netcat/src/main/java/com/github/dddpaul/netcat/NetCat.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import de.greenrobot.event.EventBus;
99
import events.ActivityEvent;
10+
import events.FragmentEvent;
11+
12+
import static com.github.dddpaul.netcat.NetCater.Op.*;
1013

1114
public class NetCat implements NetCater
1215
{
@@ -104,6 +107,9 @@ protected void onProgressUpdate( String... values )
104107
{
105108
State state = State.valueOf( String.valueOf( values[0] ) );
106109
EventBus.getDefault().post( new ActivityEvent( state ) );
110+
if( values.length == 2 ) {
111+
EventBus.getDefault().post( new FragmentEvent( HANDLE_RECEIVED_DATA, values[1] ));
112+
}
107113
}
108114

109115
@Override

netcat/src/main/java/com/github/dddpaul/netcat/NetCater.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
public interface NetCater
1212
{
13-
enum Op { CONNECT, LISTEN, RECEIVE, SEND, DISCONNECT, CLEAR_OUTPUT_VIEW }
14-
enum State { IDLE, CONNECTED, LISTENING, OUTPUT_VIEW_CLEARED, CONNECTING}
13+
enum Op { CONNECT, LISTEN, RECEIVE, SEND, DISCONNECT, CLEAR_OUTPUT_VIEW, HANDLE_RECEIVED_DATA}
14+
enum State { IDLE, CONNECTED, LISTENING, OUTPUT_VIEW_CLEARED, CONNECTING }
1515
enum Proto { TCP, UDP }
1616

1717
public void cancel();

netcat/src/main/java/com/github/dddpaul/netcat/UdpNetCat.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.nio.channels.AsynchronousCloseException;
1111
import java.nio.channels.DatagramChannel;
1212

13+
import static com.github.dddpaul.netcat.NetCater.State.CONNECTED;
14+
1315
public class UdpNetCat extends NetCat
1416
{
1517
private final String CLASS_NAME = getClass().getSimpleName();
@@ -73,6 +75,8 @@ protected Result doInBackground( String... params )
7375
Log.d( CLASS_NAME, String.format( "Connecting to %s:%d (UDP)", host, port ) );
7476
channel = DatagramChannel.open();
7577
channel.connect( new InetSocketAddress( host, port ) );
78+
channel.configureBlocking( false );
79+
Log.i( CLASS_NAME, "Connected to " + channel.socket().getRemoteSocketAddress() );
7680
result.object = channel.socket();
7781
break;
7882
case LISTEN:
@@ -121,12 +125,14 @@ private SocketAddress receiveFromChannel() throws Exception
121125
try {
122126
ByteBuffer buf = ByteBuffer.allocate( 1024 );
123127
buf.clear();
124-
while( remoteSocketAddress == null && !task.isCancelled() ) {
128+
while( !task.isCancelled() ) {
125129
remoteSocketAddress = channel.receive( buf );
126-
Thread.sleep( 100 );
127-
}
128-
if( remoteSocketAddress != null ) {
129-
output.write( buf.array(), 0, buf.position() );
130+
if( remoteSocketAddress != null ) {
131+
Log.d( CLASS_NAME, String.format( "%d bytes was received from %s", buf.position() - 1, remoteSocketAddress ));
132+
output.write( buf.array(), 0, buf.position() );
133+
publishProgress( CONNECTED.toString(), output.toString() );
134+
}
135+
Thread.sleep( 1000 );
130136
}
131137
if( task.isCancelled() ) {
132138
stopListening();

netcat/src/main/java/com/github/dddpaul/netcat/ui/ResultFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ public void onEvent( FragmentEvent event )
174174
netCat.cancel();
175175
}
176176
break;
177+
case HANDLE_RECEIVED_DATA:
178+
outputView.setText( event.data );
179+
break;
177180
case CLEAR_OUTPUT_VIEW:
178181
outputView.setText( "" );
179182
EventBus.getDefault().post( new ActivityEvent( OUTPUT_VIEW_CLEARED ) );

0 commit comments

Comments
 (0)