2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00
This commit is contained in:
2020-10-05 00:46:51 +03:00
parent 8c2d616d62
commit ba084b79e6
29 changed files with 1135 additions and 843 deletions

View File

@ -43,13 +43,11 @@ namespace Esyur.Net.HTTP
public class HTTPConnection : NetworkConnection
{
public void SetParent(HTTPServer Parent)
{
Server = Parent;
}
public bool WSMode { get; internal set; }
private HTTPServer Server;
public HTTPServer Server { get; internal set; }
public WebsocketPacket WSRequest { get; set; }
public HTTPRequestPacket Request { get; set; }
public HTTPResponsePacket Response { get; } = new HTTPResponsePacket();
@ -100,7 +98,7 @@ namespace Esyur.Net.HTTP
public void Flush()
{
// close the connection
if (Request.Headers["connection"].ToLower() != "keep-alive" & Connected)
if (Request.Headers["connection"].ToLower() != "keep-alive" & IsConnected)
Close();
}
@ -232,6 +230,102 @@ namespace Esyur.Net.HTTP
}
}
protected override void DataReceived(NetworkBuffer data)
{
byte[] msg = data.Read();
var BL = Parse(msg);
if (BL == 0)
{
if (Request.Method == HTTPRequestPacket.HTTPMethod.UNKNOWN)
{
Close();
return;
}
if (Request.URL == "")
{
Close();
return;
}
}
else if (BL == -1)
{
data.HoldForNextWrite(msg);
return;
}
else if (BL < 0)
{
data.HoldFor(msg, (uint)(msg.Length - BL));
return;
}
else if (BL > 0)
{
if (BL > Server.MaxPost)
{
Send(
"<html><body>POST method content is larger than "
+ Server.MaxPost
+ " bytes.</body></html>");
Close();
}
else
{
data.HoldFor(msg, (uint)(msg.Length + BL));
}
return;
}
else if (BL < 0) // for security
{
Close();
return;
}
if (IsWebsocketRequest() & !WSMode)
{
Upgrade();
//return;
}
//return;
try
{
if (!Server.Execute(this))
{
Response.Number = HTTPResponsePacket.ResponseCode.InternalServerError;
Send("Bad Request");
Close();
}
}
catch (Exception ex)
{
if (ex.Message != "Thread was being aborted.")
{
Global.Log("HTTPServer", LogType.Error, ex.ToString());
//Console.WriteLine(ex.ToString());
//EventLog.WriteEntry("HttpServer", ex.ToString(), EventLogEntryType.Error);
Send(Error500(ex.Message));
}
}
}
private string Error500(string msg)
{
return "<html><head><title>500 Internal Server Error</title></head><br>\r\n"
+ "<body><br>\r\n"
+ "<b>500</b> Internal Server Error<br>" + msg + "\r\n"
+ "</body><br>\r\n"
+ "</html><br>\r\n";
}
public async AsyncReply<bool> SendFile(string filename)
{
@ -329,5 +423,15 @@ namespace Esyur.Net.HTTP
return false;
}
}
protected override void Connected()
{
// do nothing
}
protected override void Disconencted()
{
// do nothing
}
}
}

View File

