Skip to content

Commit

Permalink
Merge pull request #52 from git-elliot/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
git-elliot authored Aug 6, 2022
2 parents 1ce6689 + b133943 commit 8ced0f2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
5 changes: 4 additions & 1 deletion network_tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## 3.0.0

**Breaking change** Change most of the methods names
* HostScanner.discover To HostScanner.getAllPingableDevices

* HostScanner.discover to HostScanner.getAllPingableDevices
* HostScanner.discoverPort to HostScanner.scanDevicesForSinglePort
* PortScanner.discover to PortScanner.scanPortsForSingleDevice

Expand All @@ -13,6 +14,8 @@ ActiveHost now contains host name of the address if exist.

Better naming of methods

Bug fixes and improvements

## 2.1.0

Added partly support for searching mdns devices.
Expand Down
1 change: 1 addition & 0 deletions network_tools/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Network Tools

[![pub package](https://img.shields.io/pub/v/network_tools.svg)](https://pub.dev/packages/network_tools)
[![codecov](https://codecov.io/gh/git-elliot/network_tools/branch/main/graph/badge.svg?token=J9G2472GQZ)](https://codecov.io/gh/git-elliot/network_tools)

Network Tools Supported

Expand Down
18 changes: 17 additions & 1 deletion network_tools/lib/src/mdns_scanner/mdns_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ class MdnsScanner {
) async {
final List<MdnsInfo> mdnsFoundList = [];

final MDnsClient client = MDnsClient();
final MDnsClient client = MDnsClient(
rawDatagramSocketFactory: (
dynamic host,
int port, {
bool? reuseAddress,
bool? reusePort,
int? ttl,
}) {
return RawDatagramSocket.bind(
host,
port,
reusePort: Platform.isWindows ? false : true,
ttl: ttl!,
);
},
);

await client.start();

await for (final PtrResourceRecord ptr in client.lookup<PtrResourceRecord>(
Expand Down
25 changes: 22 additions & 3 deletions network_tools/lib/src/port_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class PortScanner {
required int port,
required Duration timeout,
required StreamController<ActiveHost> activeHostsController,
int recursionCount = 0,
}) async {
try {
final Socket s = await Socket.connect(address, port, timeout: timeout);
Expand All @@ -174,10 +175,28 @@ class PortScanner {
// Check if connection timed out or we got one of predefined errors
if (e.osError == null || _errorCodes.contains(e.osError?.errorCode)) {
return null;
} else {
// Error 23,24: Too many open files in system
rethrow;
}

// Error 23,24: Too many open files in system
// e.osError can't be null here so `!` can be used
// Do no more than 2 retries to prevent infinite loops
if (recursionCount < 3 &&
(e.osError!.errorCode == 23 || e.osError!.errorCode == 24)) {
// Hotfix: Wait for the timeout (+ a little more) to complete and retry
// -> Other connections must be closed now and the file handles available again

await Future.delayed(timeout + const Duration(milliseconds: 250));

return connectToPort(
address: address,
port: port,
timeout: timeout,
activeHostsController: activeHostsController,
recursionCount: recursionCount + 1,
);
}

rethrow;
}
}

Expand Down
12 changes: 12 additions & 0 deletions network_tools/test/host_scanner_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:network_tools/network_tools.dart';
import 'package:test/test.dart';

void main() {
group('Testing Host Scanner', () {
test('Running getMaxHost tests', () {
expect(HostScanner.getMaxHost("10.0.0.0"), HostScanner.classASubnets);
expect(HostScanner.getMaxHost("164.0.0.0"), HostScanner.classBSubnets);
expect(HostScanner.getMaxHost("200.0.0.0"), HostScanner.classCSubnets);
});
});
}
11 changes: 1 addition & 10 deletions network_tools/test/network_tools_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
import 'package:network_tools/network_tools.dart';
import 'package:test/test.dart';

void main() {
test('Testing Host Scanner', () {
expect(HostScanner.getMaxHost("10.0.0.0"), HostScanner.classASubnets);
expect(HostScanner.getMaxHost("164.0.0.0"), HostScanner.classBSubnets);
expect(HostScanner.getMaxHost("200.0.0.0"), HostScanner.classCSubnets);
});
}
void main() {}

0 comments on commit 8ced0f2

Please sign in to comment.