mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-09-13 20:13:19 +00:00
1.3
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -41,11 +41,12 @@ class DistributedResource extends IResource {
|
||||
//bool _isReady = false;
|
||||
|
||||
String _link;
|
||||
int _age;
|
||||
|
||||
List _properties;
|
||||
bool _destroyed = false;
|
||||
|
||||
List<KeyValuePair<int, dynamic>> _queued_updates =
|
||||
List<KeyValuePair<int, dynamic>>();
|
||||
List<KeyValuePair<int, dynamic>> _queued_updates = [];
|
||||
|
||||
/// <summary>
|
||||
/// Connection responsible for the distributed resource.
|
||||
@@ -111,6 +112,15 @@ class DistributedResource extends IResource {
|
||||
this._link = link;
|
||||
this._connection = connection;
|
||||
this._instanceId = instanceId;
|
||||
this._age = age;
|
||||
}
|
||||
|
||||
void init(
|
||||
DistributedConnection connection, int instanceId, int age, String link) {
|
||||
this._link = link;
|
||||
this._connection = connection;
|
||||
this._instanceId = instanceId;
|
||||
this._age = age;
|
||||
}
|
||||
|
||||
//void _ready()
|
||||
@@ -165,6 +175,38 @@ class DistributedResource extends IResource {
|
||||
return true;
|
||||
}
|
||||
|
||||
AsyncReply<dynamic> listen(event) {
|
||||
EventTemplate et = event is EventTemplate
|
||||
? event
|
||||
: instance.template.getEventTemplateByName(event);
|
||||
|
||||
if (et == null)
|
||||
return AsyncReply<dynamic>().triggerError(new AsyncException(
|
||||
ErrorType.Management, ExceptionCode.MethodNotFound.index, ""));
|
||||
|
||||
if (!et.listenable)
|
||||
return AsyncReply().triggerError(new AsyncException(
|
||||
ErrorType.Management, ExceptionCode.NotListenable.index, ""));
|
||||
|
||||
return _connection.sendListenRequest(_instanceId, et.index);
|
||||
}
|
||||
|
||||
AsyncReply<dynamic> unlisten(event) {
|
||||
EventTemplate et = event is EventTemplate
|
||||
? event
|
||||
: instance.template.getEventTemplateByName(event);
|
||||
|
||||
if (et == null)
|
||||
return AsyncReply().triggerError(new AsyncException(
|
||||
ErrorType.Management, ExceptionCode.MethodNotFound.index, ""));
|
||||
|
||||
if (!et.listenable)
|
||||
return AsyncReply().triggerError(new AsyncException(
|
||||
ErrorType.Management, ExceptionCode.NotListenable.index, ""));
|
||||
|
||||
return connection.sendUnlistenRequest(_instanceId, et.index);
|
||||
}
|
||||
|
||||
void emitEventByIndex(int index, dynamic args) {
|
||||
// neglect events when the object is not yet attached
|
||||
if (!_attached) return;
|
||||
|
35
lib/src/Net/IIP/DistributedServer.dart
Normal file
35
lib/src/Net/IIP/DistributedServer.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:esiur/src/Resource/Template/TemplateDescriber.dart';
|
||||
|
||||
import '../../Resource/IResource.dart';
|
||||
import '../../Core/AsyncReply.dart';
|
||||
import '../../Resource/ResourceTrigger.dart';
|
||||
|
||||
import './EntryPoint.dart';
|
||||
|
||||
class DistributedServer extends IResource {
|
||||
@override
|
||||
void destroy() {
|
||||
this.emitArgs("destroy", []);
|
||||
}
|
||||
|
||||
@override
|
||||
AsyncReply<bool> trigger(ResourceTrigger trigger) {
|
||||
return AsyncReply.ready(true);
|
||||
}
|
||||
|
||||
EntryPoint entryPoint;
|
||||
|
||||
@override
|
||||
getProperty(String name) => null;
|
||||
|
||||
@override
|
||||
invoke(String name, List arguments) => null;
|
||||
|
||||
@override
|
||||
setProperty(String name, value) => true;
|
||||
|
||||
@override
|
||||
TemplateDescriber get template =>
|
||||
TemplateDescriber("Esiur.Net.IIP.DistributedServer");
|
||||
|
||||
}
|
12
lib/src/Net/IIP/EntryPoint.dart
Normal file
12
lib/src/Net/IIP/EntryPoint.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
import '../../Resource/IResource.dart';
|
||||
import './DistributedConnection.dart';
|
||||
import '../../Core/AsyncReply.dart';
|
||||
|
||||
abstract class EntryPoint extends IResource
|
||||
{
|
||||
|
||||
AsyncReply<List<IResource>> query(String path, DistributedConnection sender);
|
||||
bool create();
|
||||
}
|
9
lib/src/Net/INetworkReceiver.dart
Normal file
9
lib/src/Net/INetworkReceiver.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
import 'NetworkBuffer.dart';
|
||||
|
||||
abstract class INetworkReceiver<T>
|
||||
{
|
||||
void networkClose(T sender);
|
||||
void networkReceive(T sender, NetworkBuffer buffer);
|
||||
//void NetworkError(T sender);
|
||||
void networkConnect(T sender);
|
||||
}
|
@@ -22,6 +22,8 @@ SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
import 'INetworkReceiver.dart';
|
||||
|
||||
import '../Core/IDestructible.dart';
|
||||
import 'Sockets/ISocket.dart';
|
||||
import 'Sockets/SocketState.dart';
|
||||
@@ -29,178 +31,145 @@ import 'NetworkBuffer.dart';
|
||||
import '../Data/DC.dart';
|
||||
import 'Sockets/IPEndPoint.dart';
|
||||
|
||||
class NetworkConnection extends IDestructible
|
||||
{
|
||||
ISocket _sock;
|
||||
class NetworkConnection extends IDestructible with INetworkReceiver<ISocket> {
|
||||
ISocket _sock;
|
||||
|
||||
DateTime _lastAction;
|
||||
DateTime _lastAction;
|
||||
|
||||
//public delegate void DataReceivedEvent(NetworkConnection sender, NetworkBuffer data);
|
||||
//public delegate void ConnectionClosedEvent(NetworkConnection sender);
|
||||
//public delegate void ConnectionEstablishedEvent(NetworkConnection sender);
|
||||
//public delegate void DataReceivedEvent(NetworkConnection sender, NetworkBuffer data);
|
||||
//public delegate void ConnectionClosedEvent(NetworkConnection sender);
|
||||
//public delegate void ConnectionEstablishedEvent(NetworkConnection sender);
|
||||
|
||||
//public event ConnectionEstablishedEvent OnConnect;
|
||||
//public event DataReceivedEvent OnDataReceived;
|
||||
//public event ConnectionClosedEvent OnClose;
|
||||
//public event DestroyedEvent OnDestroy;
|
||||
//object receivingLock = new object();
|
||||
//public event ConnectionEstablishedEvent OnConnect;
|
||||
//public event DataReceivedEvent OnDataReceived;
|
||||
//public event ConnectionClosedEvent OnClose;
|
||||
//public event DestroyedEvent OnDestroy;
|
||||
//object receivingLock = new object();
|
||||
|
||||
bool _processing = false;
|
||||
bool _processing = false;
|
||||
|
||||
|
||||
// to be overridden
|
||||
void connectionClosed()
|
||||
{
|
||||
void destroy() {
|
||||
// if (connected)
|
||||
close();
|
||||
//emitArgs("close", [this]);
|
||||
//OnDestroy?.Invoke(this);
|
||||
}
|
||||
|
||||
NetworkConnection() {}
|
||||
|
||||
ISocket get socket => _sock;
|
||||
|
||||
void assign(ISocket socket) {
|
||||
_lastAction = DateTime.now();
|
||||
_sock = socket;
|
||||
|
||||
socket.receiver = this;
|
||||
|
||||
//socket.on("receive", socket_OnReceive);
|
||||
//socket.on("close", socket_OnClose);
|
||||
//socket.on("connect", socket_OnConnect);
|
||||
}
|
||||
|
||||
ISocket unassign() {
|
||||
if (_sock != null) {
|
||||
// connected = false;
|
||||
// _sock.off("close", socket_OnClose);
|
||||
// _sock.off("connect", socket_OnConnect);
|
||||
// _sock.off("receive", socket_OnReceive);
|
||||
|
||||
_sock.receiver = null;
|
||||
var rt = _sock;
|
||||
_sock = null;
|
||||
|
||||
return rt;
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
// to be overridden
|
||||
void dataReceived(NetworkBuffer data) {
|
||||
emitArgs("dataReceived", [data]);
|
||||
}
|
||||
|
||||
void connected(){
|
||||
|
||||
}
|
||||
|
||||
void disconnected(){
|
||||
|
||||
}
|
||||
|
||||
void close() {
|
||||
try {
|
||||
if (_sock != null) _sock.close();
|
||||
} catch (ex) {
|
||||
//Global.Log("NetworkConenction:Close", LogType.Error, ex.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
// if (connected)
|
||||
close();
|
||||
//emitArgs("close", [this]);
|
||||
//OnDestroy?.Invoke(this);
|
||||
DateTime get lastAction => _lastAction;
|
||||
|
||||
IPEndPoint get remoteEndPoint => _sock?.remoteEndPoint;
|
||||
|
||||
IPEndPoint get localEndPoint => _sock?.localEndPoint;
|
||||
|
||||
bool get isConnected => _sock.state == SocketState.Established;
|
||||
|
||||
void send(DC msg) {
|
||||
try {
|
||||
if (_sock != null) {
|
||||
_lastAction = DateTime.now();
|
||||
_sock.send(msg);
|
||||
}
|
||||
} catch (ex) {
|
||||
//Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
NetworkConnection()
|
||||
{
|
||||
void sendString(String data) {
|
||||
send(DC.stringToBytes(data));
|
||||
}
|
||||
|
||||
@override
|
||||
void networkClose(sender) {
|
||||
disconnected();
|
||||
emitArgs("close", [this]);
|
||||
}
|
||||
|
||||
@override
|
||||
void networkConnect(sender) {
|
||||
connected();
|
||||
emitArgs("connect", [this]);
|
||||
}
|
||||
|
||||
@override
|
||||
void networkReceive(sender, NetworkBuffer buffer) {
|
||||
try {
|
||||
// Unassigned ?
|
||||
if (_sock == null) return;
|
||||
|
||||
// Closed ?
|
||||
if (_sock.state == SocketState.Closed ||
|
||||
_sock.state == SocketState.Terminated) // || !connected)
|
||||
return;
|
||||
|
||||
_lastAction = DateTime.now();
|
||||
|
||||
if (!_processing) {
|
||||
_processing = true;
|
||||
|
||||
try {
|
||||
while (buffer.available > 0 && !buffer.protected)
|
||||
dataReceived(buffer);
|
||||
} catch (ex) {}
|
||||
|
||||
_processing = false;
|
||||
}
|
||||
} catch (ex) {
|
||||
print(ex);
|
||||
//Global.Log("NetworkConnection", LogType.Warning, ex.ToString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
ISocket get socket => _sock;
|
||||
|
||||
void assign(ISocket socket)
|
||||
{
|
||||
_lastAction = DateTime.now();
|
||||
_sock = socket;
|
||||
|
||||
socket.on("receive", socket_OnReceive);
|
||||
socket.on("close", socket_OnClose);
|
||||
socket.on("connect", socket_OnConnect);
|
||||
}
|
||||
|
||||
|
||||
void socket_OnConnect()
|
||||
{
|
||||
emitArgs("connect", [this]);
|
||||
}
|
||||
|
||||
void socket_OnClose()
|
||||
{
|
||||
connectionClosed();
|
||||
emitArgs("close", [this]);
|
||||
}
|
||||
|
||||
void socket_OnReceive(NetworkBuffer buffer)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
// Unassigned ?
|
||||
if (_sock == null)
|
||||
return;
|
||||
|
||||
// Closed ?
|
||||
if (_sock.state == SocketState.Closed || _sock.state == SocketState.Terminated) // || !connected)
|
||||
return;
|
||||
|
||||
_lastAction = DateTime.now();
|
||||
|
||||
if (!_processing)
|
||||
{
|
||||
_processing = true;
|
||||
|
||||
try
|
||||
{
|
||||
while (buffer.available > 0 && !buffer.protected)
|
||||
dataReceived(buffer);
|
||||
}
|
||||
catch(ex)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
_processing = false;
|
||||
}
|
||||
|
||||
}
|
||||
catch (ex)
|
||||
{
|
||||
print(ex);
|
||||
//Global.Log("NetworkConnection", LogType.Warning, ex.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ISocket unassign()
|
||||
{
|
||||
if (_sock != null)
|
||||
{
|
||||
// connected = false;
|
||||
_sock.off("close", socket_OnClose);
|
||||
_sock.off("connect", socket_OnConnect);
|
||||
_sock.off("receive", socket_OnReceive);
|
||||
|
||||
var rt = _sock;
|
||||
_sock = null;
|
||||
|
||||
return rt;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
void dataReceived(NetworkBuffer data)
|
||||
{
|
||||
emitArgs("dataReceived", [data]);
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_sock != null)
|
||||
_sock.close();
|
||||
}
|
||||
catch(ex)
|
||||
{
|
||||
//Global.Log("NetworkConenction:Close", LogType.Error, ex.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
DateTime get lastAction => _lastAction;
|
||||
|
||||
|
||||
IPEndPoint get remoteEndPoint => _sock?.remoteEndPoint;
|
||||
|
||||
IPEndPoint get localEndPoint => _sock?.localEndPoint;
|
||||
|
||||
bool get connected => _sock.state == SocketState.Established;
|
||||
|
||||
|
||||
void send(DC msg)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
if (_sock != null)
|
||||
{
|
||||
_lastAction = DateTime.now();
|
||||
_sock.send(msg);
|
||||
}
|
||||
}
|
||||
catch (ex)
|
||||
{
|
||||
//Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
void sendString(String data)
|
||||
{
|
||||
send(DC.stringToBytes(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -235,11 +235,14 @@ class IIPAuthPacket
|
||||
localMethod = AuthenticationMethod.values[ ((data[offset] >> 2) & 0x3)];
|
||||
var encrypt = ((data[offset++] & 0x2) == 0x2);
|
||||
|
||||
if (_notEnough(offset, ends, 1))
|
||||
return -_dataLengthNeeded;
|
||||
|
||||
|
||||
if (remoteMethod == AuthenticationMethod.Credentials
|
||||
if (remoteMethod == AuthenticationMethod.None)
|
||||
{
|
||||
if (localMethod == AuthenticationMethod.None)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
else if (remoteMethod == AuthenticationMethod.Credentials
|
||||
|| remoteMethod == AuthenticationMethod.Token)
|
||||
{
|
||||
if (localMethod == AuthenticationMethod.None)
|
||||
|
@@ -361,7 +361,8 @@ class IIPPacket
|
||||
resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if (action == IIPPacketAction.QueryLink)
|
||||
else if (action == IIPPacketAction.QueryLink
|
||||
|| action == IIPPacketAction.LinkTemplates)
|
||||
{
|
||||
if (_notEnough(offset, ends, 2))
|
||||
return -_dataLengthNeeded;
|
||||
@@ -420,7 +421,8 @@ class IIPPacket
|
||||
offset += cl;
|
||||
|
||||
}
|
||||
else if (action == IIPPacketAction.GetProperty)
|
||||
else if (action == IIPPacketAction.Listen
|
||||
|| action == IIPPacketAction.Unlisten)
|
||||
{
|
||||
if (_notEnough(offset, ends, 5))
|
||||
return -_dataLengthNeeded;
|
||||
@@ -429,22 +431,32 @@ class IIPPacket
|
||||
offset += 4;
|
||||
|
||||
methodIndex = data[offset++];
|
||||
|
||||
}
|
||||
else if (action == IIPPacketAction.GetPropertyIfModified)
|
||||
{
|
||||
if (_notEnough(offset, ends, 9))
|
||||
return -_dataLengthNeeded;
|
||||
// else if (action == IIPPacketAction.GetProperty)
|
||||
// {
|
||||
// if (_notEnough(offset, ends, 5))
|
||||
// return -_dataLengthNeeded;
|
||||
|
||||
resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
// resourceId = data.getUint32(offset);
|
||||
// offset += 4;
|
||||
|
||||
methodIndex = data[offset++];
|
||||
// methodIndex = data[offset++];
|
||||
|
||||
resourceAge = data.getUint64(offset);
|
||||
offset += 8;
|
||||
// }
|
||||
// else if (action == IIPPacketAction.GetPropertyIfModified)
|
||||
// {
|
||||
// if (_notEnough(offset, ends, 9))
|
||||
// return -_dataLengthNeeded;
|
||||
|
||||
}
|
||||
// resourceId = data.getUint32(offset);
|
||||
// offset += 4;
|
||||
|
||||
// methodIndex = data[offset++];
|
||||
|
||||
// resourceAge = data.getUint64(offset);
|
||||
// offset += 8;
|
||||
|
||||
// }
|
||||
else if (action == IIPPacketAction.SetProperty)
|
||||
{
|
||||
if (_notEnough(offset, ends, 6))
|
||||
@@ -567,6 +579,7 @@ class IIPPacket
|
||||
|| action == IIPPacketAction.ResourceChildren
|
||||
|| action == IIPPacketAction.ResourceParents
|
||||
|| action == IIPPacketAction.ResourceHistory
|
||||
|| action == IIPPacketAction.LinkTemplates
|
||||
// Attribute
|
||||
|| action == IIPPacketAction.GetAllAttributes
|
||||
|| action == IIPPacketAction.GetAttributes)
|
||||
@@ -584,9 +597,9 @@ class IIPPacket
|
||||
offset += cl;
|
||||
}
|
||||
else if (action == IIPPacketAction.InvokeFunctionArrayArguments
|
||||
|| action == IIPPacketAction.InvokeFunctionNamedArguments
|
||||
|| action == IIPPacketAction.GetProperty
|
||||
|| action == IIPPacketAction.GetPropertyIfModified)
|
||||
|| action == IIPPacketAction.InvokeFunctionNamedArguments)
|
||||
//|| action == IIPPacketAction.GetProperty
|
||||
//|| action == IIPPacketAction.GetPropertyIfModified)
|
||||
{
|
||||
if (_notEnough(offset, ends, 1))
|
||||
return -_dataLengthNeeded;
|
||||
@@ -617,7 +630,9 @@ class IIPPacket
|
||||
offset += size;
|
||||
}
|
||||
}
|
||||
else if (action == IIPPacketAction.SetProperty)
|
||||
else if (action == IIPPacketAction.SetProperty
|
||||
|| action == IIPPacketAction.Listen
|
||||
|| action == IIPPacketAction.Unlisten)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
@@ -1,36 +1,36 @@
|
||||
class IIPPacketAction
|
||||
{
|
||||
// Request Manage
|
||||
static const int AttachResource = 0x0;
|
||||
static const int ReattachResource = 0x1;
|
||||
static const int DetachResource = 0x2;
|
||||
static const int CreateResource = 0x3;
|
||||
static const int DeleteResource = 0x4;
|
||||
static const int AddChild = 0x5;
|
||||
static const int RemoveChild = 0x6;
|
||||
static const int RenameResource = 0x7;
|
||||
class IIPPacketAction {
|
||||
// Request Manage
|
||||
static const int AttachResource = 0x0;
|
||||
static const int ReattachResource = 0x1;
|
||||
static const int DetachResource = 0x2;
|
||||
static const int CreateResource = 0x3;
|
||||
static const int DeleteResource = 0x4;
|
||||
static const int AddChild = 0x5;
|
||||
static const int RemoveChild = 0x6;
|
||||
static const int RenameResource = 0x7;
|
||||
|
||||
// Request Inquire
|
||||
static const int TemplateFromClassName = 0x8;
|
||||
static const int TemplateFromClassId = 0x9;
|
||||
static const int TemplateFromResourceId = 0xA;
|
||||
static const int QueryLink = 0xB;
|
||||
static const int ResourceHistory = 0xC;
|
||||
static const int ResourceChildren = 0xD;
|
||||
static const int ResourceParents = 0xE;
|
||||
// Request Inquire
|
||||
static const int TemplateFromClassName = 0x8;
|
||||
static const int TemplateFromClassId = 0x9;
|
||||
static const int TemplateFromResourceId = 0xA;
|
||||
static const int QueryLink = 0xB;
|
||||
static const int ResourceHistory = 0xC;
|
||||
static const int ResourceChildren = 0xD;
|
||||
static const int ResourceParents = 0xE;
|
||||
static const int LinkTemplates = 0xF;
|
||||
|
||||
// Request Invoke
|
||||
static const int InvokeFunctionArrayArguments = 0x10;
|
||||
static const int GetProperty = 0x11;
|
||||
static const int GetPropertyIfModified = 0x12;
|
||||
static const int SetProperty = 0x13;
|
||||
static const int InvokeFunctionNamedArguments = 0x14;
|
||||
// Request Invoke
|
||||
static const int InvokeFunctionArrayArguments = 0x10;
|
||||
static const int InvokeFunctionNamedArguments = 0x11;
|
||||
static const int Listen = 0x12;
|
||||
static const int Unlisten = 0x13;
|
||||
static const int SetProperty = 0x14;
|
||||
|
||||
// Request Attribute
|
||||
static const int GetAllAttributes = 0x18;
|
||||
static const int UpdateAllAttributes = 0x19;
|
||||
static const int ClearAllAttributes = 0x1A;
|
||||
static const int GetAttributes = 0x1B;
|
||||
static const int UpdateAttributes = 0x1C;
|
||||
static const int ClearAttributes = 0x1D;
|
||||
}
|
||||
// Request Attribute
|
||||
static const int GetAllAttributes = 0x18;
|
||||
static const int UpdateAllAttributes = 0x19;
|
||||
static const int ClearAllAttributes = 0x1A;
|
||||
static const int GetAttributes = 0x1B;
|
||||
static const int UpdateAttributes = 0x1C;
|
||||
static const int ClearAttributes = 0x1D;
|
||||
}
|
||||
|
@@ -23,25 +23,28 @@ SOFTWARE.
|
||||
*/
|
||||
import '../../Core/IDestructible.dart';
|
||||
import '../../Data/DC.dart';
|
||||
import '../INetworkReceiver.dart';
|
||||
import 'IPEndPoint.dart';
|
||||
import '../../Core/AsyncReply.dart';
|
||||
import 'SocketState.dart';
|
||||
|
||||
abstract class ISocket extends IDestructible
|
||||
{
|
||||
SocketState get state ; //{ get; }
|
||||
abstract class ISocket extends IDestructible {
|
||||
SocketState get state; //{ get; }
|
||||
|
||||
//event ISocketReceiveEvent OnReceive;
|
||||
//event ISocketConnectEvent OnConnect;
|
||||
//event ISocketCloseEvent OnClose;
|
||||
//event ISocketReceiveEvent OnReceive;
|
||||
//event ISocketConnectEvent OnConnect;
|
||||
//event ISocketCloseEvent OnClose;
|
||||
|
||||
//void send(DC message);
|
||||
void send(DC message, [int offset, int size]);
|
||||
void close();
|
||||
AsyncReply<bool> connect(String hostname, int port);
|
||||
bool begin();
|
||||
//void send(DC message);
|
||||
|
||||
AsyncReply<ISocket> accept();
|
||||
IPEndPoint remoteEndPoint;
|
||||
IPEndPoint localEndPoint;
|
||||
INetworkReceiver<ISocket> receiver;
|
||||
|
||||
void send(DC message, [int offset, int size]);
|
||||
void close();
|
||||
AsyncReply<bool> connect(String hostname, int port);
|
||||
bool begin();
|
||||
|
||||
AsyncReply<ISocket> accept();
|
||||
IPEndPoint remoteEndPoint;
|
||||
IPEndPoint localEndPoint;
|
||||
}
|
||||
|
@@ -32,24 +32,23 @@ import 'SocketState.dart';
|
||||
import 'IPEndPoint.dart';
|
||||
import '../../Core/AsyncReply.dart';
|
||||
|
||||
class TCPSocket extends ISocket
|
||||
{
|
||||
Socket sock;
|
||||
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
||||
class TCPSocket extends ISocket {
|
||||
Socket sock;
|
||||
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
||||
|
||||
//bool asyncSending;
|
||||
bool began = false;
|
||||
//bool asyncSending;
|
||||
bool began = false;
|
||||
|
||||
SocketState _state = SocketState.Initial;
|
||||
SocketState _state = SocketState.Initial;
|
||||
|
||||
//public event ISocketReceiveEvent OnReceive;
|
||||
//public event ISocketConnectEvent OnConnect;
|
||||
//public event ISocketCloseEvent OnClose;
|
||||
//public event DestroyedEvent OnDestroy;
|
||||
//public event ISocketReceiveEvent OnReceive;
|
||||
//public event ISocketConnectEvent OnConnect;
|
||||
//public event ISocketCloseEvent OnClose;
|
||||
//public event DestroyedEvent OnDestroy;
|
||||
|
||||
//SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
||||
//SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
|
||||
|
||||
/*
|
||||
/*
|
||||
void connected(Task t)
|
||||
{
|
||||
state = SocketState.Established;
|
||||
@@ -58,143 +57,117 @@ class TCPSocket extends ISocket
|
||||
}
|
||||
*/
|
||||
|
||||
IPEndPoint _localEP, _remoteEP;
|
||||
IPEndPoint _localEP, _remoteEP;
|
||||
|
||||
bool begin()
|
||||
{
|
||||
if (began)
|
||||
return false;
|
||||
bool begin() {
|
||||
if (began) return false;
|
||||
|
||||
began = true;
|
||||
began = true;
|
||||
|
||||
_localEP = IPEndPoint(sock.address.rawAddress, sock.port);
|
||||
_remoteEP = IPEndPoint(sock.remoteAddress.rawAddress, sock.remotePort);
|
||||
return true;
|
||||
}
|
||||
_localEP = IPEndPoint(sock.address.rawAddress, sock.port);
|
||||
_remoteEP = IPEndPoint(sock.remoteAddress.rawAddress, sock.remotePort);
|
||||
return true;
|
||||
}
|
||||
|
||||
void dataHandler(List<int> data){
|
||||
//print(new String.fromCharCodes(data).trim());
|
||||
void dataHandler(List<int> data) {
|
||||
//print(new String.fromCharCodes(data).trim());
|
||||
|
||||
try {
|
||||
if (_state == SocketState.Closed || _state == SocketState.Terminated)
|
||||
return;
|
||||
|
||||
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);
|
||||
|
||||
//emitArgs("receive", [receiveNetworkBuffer]);
|
||||
|
||||
var dc = new DC.fromList(data);
|
||||
receiveNetworkBuffer.write(dc, 0, dc.length);
|
||||
emitArgs("receive", [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;
|
||||
|
||||
Socket.connect(hostname, port).then((s){
|
||||
sock = s;
|
||||
s.listen(dataHandler,
|
||||
onError: errorHandler,
|
||||
onDone: doneHandler,
|
||||
cancelOnError: false);
|
||||
_state = SocketState.Established;
|
||||
emitArgs("connect", []);
|
||||
begin();
|
||||
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.fromSocket(Socket socket)
|
||||
{
|
||||
sock = socket;
|
||||
//if (socket.)
|
||||
// _state = SocketState.Established;
|
||||
}
|
||||
|
||||
TCPSocket()
|
||||
{
|
||||
// default constructor
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
if (state != SocketState.Closed && state != SocketState.Terminated)
|
||||
_state = SocketState.Closed;
|
||||
|
||||
sock?.close();
|
||||
|
||||
emitArgs("close", []);
|
||||
}
|
||||
|
||||
|
||||
void send(DC message, [int offset, int size])
|
||||
{
|
||||
if (state == SocketState.Established)
|
||||
sock.add(message.toList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void destroy()
|
||||
{
|
||||
} catch (ex) {
|
||||
if (_state != SocketState.Closed) // && !sock.connected)
|
||||
{
|
||||
_state = SocketState.Terminated;
|
||||
close();
|
||||
emitArgs("destroy", [this]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Socket.connect(hostname, port).then((s) {
|
||||
sock = s;
|
||||
s.listen(dataHandler,
|
||||
onError: errorHandler, onDone: doneHandler, cancelOnError: false);
|
||||
_state = SocketState.Established;
|
||||
|
||||
//emitArgs("connect", []);
|
||||
receiver?.networkConnect(this);
|
||||
|
||||
begin();
|
||||
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()));
|
||||
}
|
||||
|
||||
AsyncReply<ISocket> accept()
|
||||
{
|
||||
return rt;
|
||||
}
|
||||
|
||||
IPEndPoint get localEndPoint => _localEP;
|
||||
IPEndPoint get remoteEndPoint => _remoteEP;
|
||||
|
||||
var reply = new AsyncReply<ISocket>();
|
||||
return reply;
|
||||
SocketState get state => _state;
|
||||
|
||||
TCPSocket.fromSocket(Socket socket) {
|
||||
sock = socket;
|
||||
//if (socket.)
|
||||
// _state = SocketState.Established;
|
||||
}
|
||||
|
||||
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) sock.add(message.toList());
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
close();
|
||||
emitArgs("destroy", [this]);
|
||||
}
|
||||
|
||||
AsyncReply<ISocket> accept() {
|
||||
var reply = new AsyncReply<ISocket>();
|
||||
return reply;
|
||||
|
||||
/*
|
||||
ServerSocket.bind(InternetAddress.ANY_IP_V4, 4567).then(
|
||||
@@ -212,5 +185,5 @@ class TCPSocket extends ISocket
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user