Merge branch 'v5' into flutter-app

This commit is contained in:
Evgeny Poberezkin 2021-10-09 13:13:27 +01:00 committed by GitHub
commit 44892e5b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 375 additions and 149 deletions

3
.gitignore vendored
View File

@ -77,6 +77,8 @@ stack.yaml.lock
.pub-cache/ .pub-cache/
.pub/ .pub/
build/ build/
# Default behavior, only keep it for apps
pubspec.lock
# Web # Web
lib/generated_plugin_registrant.dart lib/generated_plugin_registrant.dart
@ -91,3 +93,4 @@ app.*.map.json
/android/app/debug /android/app/debug
/android/app/profile /android/app/profile
/android/app/release /android/app/release

View File

@ -2,7 +2,7 @@ include: package:lints/recommended.yaml
linter: linter:
rules: rules:
prefer_double_quotes: true prefer_single_quotes: true
constant_identifier_names: false constant_identifier_names: false
always_declare_return_types: true always_declare_return_types: true
avoid_dynamic_calls: true avoid_dynamic_calls: true

2
packages/simplex_app/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Keep for apps
!pubspec.lock

View File

@ -1,3 +0,0 @@
# Omit committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

View File

@ -3,6 +3,5 @@
/// More dartdocs go here. /// More dartdocs go here.
library simplexmq; library simplexmq;
export "src/protocol.dart"; export 'src/protocol.dart';
export 'src/transport.dart' show Transport;
// TODO: Export any libraries intended for clients of this package.

View File

