Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const styles = StyleSheet.create({
height: '100%',
maxHeight: '100%',
minHeight: '100%',
marginTop: 10,
},
wrapper: {
flex: 1,
Expand All @@ -23,7 +22,6 @@ const styles = StyleSheet.create({
textAlign: 'center',
color: 'red',
fontSize: 20,
marginBottom: 20,
},
});

Expand Down Expand Up @@ -68,17 +66,22 @@ export default function App() {
init();
}, []);

const reset = () => {
setCheckedDevice(null);
setIsFinished(false);
setDeviceFound([]);
setScanner(null);
};

const start = () => {
console.log('init');
reset();
scanner?.start();
};

const stop = () => {
scanner?.stop();
setCheckedDevice(null);
setIsFinished(false);
setDeviceFound([]);
setScanner(null);
reset();
init();
};

Expand Down
40 changes: 23 additions & 17 deletions ios/FindLocalDevices.mm
Original file line number Diff line number Diff line change
Expand Up @@ -172,41 +172,47 @@ - (BOOL)socketIsAvailableForHost:(NSString *)host port:(int)port timeout:(int)ti
addr.sin_port = htons(port);
inet_pton(AF_INET, [host UTF8String], &addr.sin_addr);

// Start the connection
int result = connect(sock, (struct sockaddr *)&addr, sizeof(addr));

// If connect() returns 0, the connection was established immediately
if (result == 0) {
close(sock);
return YES; // Connected successfully
} else if (errno != EINPROGRESS) {
return YES; // Port is open and reachable
}

// If connect() returns -1, check if it's due to EINPROGRESS (indicating non-blocking in progress)
if (result < 0 && errno != EINPROGRESS) {
close(sock);
return NO; // Connection failed immediately for non-timeout reasons
return NO; // Connection failed for reasons other than timeout
}

// Use select() to wait for the socket to become writable within the timeout
// Set up the file descriptor set and timeout for select()
fd_set writefds;
struct timeval tv;
tv.tv_sec = timeout / 1000; // Seconds
tv.tv_usec = (timeout % 1000) * 1000; // Microseconds
tv.tv_sec = timeout / 1000; // Convert milliseconds to seconds
tv.tv_usec = (timeout % 1000) * 1000; // Convert remainder to microseconds

FD_ZERO(&writefds);
FD_SET(sock, &writefds);

// Wait for the socket to be writable, indicating a successful connection
int selectResult = select(sock + 1, NULL, &writefds, NULL, &tv);
close(sock);

if (selectResult > 0 && FD_ISSET(sock, &writefds)) {
// The connection attempt was successful
return YES;
} else {
// Report connection error if listeners are active
if (hasListeners) {
NSDictionary *errorInfo = @{@"ip": host, @"port": @(port)};
[self sendEventWithName:@"FLD_CONNECTION_ERROR" body:errorInfo];
int so_error;
socklen_t len = sizeof(so_error);
getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);

if (so_error == 0) {
close(sock);
return YES; // Port is open
}
return NO; // Connection failed or timed out
}
}

// Connection failed or timed out
close(sock);
return NO;
}

RCT_EXPORT_METHOD(cancelDiscovering) {
isDiscovering = NO; // Set flag to NO to cancel discovery
Expand Down