mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-06-27 06:43:13 +00:00
1.3
This commit is contained in:
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user