@ -62,7 +62,7 @@ namespace Esyur.Net.HTTP
}
*/
public abstract bool Execute(HTTPConnection sender);
public abstract AsyncReply<bool> Execute(HTTPConnection sender);
public virtual void ClientConnected(HTTPConnection HTTP)
{

View File

@ -140,113 +140,23 @@ namespace Esyur.Net.HTTP
return Cookie;
}
protected override void ClientDisconnected(HTTPConnection sender)
protected override void ClientDisconnected(HTTPConnection connection)
{
//Console.WriteLine("OUT: " + this.Connections.Count);
foreach (var filter in filters)
filter.ClientDisconnected(sender);
filter.ClientDisconnected(connection);
}
protected override void DataReceived(HTTPConnection sender, NetworkBuffer data)
internal bool Execute(HTTPConnection sender)
{
byte[] msg = data.Read();
var BL = sender.Parse(msg);
if (BL == 0)
{
if (sender.Request.Method == HTTPRequestPacket.HTTPMethod.UNKNOWN)
{
sender.Close();
return;
}
if (sender.Request.URL == "")
{
sender.Close();
return;
}
}
else if (BL == -1)
{
data.HoldForNextWrite(msg);
return;
}
else if (BL < 0)
{
data.HoldFor(msg, (uint) (msg.Length - BL));
return;
}
else if (BL > 0)
{
if (BL > MaxPost)
{
sender.Send(
"<html><body>POST method content is larger than "
+ MaxPost
+ " bytes.</body></html>");
sender.Close();
}
else
{
data.HoldFor(msg, (uint)(msg.Length + BL));
}
return;
}
else if (BL < 0) // for security
{
sender.Close();
return;
}
if (sender.IsWebsocketRequest() & !sender.WSMode)
{
sender.Upgrade();
//return;
}
//return;
try
{
foreach (var resource in filters)
if (resource.Execute(sender))
return;
sender.Response.Number = HTTPResponsePacket.ResponseCode.InternalServerError;
sender.Send("Bad Request");
sender.Close();
}
catch (Exception ex)
{
if (ex.Message != "Thread was being aborted.")
{
Global.Log("HTTPServer", LogType.Error, ex.ToString());
//Console.WriteLine(ex.ToString());
//EventLog.WriteEntry("HttpServer", ex.ToString(), EventLogEntryType.Error);
sender.Send(Error500(ex.Message));
}
}
foreach (var resource in filters)
if (resource.Execute(sender).Wait(30000))
return true;
return false;
}
private string Error500(string msg)
{
return "<html><head><title>500 Internal Server Error</title></head><br>\r\n"
+ "<body><br>\r\n"
+ "<b>500</b> Internal Server Error<br>" + msg + "\r\n"
+ "</body><br>\r\n"
+ "</html><br>\r\n";
}
/*
@ -283,7 +193,7 @@ namespace Esyur.Net.HTTP
return Timeout;// mTimeout;
}
}
*/
*/
public async AsyncReply<bool> Trigger(ResourceTrigger trigger)
@ -298,7 +208,7 @@ namespace Esyur.Net.HTTP
//if (ip == null) ip = IPAddress.Any;
ISocket listener;
Sockets.ISocket listener;
IPAddress ipAdd;
if (IP == null)
@ -330,30 +240,34 @@ namespace Esyur.Net.HTTP
return true;
}
protected override void ClientConnected(HTTPConnection sender)
{
//sender.SessionModified += SessionModified;
//sender.SessionEnded += SessionExpired;
sender.SetParent(this);
//Console.WriteLine("IN: " + this.Connections.Count);
public override void Add(HTTPConnection connection)
{
connection.Server = this;
base.Add(connection);
}
public override void Remove(HTTPConnection connection)
{
connection.Server = null;
base.Remove(connection);
}
protected override void ClientConnected(HTTPConnection connection)
{
if (filters.Length == 0)
{
sender.Close();
connection.Close();
return;
}
foreach (var resource in filters)
{
resource.ClientConnected(sender);
resource.ClientConnected(connection);
}
}
public void Destroy()
{
}
/*
public int LocalPort

View File

@ -13,7 +13,7 @@ namespace Esyur.Net.HTTP
[Attribute]
EntryPoint EntryPoint { get; set; }
public override bool Execute(HTTPConnection sender)
public async override AsyncReply<bool> Execute(HTTPConnection sender)
{
if (sender.Request.URL != "iip")
return false;

View File

@ -43,7 +43,7 @@ namespace Esyur.Net.HTTP
set;
}
public override bool Execute(HTTPConnection sender)
public async override AsyncReply<bool> Execute(HTTPConnection sender)
{
if (sender.IsWebsocketRequest())
@ -58,11 +58,11 @@ namespace Esyur.Net.HTTP
var httpServer = sender.Parent;
var wsSocket = new WSSocket(tcpSocket);
httpServer.RemoveConnection(sender);
httpServer.Remove(sender);
var iipConnection = new DistributedConnection();
Server.AddConnection(iipConnection);
Server.Add(iipConnection);
iipConnection.Assign(wsSocket);
wsSocket.Begin();

View File

@ -101,11 +101,7 @@ namespace Esyur.Net.IIP
/// <summary>
/// Distributed server responsible for this connection, usually for incoming connections.
/// </summary>
public DistributedServer Server
{
get;
set;
}
public DistributedServer Server { get; internal set; }
public bool Remove(IResource resource)
{
@ -193,7 +189,7 @@ namespace Esyur.Net.IIP
/// Assign a socket to the connection.
/// </summary>
/// <param name="socket">Any socket that implements ISocket.</param>
public override void Assign(ISocket socket)
public override void Assign(Sockets.ISocket socket)
{
base.Assign(socket);
@ -202,39 +198,22 @@ namespace Esyur.Net.IIP
session.LocalAuthentication.Source.Attributes[SourceAttributeType.IPv4] = socket.LocalEndPoint.Address;
session.LocalAuthentication.Source.Attributes[SourceAttributeType.Port] = socket.LocalEndPoint.Port;
if (session.LocalAuthentication.Type == AuthenticationType.Client)
if (socket.State == SocketState.Established &&
session.LocalAuthentication.Type == AuthenticationType.Client)
{
// declare (Credentials -> No Auth, No Enctypt)
var un = DC.ToBytes(session.LocalAuthentication.Username);
var dmn = DC.ToBytes(session.LocalAuthentication.Domain);// domain);
if (socket.State == SocketState.Established)
{
SendParams()
.AddUInt8(0x60)
.AddUInt8((byte)dmn.Length)
.AddUInt8Array(dmn)
.AddUInt8Array(localNonce)
.AddUInt8((byte)un.Length)
.AddUInt8Array(un)
.Done();//, dmn, localNonce, (byte)un.Length, un);
}
else
{
socket.OnConnect += () =>
{ // declare (Credentials -> No Auth, No Enctypt)
//SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un);
SendParams()
.AddUInt8(0x60)
.AddUInt8((byte)dmn.Length)
.AddUInt8Array(dmn)
.AddUInt8Array(localNonce)
.AddUInt8((byte)un.Length)
.AddUInt8Array(un)
.Done();
};
}
SendParams()
.AddUInt8(0x60)
.AddUInt8((byte)dmn.Length)
.AddUInt8Array(dmn)
.AddUInt8Array(localNonce)
.AddUInt8((byte)un.Length)
.AddUInt8Array(un)
.Done();//, dmn, localNonce, (byte)un.Length, un);
}
}
@ -246,7 +225,7 @@ namespace Esyur.Net.IIP
/// <param name="domain">Working domain.</param>
/// <param name="username">Username.</param>
/// <param name="password">Password.</param>
public DistributedConnection(ISocket socket, string domain, string username, string password)
public DistributedConnection(Sockets.ISocket socket, string domain, string username, string password)
{
this.session = new Session(new ClientAuthentication()
, new HostAuthentication());
@ -264,7 +243,7 @@ namespace Esyur.Net.IIP
Assign(socket);
}
public DistributedConnection(ISocket socket, string domain, ulong tokenIndex, string token)
public DistributedConnection(Sockets.ISocket socket, string domain, ulong tokenIndex, string token)
{
this.session = new Session(new ClientAuthentication()
, new HostAuthentication());
@ -327,6 +306,12 @@ namespace Esyur.Net.IIP
r.NextBytes(localNonce);
}
public override void Destroy()
{
this.OnReady = null;
this.OnError = null;
base.Destroy();
}
private uint processPacket(byte[] msg, uint offset, uint ends, NetworkBuffer data, int chunkId)
@ -620,55 +605,78 @@ namespace Esyur.Net.IIP
if (authPacket.RemoteMethod == AuthenticationMethod.Credentials && authPacket.LocalMethod == AuthenticationMethod.None)
{
Server.Membership.UserExists(authPacket.RemoteUsername, authPacket.Domain).Then(x =>
try
{
if (x)
Server.Membership.UserExists(authPacket.RemoteUsername, authPacket.Domain).Then(x =>
{
session.RemoteAuthentication.Username = authPacket.RemoteUsername;
remoteNonce = authPacket.RemoteNonce;
session.RemoteAuthentication.Domain = authPacket.Domain;
SendParams()
.AddUInt8(0xa0)
.AddUInt8Array(localNonce)
.Done();
//SendParams((byte)0xa0, localNonce);
}
else
{
//Console.WriteLine("User not found");
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
.AddUInt16(14)
.AddString("User not found").Done();
}
});
if (x)
{
session.RemoteAuthentication.Username = authPacket.RemoteUsername;
remoteNonce = authPacket.RemoteNonce;
session.RemoteAuthentication.Domain = authPacket.Domain;
SendParams()
.AddUInt8(0xa0)
.AddUInt8Array(localNonce)
.Done();
//SendParams((byte)0xa0, localNonce);
}
else
{
//Console.WriteLine("User not found");
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
.AddUInt16(14)
.AddString("User not found").Done();
}
});
}
catch (Exception ex)
{
var errMsg = DC.ToBytes(ex.Message);
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.GeneralFailure)
.AddUInt16((ushort)errMsg.Length)
.AddUInt8Array(errMsg).Done();
}
}
else if (authPacket.RemoteMethod == AuthenticationMethod.Token && authPacket.LocalMethod == AuthenticationMethod.None)
{
// Check if user and token exists
Server.Membership.TokenExists(authPacket.RemoteTokenIndex, authPacket.Domain).Then(x =>
try
{
if (x != null)
// Check if user and token exists
Server.Membership.TokenExists(authPacket.RemoteTokenIndex, authPacket.Domain).Then(x =>
{
session.RemoteAuthentication.Username = x;
session.RemoteAuthentication.TokenIndex = authPacket.RemoteTokenIndex;
remoteNonce = authPacket.RemoteNonce;
session.RemoteAuthentication.Domain = authPacket.Domain;
SendParams()
.AddUInt8(0xa0)
.AddUInt8Array(localNonce)
.Done();
}
else
{
//Console.WriteLine("User not found");
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
.AddUInt16(15)
.AddString("Token not found").Done();
}
});
if (x != null)
{
session.RemoteAuthentication.Username = x;
session.RemoteAuthentication.TokenIndex = authPacket.RemoteTokenIndex;
remoteNonce = authPacket.RemoteNonce;
session.RemoteAuthentication.Domain = authPacket.Domain;
SendParams()
.AddUInt8(0xa0)
.AddUInt8Array(localNonce)
.Done();
}
else
{
//Console.WriteLine("User not found");
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.UserOrTokenNotFound)
.AddUInt16(15)
.AddString("Token not found").Done();
}
});
}
catch (Exception ex)
{
var errMsg = DC.ToBytes(ex.Message);
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.GeneralFailure)
.AddUInt16((ushort)errMsg.Length)
.AddUInt8Array(errMsg).Done();
}
}
}
else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Action)
@ -678,54 +686,66 @@ namespace Esyur.Net.IIP
var remoteHash = authPacket.Hash;
AsyncReply<byte[]> reply = null;
if (session.RemoteAuthentication.Method == AuthenticationMethod.Credentials)
try
{
reply = Server.Membership.GetPassword(session.RemoteAuthentication.Username,
session.RemoteAuthentication.Domain);
}
else if (session.RemoteAuthentication.Method == AuthenticationMethod.Token)
{
reply = Server.Membership.GetToken(session.RemoteAuthentication.TokenIndex,
session.RemoteAuthentication.Domain);
}
else
{
// Error
}
reply.Then((pw) =>
{
if (pw != null)
if (session.RemoteAuthentication.Method == AuthenticationMethod.Credentials)
{
var hashFunc = SHA256.Create();
//var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce));
var hash = hashFunc.ComputeHash((new BinaryList())
.AddUInt8Array(pw)
.AddUInt8Array(remoteNonce)
.AddUInt8Array(localNonce)
.ToArray());
if (hash.SequenceEqual(remoteHash))
{
// send our hash
//var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw));
//SendParams((byte)0, localHash);
var localHash = hashFunc.ComputeHash((new BinaryList()).AddUInt8Array(localNonce).AddUInt8Array(remoteNonce).AddUInt8Array(pw).ToArray());
SendParams().AddUInt8(0).AddUInt8Array(localHash).Done();
readyToEstablish = true;
}
else
{
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED");
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.AccessDenied)
.AddUInt16(13)
.AddString("Access Denied")
.Done();
}
reply = Server.Membership.GetPassword(session.RemoteAuthentication.Username,
session.RemoteAuthentication.Domain);
}
});
else if (session.RemoteAuthentication.Method == AuthenticationMethod.Token)
{
reply = Server.Membership.GetToken(session.RemoteAuthentication.TokenIndex,
session.RemoteAuthentication.Domain);
}
else
{
// Error
}
reply.Then((pw) =>
{
if (pw != null)
{
var hashFunc = SHA256.Create();
//var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce));
var hash = hashFunc.ComputeHash((new BinaryList())
.AddUInt8Array(pw)
.AddUInt8Array(remoteNonce)
.AddUInt8Array(localNonce)
.ToArray());
if (hash.SequenceEqual(remoteHash))
{
// send our hash
//var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw));
//SendParams((byte)0, localHash);
var localHash = hashFunc.ComputeHash((new BinaryList()).AddUInt8Array(localNonce).AddUInt8Array(remoteNonce).AddUInt8Array(pw).ToArray());
SendParams().AddUInt8(0).AddUInt8Array(localHash).Done();
readyToEstablish = true;
}
else
{
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED");
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.AccessDenied)
.AddUInt16(13)
.AddString("Access Denied")
.Done();
}
}
});
}
catch (Exception ex)
{
var errMsg = DC.ToBytes(ex.Message);
SendParams().AddUInt8(0xc0)
.AddUInt8((byte)ExceptionCode.GeneralFailure)
.AddUInt16((ushort)errMsg.Length)
.AddUInt8Array(errMsg).Done();
}
}
else if (authPacket.Action == IIPAuthPacket.IIPAuthPacketAction.NewConnection)
{
@ -741,9 +761,12 @@ namespace Esyur.Net.IIP
.Done();
ready = true;
Warehouse.Put(this, this.LocalUsername, null, Server);
openReply?.Trigger(true);
OnReady?.Invoke(this);
Server.Membership.Login(session);
Server?.Membership.Login(session);
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH");
@ -813,6 +836,9 @@ namespace Esyur.Net.IIP
session.Id = authPacket.SessionId;
ready = true;
// put it in the warehouse
Warehouse.Put(this, this.LocalUsername, null, Server);
openReply?.Trigger(true);
OnReady?.Invoke(this);
@ -925,7 +951,7 @@ namespace Esyur.Net.IIP
}
protected override void ConnectionClosed()
protected void NetworkClose()
{
// clean up
ready = false;
@ -948,7 +974,7 @@ namespace Esyur.Net.IIP
x.Suspend();
}
public AsyncReply<bool> Connect(AuthenticationMethod method = AuthenticationMethod.Certificate, ISocket socket = null, string hostname = null, ushort port = 0, string username = null, ulong tokenIndex = 0, byte[] passwordOrToken = null, string domain = null)
public AsyncReply<bool> Connect(AuthenticationMethod method = AuthenticationMethod.Certificate, Sockets.ISocket socket = null, string hostname = null, ushort port = 0, string username = null, ulong tokenIndex = 0, byte[] passwordOrToken = null, string domain = null)
{
if (openReply != null)
throw new AsyncException(ErrorType.Exception, 0, "Connection in progress");
@ -1093,6 +1119,37 @@ namespace Esyur.Net.IIP
return null;
}
protected override void Connected()
{
if (session.LocalAuthentication.Type == AuthenticationType.Client)
{
// declare (Credentials -> No Auth, No Enctypt)
var un = DC.ToBytes(session.LocalAuthentication.Username);
var dmn = DC.ToBytes(session.LocalAuthentication.Domain);// domain);
SendParams()
.AddUInt8(0x60)
.AddUInt8((byte)dmn.Length)
.AddUInt8Array(dmn)
.AddUInt8Array(localNonce)
.AddUInt8((byte)un.Length)
.AddUInt8Array(un)
.Done();
}
}
protected override void Disconencted()
{
if (ready)
{
Server?.Membership.Logout(session);
Warehouse.Remove(this);
ready = false;
}
}
/*
public AsyncBag<T> Children<T>(IResource resource)
{

View File

@ -103,44 +103,67 @@ namespace Esyur.Net.IIP
protected override void DataReceived(DistributedConnection sender, NetworkBuffer data)
{
//throw new NotImplementedException();
//protected override void DataReceived(DistributedConnection sender, NetworkBuffer data)
//{
// //throw new NotImplementedException();
//}
//protected override void ClientConnected(DistributedConnection sender)
//{
// //Console.WriteLine("DistributedConnection Client Connected");
//}
//private void ConnectionReadyEventReceiver(DistributedConnection sender)
//{
// sender.OnReady -= ConnectionReadyEventReceiver;
// Warehouse.Put(sender, sender.LocalUsername, null, this);
//}
//public override void RemoveConnection(DistributedConnection connection)
//{
// connection.OnReady -= Sender_OnReady;
// //connection.Server = null;
// base.RemoveConnection(connection);
//}
//public override void AddConnection(DistributedConnection connection)
//{
// connection.OnReady += Sender_OnReady;
// connection.Server = this;
// base.AddConnection(connection);
//}
protected override void ClientConnected(DistributedConnection connection)
{
//connection.OnReady += ConnectionReadyEventReceiver;
}
private void SessionModified(DistributedConnection session, string key, object newValue)
public override void Add(DistributedConnection connection)
{
}
protected override void ClientConnected(DistributedConnection sender)
{
//Console.WriteLine("DistributedConnection Client Connected");
}
private void Sender_OnReady(DistributedConnection sender)
{
Warehouse.Put(sender, sender.LocalUsername, null, this);
}
public override void RemoveConnection(DistributedConnection connection)
{
connection.OnReady -= Sender_OnReady;
//connection.Server = null;
base.RemoveConnection(connection);
}
public override void AddConnection(DistributedConnection connection)
{
connection.OnReady += Sender_OnReady;
connection.Server = this;
base.AddConnection(connection);
base.Add(connection);
}
protected override void ClientDisconnected(DistributedConnection sender)
public override void Remove(DistributedConnection connection)
{
Warehouse.Remove(sender);
connection.Server = null;
base.Remove(connection);
}
protected override void ClientDisconnected(DistributedConnection connection)
{
//connection.OnReady -= ConnectionReadyEventReceiver;
//Warehouse.Remove(connection);
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esyur.Net
{
public interface INetworkReceiver<T>
{
void NetworkClose(T sender);
void NetworkReceive(T sender, NetworkBuffer buffer);
//void NetworkError(T sender);
void NetworkConnect(T sender);
}
}

View File

@ -38,20 +38,22 @@ using Esyur.Resource;
namespace Esyur.Net
{
public class NetworkConnection : IDestructible// <TS>: IResource where TS : NetworkSession
public abstract class NetworkConnection: IDestructible, INetworkReceiver<ISocket>// <TS>: IResource where TS : NetworkSession
{
private ISocket sock;
private Sockets.ISocket sock;
// private bool connected;
private 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 NetworkConnectionEvent(NetworkConnection connection);
public event ConnectionEstablishedEvent OnConnect;
public event DataReceivedEvent OnDataReceived;
public event ConnectionClosedEvent OnClose;
public event NetworkConnectionEvent OnConnect;
//public event DataReceivedEvent OnDataReceived;
public event NetworkConnectionEvent OnClose;
public event DestroyedEvent OnDestroy;
//object receivingLock = new object();
@ -59,22 +61,31 @@ namespace Esyur.Net
bool processing = false;
// public INetworkReceiver<NetworkConnection> Receiver { get; set; }
public void Destroy()
public virtual void Destroy()
{
// if (connected)
// remove references
//sock.OnClose -= Socket_OnClose;
//sock.OnConnect -= Socket_OnConnect;
//sock.OnReceive -= Socket_OnReceive;
sock.Destroy();
//Receiver = null;
Close();
sock = null;
OnClose = null;
OnConnect = null;
//OnDataReceived = null;
OnDestroy?.Invoke(this);
OnDestroy = null;
}
public NetworkConnection()
{
}
public ISocket Socket
public Sockets.ISocket Socket
{
get
{
@ -82,81 +93,46 @@ namespace Esyur.Net
}
}
public virtual void Assign(ISocket socket)
public virtual void Assign(Sockets.ISocket socket)
{
lastAction = DateTime.Now;
sock = socket;
//connected = true;
socket.OnReceive += Socket_OnReceive;
socket.OnClose += Socket_OnClose;
socket.OnConnect += Socket_OnConnect;
//if (socket.State == SocketState.Established)
// socket.Begin();
sock.Receiver = this;
//socket.OnReceive += Socket_OnReceive;
//socket.OnClose += Socket_OnClose;
//socket.OnConnect += Socket_OnConnect;
}
private void Socket_OnConnect()
{
OnConnect?.Invoke(this);
}
//private void Socket_OnConnect()
//{
// OnConnect?.Invoke(this);
//}
private void Socket_OnClose()
{
ConnectionClosed();
OnClose?.Invoke(this);
}
//private void Socket_OnClose()
//{
// ConnectionClosed();
// OnClose?.Invoke(this);
//}
protected virtual void ConnectionClosed()
{
//protected virtual void ConnectionClosed()
//{
}
//}
private void Socket_OnReceive(NetworkBuffer buffer)
{
try
{
// Unassigned ?
if (sock == null)
return;
//private void Socket_OnReceive(NetworkBuffer buffer)
//{
//}
// Closed ?
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated) // || !connected)
return;
lastAction = DateTime.Now;
if (!processing)
{
processing = true;
try
{
//lock(buffer.SyncLock)
while (buffer.Available > 0 && !buffer.Protected)
DataReceived(buffer);
}
catch
{
}
processing = false;
}
}
catch (Exception ex)
{
Global.Log("NetworkConnection", LogType.Warning, ex.ToString());
}
}
public ISocket Unassign()
public Sockets.ISocket Unassign()
{
if (sock != null)
{
// connected = false;
sock.OnClose -= Socket_OnClose;
sock.OnConnect -= Socket_OnConnect;
sock.OnReceive -= Socket_OnReceive;
//sock.OnClose -= Socket_OnClose;
//sock.OnConnect -= Socket_OnConnect;
//sock.OnReceive -= Socket_OnReceive;
sock.Receiver = null;
var rt = sock;
sock = null;
@ -167,20 +143,20 @@ namespace Esyur.Net
return null;
}
protected virtual void DataReceived(NetworkBuffer data)
{
if (OnDataReceived != null)
{
try
{
OnDataReceived?.Invoke(this, data);
}
catch (Exception ex)
{
Global.Log("NetworkConenction:DataReceived", LogType.Error, ex.ToString());
}
}
}
//protected virtual void DataReceived(NetworkBuffer data)
//{
// if (OnDataReceived != null)
// {
// try
// {
// OnDataReceived?.Invoke(this, data);
// }
// catch (Exception ex)
// {
// Global.Log("NetworkConenction:DataReceived", LogType.Error, ex.ToString());
// }
// }
//}
public void Close()
{
@ -234,11 +210,11 @@ namespace Esyur.Net
}
public bool Connected
public bool IsConnected
{
get
{
return sock.State == SocketState.Established;// connected;
return sock.State == SocketState.Established;
}
}
@ -283,7 +259,7 @@ namespace Esyur.Net
{
try
{
sock.Send(msg);
sock?.Send(msg);
lastAction = DateTime.Now;
}
catch
@ -309,5 +285,87 @@ namespace Esyur.Net
{
Send(Encoding.UTF8.GetBytes(data));
}
public void NetworkClose(ISocket socket)
{
Disconencted();
OnClose?.Invoke(this);
}
public void NetworkConnect(ISocket socket)
{
Connected();
OnConnect?.Invoke(this);
}
//{
//ConnectionClosed();
//OnClose?.Invoke(this);
//Receiver?.NetworkClose(this);
//}
//public void NetworkConenct(ISocket sender)
//{
// OnConnect?.Invoke(this);
//}
protected abstract void DataReceived(NetworkBuffer buffer);
protected abstract void Connected();
protected abstract void Disconencted();
public void NetworkReceive(ISocket 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
{
//lock(buffer.SyncLock)
while (buffer.Available > 0 && !buffer.Protected)
{
//Receiver?.NetworkReceive(this, buffer);
DataReceived(buffer);
}
}
catch
{
}
processing = false;
}
}
catch (Exception ex)
{
Global.Log("NetworkConnection", LogType.Warning, ex.ToString());
}
}
//{
// Receiver?.NetworkError(this);
//throw new NotImplementedException();
//}
//public void NetworkConnect(ISocket sender)
//{
// Receiver?.NetworkConnect(this);
//throw new NotImplementedException();
//}
}
}

View File

@ -39,14 +39,14 @@ namespace Esyur.Net
public abstract class NetworkServer<TConnection> : IDestructible where TConnection : NetworkConnection, new()
{
//private bool isRunning;
private ISocket listener;
private AutoList<TConnection, NetworkServer<TConnection>> connections;
private Sockets.ISocket listener;
public AutoList<TConnection, NetworkServer<TConnection>> Connections { get; private set; }
private Thread thread;
protected abstract void DataReceived(TConnection sender, NetworkBuffer data);
protected abstract void ClientConnected(TConnection sender);
protected abstract void ClientDisconnected(TConnection sender);
//protected abstract void DataReceived(TConnection sender, NetworkBuffer data);
//protected abstract void ClientConnected(TConnection sender);
//protected abstract void ClientDisconnected(TConnection sender);
private Timer timer;
@ -54,74 +54,16 @@ namespace Esyur.Net
public event DestroyedEvent OnDestroy;
public AutoList<TConnection, NetworkServer<TConnection>> Connections
{
get
{
return connections;
}
}
/*
public void RemoveSession(string ID)
{
Sessions.Remove(ID);
}
public void RemoveSession(TSession Session)
{
if (Session != null)
Sessions.Remove(Session.Id);
}
*/
/*
public TSession CreateSession(string ID, int Timeout)
{
TSession s = new TSession();
s.SetSession(ID, Timeout, new NetworkSession.SessionModifiedEvent(SessionModified)
, new NetworkSession.SessionEndedEvent(SessionEnded));
Sessions.Add(ID, s);
return s;
}
*/
/*
private void pSessionModified(TSession session, string key, object oldValue, object newValue)
{
SessionModified((TSession)session, key, oldValue, newValue);
}
private void pSessionEnded(NetworkSession session)
{
SessionEnded((TSession)session);
}
*/
/*
protected virtual void SessionModified(NetworkSession session, string key, object oldValue, object newValue)
{
}
protected virtual void SessionEnded(NetworkSession session)
{
Sessions.Remove(session.Id);
session.Destroy();
}
*/
//public AutoList<TConnection, NetworkServer<TConnection>> Connections => connections;
private void MinuteThread(object state)
{
List<TConnection> ToBeClosed = null;
lock (connections.SyncRoot)
lock (Connections.SyncRoot)
{
foreach (TConnection c in connections)
foreach (TConnection c in Connections)
{
if (DateTime.Now.Subtract(c.LastAction).TotalSeconds >= Timeout)
{
@ -132,8 +74,6 @@ namespace Esyur.Net
}
}
//Console.WriteLine("UnLock MinuteThread");
if (ToBeClosed != null)
{
//Console.WriteLine("Term: " + ToBeClosed.Count + " " + this.listener.LocalEndPoint.ToString());
@ -145,18 +85,13 @@ namespace Esyur.Net
}
}
public void Start(ISocket socket)//, uint timeout, uint clock)
public void Start(Sockets.ISocket socket)//, uint timeout, uint clock)
{
if (listener != null)
return;
//if (socket.State == SocketState.Listening)
// return;
//if (isRunning)
// return;
connections = new AutoList<TConnection, NetworkServer<TConnection>>(this);
Connections = new AutoList<TConnection, NetworkServer<TConnection>>(this);
if (Timeout > 0 & Clock > 0)
@ -165,11 +100,6 @@ namespace Esyur.Net
}
// start a new thread for the server to live on
//isRunning = true;
listener = socket;
// Start accepting
@ -182,8 +112,43 @@ namespace Esyur.Net
{
while (true)
{
var s = listener.Accept();
NewConnection(s);
try
{
var s = listener.Accept();
if (s == null)
{
Console.Write("sock == null");
return;
}
var c = new TConnection();
//c.OnClose += ClientDisconnectedEventReceiver;
c.Assign(s);
Add(c);
//Connections.Add(c);
try
{
//ClientConnected(c);
ClientConnected(c);
//NetworkConnect(c);
}
catch
{
// something wrong with the child.
}
s.Begin();
// Accept more
//listener.Accept().Then(NewConnection);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}));
@ -191,15 +156,6 @@ namespace Esyur.Net
}
/*
public int LocalPort
{
get
{
return port;
}
}
*/
[Attribute]
public uint Timeout
@ -237,7 +193,7 @@ namespace Esyur.Net
//Console.WriteLine("Listener stopped");
var cons = connections.ToArray();
var cons = Connections.ToArray();
//lock (connections.SyncRoot)
//{
@ -261,67 +217,22 @@ namespace Esyur.Net
}
public virtual void RemoveConnection(TConnection connection)
public virtual void Remove(TConnection connection)
{
connection.OnDataReceived -= OnDataReceived;
connection.OnConnect -= OnClientConnect;
connection.OnClose -= OnClientClose;
connections.Remove(connection);
//connection.OnDataReceived -= OnDataReceived;
//connection.OnConnect -= OnClientConnect;
connection.OnClose -= ClientDisconnectedEventReceiver;
Connections.Remove(connection);
}
public virtual void AddConnection(TConnection connection)
public virtual void Add(TConnection connection)
{
connection.OnDataReceived += OnDataReceived;
connection.OnConnect += OnClientConnect;
connection.OnClose += OnClientClose;
connections.Add(connection);
//connection.OnDataReceived += OnDataReceived;
//connection.OnConnect += OnClientConnect;
connection.OnClose += ClientDisconnectedEventReceiver;// OnClientClose;
Connections.Add(connection);
}
private void NewConnection(ISocket sock)
{
try
{
if (sock == null)
{
Console.Write("sock == null");
return;
}
TConnection c = new TConnection();
AddConnection(c);
c.Assign(sock);
try
{
ClientConnected(c);
}
catch
{
// something wrong with the child.
}
sock.Begin();
// Accept more
//listener.Accept().Then(NewConnection);
}
catch (Exception ex)
{
//Console.WriteLine("TSERVER " + ex.ToString());
Global.Log("NetworkServer", LogType.Error, ex.ToString());
}
//isRunning = false;
}
public bool IsRunning
{
@ -332,44 +243,31 @@ namespace Esyur.Net
}
}
public void OnDataReceived(NetworkConnection sender, NetworkBuffer data)
{
DataReceived((TConnection)sender, data);
}
//public void OnDataReceived(ISocket sender, NetworkBuffer data)
//{
// DataReceived((TConnection)sender, data);
//}
public void OnClientConnect(NetworkConnection sender)
{
if (sender == null)
return;
//public void OnClientConnect(ISocket sender)
//{
// if (sender == null)
// return;
if (sender.RemoteEndPoint == null || sender.LocalEndPoint == null)
{ }
//Console.WriteLine("NULL");
else
Global.Log("Connections", LogType.Debug, sender.RemoteEndPoint.Address.ToString()
+ "->" + sender.LocalEndPoint.Port + " at " + DateTime.UtcNow.ToString("d")
+ " " + DateTime.UtcNow.ToString("d"), false);
// if (sender.RemoteEndPoint == null || sender.LocalEndPoint == null)
// { }
// //Console.WriteLine("NULL");
// else
// Global.Log("Connections", LogType.Debug, sender.RemoteEndPoint.Address.ToString()
// + "->" + sender.LocalEndPoint.Port + " at " + DateTime.UtcNow.ToString("d")
// + " " + DateTime.UtcNow.ToString("d"), false);
// Console.WriteLine("Connected " + sender.RemoteEndPoint.ToString());
ClientConnected((TConnection)sender);
}
// // Console.WriteLine("Connected " + sender.RemoteEndPoint.ToString());
// ClientConnected((TConnection)sender);
//}
public void OnClientClose(NetworkConnection sender)
{
try
{
sender.Destroy();
RemoveConnection((TConnection)sender);
ClientDisconnected((TConnection)sender);
}
catch (Exception ex)
{
Global.Log("NetworkServer:OnClientDisconnect", LogType.Error, ex.ToString());
}
sender = null;
GC.Collect();
}
//public void OnClientClose(ISocket sender)
//{
//}
public void Destroy()
@ -378,10 +276,34 @@ namespace Esyur.Net
OnDestroy?.Invoke(this);
}
private void ClientDisconnectedEventReceiver(NetworkConnection connection)
{
try
{
var con = connection as TConnection;
con.Destroy();
// con.OnClose -= ClientDisconnectedEventReceiver;
Remove(con);
//Connections.Remove(con);
ClientDisconnected(con);
//RemoveConnection((TConnection)sender);
//connections.Remove(sender)
//ClientDisconnected((TConnection)sender);
}
catch (Exception ex)
{
Global.Log("NetworkServer:OnClientDisconnect", LogType.Error, ex.ToString());
}
}
protected abstract void ClientDisconnected(TConnection connection);
protected abstract void ClientConnected(TConnection connection);
~NetworkServer()
{
Stop();
//Connections = null;
listener = null;
}
}

