Skip to content

Commit d91f904

Browse files
Merge pull request #35 from RichardRNStudio/feat/2-make-sure-only-open-ports-available
feat(#2): extend IOS implementation - only open port(s) listed
2 parents 73ffe7a + fb3b27d commit d91f904

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

example/src/App.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const styles = StyleSheet.create({
99
height: '100%',
1010
maxHeight: '100%',
1111
minHeight: '100%',
12-
marginTop: 10,
1312
},
1413
wrapper: {
1514
flex: 1,
@@ -23,7 +22,6 @@ const styles = StyleSheet.create({
2322
textAlign: 'center',
2423
color: 'red',
2524
fontSize: 20,
26-
marginBottom: 20,
2725
},
2826
});
2927

@@ -68,17 +66,22 @@ export default function App() {
6866
init();
6967
}, []);
7068

69+
const reset = () => {
70+
setCheckedDevice(null);
71+
setIsFinished(false);
72+
setDeviceFound([]);
73+
setScanner(null);
74+
};
75+
7176
const start = () => {
7277
console.log('init');
78+
reset();
7379
scanner?.start();
7480
};
7581

7682
const stop = () => {
7783
scanner?.stop();
78-
setCheckedDevice(null);
79-
setIsFinished(false);
80-
setDeviceFound([]);
81-
setScanner(null);
84+
reset();
8285
init();
8386
};
8487

ios/FindLocalDevices.mm

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,41 +172,47 @@ - (BOOL)socketIsAvailableForHost:(NSString *)host port:(int)port timeout:(int)ti
172172
addr.sin_port = htons(port);
173173
inet_pton(AF_INET, [host UTF8String], &addr.sin_addr);
174174

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

178+
// If connect() returns 0, the connection was established immediately
177179
if (result == 0) {
178180
close(sock);
179-
return YES; // Connected successfully
180-
} else if (errno != EINPROGRESS) {
181+
return YES; // Port is open and reachable
182+
}
183+
184+
// If connect() returns -1, check if it's due to EINPROGRESS (indicating non-blocking in progress)
185+
if (result < 0 && errno != EINPROGRESS) {
181186
close(sock);
182-
return NO; // Connection failed immediately for non-timeout reasons
187+
return NO; // Connection failed for reasons other than timeout
183188
}
184189

185-
// Use select() to wait for the socket to become writable within the timeout
190+
// Set up the file descriptor set and timeout for select()
186191
fd_set writefds;
187192
struct timeval tv;
188-
tv.tv_sec = timeout / 1000; // Seconds
189-
tv.tv_usec = (timeout % 1000) * 1000; // Microseconds
193+
tv.tv_sec = timeout / 1000; // Convert milliseconds to seconds
194+
tv.tv_usec = (timeout % 1000) * 1000; // Convert remainder to microseconds
190195

191196
FD_ZERO(&writefds);
192197
FD_SET(sock, &writefds);
193198

199+
// Wait for the socket to be writable, indicating a successful connection
194200
int selectResult = select(sock + 1, NULL, &writefds, NULL, &tv);
195-
close(sock);
196-
197201
if (selectResult > 0 && FD_ISSET(sock, &writefds)) {
198-
// The connection attempt was successful
199-
return YES;
200-
} else {
201-
// Report connection error if listeners are active
202-
if (hasListeners) {
203-
NSDictionary *errorInfo = @{@"ip": host, @"port": @(port)};
204-
[self sendEventWithName:@"FLD_CONNECTION_ERROR" body:errorInfo];
202+
int so_error;
203+
socklen_t len = sizeof(so_error);
204+
getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
205+
206+
if (so_error == 0) {
207+
close(sock);
208+
return YES; // Port is open
205209
}
206-
return NO; // Connection failed or timed out
207210
}
208-
}
209211

212+
// Connection failed or timed out
213+
close(sock);
214+
return NO;
215+
}
210216

211217
RCT_EXPORT_METHOD(cancelDiscovering) {
212218
isDiscovering = NO; // Set flag to NO to cancel discovery

0 commit comments

Comments
 (0)