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

Commit

Permalink
🐛: fixed socket issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushpavel committed Sep 3, 2021
1 parent b5d55e3 commit 6c2e2c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class App extends HookWidget {
],
),
SizedBox(height: 18),
...snapshot.requireData.map((e) => Text(e.address.address)),
Text("Neighbour Devices: " + snapshot.requireData.length.toString()),
],
);
},
Expand Down
24 changes: 18 additions & 6 deletions lib/logic/network/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@ import 'dart:io';

import 'package:clipboard_sync/logic/clipboard/clipboard.dart';
import 'package:clipboard_sync/logic/models/messages.dart';
import 'package:rxdart/rxdart.dart';

handleClient(Socket socket) async {
bool pause = false;
const KEEP_ALIVE_INTERVAL = Duration(seconds: 1);

clipboard.listen((value) {
if (!pause) {
handleClient(BehaviorSubject<List<Socket>> sockets, Socket socket) async {
String socketValue = "";
final sub = clipboard.listen((value) {
if (socketValue != value.value) {
final clipboardValue = encodeClipboardValue(value);
print("sending $clipboardValue");
socket.write(clipboardValue);
}
});

socket.listen((event) {
pause = true;
if (String.fromCharCodes(event).trim() == "alive") return;

final clipboardValue = decodeClipboardValue(event);
if (clipboard.value.value != clipboardValue.value &&
clipboardValue.timestamp.compareTo(clipboardValue.timestamp) >= 0) {
print("receiving ${String.fromCharCodes(event).trim()}");
socketValue = clipboardValue.value;
clipboard.value = clipboardValue;
}
pause = false;
});

while (sockets.value.contains(socket)) {
await Future.delayed(KEEP_ALIVE_INTERVAL);
socket.write("alive");
}

sub.cancel();
}
6 changes: 3 additions & 3 deletions lib/logic/network/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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";
const BROADCAST_ADDRESS = "192.168.255.255";

BehaviorSubject<List<Socket>> initializeNetwork() {
final socketStream = BehaviorSubject<List<Socket>>.seeded([]);
Expand All @@ -29,7 +29,7 @@ _initializeNetwork(BehaviorSubject<List<Socket>> socketStream) async {
list.add(socket);
socketStream.value = list;
_removeSocketOnClose(socket, socketStream);
handleClient(socket);
handleClient(socketStream, socket);
});

_handleServerSearchers(deviceInfo, serverSocket);
Expand Down Expand Up @@ -105,7 +105,7 @@ _handleServerConnections(RawDatagramSocket udpSocket, BehaviorSubject<List<Socke
_removeSocketOnClose(socket, socketStream);
list.add(socket);
socketStream.value = list;
handleClient(socket);
handleClient(socketStream, socket);
} catch (e) {
print(e);
}
Expand Down

0 comments on commit 6c2e2c3

Please sign in to comment.