View File

@ -240,7 +240,7 @@ namespace Esyur.Net.Packets
// check limit
if (postSize > data.Length - headerSize)
return postSize - (data.Length - headerSize);
return -(postSize - (data.Length - headerSize));
if (Headers["content-type"].StartsWith("application/x-www-form-urlencoded")

View File

@ -38,17 +38,19 @@ using Esyur.Core;
namespace Esyur.Net.Sockets
{
public delegate void ISocketReceiveEvent(NetworkBuffer buffer);
public delegate void ISocketConnectEvent();
public delegate void ISocketCloseEvent();
//public delegate void ISocketReceiveEvent(NetworkBuffer buffer);
//public delegate void ISocketConnectEvent();
//public delegate void ISocketCloseEvent();
public interface ISocket : IDestructible
{
SocketState State { get; }
event ISocketReceiveEvent OnReceive;
event ISocketConnectEvent OnConnect;
event ISocketCloseEvent OnClose;
//event ISocketReceiveEvent OnReceive;
//event ISocketConnectEvent OnConnect;
//event ISocketCloseEvent OnClose;
INetworkReceiver<ISocket> Receiver { get; set; }
AsyncReply<bool> SendAsync(byte[] message, int offset, int length);

View File

@ -41,6 +41,9 @@ namespace Esyur.Net.Sockets
{
public class SSLSocket : ISocket
{
public INetworkReceiver<ISocket> Receiver { get; set; }
Socket sock;
byte[] receiveBuffer;
@ -59,12 +62,11 @@ namespace Esyur.Net.Sockets
SocketState state = SocketState.Initial;
public event ISocketReceiveEvent OnReceive;
public event ISocketConnectEvent OnConnect;
public event ISocketCloseEvent OnClose;
//public event ISocketReceiveEvent OnReceive;
//public event ISocketConnectEvent OnConnect;
//public event ISocketCloseEvent OnClose;
public event DestroyedEvent OnDestroy;
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
SslStream ssl;
X509Certificate2 cert;
@ -87,8 +89,8 @@ namespace Esyur.Net.Sockets
{
await BeginAsync();
state = SocketState.Established;
OnConnect?.Invoke();
//OnConnect?.Invoke();
Receiver?.NetworkConnect(this);
}
catch (Exception ex)
{
@ -271,7 +273,8 @@ namespace Esyur.Net.Sockets
}
}
OnClose?.Invoke();
Receiver?.NetworkClose(this);
//OnClose?.Invoke();
}
}
@ -428,7 +431,9 @@ namespace Esyur.Net.Sockets
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)bytesReceived);
OnReceive?.Invoke(receiveNetworkBuffer);
//OnReceive?.Invoke(receiveNetworkBuffer);
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
ssl.BeginRead(receiveBuffer, 0, receiveBuffer.Length, ReceiveCallback, this);
@ -453,7 +458,10 @@ namespace Esyur.Net.Sockets
public void Destroy()
{
Close();
Receiver = null;
receiveNetworkBuffer = null;
OnDestroy?.Invoke(this);
OnDestroy = null;
}
public async AsyncReply<ISocket> AcceptAsync()

