Skip to content

Commit f071a9c

Browse files
committed
Add support for byteorder and unconnected datagram sockets.
Android Studio stupport.
1 parent bf79ccd commit f071a9c

32 files changed

Lines changed: 320 additions & 69 deletions

AndroidAsync/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="src" path="gen"/>
55
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
66
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
7+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
78
<classpathentry kind="output" path="bin/classes"/>
89
</classpath>

AndroidAsync/AndroidAsync.iml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="android" name="Android">
5+
<configuration>
6+
<option name="LIBRARY_PROJECT" value="true" />
7+
<notImportedProperties>
8+
<property>MANIFEST_FILE_PATH</property>
9+
<property>RESOURCES_DIR_PATH</property>
10+
<property>ASSETS_DIR_PATH</property>
11+
<property>NATIVE_LIBS_DIR_PATH</property>
12+
</notImportedProperties>
13+
</configuration>
14+
</facet>
15+
</component>
16+
<component name="NewModuleRootManager" inherit-compiler-output="true">
17+
<exclude-output />
18+
<content url="file://$MODULE_DIR$">
19+
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" />
20+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
21+
</content>
22+
<orderEntry type="jdk" jdkName="Android 4.2.2" jdkType="Android SDK" />
23+
<orderEntry type="sourceFolder" forTests="false" />
24+
</component>
25+
</module>
26+

AndroidAsync/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<uses-sdk
77
android:minSdkVersion="8"
8-
android:targetSdkVersion="15" />
8+
android:targetSdkVersion="17" />
99
<uses-permission android:name="android.permission.INTERNET"/>
1010
<uses-permission android:name="android.permission.READ_LOGS"/>
1111
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

AndroidAsync/gen/com/koushikdutta/async/BuildConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/*___Generated_by_IDEA___*/
2+
13
/** Automatically generated file. DO NOT MODIFY */
24
package com.koushikdutta.async;
35

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*___Generated_by_IDEA___*/
2+
3+
package com.koushikdutta.async;
4+
5+
/* This stub is only used by the IDE. It is NOT the Manifest class actually packed into the APK */
6+
public final class Manifest {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*___Generated_by_IDEA___*/
2+
3+
package com.koushikdutta.async;
4+
5+
/* This stub is only used by the IDE. It is NOT the R class actually packed into the APK */
6+
public final class R {
7+
}

AndroidAsync/lint.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
</lint>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.koushikdutta.async;
2+
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
6+
import java.nio.ByteBuffer;
7+
8+
public class AsyncDatagramSocket extends AsyncNetworkSocket {
9+
public void disconnect() throws IOException {
10+
// ((DatagramChannelWrapper)getChannel()).disconnect();
11+
}
12+
13+
public void connect(SocketAddress address) throws IOException {
14+
((DatagramChannelWrapper)getChannel()).mChannel.connect(address);
15+
}
16+
17+
public void send(final String host, final int port, final ByteBuffer buffer) {
18+
if (getServer().getAffinity() != Thread.currentThread()) {
19+
getServer().run(new Runnable() {
20+
@Override
21+
public void run() {
22+
send(host, port, buffer);
23+
}
24+
});
25+
return;
26+
}
27+
28+
try {
29+
((DatagramChannelWrapper)getChannel()).mChannel.send(buffer, new InetSocketAddress(host, port));
30+
}
31+
catch (IOException e) {
32+
close();
33+
reportEndPending(e);
34+
//reportClose(e);
35+
}
36+
37+
}
38+
public void send(final SocketAddress address, final ByteBuffer buffer) {
39+
if (getServer().getAffinity() != Thread.currentThread()) {
40+
getServer().run(new Runnable() {
41+
@Override
42+
public void run() {
43+
send(address, buffer);
44+
}
45+
});
46+
return;
47+
}
48+
49+
try {
50+
((DatagramChannelWrapper)getChannel()).mChannel.send(buffer, address);
51+
}
52+
catch (IOException e) {
53+
close();
54+
reportEndPending(e);
55+
//reportClose(e);
56+
}
57+
}
58+
}

AndroidAsync/src/com/koushikdutta/async/AsyncNetworkSocket.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int onReadable() {
173173
}
174174

175175
boolean closeReported;
176-
private void reportClose(Exception e) {
176+
protected void reportClose(Exception e) {
177177
if (closeReported)
178178
return;
179179
closeReported = true;
@@ -332,4 +332,8 @@ public boolean isPaused() {
332332
public AsyncServer getServer() {
333333
return mServer;
334334
}
335+
336+
public int getLocalPort() {
337+
return mChannel.getLocalPort();
338+
}
335339
}

AndroidAsync/src/com/koushikdutta/async/AsyncServer.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,54 @@ public void run() {
477477
}
478478
}
479479

480-
public AsyncSocket connectDatagram(final SocketAddress remote) throws IOException {
480+
public AsyncDatagramSocket connectDatagram(final String host, final int port) throws IOException {
481481
final DatagramChannel socket = DatagramChannel.open();
482-
final AsyncNetworkSocket handler = new AsyncNetworkSocket();
482+
final AsyncDatagramSocket handler = new AsyncDatagramSocket();
483+
handler.attach(socket);
484+
// ugh.. this should really be post to make it nonblocking...
485+
// but i want datagrams to be immediately writable.
486+
// they're not really used anyways.
487+
run(new Runnable() {
488+
@Override
489+
public void run() {
490+
try {
491+
final SocketAddress remote = new InetSocketAddress(host, port);
492+
handleSocket(handler);
493+
socket.connect(remote);
494+
}
495+
catch (Exception e) {
496+
e.printStackTrace();
497+
}
498+
}
499+
});
500+
return handler;
501+
}
502+
503+
public AsyncDatagramSocket openDatagram() throws IOException {
504+
final DatagramChannel socket = DatagramChannel.open();
505+
final AsyncDatagramSocket handler = new AsyncDatagramSocket();
506+
handler.attach(socket);
507+
// ugh.. this should really be post to make it nonblocking...
508+
// but i want datagrams to be immediately writable.
509+
// they're not really used anyways.
510+
run(new Runnable() {
511+
@Override
512+
public void run() {
513+
try {
514+
socket.socket().bind(null);
515+
handleSocket(handler);
516+
}
517+
catch (Exception e) {
518+
e.printStackTrace();
519+
}
520+
}
521+
});
522+
return handler;
523+
}
524+
525+
public AsyncDatagramSocket connectDatagram(final SocketAddress remote) throws IOException {
526+
final DatagramChannel socket = DatagramChannel.open();
527+
final AsyncDatagramSocket handler = new AsyncDatagramSocket();
483528
handler.attach(socket);
484529
// ugh.. this should really be post to make it nonblocking...
485530
// but i want datagrams to be immediately writable.

0 commit comments

Comments
 (0)