2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-05-06 12:02:57 +00:00
This commit is contained in:
Ahmed Zamil 2022-02-12 15:37:45 +03:00
parent 25260c6155
commit d41911b1ab
9 changed files with 280 additions and 77 deletions

View File

@ -20,12 +20,15 @@
* SOFTWARE. * SOFTWARE.
*/ */
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:convert'; import 'dart:convert';
import 'BinaryList.dart'; import 'BinaryList.dart';
import 'dart:collection'; import 'dart:collection';
import 'Guid.dart'; import 'Guid.dart';
const bool kIsWeb = identical(0, 0.0);
/** /**
* Created by Ahmed Zamil on 6/10/2019. * Created by Ahmed Zamil on 6/10/2019.
*/ */
@ -245,7 +248,8 @@ class DC with IterableMixin<int> {
var list = new BinaryList(); var list = new BinaryList();
for (var i = 0; i < value.length; i++) { for (var i = 0; i < value.length; i++) {
var s = DC.stringToBytes(value[i]); var s = DC.stringToBytes(value[i]);
list..addUint32(s.length) list
..addUint32(s.length)
..addUint8Array(s.toArray()); ..addUint8Array(s.toArray());
} }
@ -386,13 +390,27 @@ class DC with IterableMixin<int> {
return _data.buffer.asFloat64List(offset, length); return _data.buffer.asFloat64List(offset, length);
} }
Int64List getInt64Array(int offset, int length) { //Int64List
getInt64Array(int offset, int length) {
if (kIsWeb) {
var rt = <int>[];
for (var i = offset; i < length; i += 4) rt.add(this.getInt64(offset));
return rt;
} else {
return _data.buffer.asInt64List(offset, length); return _data.buffer.asInt64List(offset, length);
} }
}
Uint64List getUint64Array(int offset, int length) { //Uint64List
getUint64Array(int offset, int length) {
if (kIsWeb) {
var rt = <int>[];
for (var i = offset; i < length; i += 4) rt.add(this.getUint64(offset));
return rt;
} else {
return _data.buffer.asUint64List(offset, length); return _data.buffer.asUint64List(offset, length);
} }
}
bool getBoolean(int offset) { bool getBoolean(int offset) {
return this.getUint8(offset) > 0; return this.getUint8(offset) > 0;
@ -464,20 +482,39 @@ class DC with IterableMixin<int> {
} }
int getInt64(offset) { int getInt64(offset) {
if (kIsWeb) {
var h = this.getUint32(offset);
var l = this.getUint32(offset + 4);
return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
} else {
return _dv.getUint64(offset); return _dv.getUint64(offset);
} }
}
int getUint64(offset) { int getUint64(offset) {
if (kIsWeb) {
var h = this.getUint32(offset);
var l = this.getUint32(offset + 4);
return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
} else {
return _dv.getInt64(offset); return _dv.getInt64(offset);
} }
}
void setInt64(offset, value) { void setInt64(offset, value) {
_dv.setInt64(offset, value); _dv.setInt64(offset, value);
} }
void setUint64(offset, value) { void setUint64(offset, value) {
if (kIsWeb) {
var l = (value % TWO_PWR_32) | 0;
var h = (value / TWO_PWR_32) | 0;
_dv.setInt32(offset, h);
_dv.setInt32(offset + 4, l);
} else {
_dv.setUint64(offset, value); _dv.setUint64(offset, value);
} }
}
void setDateTime(offset, DateTime value) { void setDateTime(offset, DateTime value) {
// Unix Epoch // Unix Epoch

View File

@ -267,7 +267,7 @@ class DistributedConnection extends NetworkConnection with IStore {
throw AsyncException(ErrorType.Exception, 0, "Session not initialized"); throw AsyncException(ErrorType.Exception, 0, "Session not initialized");
if (socket == null) { if (socket == null) {
if (useWebsocket) { if (useWebsocket || kIsWeb) {
socket = new WSocket()..secure = secureWebSocket; socket = new WSocket()..secure = secureWebSocket;
} else } else
socket = new TCPSocket(); socket = new TCPSocket();

View File

@ -22,10 +22,8 @@ SOFTWARE.
*/ */
import 'dart:io'; //import 'package:web_socket_channel/html.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
//import 'package:web_socket_channel/io.dart' as WS;
import 'package:web_socket_channel/io.dart';
import '../../Core/ErrorType.dart'; import '../../Core/ErrorType.dart';
import '../../Core/ExceptionCode.dart'; import '../../Core/ExceptionCode.dart';
@ -40,8 +38,7 @@ import 'IPEndPoint.dart';
import '../../Core/AsyncReply.dart'; import '../../Core/AsyncReply.dart';
class WSocket extends ISocket { class WSocket extends ISocket {
WebSocket? _sock; WebSocketChannel? _channel;
IOWebSocketChannel? _channel;
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer(); NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
@ -58,16 +55,19 @@ class WSocket extends ISocket {
began = true; began = true;
if (_sock != null) { if (_channel != null) {
var s = _sock as Socket; _localEP = IPEndPoint([0, 0, 0, 0], 0);
_localEP = IPEndPoint(s.address.rawAddress, s.port); _remoteEP = IPEndPoint([0, 0, 0, 0], 0);
_remoteEP = IPEndPoint(s.remoteAddress.rawAddress, s.remotePort); _channel?.stream
.listen(_dataHandler, onError: errorHandler, onDone: doneHandler);
} }
return true; return true;
} }
void dataHandler(List<int> data) { void _dataHandler(data) {
try { try {
//List<int> data
if (_state == SocketState.Closed || _state == SocketState.Terminated) if (_state == SocketState.Closed || _state == SocketState.Terminated)
return; return;
@ -85,6 +85,7 @@ class WSocket extends ISocket {
void errorHandler(error, StackTrace trace) { void errorHandler(error, StackTrace trace) {
print(error); print(error);
close();
} }
void doneHandler() { void doneHandler() {
@ -98,19 +99,15 @@ class WSocket extends ISocket {
try { try {
_state = SocketState.Connecting; _state = SocketState.Connecting;
WebSocket.connect("${secure ? 'wss' : 'ws'}://${hostname}:${port}'") _channel = WebSocketChannel.connect(
.then((s) { Uri.parse("${secure ? 'wss' : 'ws'}://${hostname}:${port}"),
_sock = s; ); //binaryType: BinaryType.list);
_state = SocketState.Established; _state = SocketState.Established;
_channel = IOWebSocketChannel(s);
begin(); begin();
receiver?.networkConnect(this); receiver?.networkConnect(this);
rt.trigger(true); rt.trigger(true);
}).catchError((ex) {
close();
rt.triggerError(AsyncException(ErrorType.Management,
ExceptionCode.HostNotReachable.index, ex.toString()));
});
} catch (ex) { } catch (ex) {
rt.triggerError(AsyncException(ErrorType.Management, rt.triggerError(AsyncException(ErrorType.Management,
ExceptionCode.HostNotReachable.index, ex.toString())); ExceptionCode.HostNotReachable.index, ex.toString()));
@ -132,22 +129,19 @@ class WSocket extends ISocket {
if (state != SocketState.Closed && state != SocketState.Terminated) if (state != SocketState.Closed && state != SocketState.Terminated)
_state = SocketState.Closed; _state = SocketState.Closed;
_sock?.close(); _channel?.sink.close();
receiver?.networkClose(this); receiver?.networkClose(this);
//emitArgs("close", []);
} }
void send(DC message, [int? offset, int? size]) { void send(DC message, [int? offset, int? size]) {
if (state == SocketState.Established) { if (state == SocketState.Established) {
if (offset != null && size == null) { if (offset != null && size == null) {
_channel?.sink _channel?.sink
.add(message.clip(offset, message.length - offset).toList()); .add(message.clip(offset, message.length - offset).toArray());
} else if (offset != null && size != null) { } else if (offset != null && size != null) {
_channel?.sink.add(message.clip(offset, size).toList()); _channel?.sink.add(message.clip(offset, size).toArray());
} else { } else {
_channel?.sink.add(message.toList()); _channel?.sink.add(message.toArray());
} }
} }
} }

View File

@ -0,0 +1,165 @@
/*
Copyright (c) 2019 Ahmed Kh. Zamil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import 'dart:io';
//import 'package:web_socket_channel/io.dart' as WS;
import 'package:web_socket_channel/io.dart';
import '../../Core/ErrorType.dart';
import '../../Core/ExceptionCode.dart';
import '../../Core/AsyncException.dart';
import 'ISocket.dart';
import '../../Data/DC.dart';
import '../NetworkBuffer.dart';
import 'SocketState.dart';
import 'IPEndPoint.dart';
import '../../Core/AsyncReply.dart';
class WSocketIO extends ISocket {
WebSocket? _sock;
IOWebSocketChannel? _channel;
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
bool began = false;
bool secure = false;
SocketState _state = SocketState.Initial;
IPEndPoint? _localEP, _remoteEP;
bool begin() {
if (began) return false;
began = true;
if (_sock != null) {
var s = _sock as Socket;
_localEP = IPEndPoint(s.address.rawAddress, s.port);
_remoteEP = IPEndPoint(s.remoteAddress.rawAddress, s.remotePort);
}
return true;
}
void dataHandler(List<int> data) {
try {
if (_state == SocketState.Closed || _state == SocketState.Terminated)
return;
var dc = new DC.fromList(data);
receiveNetworkBuffer.write(dc, 0, dc.length);
receiver?.networkReceive(this, receiveNetworkBuffer);
} catch (ex) {
if (_state != SocketState.Closed) // && !sock.connected)
{
_state = SocketState.Terminated;
close();
}
}
}
void errorHandler(error, StackTrace trace) {
print(error);
}
void doneHandler() {
close();
//_sock?.destroy();
}
AsyncReply<bool> connect(String hostname, int port) {
var rt = new AsyncReply<bool>();
try {
_state = SocketState.Connecting;
WebSocket.connect("${secure ? 'wss' : 'ws'}://${hostname}:${port}'")
.then((s) {
_sock = s;
_state = SocketState.Established;
_channel = IOWebSocketChannel(s);
begin();
receiver?.networkConnect(this);
rt.trigger(true);
}).catchError((ex) {
close();
rt.triggerError(AsyncException(ErrorType.Management,
ExceptionCode.HostNotReachable.index, ex.toString()));
});
} catch (ex) {
rt.triggerError(AsyncException(ErrorType.Management,
ExceptionCode.HostNotReachable.index, ex.toString()));
}
return rt;
}
IPEndPoint? get localEndPoint => _localEP;
IPEndPoint? get remoteEndPoint => _remoteEP;
SocketState get state => _state;
TCPSocket() {
// default constructor
}
void close() {
if (state != SocketState.Closed && state != SocketState.Terminated)
_state = SocketState.Closed;
_sock?.close();
receiver?.networkClose(this);
//emitArgs("close", []);
}
void send(DC message, [int? offset, int? size]) {
if (state == SocketState.Established) {
if (offset != null && size == null) {
_channel?.sink
.add(message.clip(offset, message.length - offset).toList());
} else if (offset != null && size != null) {
_channel?.sink.add(message.clip(offset, size).toList());
} else {
_channel?.sink.add(message.toList());
}
}
}
void destroy() {
close();
emitArgs("destroy", [this]);
}
AsyncReply<ISocket> accept() {
var reply = new AsyncReply<ISocket>();
return reply;
}
}

View File

@ -1,4 +1,5 @@
import 'dart:core'; import 'dart:core';
import '../Data/DC.dart'; import '../Data/DC.dart';
import '../Data/Structure.dart'; import '../Data/Structure.dart';
import '../Data/AutoList.dart'; import '../Data/AutoList.dart';
@ -21,6 +22,9 @@ import './Template/MemberTemplate.dart';
import '../Data/PropertyValue.dart'; import '../Data/PropertyValue.dart';
import 'Warehouse.dart'; import 'Warehouse.dart';
import '../Core/PropertyModificationInfo.dart';
class Instance extends IEventHandler { class Instance extends IEventHandler {
String _name; String _name;
@ -387,7 +391,8 @@ class Instance extends IEventHandler {
//_resource.emitArgs("modified", [pt.name, value]); //_resource.emitArgs("modified", [pt.name, value]);
_resource.emitArgs(":${pt.name}", [value]); _resource.emitArgs(":${pt.name}", [value]);
_resource.emitProperty(pt.name); _resource.emitProperty(
PropertyModificationInfo(_resource, pt, value, _instanceAge));
} }
/// <summary> /// <summary>
@ -542,7 +547,7 @@ class Instance extends IEventHandler {
if (customTemplate != null) if (customTemplate != null)
_template = customTemplate; _template = customTemplate;
else else
_template = Warehouse.getTemplateByType(resource.runtimeType); _template = Warehouse.getTemplateByType(resource.runtimeType)!;
// set ages // set ages
for (int i = 0; i < _template.properties.length; i++) { for (int i = 0; i < _template.properties.length; i++) {

View File

@ -1,4 +1,4 @@
import 'dart:ffi'; //import 'dart:ffi';
import '../../Data/IRecord.dart'; import '../../Data/IRecord.dart';
import '../../Resource/IResource.dart'; import '../../Resource/IResource.dart';
@ -27,31 +27,32 @@ class TemplateDataType {
TemplateDataType.fromType(type, bool isArray) { TemplateDataType.fromType(type, bool isArray) {
int dt; int dt;
if (type == null || type == dynamic) if (type == null || type == dynamic) {
dt = DataType.Void; dt = DataType.Void;
else if (type is int) { }
dt = type; // else if (type is int) {
} else if (type == bool) // dt = type;
else if (type == bool)
dt = DataType.Bool; dt = DataType.Bool;
else if (type == Uint8) // else if (type == Uint8)
dt = DataType.UInt8; // dt = DataType.UInt8;
else if (type == Int8) // else if (type == Int8)
dt = DataType.Int8; // dt = DataType.Int8;
else if (type == Uint16) // else if (type == Uint16)
dt = DataType.UInt16; // dt = DataType.UInt16;
else if (type == Int16) // else if (type == Int16)
dt = DataType.Int16; // dt = DataType.Int16;
else if (type == Uint32) // else if (type == Uint32)
dt = DataType.UInt32; // dt = DataType.UInt32;
else if (type == Int32) // else if (type == Int32)
dt = DataType.Int32; // dt = DataType.Int32;
else if (type == Uint64) // else if (type == Uint64)
dt = DataType.UInt64; // dt = DataType.UInt64;
else if (type == Int64 || type == int) else if (/* type == Int64 || */ type == int)
dt = DataType.Int64; dt = DataType.Int64;
else if (type == Float) // else if (type == Float)
dt = DataType.Float32; // dt = DataType.Float32;
else if (type == Double || type == double) else if (/* type == Double || */ type == double)
dt = DataType.Float64; dt = DataType.Float64;
else if (type == String) else if (type == String)
dt = DataType.String; dt = DataType.String;

View File

@ -1,4 +1,4 @@
import 'dart:ffi'; //import 'dart:ffi';
import '../../Net/IIP/DistributedResource.dart'; import '../../Net/IIP/DistributedResource.dart';

View File

@ -482,12 +482,12 @@ class Warehouse {
return rt; return rt;
} }
static T createInstance<T>(Type T) { static T createInstance<T>(Type type) {
return _factory[T]?.instanceCreator.call(); return _factory[type]?.instanceCreator.call();
} }
static List<T> createArray<T>(Type T) { static List<T> createArray<T>(Type type) {
return _factory[T]?.arrayCreator.call(); return _factory[type]?.arrayCreator.call();
} }
static AsyncReply<T> newResource<T extends IResource>(String name, static AsyncReply<T> newResource<T extends IResource>(String name,
@ -546,16 +546,17 @@ class Warehouse {
/// </summary> /// </summary>
/// <param name="type">.Net type.</param> /// <param name="type">.Net type.</param>
/// <returns>Resource template.</returns> /// <returns>Resource template.</returns>
static TypeTemplate getTemplateByType(Type type) { static TypeTemplate? getTemplateByType(Type type) {
// loaded ? // loaded ?
for (var tmps in _templates.values) for (var tmps in _templates.values)
for (var tmp in tmps.values) if (tmp.definedType == type) return tmp; for (var tmp in tmps.values) if (tmp.definedType == type) return tmp;
//if (tmp.className == type.toString()) return tmp; //try {
var template = new TypeTemplate.fromType(type, true); var template = new TypeTemplate.fromType(type, true);
return template; return template;
//} catch (ex) {
// return null;
//}
} }
/// <summary> /// <summary>

View File

@ -137,7 +137,7 @@ class SHA256 {
// (The initial values in w[0..63] don't matter, so many implementations zero them here) // (The initial values in w[0..63] don't matter, so many implementations zero them here)
// copy chunk into first 16 words w[0..15] of the message schedule array // copy chunk into first 16 words w[0..15] of the message schedule array
var w = new Uint64List(64); // uint[64]; var w = new Uint32List(64); // new Uint64List(64); // uint[64];
for (var i = 0; i < 16; i++) w[i] = data.getUint32(chunk + (i * 4)); for (var i = 0; i < 16; i++) w[i] = data.getUint32(chunk + (i * 4));
//for(var i = 16; i < 64; i++) //for(var i = 16; i < 64; i++)