View File

@ -39,6 +39,8 @@ namespace Esyur.Net.Sockets
{
public class TCPSocket : ISocket
{
public INetworkReceiver<ISocket> Receiver { get; set; }
Socket sock;
byte[] receiveBuffer;
@ -58,9 +60,9 @@ namespace Esyur.Net.Sockets
SocketState state = SocketState.Initial;
public event ISocketReceiveEvent OnReceive;
public event ISocketConnectEvent OnConnect;
public event ISocketCloseEvent OnClose;
//public event ISocketReceiveEvent OnReceive;
//public event ISocketConnectEvent OnConnect;
//public event ISocketCloseEvent OnClose;
public event DestroyedEvent OnDestroy;
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
@ -103,7 +105,8 @@ namespace Esyur.Net.Sockets
{
state = SocketState.Established;
OnConnect?.Invoke();
//OnConnect?.Invoke();
Receiver?.NetworkConnect(this);
Begin();
rt.Trigger(true);
}
@ -134,7 +137,8 @@ namespace Esyur.Net.Sockets
}
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
OnReceive?.Invoke(receiveNetworkBuffer);
//OnReceive?.Invoke(receiveNetworkBuffer);
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
if (state == SocketState.Established)
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
@ -163,11 +167,18 @@ namespace Esyur.Net.Sockets
Close();
return;
}
else if (e.SocketError != SocketError.Success)
{
Close();
return;
}
var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred;
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
OnReceive?.Invoke(receiveNetworkBuffer);
//OnReceive?.Invoke(receiveNetworkBuffer);
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
if (state == SocketState.Established)
while (!sock.ReceiveAsync(e))
@ -189,6 +200,12 @@ namespace Esyur.Net.Sockets
Close();
return;
}
else if (e.SocketError != SocketError.Success)
{
Close();
return;
}
//if (e.BytesTransferred > 100000)
// Console.WriteLine("BytesTransferred is large " + e.BytesTransferred);
@ -197,7 +214,8 @@ namespace Esyur.Net.Sockets
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount);
OnReceive?.Invoke(receiveNetworkBuffer);
//OnReceive?.Invoke(receiveNetworkBuffer);
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
}
}
@ -341,7 +359,8 @@ namespace Esyur.Net.Sockets
}
}
OnClose?.Invoke();
//OnClose?.Invoke();
Receiver?.NetworkClose(this);
}
}
@ -454,7 +473,15 @@ namespace Esyur.Net.Sockets
public void Destroy()
{
Close();
//OnClose = null;
//OnConnect = null;
//OnReceive = null;
Receiver = null;
receiveNetworkBuffer = null;
socketArgs.Completed -= SocketArgs_Completed;
socketArgs = null;
OnDestroy?.Invoke(this);
OnDestroy = null;
}
public ISocket Accept()

