#first commit
This commit is contained in:
16
lib/core/network/dio_client.dart
Normal file
16
lib/core/network/dio_client.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class DioClient {
|
||||
static Dio create() {
|
||||
final dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: 'http://1.95.137.212:8081',
|
||||
connectTimeout: const Duration(seconds: 5),
|
||||
receiveTimeout: const Duration(seconds: 5),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
),
|
||||
);
|
||||
dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));
|
||||
return dio;
|
||||
}
|
||||
}
|
||||
26
lib/core/network/net_message_dispatcher.dart
Normal file
26
lib/core/network/net_message_dispatcher.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'protocol_decoder.dart';
|
||||
import 'tcp/tcp_client.dart';
|
||||
// 注意:这里需要导入你具体的 UserModel
|
||||
// import '../../features/auth/data/models/user_model.dart';
|
||||
|
||||
class NetMessageDispatcher {
|
||||
final TcpClient tcpClient;
|
||||
NetMessageDispatcher(this.tcpClient);
|
||||
|
||||
/// 过滤特定指令的流
|
||||
Stream<RawPacket> onCommand(int commandCode) {
|
||||
return tcpClient.packetStream.where((p) => p.command == commandCode);
|
||||
}
|
||||
|
||||
/// 示例解析方法:将 0x02 指令解析为 String
|
||||
Stream<String> onStringMessage() {
|
||||
return onCommand(0x02).map((p) => utf8.decode(p.payload));
|
||||
}
|
||||
|
||||
/// 示例解析方法:将 0x12 指令解析为 JSON 并转为模型
|
||||
Stream<Map<String, dynamic>> onJsonMessage(int cmd) {
|
||||
return onCommand(cmd).map((p) => jsonDecode(utf8.decode(p.payload)));
|
||||
}
|
||||
}
|
||||
59
lib/core/network/protocol_decoder.dart
Normal file
59
lib/core/network/protocol_decoder.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
class RawPacket {
|
||||
final int command; // 第三位指令位
|
||||
final Uint8List payload; // 数据位
|
||||
RawPacket({required this.command, required this.payload});
|
||||
}
|
||||
|
||||
class ProtocolDecoder {
|
||||
static const head = [0xAB, 0xAA];
|
||||
static const tail = [0xAA, 0xAB];
|
||||
final List<int> _buffer = [];
|
||||
|
||||
List<RawPacket> decode(List<int> newData) {
|
||||
_buffer.addAll(newData);
|
||||
List<RawPacket> packets = [];
|
||||
|
||||
while (_buffer.length >= 5) {
|
||||
// 最小包长度:头2+指令1+尾2 = 5,加1位数据=6,但是心跳包是AB AA FF AA AB
|
||||
// 1. 找头
|
||||
int headIdx = -1;
|
||||
for (int i = 0; i < _buffer.length - 1; i++) {
|
||||
if (_buffer[i] == head[0] && _buffer[i + 1] == head[1]) {
|
||||
headIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (headIdx == -1) {
|
||||
_buffer.clear(); // 没头,全是垃圾数据
|
||||
break;
|
||||
}
|
||||
|
||||
// 2. 找尾
|
||||
int tailIdx = -1;
|
||||
for (int i = headIdx + 2; i < _buffer.length - 1; i++) {
|
||||
if (_buffer[i] == tail[0] && _buffer[i + 1] == tail[1]) {
|
||||
tailIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tailIdx != -1) {
|
||||
// 3. 提取
|
||||
int cmd = _buffer[headIdx + 2];
|
||||
Uint8List payload = Uint8List.fromList(
|
||||
_buffer.sublist(headIdx + 3, tailIdx),
|
||||
);
|
||||
packets.add(RawPacket(command: cmd, payload: payload));
|
||||
|
||||
// 4. 移除已处理部分
|
||||
_buffer.removeRange(0, tailIdx + 2);
|
||||
} else {
|
||||
break; // 还没传完
|
||||
}
|
||||
}
|
||||
return packets;
|
||||
}
|
||||
}
|
||||
54
lib/core/network/tcp/tcp_client.dart
Normal file
54
lib/core/network/tcp/tcp_client.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:maibu_satabot_v2/core/network/protocol_decoder.dart';
|
||||
|
||||
class TcpClient {
|
||||
Socket? _socket;
|
||||
final _decoder = ProtocolDecoder();
|
||||
// 使用 StreamController 将原始数据流暴露给外部,以便多个 DataSource 监听
|
||||
final _controller = StreamController<RawPacket>.broadcast();
|
||||
Stream<RawPacket> get packetStream => _controller.stream;
|
||||
|
||||
// 通过参数配置,不硬编码
|
||||
Future<void> connect({required String host, required int port}) async {
|
||||
try {
|
||||
_socket = await Socket.connect(
|
||||
host,
|
||||
port,
|
||||
timeout: const Duration(seconds: 5),
|
||||
);
|
||||
|
||||
_socket!.listen(
|
||||
(data) {
|
||||
var packets = _decoder.decode(data);
|
||||
for (var packet in packets) {
|
||||
_controller.add(packet);
|
||||
}
|
||||
},
|
||||
onDone: disconnect,
|
||||
onError: (e) => disconnect(),
|
||||
);
|
||||
} catch (e) {
|
||||
rethrow; // 向上抛出连接异常
|
||||
}
|
||||
}
|
||||
|
||||
void send(Map<String, dynamic> data) {
|
||||
if (_socket == null) throw Exception("Socket not connected");
|
||||
_socket!.write(jsonEncode(data));
|
||||
}
|
||||
|
||||
void sendRaw(int cmd, List<int> payload) {
|
||||
if (_socket == null) return;
|
||||
// 组装:头 AB AA + 指令 + 数据 + 尾 AA AB
|
||||
final data = [0xAB, 0xAA, cmd, ...payload, 0xAA, 0xAB];
|
||||
_socket!.add(data);
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
_socket?.destroy();
|
||||
_socket = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user