@ -1,4 +1,4 @@
import "dart:typed_data"; import 'dart:typed_data';
Uint8List encodeAscii(String s) => Uint8List.fromList(s.codeUnits); Uint8List encodeAscii(String s) => Uint8List.fromList(s.codeUnits);
@ -29,8 +29,8 @@ Uint8List concatN(List<Uint8List> bs) {
return a; return a;
} }
final charSpace = " ".codeUnitAt(0); final charSpace = ' '.codeUnitAt(0);
final charEqual = "=".codeUnitAt(0); final charEqual = '='.codeUnitAt(0);
final empty = Uint8List(0); final empty = Uint8List(0);
Uint8List unwords(Uint8List b1, Uint8List b2) { Uint8List unwords(Uint8List b1, Uint8List b2) {
@ -61,7 +61,7 @@ Uint8List unwordsN(List<Uint8List> bs) {
} }
final _base64chars = Uint8List.fromList( final _base64chars = Uint8List.fromList(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
.codeUnits); .codeUnits);
List<int?> __base64lookup() { List<int?> __base64lookup() {

View File

@ -1,18 +1,18 @@
import "dart:typed_data"; import 'dart:typed_data';
import "buffer.dart"; import 'buffer.dart';
typedef BinaryTags<T> = Map<T, Uint8List>; typedef BinaryTags<T> = Map<T, Uint8List>;
int cc(String c) => c.codeUnitAt(0); int cc(String c) => c.codeUnitAt(0);
final char0 = cc("0"); final char0 = cc('0');
final char9 = cc("9"); final char9 = cc('9');
final charLowerA = cc("a"); final charLowerA = cc('a');
final charLowerZ = cc("z"); final charLowerZ = cc('z');
final charUpperA = cc("A"); final charUpperA = cc('A');
final charUpperZ = cc("Z"); final charUpperZ = cc('Z');
final charPlus = cc("+"); final charPlus = cc('+');
final charSlash = cc("/"); final charSlash = cc('/');
class Parser { class Parser {
final Uint8List _s; final Uint8List _s;

View File

@ -1,6 +1,6 @@
import "dart:typed_data"; import 'dart:typed_data';
import "buffer.dart"; import 'buffer.dart';
import "parser.dart"; import 'parser.dart';
abstract class SMPCommand { abstract class SMPCommand {
Uint8List serialize(); Uint8List serialize();
@ -10,25 +10,25 @@ abstract class ClientCommand extends SMPCommand {}
abstract class BrokerCommand extends SMPCommand {} abstract class BrokerCommand extends SMPCommand {}
final rsaPrefix = encodeAscii("rsa:"); final rsaPrefix = encodeAscii('rsa:');
Uint8List serializePubKey(Uint8List rcvPubKey) => Uint8List serializePubKey(Uint8List rcvPubKey) =>
concat(rsaPrefix, encode64(rcvPubKey)); concat(rsaPrefix, encode64(rcvPubKey));
final Uint8List cNEW = encodeAscii("NEW"); final Uint8List cNEW = encodeAscii('NEW');
final Uint8List cSUB = encodeAscii("SUB"); final Uint8List cSUB = encodeAscii('SUB');
final Uint8List cKEY = encodeAscii("KEY"); final Uint8List cKEY = encodeAscii('KEY');
final Uint8List cACK = encodeAscii("ACK"); final Uint8List cACK = encodeAscii('ACK');
final Uint8List cOFF = encodeAscii("OFF"); final Uint8List cOFF = encodeAscii('OFF');
final Uint8List cDEL = encodeAscii("DEL"); final Uint8List cDEL = encodeAscii('DEL');
final Uint8List cSEND = encodeAscii("SEND"); final Uint8List cSEND = encodeAscii('SEND');
final Uint8List cPING = encodeAscii("PING"); final Uint8List cPING = encodeAscii('PING');
final Uint8List cIDS = encodeAscii("IDS"); final Uint8List cIDS = encodeAscii('IDS');
final Uint8List cMSG = encodeAscii("MSG"); final Uint8List cMSG = encodeAscii('MSG');
final Uint8List cEND = encodeAscii("END"); final Uint8List cEND = encodeAscii('END');
final Uint8List cOK = encodeAscii("OK"); final Uint8List cOK = encodeAscii('OK');
final Uint8List cERR = encodeAscii("ERR"); final Uint8List cERR = encodeAscii('ERR');
final Uint8List cPONG = encodeAscii("PONG"); final Uint8List cPONG = encodeAscii('PONG');
enum SMPCmdTag { enum SMPCmdTag {
NEW, NEW,
@ -148,23 +148,23 @@ class OK extends BrokerCommand {
enum ErrorType { BLOCK, CMD, AUTH, QUOTA, NO_MSG, INTERNAL } enum ErrorType { BLOCK, CMD, AUTH, QUOTA, NO_MSG, INTERNAL }
final BinaryTags<ErrorType> errorTags = { final BinaryTags<ErrorType> errorTags = {
ErrorType.BLOCK: encodeAscii("BLOCK"), ErrorType.BLOCK: encodeAscii('BLOCK'),
ErrorType.CMD: encodeAscii("CMD"), ErrorType.CMD: encodeAscii('CMD'),
ErrorType.AUTH: encodeAscii("AUTH"), ErrorType.AUTH: encodeAscii('AUTH'),
ErrorType.QUOTA: encodeAscii("QUOTA"), ErrorType.QUOTA: encodeAscii('QUOTA'),
ErrorType.NO_MSG: encodeAscii("NO_MSG"), ErrorType.NO_MSG: encodeAscii('NO_MSG'),
ErrorType.INTERNAL: encodeAscii("INTERNAL"), ErrorType.INTERNAL: encodeAscii('INTERNAL'),
}; };
enum CmdErrorType { PROHIBITED, KEY_SIZE, SYNTAX, NO_AUTH, HAS_AUTH, NO_QUEUE } enum CmdErrorType { PROHIBITED, KEY_SIZE, SYNTAX, NO_AUTH, HAS_AUTH, NO_QUEUE }
final BinaryTags<CmdErrorType> cmdErrorTags = { final BinaryTags<CmdErrorType> cmdErrorTags = {
CmdErrorType.PROHIBITED: encodeAscii("PROHIBITED"), CmdErrorType.PROHIBITED: encodeAscii('PROHIBITED'),
CmdErrorType.KEY_SIZE: encodeAscii("KEY_SIZE"), CmdErrorType.KEY_SIZE: encodeAscii('KEY_SIZE'),
CmdErrorType.SYNTAX: encodeAscii("SYNTAX"), CmdErrorType.SYNTAX: encodeAscii('SYNTAX'),
CmdErrorType.NO_AUTH: encodeAscii("NO_AUTH"), CmdErrorType.NO_AUTH: encodeAscii('NO_AUTH'),
CmdErrorType.HAS_AUTH: encodeAscii("HAS_AUTH"), CmdErrorType.HAS_AUTH: encodeAscii('HAS_AUTH'),
CmdErrorType.NO_QUEUE: encodeAscii("NO_QUEUE"), CmdErrorType.NO_QUEUE: encodeAscii('NO_QUEUE'),
}; };
class ERR extends BrokerCommand { class ERR extends BrokerCommand {
@ -172,7 +172,7 @@ class ERR extends BrokerCommand {
final CmdErrorType? cmdErr; final CmdErrorType? cmdErr;
ERR(this.err) ERR(this.err)
: cmdErr = err == ErrorType.CMD : cmdErr = err == ErrorType.CMD
? throw ArgumentError("CMD error should be created with ERR.CMD") ? throw ArgumentError('CMD error should be created with ERR.CMD')
: null; : null;
ERR.cmd(this.cmdErr) : err = ErrorType.CMD; ERR.cmd(this.cmdErr) : err = ErrorType.CMD;
@override @override

View File

@ -0,0 +1,17 @@
import 'dart:async';
import 'dart:typed_data';
abstract class Transport {
Future<Uint8List> read(int n);
Future<void> write(Uint8List data);
}
Stream<Uint8List> blockStream(Transport t, int blockSize) async* {
try {
while (true) {
yield await t.read(blockSize);
}
} catch (e) {
return;
}
}

View File

@ -1,7 +1,7 @@
name: simplexmq name: simplexmq
description: A starting point for Dart libraries or applications. description: A starting point for Dart libraries or applications.
version: 0.0.1 version: 0.0.1
# homepage: https://www.example.com publish_to: none
environment: environment:
sdk: '>=2.14.0 <3.0.0' sdk: '>=2.14.0 <3.0.0'

View File

@ -1,6 +1,6 @@
import "dart:typed_data"; import 'dart:typed_data';
import "package:simplexmq/src/buffer.dart"; import 'package:simplexmq/src/buffer.dart';
import "package:test/test.dart"; import 'package:test/test.dart';
final hello123 = Uint8List.fromList([104, 101, 108, 108, 111, 49, 50, 51]); final hello123 = Uint8List.fromList([104, 101, 108, 108, 111, 49, 50, 51]);
@ -14,17 +14,17 @@ class Base64Test {
} }
void main() { void main() {
group("ascii encoding/decoding", () { group('ascii encoding/decoding', () {
test("encodeAscii", () { test('encodeAscii', () {
expect(encodeAscii("hello123"), hello123); expect(encodeAscii('hello123'), hello123);
}); });
test("decodeAscii", () { test('decodeAscii', () {
expect(decodeAscii(hello123), "hello123"); expect(decodeAscii(hello123), 'hello123');
}); });
}); });
group("base-64 encoding/decoding", () { group('base-64 encoding/decoding', () {
String allBinaryChars() { String allBinaryChars() {
final a = Uint8List(256); final a = Uint8List(256);
for (var i = 0; i < 256; i++) { for (var i = 0; i < 256; i++) {
@ -34,33 +34,33 @@ void main() {
} }
final base64tests = [ final base64tests = [
Base64Test("\x12\x34\x56\x78", "EjRWeA=="), Base64Test('\x12\x34\x56\x78', 'EjRWeA=='),
Base64Test("hello123", "aGVsbG8xMjM="), Base64Test('hello123', 'aGVsbG8xMjM='),
Base64Test("Hello world", "SGVsbG8gd29ybGQ="), Base64Test('Hello world', 'SGVsbG8gd29ybGQ='),
Base64Test("Hello worlds!", "SGVsbG8gd29ybGRzIQ=="), Base64Test('Hello worlds!', 'SGVsbG8gd29ybGRzIQ=='),
Base64Test("May", "TWF5"), Base64Test('May', 'TWF5'),
Base64Test("Ma", "TWE="), Base64Test('Ma', 'TWE='),
Base64Test("M", "TQ=="), Base64Test('M', 'TQ=='),
Base64Test.withDescription( Base64Test.withDescription(
"all binary chars", 'all binary chars',
allBinaryChars(), allBinaryChars(),
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==", 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==',
), ),
]; ];
test("encode64", () { test('encode64', () {
for (final t in base64tests) { for (final t in base64tests) {
expect(encode64(encodeAscii(t.binary)), encodeAscii(t.base64)); expect(encode64(encodeAscii(t.binary)), encodeAscii(t.base64));
} }
}); });
test("decode64", () { test('decode64', () {
for (final t in base64tests) { for (final t in base64tests) {
expect(decode64(encodeAscii(t.base64)), encodeAscii(t.binary)); expect(decode64(encodeAscii(t.base64)), encodeAscii(t.binary));
} }
expect(decode64(encodeAscii("TWE")), null); expect(decode64(encodeAscii('TWE')), null);
expect(decode64(encodeAscii("TWE==")), null); expect(decode64(encodeAscii('TWE==')), null);
expect(decode64(encodeAscii("TW!=")), null); expect(decode64(encodeAscii('TW!=')), null);
}); });
}); });
} }

View File

@ -1,11 +1,11 @@
import "dart:typed_data"; import 'dart:typed_data';
import "package:simplexmq/src/buffer.dart"; import 'package:simplexmq/src/buffer.dart';
import "package:simplexmq/src/protocol.dart"; import 'package:simplexmq/src/protocol.dart';
import "package:test/test.dart"; import 'package:test/test.dart';
void main() { void main() {
group("Parsing & serializing SMP commands", () { group('Parsing & serializing SMP commands', () {
group("valid commands", () { group('valid commands', () {
Null Function() parseSerialize(SMPCommand cmd) => () { Null Function() parseSerialize(SMPCommand cmd) => () {
final s = cmd.serialize(); final s = cmd.serialize();
expect(parseSMPCommand(s)?.serialize(), s); expect(parseSMPCommand(s)?.serialize(), s);
@ -13,92 +13,92 @@ void main() {
null); null);
}; };
test("NEW", parseSerialize(NEW(encodeAscii("rsa:1234")))); test('NEW', parseSerialize(NEW(encodeAscii('rsa:1234'))));
test("SUB", parseSerialize(SUB())); test('SUB', parseSerialize(SUB()));
test("KEY", parseSerialize(KEY(encodeAscii("rsa:1234")))); test('KEY', parseSerialize(KEY(encodeAscii('rsa:1234'))));
test("ACK", parseSerialize(ACK())); test('ACK', parseSerialize(ACK()));
test("OFF", parseSerialize(OFF())); test('OFF', parseSerialize(OFF()));
test("DEL", parseSerialize(DEL())); test('DEL', parseSerialize(DEL()));
test("SEND", parseSerialize(SEND(encodeAscii("hello")))); test('SEND', parseSerialize(SEND(encodeAscii('hello'))));
test("PING", parseSerialize(PING())); test('PING', parseSerialize(PING()));
test("IDS", parseSerialize(IDS(encodeAscii("abc"), encodeAscii("def")))); test('IDS', parseSerialize(IDS(encodeAscii('abc'), encodeAscii('def'))));
test( test(
"MSG", 'MSG',
parseSerialize(MSG(encodeAscii("fgh"), DateTime.now().toUtc(), parseSerialize(MSG(encodeAscii('fgh'), DateTime.now().toUtc(),
encodeAscii("hello")))); encodeAscii('hello'))));
test("END", parseSerialize(END())); test('END', parseSerialize(END()));
test("OK", parseSerialize(OK())); test('OK', parseSerialize(OK()));
test("ERR", parseSerialize(ERR(ErrorType.AUTH))); test('ERR', parseSerialize(ERR(ErrorType.AUTH)));
test("ERR CMD", parseSerialize(ERR.cmd(CmdErrorType.SYNTAX))); test('ERR CMD', parseSerialize(ERR.cmd(CmdErrorType.SYNTAX)));
test("PONG", parseSerialize(PONG())); test('PONG', parseSerialize(PONG()));
}); });
group("invalid commands", () { group('invalid commands', () {
void Function() parseFailure(String s) => void Function() parseFailure(String s) =>
() => expect(parseSMPCommand(encodeAscii(s)), null); () => expect(parseSMPCommand(encodeAscii(s)), null);
void Function() parseSuccess(String s) => void Function() parseSuccess(String s) =>
() => expect(parseSMPCommand(encodeAscii(s)) is SMPCommand, true); () => expect(parseSMPCommand(encodeAscii(s)) is SMPCommand, true);
group("NEW", () { group('NEW', () {
test("ok", parseSuccess("NEW rsa:abcd")); test('ok', parseSuccess('NEW rsa:abcd'));
test("no key", parseFailure("NEW")); test('no key', parseFailure('NEW'));
test("invalid base64", parseFailure("NEW rsa:abc")); test('invalid base64', parseFailure('NEW rsa:abc'));
test("double space", parseFailure("NEW rsa:abcd")); test('double space', parseFailure('NEW rsa:abcd'));
}); });
group("KEY", () { group('KEY', () {
test("ok", parseSuccess("KEY rsa:abcd")); test('ok', parseSuccess('KEY rsa:abcd'));
test("no key", parseFailure("KEY")); test('no key', parseFailure('KEY'));
test("invalid base64", parseFailure("KEY rsa:abc")); test('invalid base64', parseFailure('KEY rsa:abc'));
test("double space", parseFailure("KEY rsa:abcd")); test('double space', parseFailure('KEY rsa:abcd'));
}); });
group("SEND", () { group('SEND', () {
test("ok", parseSuccess("SEND 5 hello ")); test('ok', parseSuccess('SEND 5 hello '));
test("no size", parseFailure("SEND hello ")); test('no size', parseFailure('SEND hello '));
test("incorrect size", parseFailure("SEND 6 hello ")); test('incorrect size', parseFailure('SEND 6 hello '));
test("no trailing space", parseFailure("SEND 5 hello")); test('no trailing space', parseFailure('SEND 5 hello'));
test("double space 1", parseFailure("SEND 5 hello ")); test('double space 1', parseFailure('SEND 5 hello '));
test("double space 2", parseFailure("SEND 5 hello ")); test('double space 2', parseFailure('SEND 5 hello '));
}); });
group("IDS", () { group('IDS', () {
test("ok", parseSuccess("IDS abcd efgh")); test('ok', parseSuccess('IDS abcd efgh'));
test("no IDs", parseFailure("IDS")); test('no IDs', parseFailure('IDS'));
test("only one ID", parseFailure("IDS abcd")); test('only one ID', parseFailure('IDS abcd'));
test("invalid base64 1", parseFailure("IDS abc efgh")); test('invalid base64 1', parseFailure('IDS abc efgh'));
test("invalid base64 2", parseFailure("IDS abcd efg")); test('invalid base64 2', parseFailure('IDS abcd efg'));
test("double space 1", parseFailure("IDS abcd efgh")); test('double space 1', parseFailure('IDS abcd efgh'));
test("double space 2", parseFailure("IDS abcd efgh")); test('double space 2', parseFailure('IDS abcd efgh'));
}); });
group("MSG", () { group('MSG', () {
final String ts = "2021-10-03T10:50:59.895Z"; final String ts = '2021-10-03T10:50:59.895Z';
test("ok", parseSuccess("MSG abcd $ts 5 hello ")); test('ok', parseSuccess('MSG abcd $ts 5 hello '));
test("invalid base64", parseFailure("MSG abc $ts 5 hello ")); test('invalid base64', parseFailure('MSG abc $ts 5 hello '));
test("invalid timestamp 1", test('invalid timestamp 1',
parseFailure("MSG abc 2021-10-03T10:50:59.895 5 hello ")); parseFailure('MSG abc 2021-10-03T10:50:59.895 5 hello '));
test("invalid timestamp 2", test('invalid timestamp 2',
parseFailure("MSG abc 2021-14-03T10:50:59.895Z 5 hello ")); parseFailure('MSG abc 2021-14-03T10:50:59.895Z 5 hello '));
test("no size", parseFailure("MSG abcd $ts hello ")); test('no size', parseFailure('MSG abcd $ts hello '));
test("incorrect size", parseFailure("MSG abcd $ts 6 hello ")); test('incorrect size', parseFailure('MSG abcd $ts 6 hello '));
test("no trailing space", parseFailure("MSG abcd $ts 5 hello")); test('no trailing space', parseFailure('MSG abcd $ts 5 hello'));
test("double space 1", parseFailure("MSG abcd $ts 5 hello ")); test('double space 1', parseFailure('MSG abcd $ts 5 hello '));
test("double space 2", parseFailure("MSG abcd $ts 5 hello ")); test('double space 2', parseFailure('MSG abcd $ts 5 hello '));
test("double space 3", parseFailure("MSG abcd $ts 5 hello ")); test('double space 3', parseFailure('MSG abcd $ts 5 hello '));
test("double space 4", parseFailure("MSG abcd $ts 5 hello ")); test('double space 4', parseFailure('MSG abcd $ts 5 hello '));
}); });
group("ERR", () { group('ERR', () {
test("ok 1", parseSuccess("ERR AUTH")); test('ok 1', parseSuccess('ERR AUTH'));
test("ok 2", parseSuccess("ERR CMD SYNTAX")); test('ok 2', parseSuccess('ERR CMD SYNTAX'));
test("unknown error", parseFailure("ERR HELLO")); test('unknown error', parseFailure('ERR HELLO'));
test("unknown CMD error", parseFailure("ERR CMD HELLO")); test('unknown CMD error', parseFailure('ERR CMD HELLO'));
test("bad sub-error", parseFailure("ERR AUTH SYNTAX")); test('bad sub-error', parseFailure('ERR AUTH SYNTAX'));
test("double space 1", parseFailure("ERR AUTH")); test('double space 1', parseFailure('ERR AUTH'));
test("double space 2", parseFailure("ERR CMD SYNTAX")); test('double space 2', parseFailure('ERR CMD SYNTAX'));
test("double space 3", parseFailure("ERR CMD SYNTAX")); test('double space 3', parseFailure('ERR CMD SYNTAX'));
}); });
}); });
}); });

View File

@ -0,0 +1,3 @@
## 1.0.0
- Initial version.

View File

@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->
TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
## Features
TODO: List what your package can do. Maybe include images, gifs, or videos.
## Getting started
TODO: List prerequisites and provide or point to information on how to
start using the package.
## Usage
TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
```dart
const like = 'sample';
```
## Additional information
TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.

View File

@ -0,0 +1,6 @@
// import 'package:simplexmq_io/simplexmq_io.dart';
// void main() {
// var awesome = Awesome();
// print('awesome: ${awesome.isAwesome}');
// }

View File

@ -0,0 +1,3 @@
library simplexmq_io;
export 'src/socket.dart';

View File

@ -0,0 +1,87 @@
import 'dart:async';
import 'dart:collection';
import 'dart:io';
import 'dart:typed_data';
import 'package:simplexmq/simplexmq.dart';
class _STReaders {
final Completer<Uint8List> completer = Completer();
final int size;
_STReaders(this.size);
}
class SocketTransport implements Transport {
final Socket _socket;
late final StreamSubscription _subscription;
// ignore: unused_field
final Duration _timeout;
final int _bufferSize;
Uint8List _buffer = Uint8List(0);
final ListQueue<_STReaders> _readers = ListQueue(16);
SocketTransport._new(this._socket, this._timeout, this._bufferSize);
static Future<SocketTransport> connect(String host, int port,
{Duration timeout = const Duration(seconds: 1),
int bufferSize = 16384}) async {
final socket = await Socket.connect(host, port, timeout: timeout);
final t = SocketTransport._new(socket, timeout, bufferSize);
// ignore: cancel_subscriptions
final subscription = socket.listen(t._onData,
onError: (Object e) => t._finalize, onDone: t._finalize);
t._subscription = subscription;
return t;
}
@override
Future<Uint8List> read(int n) async {
if (_readers.isEmpty && _buffer.length >= n) {
final data = _buffer.sublist(0, n);
_buffer = _buffer.sublist(n);
return Future.value(data);
}
final r = _STReaders(n);
_readers.add(r);
return r.completer.future;
}
@override
Future<void> write(Uint8List data) {
_socket.add(data);
return _socket.flush();
}
void _onData(Uint8List data) {
final b = Uint8List(_buffer.length + data.length);
b.setAll(0, _buffer);
b.setAll(_buffer.length, data);
_buffer = b;
while (_readers.isNotEmpty && _readers.first.size <= _buffer.length) {
final r = _readers.removeFirst();
final d = _buffer.sublist(0, r.size);
r.completer.complete(d);
_buffer = _buffer.sublist(r.size);
}
final overflow = _buffer.length >= _bufferSize;
if (_subscription.isPaused && !overflow) {
_subscription.resume();
} else if (!_subscription.isPaused && overflow) {
_subscription.pause();
}
}
void _finalize() {
_subscription.cancel();
_socket.destroy();
while (_readers.isNotEmpty) {
final r = _readers.removeFirst();
r.completer.completeError(Exception('socket closed'));
}
}
/// Allow closing the client transport.
void close() {
_finalize();
}
}

View File

@ -0,0 +1,22 @@
name: simplexmq_io
description: A starting point for Dart libraries or applications.
version: 1.0.0
publish_to: none
environment:
sdk: '>=2.14.0 <3.0.0'
dependencies:
simplexmq:
git:
url: git://github.com/simplex-chat/simplex-chat
path: packages/simplexmq
ref: ep/socket-transport
dependency_overrides:
simplexmq:
path: ../simplexmq
dev_dependencies:
lints: ^1.0.0
test: ^1.16.0

View File

@ -0,0 +1,48 @@
// ignore_for_file: prefer_double_quotes
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:simplexmq_io/simplexmq_io.dart';
import 'package:test/test.dart';
const localhost = 'localhost';
void main() {
group('transport', () {
Future<ServerSocket> startServer(
void Function(Socket client) handleConnection) async {
var server = await ServerSocket.bind(InternetAddress.anyIPv4, 0);
server.listen(handleConnection);
return server;
}
test('simple write', () async {
var completer = Completer<Uint8List>();
var server = await startServer((Socket client) {
client.listen(
(Uint8List data) async {
completer.complete(data);
},
);
});
var transport = await SocketTransport.connect(localhost, server.port);
await transport.write(Uint8List.fromList([1, 2, 3]));
expect(await completer.future, [1, 2, 3]);
transport.close();
await server.close();
});
test('simple read', () async {
var server = await startServer((Socket client) {
client.add(Uint8List.fromList([1, 2, 3]));
});
var transport = await SocketTransport.connect(localhost, server.port);
expect(await transport.read(3), [1, 2, 3]);
transport.close();
await server.close();
});
});
}