View File

@ -34,10 +34,11 @@ using System.IO;
using Esyur.Core;
using Esyur.Resource;
using Esyur.Data;
using System.Globalization;
namespace Esyur.Net.Sockets
{
public class WSSocket : ISocket
public class WSSocket : ISocket, INetworkReceiver<ISocket>
{
WebsocketPacket pkt_receive = new WebsocketPacket();
WebsocketPacket pkt_send = new WebsocketPacket();
@ -49,9 +50,9 @@ namespace Esyur.Net.Sockets
object sendLock = new object();
bool held;
public event ISocketReceiveEvent OnReceive;
public event ISocketConnectEvent OnConnect;
public event ISocketCloseEvent OnClose;
//public event ISocketReceiveEvent OnReceive;
//public event ISocketConnectEvent OnConnect;
//public event ISocketCloseEvent OnClose;
public event DestroyedEvent OnDestroy;
long totalSent, totalReceived;
@ -63,8 +64,6 @@ namespace Esyur.Net.Sockets
get { return (IPEndPoint)sock.LocalEndPoint; }
}
public IPEndPoint RemoteEndPoint
{
get { return sock.RemoteEndPoint; }
@ -79,6 +78,7 @@ namespace Esyur.Net.Sockets
}
}
public INetworkReceiver<ISocket> Receiver { get; set; }
public WSSocket(ISocket socket)
{
@ -86,12 +86,175 @@ namespace Esyur.Net.Sockets
pkt_send.Mask = false;
pkt_send.Opcode = WebsocketPacket.WSOpcode.BinaryFrame;
sock = socket;
sock.OnClose += Sock_OnClose;
sock.OnConnect += Sock_OnConnect;
sock.OnReceive += Sock_OnReceive;
sock.Receiver = this;
//sock.OnClose += Sock_OnClose;
//sock.OnConnect += Sock_OnConnect;
//sock.OnReceive += Sock_OnReceive;
}
private void Sock_OnReceive(NetworkBuffer buffer)
//private void Sock_OnReceive(NetworkBuffer buffer)
//{
//}
//private void Sock_OnConnect()
//{
// OnConnect?.Invoke();
//}
//private void Sock_OnClose()
//{
// OnClose?.Invoke();
//}
public void Send(WebsocketPacket packet)
{
lock (sendLock)
if (packet.Compose())
sock.Send(packet.Data);
}
public void Send(byte[] message)
{
lock (sendLock)
{
if (held)
{
sendNetworkBuffer.Write(message);
}
else
{
totalSent += message.Length;
//Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
pkt_send.Message = message;
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
}
public void Send(byte[] message, int offset, int size)
{
lock (sendLock)
{
if (held)
{
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
}
else
{
totalSent += size;
//Console.WriteLine("TX " + size + "/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
pkt_send.Message = new byte[size];
Buffer.BlockCopy(message, offset, pkt_send.Message, 0, size);
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
}
public void Close()
{
sock?.Close();
}
public AsyncReply<bool> Connect(string hostname, ushort port)
{
throw new NotImplementedException();
}
public bool Begin()
{
return sock.Begin();
}
public bool Trigger(ResourceTrigger trigger)
{
return true;
}
public void Destroy()
{
Close();
//OnClose = null;
//OnConnect = null;
//OnReceive = null;
receiveNetworkBuffer = null;
//sock.OnReceive -= Sock_OnReceive;
//sock.OnClose -= Sock_OnClose;
//sock.OnConnect -= Sock_OnConnect;
sock.Receiver = null;
sock = null;
OnDestroy?.Invoke(this);
OnDestroy = null;
}
public AsyncReply<ISocket> AcceptAsync()
{
throw new NotImplementedException();
}
public void Hold()
{
//Console.WriteLine("WS Hold ");
held = true;
}
public void Unhold()
{
lock (sendLock)
{
held = false;
var message = sendNetworkBuffer.Read();
//Console.WriteLine("WS Unhold {0}", message == null ? 0 : message.Length);
if (message == null)
return;
totalSent += message.Length;
pkt_send.Message = message;
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
{
throw new NotImplementedException();
}
public ISocket Accept()
{
throw new NotImplementedException();
}
public AsyncReply<bool> BeginAsync()
{
return sock.BeginAsync();
}
public void NetworkClose(ISocket sender)
{
Receiver?.NetworkClose(sender);
}
public void NetworkReceive(ISocket sender, NetworkBuffer buffer)
{
if (sock.State == SocketState.Closed || sock.State == SocketState.Terminated)
@ -161,8 +324,9 @@ namespace Esyur.Net.Sockets
if (offset == msg.Length)
{
// Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
OnReceive?.Invoke(receiveNetworkBuffer);
//OnReceive?.Invoke(receiveNetworkBuffer);
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
return;
}
@ -180,152 +344,20 @@ namespace Esyur.Net.Sockets
//Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available);
OnReceive?.Invoke(receiveNetworkBuffer);
//OnReceive?.Invoke(receiveNetworkBuffer);
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
processing = false;
if (buffer.Available > 0 && !buffer.Protected)
Sock_OnReceive(buffer);
Receiver?.NetworkReceive(this, buffer);
//Sock_OnReceive(buffer);
}
private void Sock_OnConnect()
public void NetworkConnect(ISocket sender)
{
OnConnect?.Invoke();
}
private void Sock_OnClose()
{
OnClose?.Invoke();
}
public void Send(WebsocketPacket packet)
{
lock (sendLock)
if (packet.Compose())
sock.Send(packet.Data);
}
public void Send(byte[] message)
{
lock (sendLock)
{
if (held)
{
sendNetworkBuffer.Write(message);
}
else
{
totalSent += message.Length;
//Console.WriteLine("TX " + message.Length +"/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
pkt_send.Message = message;
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
}
public void Send(byte[] message, int offset, int size)
{
lock (sendLock)
{
if (held)
{
sendNetworkBuffer.Write(message, (uint)offset, (uint)size);
}
else
{
totalSent += size;
//Console.WriteLine("TX " + size + "/"+totalSent);// + " " + DC.ToHex(message, 0, (uint)size));
pkt_send.Message = new byte[size];
Buffer.BlockCopy(message, offset, pkt_send.Message, 0, size);
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
}
public void Close()
{
sock.Close();
}
public AsyncReply<bool> Connect(string hostname, ushort port)
{
throw new NotImplementedException();
}
public bool Begin()
{
return sock.Begin();
}
public bool Trigger(ResourceTrigger trigger)
{
return true;
}
public void Destroy()
{
Close();
OnDestroy?.Invoke(this);
}
public AsyncReply<ISocket> AcceptAsync()
{
throw new NotImplementedException();
}
public void Hold()
{
//Console.WriteLine("WS Hold ");
held = true;
}
public void Unhold()
{
lock (sendLock)
{
held = false;
var message = sendNetworkBuffer.Read();
//Console.WriteLine("WS Unhold {0}", message == null ? 0 : message.Length);
if (message == null)
return;
totalSent += message.Length;
pkt_send.Message = message;
if (pkt_send.Compose())
sock.Send(pkt_send.Data);
}
}
public AsyncReply<bool> SendAsync(byte[] message, int offset, int length)
{
throw new NotImplementedException();
}
public ISocket Accept()
{
throw new NotImplementedException();
}
public AsyncReply<bool> BeginAsync()
{
return sock.BeginAsync();
Receiver?.NetworkConnect(this);
}
}
}

View File

@ -34,11 +34,11 @@ using Esyur.Data;
namespace Esyur.Net.TCP
{
public class TCPConnection: NetworkConnection
{
public class TCPConnection:NetworkConnection {
private KeyList<string, object> variables = new KeyList<string, object>();
public TCPServer Server { get; internal set; }
public KeyList<string, object> Variables
{
@ -47,5 +47,20 @@ namespace Esyur.Net.TCP
return variables;
}
}
protected override void Connected()
{
// do nothing
}
protected override void DataReceived(NetworkBuffer buffer)
{
Server?.Execute(this, buffer);
}
protected override void Disconencted()
{
// do nothing
}
}
}

View File

@ -103,15 +103,19 @@ namespace Esyur.Net.TCP
protected override void DataReceived(TCPConnection sender, NetworkBuffer data)
internal bool Execute(TCPConnection sender, NetworkBuffer data)
{
var msg = data.Read();
foreach (var filter in filters)
{
if (filter.Execute(msg, data, sender))
return;
return true;
}
return false;
}
private void SessionModified(TCPConnection session, string key, object newValue)
@ -119,22 +123,34 @@ namespace Esyur.Net.TCP
}
protected override void ClientConnected(TCPConnection sender)
protected override void ClientDisconnected(TCPConnection connection)
{
foreach (var filter in filters)
{
filter.Connected(sender);
filter.Connected(connection);
}
}
protected override void ClientDisconnected(TCPConnection sender)
public override void Add(TCPConnection connection)
{
connection.Server = this;
base.Add(connection);
}
public override void Remove(TCPConnection connection)
{
connection.Server = null;
base.Remove(connection);
}
protected override void ClientConnected(TCPConnection connection)
{
foreach (var filter in filters)
{
filter.Disconnected(sender);
filter.Disconnected(connection);
}
}
}
}
}