mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-09-13 20:13:19 +00:00
WS
This commit is contained in:
@@ -267,7 +267,7 @@ class DistributedConnection extends NetworkConnection with IStore {
|
||||
throw AsyncException(ErrorType.Exception, 0, "Session not initialized");
|
||||
|
||||
if (socket == null) {
|
||||
if (useWebsocket) {
|
||||
if (useWebsocket || kIsWeb) {
|
||||
socket = new WSocket()..secure = secureWebSocket;
|
||||
} else
|
||||
socket = new TCPSocket();
|
||||
@@ -2381,7 +2381,7 @@ class DistributedConnection extends NetworkConnection with IStore {
|
||||
// @TODO: Generator code
|
||||
DistributedResource dr;
|
||||
|
||||
if (resource == null) {
|
||||
if (resource == null) {
|
||||
var template =
|
||||
Warehouse.getTemplateByClassId(rt[0], TemplateType.Wrapper);
|
||||
if (template?.definedType != null) {
|
||||
|
@@ -22,10 +22,8 @@ SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
//import 'package:web_socket_channel/io.dart' as WS;
|
||||
import 'package:web_socket_channel/io.dart';
|
||||
//import 'package:web_socket_channel/html.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
import '../../Core/ErrorType.dart';
|
||||
import '../../Core/ExceptionCode.dart';
|
||||
@@ -40,8 +38,7 @@ import 'IPEndPoint.dart';
|
||||
import '../../Core/AsyncReply.dart';
|
||||
|
||||
class WSocket extends ISocket {
|
||||
WebSocket? _sock;
|
||||
IOWebSocketChannel? _channel;
|
||||
WebSocketChannel? _channel;
|
||||
|
||||
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
||||
|
||||
@@ -58,16 +55,19 @@ class WSocket extends ISocket {
|
||||
|
||||
began = true;
|
||||
|
||||
if (_sock != null) {
|
||||
var s = _sock as Socket;
|
||||
_localEP = IPEndPoint(s.address.rawAddress, s.port);
|
||||
_remoteEP = IPEndPoint(s.remoteAddress.rawAddress, s.remotePort);
|
||||
if (_channel != null) {
|
||||
_localEP = IPEndPoint([0, 0, 0, 0], 0);
|
||||
_remoteEP = IPEndPoint([0, 0, 0, 0], 0);
|
||||
_channel?.stream
|
||||
.listen(_dataHandler, onError: errorHandler, onDone: doneHandler);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void dataHandler(List<int> data) {
|
||||
void _dataHandler(data) {
|
||||
try {
|
||||
//List<int> data
|
||||
if (_state == SocketState.Closed || _state == SocketState.Terminated)
|
||||
return;
|
||||
|
||||
@@ -85,6 +85,7 @@ class WSocket extends ISocket {
|
||||
|
||||
void errorHandler(error, StackTrace trace) {
|
||||
print(error);
|
||||
close();
|
||||
}
|
||||
|
||||
void doneHandler() {
|
||||
@@ -98,19 +99,15 @@ class WSocket extends ISocket {
|
||||
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()));
|
||||
});
|
||||
_channel = WebSocketChannel.connect(
|
||||
Uri.parse("${secure ? 'wss' : 'ws'}://${hostname}:${port}"),
|
||||
); //binaryType: BinaryType.list);
|
||||
|
||||
_state = SocketState.Established;
|
||||
|
||||
begin();
|
||||
receiver?.networkConnect(this);
|
||||
rt.trigger(true);
|
||||
} catch (ex) {
|
||||
rt.triggerError(AsyncException(ErrorType.Management,
|
||||
ExceptionCode.HostNotReachable.index, ex.toString()));
|
||||
@@ -132,22 +129,19 @@ class WSocket extends ISocket {
|
||||
if (state != SocketState.Closed && state != SocketState.Terminated)
|
||||
_state = SocketState.Closed;
|
||||
|
||||
_sock?.close();
|
||||
|
||||
_channel?.sink.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());
|
||||
.add(message.clip(offset, message.length - offset).toArray());
|
||||
} else if (offset != null && size != null) {
|
||||
_channel?.sink.add(message.clip(offset, size).toList());
|
||||
_channel?.sink.add(message.clip(offset, size).toArray());
|
||||
} else {
|
||||
_channel?.sink.add(message.toList());
|
||||
_channel?.sink.add(message.toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
165
lib/src/Net/Sockets/WSocketIO.dart
Normal file
165
lib/src/Net/Sockets/WSocketIO.dart
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user