Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
- add udps subscription config
Browse files Browse the repository at this point in the history
- add udps subscription in asyncfetcher
  • Loading branch information
ogios committed Oct 28, 2023
1 parent 6b79d39 commit 02df387
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 49 deletions.
41 changes: 25 additions & 16 deletions lib/api/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:intl/intl.dart';
import 'package:ogios_sutils/in.dart';
import 'package:ogios_sutils/out.dart';
import 'package:transfer_client/api/subscribe.dart';
import 'package:transfer_client/page/home/config/page.dart';
import 'package:collection/collection.dart';

import '../page/home/main/message_list.dart';

const int TYPE_TEXT = 1;
const int TYPE_BYTE = 2;
const DeepCollectionEquality deepcheck = DeepCollectionEquality();
final AsyncFetcher GlobalFetcher = AsyncFetcher();
typedef FetchCallback = Function(List<Message>, Object?);

typedef FetchCallback = Function(List<Message>, Object?);

class AsyncFetcher {
bool running = false;
Expand All @@ -28,6 +30,7 @@ class AsyncFetcher {
int total = 10;
FetchCallback _defultCallback = (List<Message> msg, Object? err) {};
FetchCallback callback = (List<Message> msg, Object? err) {};
MsgSubcribe sub = MsgSubcribe();

void registerCallback(Function(List<Message>, Object?) call) {
this.callback = call;
Expand All @@ -38,24 +41,29 @@ class AsyncFetcher {
this.callback = _defultCallback;
}

Future<void> syncData(Timer? t) async {
try {
log("syncing...");
await _syncData();
} catch (err) {
Fluttertoast.showToast(msg: 'Err in fetch: ${err}');
log("Error in async fetch!! ", error: err);
}
}

void startSync() {
log("start sync");
if (running) return;
running = true;
a(timer) async {
try {
log("syncing...");
await _syncData();
} catch (err) {
Fluttertoast.showToast(msg: 'Err in fetch: ${err}');
log("Error in async fetch!! ", error: err);
}
}
a(null);
_timer = Timer.periodic(const Duration(seconds: 5), a);
syncData(null);
_timer = Timer.periodic(const Duration(seconds: 5), syncData);
sub.setCallback(syncData);
sub.startSub();
}

void stopSync() {
sub.clearCallback();
sub.stopSub();
log("stop sync");
running = false;
_timer.cancel();
Expand All @@ -66,7 +74,7 @@ class AsyncFetcher {
this._err = null;
_messages = msgs;
this.callback(this._messages, this._err);
}
}

Future _syncData() async {
try {
Expand Down Expand Up @@ -110,7 +118,8 @@ class AsyncFetcher {
Future<String> fetchDataFromBackend() async {
Socket socket;
try {
socket = await Socket.connect(GlobalConfig.host, GlobalConfig.port, timeout: Duration(seconds: 5));
socket = await Socket.connect(GlobalConfig.host, GlobalConfig.port,
timeout: Duration(seconds: 5));
} catch (err) {
log("Socket connection error: $err; Config: $GlobalConfig");
throw err;
Expand Down
67 changes: 67 additions & 0 deletions lib/api/subscribe.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:transfer_client/page/home/config/page.dart';

class MsgSubcribe {
bool running = false;
Timer? _timer;
bool subed = false;
RawDatagramSocket? socket;
Function callback = () {};

void setCallback(Function call) {
this.callback = call;
}

void clearCallback() {
this.callback = () {};
}

void onMsgCallback(Uint8List data) {
if (data.length == 1) {
if (data[0] == 2) {
callback();
} else if (data[0] == 1) {
this.socket!.send(
[1], InternetAddress(GlobalConfig.u_host), GlobalConfig.u_port);
}
} else if (String.fromCharCodes(data) == "ok") {
subed = true;
}
}

void startSub() {
if (this.running) return;
() async {
var socket = await RawDatagramSocket.bind(InternetAddress("0.0.0.0"), GlobalConfig.u_port);
this.running = true;
this.socket = socket;
socket.listen((e) async {
if (e == RawSocketEvent.read) {
Uint8List data = socket.receive()!.data;
onMsgCallback(data);
}
});
await sub(null);
_timer = Timer.periodic(const Duration(seconds: 30), sub);
}();
}

void stopSub() {
if (this._timer != null) {
this._timer!.cancel();
}
this.running = false;
}

Future<void> sub(Timer? t) async {
this.subed = false;
while (this.subed) {
this.socket!.send("sub".codeUnits, InternetAddress(GlobalConfig.u_host),
GlobalConfig.u_port);
await Future.delayed(Duration(seconds: 3));
}
}
}
8 changes: 6 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:transfer_client/desktop.dart';
import 'package:transfer_client/mobile.dart';
import 'package:transfer_client/page/home/config/c_host.dart';
import 'package:transfer_client/page/home/config/c_port.dart';
import 'package:transfer_client/page/home/config/tserv/c_host.dart';
import 'package:transfer_client/page/home/config/tserv/c_port.dart';
import 'package:transfer_client/page/home/config/page.dart';
import 'package:transfer_client/page/home/config/udp/u_host.dart';
import 'package:transfer_client/page/home/config/udp/u_port.dart';
import 'package:transfer_client/page/home/main/ftoast.dart';
import 'package:transfer_client/receive_share.dart';

Expand All @@ -17,6 +19,8 @@ Future<void> initConfig() async {
var prefs = await SharedPreferences.getInstance();
CHost.initConfig(GlobalConfig, prefs);
CPort.initConfig(GlobalConfig, prefs);
UHost.initConfig(GlobalConfig, prefs);
UPort.initConfig(GlobalConfig, prefs);
GlobalConfig.done = true;
}

Expand Down
48 changes: 31 additions & 17 deletions lib/page/home/config/page.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:transfer_client/page/home/config/c_host.dart';
import 'package:transfer_client/page/home/config/c_port.dart';
import 'package:transfer_client/page/home/config/tserv/sec.dart';
import 'package:transfer_client/page/home/config/udp/sec.dart';
import 'package:transfer_client/page/home/custom_component.dart';

import '../homepage.dart';
import 'package:transfer_client/page/home/homepage.dart';

class Config {
bool done = false;
String host = "";
int port = 0;
String u_host = "";
int u_port = 0;

@override
String toString() {
Expand Down Expand Up @@ -38,7 +39,10 @@ class ConfigPage extends StatefulWidget {

class _ConfigPage extends State<ConfigPage> {
late final SharedPreferences prefs;
List<Widget> ConfigViews = [];
List<Widget> ConfigViews = [
TservConfig(),
UDPConfig(),
];

@override
void initState() {
Expand All @@ -47,16 +51,23 @@ class _ConfigPage extends State<ConfigPage> {
}

void initViews() async {
print("Gobalconfig: ${GlobalConfig.toString()}");
var temp = <ConfigWiget>[
CHost(global: GlobalConfig),
CPort(global: GlobalConfig),
];
for (ConfigWiget a in temp) {
ConfigViews.add(
Card(color: actionColor, child: SizedBox.expand(child: a)));
// ConfigViews.add(const Divider());
}
// print("Gobalconfig: ${GlobalConfig.toString()}");
// var temp = <Widget>[
// TservConfig(),
// // CHost(),
// // CPort(),
// ];
// for (Widget a in temp) {
// ConfigViews.add(
// // Card(color: actionColor, child: SizedBox.expand(child: a)));
// // SizedBox.expand(child: a));
// Container(
// padding: const EdgeInsets.all(5), color: accentCanvasColor,
// child: a,
// ));
// // a);
// // ConfigViews.add(const Divider());
// }
}

@override
Expand All @@ -68,8 +79,11 @@ class _ConfigPage extends State<ConfigPage> {
style: Theme.of(context).textTheme.titleLarge,
),
null),
body: ListWheelScrollView(
itemExtent: 100,
// body: ListWheelScrollView(
// itemExtent: 500,
// children: ConfigViews,
// ));
body: ListView(
children: ConfigViews,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import 'package:transfer_client/main.dart';
import 'package:transfer_client/page/home/config/page.dart';

class CHost extends StatelessWidget implements ConfigWiget {
CHost({required this.global, super.key}) {
CHost({super.key}) {
textEditingController = TextEditingController();
textEditingController.text = this.global.host;
textEditingController.text = GlobalConfig.host;
fToast = FToast();
fToast.init(navigatorKey.currentContext!);
}

Config global;
Config global = GlobalConfig;
static final String PrefKey = "config.host";
late FToast fToast;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import 'package:transfer_client/main.dart';
import 'package:transfer_client/page/home/config/page.dart';

class CPort extends StatelessWidget implements ConfigWiget{
CPort({required this.global, super.key}) {
CPort({super.key}) {
textEditingController = TextEditingController();
textEditingController.text = global.port.toString();
textEditingController.text = GlobalConfig.port.toString();
fToast = FToast();
fToast.init(navigatorKey.currentContext!);
}
Config global;
Config global = GlobalConfig;
static final String PrefKey = "config.port";
late FToast fToast;

Expand Down
31 changes: 31 additions & 0 deletions lib/page/home/config/tserv/sec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/material.dart';
import 'package:transfer_client/page/home/custom_component.dart';
import 'package:transfer_client/page/home/homepage.dart';

import 'c_host.dart';
import 'c_port.dart';

class TservConfig extends StatelessWidget {
List<Widget> getTservConfig() {
List<Widget> b = [];
var temp = <Widget>[
CHost(),
CPort(),
];
for (Widget a in temp) {
// b.add(Card(color: actionColor, child: SizedBox.expand(child: a)));
b.add(Card(color: actionColor, child: a));
}
return b;
}

@override
Widget build(BuildContext context) {
return CustomConfigSec(
context,
"Tserv",
Column(
children: getTservConfig(),
));
}
}
32 changes: 32 additions & 0 deletions lib/page/home/config/udp/sec.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
import 'package:transfer_client/page/home/custom_component.dart';
import 'package:transfer_client/page/home/homepage.dart';

import 'u_host.dart';
import 'u_port.dart';

class UDPConfig extends StatelessWidget {

List<Widget> getTservConfig() {
List<Widget> b = [];
var temp = <Widget>[
UHost(),
UPort(),
];
for (Widget a in temp) {
// b.add(Card(color: actionColor, child: SizedBox.expand(child: a)));
b.add(Card(color: actionColor, child: a));
}
return b;
}

@override
Widget build(BuildContext context) {
return CustomConfigSec(
context,
"UDP Subcription",
Column(
children: getTservConfig(),
));
}
}
Loading

0 comments on commit 02df387

Please sign in to comment.