Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
🐛: fixed address issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushpavel committed Sep 2, 2021
1 parent 0f5609b commit 71b8376
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/logic/models/discoveryMessages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class ServerInfoMessage {
ServerInfoMessage(this.port, this.address);
}

List<int> encodeServerInfoMessage(ServerSocket serverSocket) {
Future<List<int>> encodeServerInfoMessage(ServerSocket serverSocket) async {
final l = await NetworkInterface.list(type: InternetAddressType.IPv4);
final address = l.firstWhere((element) => element.addresses[0].address.startsWith("192.168."));
final json = {
"type": "ServerInfoMessage",
"port": serverSocket.port,
"address": serverSocket.address.address,
"address": address.addresses[0].address,
};
return utf8.encode(jsonEncode(json));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/logic/models/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ getJsonMessage(String type, List<int> bytes) {
final jsonString = String.fromCharCodes(bytes).trim();
final json = jsonDecode(jsonString);

if (json.type != type) throw Exception("unexpected json type, ${json.type} expected: $type");
if (json['type'] != type) throw Exception("unexpected json type, ${json.type} expected: $type");

return json;
}
42 changes: 27 additions & 15 deletions lib/logic/network/network.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:clipboard_sync/logic/models/discoveryMessages.dart';
Expand All @@ -6,9 +7,9 @@ import 'package:clipboard_sync/logic/utils/uuid.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';

const SERVER_PORT = 38899;
const SEARCH_PORT = 31000;
const SEARCH_SOURCE_PORT = 35560;
const SERVER_PORT = 10542;
const SEARCH_PORT = 38899;
const SEARCH_SOURCE_PORT = 40000;
const SEARCH_INTERVAL = const Duration(seconds: 1);
const BROADCAST_ADDRESS = "255.255.255.255";

Expand Down Expand Up @@ -37,35 +38,40 @@ _handleServerSearchers(DeviceInfo deviceInfo, ServerSocket serverSocket) async {
socket.writeEventsEnabled = true;
socket.broadcastEnabled = true;

socket.listen((event) {
socket.listen((event) async {
if (event != RawSocketEvent.read) return;
final datagram = socket.receive()!;
final id = decodeDeviceId(datagram.data);

if (id.compareTo(deviceInfo.id) <= 0) return;

final serverInfo = encodeServerInfoMessage(serverSocket);
print(id);

final serverInfo = await encodeServerInfoMessage(serverSocket);
socket.send(serverInfo, datagram.address, datagram.port);
});
}

_searchServer(DeviceInfo deviceInfo, ValueNotifier<Set<Socket>> socketStream) async {
RawDatagramSocket? socket;

socketStream.addListener((devices) async {
manageSocket() async {
final devices = socketStream.value;
if (devices.isNotEmpty) {
socket?.close();
socket = null;
} else if (socket == null) {
try {
socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, SEARCH_SOURCE_PORT);
_handleServerConnections(socket!, socketStream);
} finally {
socket?.close();
socket = null;
}
socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, SEARCH_SOURCE_PORT);

socket!.readEventsEnabled = true;
socket!.writeEventsEnabled = true;
socket!.broadcastEnabled = true;

_handleServerConnections(socket!, socketStream);
}
} as VoidCallback);
}

socketStream.addListener(manageSocket);
manageSocket();

while (true) {
await Future.delayed(SEARCH_INTERVAL);
Expand All @@ -81,6 +87,12 @@ _handleServerConnections(RawDatagramSocket udpSocket, ValueNotifier<Set<Socket>>
final serverMessage = decodeServerInfoMessage(datagram.data);
if (socketStream.value.isNotEmpty) return;
final socket = await Socket.connect(serverMessage.address, serverMessage.port);

if (socketStream.value.isNotEmpty) {
socket.close();
return;
}

final list = socketStream.value.toSet();
_removeSocketOnClose(socket, socketStream);
list.add(socket);
Expand Down

0 comments on commit 71b8376

Please sign in to comment.