Skip to content

Commit ceba58e

Browse files
committed
Fixed timeout support for iOS, Android, and Simulator
1 parent 8448c7e commit ceba58e

File tree

7 files changed

+1449
-7
lines changed

7 files changed

+1449
-7
lines changed

CN1SocketsDemo/lib/CN1Sockets.cn1lib

-11.7 KB
Binary file not shown.

dist/CN1Sockets.cn1lib

2.88 KB
Binary file not shown.

native/android/ca/weblite/codename1/net/impl/NativeSocketImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ca.weblite.codename1.net.impl;
22

33

4+
import java.net.InetSocketAddress;
45
import java.net.Socket;
56
public class NativeSocketImpl {
67
private Socket socket;
@@ -159,7 +160,8 @@ public boolean createSocket(String host, int port) {
159160

160161
public boolean connectSocket(int timeout){
161162
try {
162-
socket = new Socket(host, port);
163+
socket = new Socket();
164+
socket.connect(new InetSocketAddress(host, port), timeout);
163165
return true;
164166

165167
} catch ( Throwable t){

native/ios/ca_weblite_codename1_net_impl_NativeSocketImpl.m

+15-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ -(BOOL)setKeepAlive:(BOOL)param{
144144
-(BOOL)isInputShutdown{
145145
errorMessage = NULL;
146146
NSStreamStatus status = [inputStream streamStatus];
147-
return ( status == NSStreamStatusNotOpen || NSStreamStatusClosed == status );
147+
return (status == NSStreamStatusOpening || status == NSStreamStatusNotOpen || NSStreamStatusClosed == status );
148148
}
149149

150150
-(BOOL)isOutputShutdown{
151151
errorMessage = NULL;
152152
NSStreamStatus status = [outputStream streamStatus];
153-
return ( status == NSStreamStatusNotOpen || NSStreamStatusClosed == status );
153+
return (status == NSStreamStatusOpening || status == NSStreamStatusNotOpen || NSStreamStatusClosed == status );
154154
}
155155

156156
-(NSString*)getErrorMessage{
@@ -296,6 +296,19 @@ -(BOOL)connectSocket:(int)timeout{
296296
errorMessage = NULL;
297297
[inputStream open];
298298
[outputStream open];
299+
while ([outputStream streamStatus] == NSStreamStatusOpening) {
300+
_yield();
301+
usleep(100000);
302+
_resume();
303+
}
304+
while ([inputStream streamStatus] == NSStreamStatusOpening) {
305+
_yield();
306+
usleep(100000);
307+
_resume();
308+
}
309+
if ([self isInputShutdown] || [self isOutputShutdown]) {
310+
return NO;
311+
}
299312
return YES;
300313
}
301314

native/javase/ca/weblite/codename1/net/impl/NativeSocketImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ca.weblite.codename1.net.impl;
22

33

4+
import java.net.InetSocketAddress;
45
import java.net.Socket;
56
public class NativeSocketImpl implements ca.weblite.codename1.net.impl.NativeSocket {
67
private Socket socket;
@@ -159,7 +160,8 @@ public boolean createSocket(String host, int port) {
159160

160161
public boolean connectSocket(int timeout){
161162
try {
162-
socket = new Socket(host, port);
163+
socket = new Socket();
164+
socket.connect(new InetSocketAddress(host, port), timeout);
163165
return true;
164166

165167
} catch ( Throwable t){

0 commit comments

Comments
 (0)