mirror of
				https://github.com/esiur/esiur-dotnet.git
				synced 2025-10-31 16:01:34 +00:00 
			
		
		
		
	1.5
This commit is contained in:
		| @@ -141,6 +141,12 @@ namespace Esyur.Net.HTTP | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public void Send(WebsocketPacket packet) | ||||
|         { | ||||
|             if (packet.Data != null) | ||||
|                 base.Send(packet.Data); | ||||
|         } | ||||
|  | ||||
|         public override void Send(string data) | ||||
|         { | ||||
|             Response.Message = Encoding.UTF8.GetBytes(data); | ||||
|   | ||||
| @@ -44,7 +44,7 @@ namespace Esyur.Net.HTTP | ||||
|     public class HTTPServer : NetworkServer<HTTPConnection>, IResource | ||||
|     { | ||||
|         Dictionary<string, HTTPSession> sessions= new Dictionary<string, HTTPSession>(); | ||||
|         HTTPFilter[] filters = null; | ||||
|         HTTPFilter[] filters = new HTTPFilter[0]; | ||||
|  | ||||
|         public Instance Instance | ||||
|         { | ||||
| @@ -52,63 +52,55 @@ namespace Esyur.Net.HTTP | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         public virtual string ip | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|         [Storable] | ||||
|         public virtual ushort port | ||||
|         [Attribute] | ||||
|         public virtual string IP | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         public virtual uint timeout | ||||
|         [Attribute] | ||||
|         public virtual ushort Port | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         public virtual uint clock | ||||
|         [Attribute] | ||||
|         public virtual uint Timeout | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         public virtual uint maxPost | ||||
|         [Attribute] | ||||
|         public virtual uint Clock | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         public virtual bool ssl | ||||
|         [Attribute] | ||||
|         public virtual uint MaxPost | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         public virtual string certificate | ||||
|         [Attribute] | ||||
|         public virtual bool SSL | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         //public override void ClientConnected(TClient Sender) | ||||
|         //{ | ||||
|         //} | ||||
|         /* | ||||
|         public DStringDictionary Configurations | ||||
|         [Attribute] | ||||
|         public virtual string Certificate | ||||
|         { | ||||
|             get { return config; } | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|          */  | ||||
|  | ||||
|  | ||||
|         public enum ResponseCodes : int | ||||
|         { | ||||
| @@ -118,7 +110,7 @@ namespace Esyur.Net.HTTP | ||||
|             HTTP_MOVED = 301, | ||||
|             HTTP_NOTMODIFIED = 304, | ||||
|             HTTP_REDIRECT = 307 | ||||
|         } | ||||
|        } | ||||
|  | ||||
|     | ||||
|        public HTTPSession CreateSession(string id, int timeout) | ||||
| @@ -132,27 +124,6 @@ namespace Esyur.Net.HTTP | ||||
|  | ||||
|             return s; | ||||
|        } | ||||
|   | ||||
|  | ||||
|         /* | ||||
|         protected override void SessionModified(NetworkSession session, string key, object oldValue, object newValue) | ||||
|         { | ||||
|             foreach (var instance in Instance.Children) | ||||
|             { | ||||
|                 var f = (HTTPFilter)instance; | ||||
|                 f.SessionModified(session as HTTPSession, key, oldValue, newValue); | ||||
|             } | ||||
|         } | ||||
|         */ | ||||
|  | ||||
|         //public override object InitializeLifetimeService() | ||||
|         //{ | ||||
|         //  return null; | ||||
|         //} | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|         public static string MakeCookie(string Item, string Value, DateTime Expires, string Domain, string Path, bool HttpOnly) | ||||
|         { | ||||
| @@ -183,7 +154,7 @@ namespace Esyur.Net.HTTP | ||||
|         protected override void ClientDisconnected(HTTPConnection sender) | ||||
|         { | ||||
|             //Console.WriteLine("OUT: " + this.Connections.Count); | ||||
|  | ||||
|              | ||||
|             foreach (var filter in filters) | ||||
|                 filter.ClientDisconnected(sender); | ||||
|         } | ||||
| @@ -222,11 +193,11 @@ namespace Esyur.Net.HTTP | ||||
|             } | ||||
|             else if (BL > 0) | ||||
|             { | ||||
|                 if (BL > maxPost) | ||||
|                 if (BL > MaxPost) | ||||
|                 { | ||||
|                     sender.Send( | ||||
|                         "<html><body>POST method content is larger than " | ||||
|                         + maxPost | ||||
|                         + MaxPost | ||||
|                         + " bytes.</body></html>"); | ||||
|  | ||||
|                     sender.Close(); | ||||
| @@ -326,7 +297,7 @@ namespace Esyur.Net.HTTP | ||||
|          */  | ||||
|  | ||||
|  | ||||
|         public AsyncReply<bool> Trigger(ResourceTrigger trigger) | ||||
|         public async AsyncReply<bool> Trigger(ResourceTrigger trigger) | ||||
|         { | ||||
|  | ||||
|             if (trigger == ResourceTrigger.Initialize) | ||||
| @@ -341,19 +312,19 @@ namespace Esyur.Net.HTTP | ||||
|                 ISocket listener; | ||||
|                 IPAddress ipAdd; | ||||
|  | ||||
|                 if (ip == null) | ||||
|                 if (IP == null) | ||||
|                     ipAdd = IPAddress.Any; | ||||
|                 else | ||||
|                     ipAdd = IPAddress.Parse(ip); | ||||
|                     ipAdd = IPAddress.Parse(IP); | ||||
|  | ||||
|                // if (ssl) | ||||
|                 //    listener = new SSLSocket(new IPEndPoint(ipAdd, port), new X509Certificate2(certificate)); | ||||
|                // else | ||||
|                     listener = new TCPSocket(new IPEndPoint(ipAdd, port)); | ||||
|                     listener = new TCPSocket(new IPEndPoint(ipAdd, Port)); | ||||
|  | ||||
|                 Start(listener, | ||||
|                             timeout, | ||||
|                             clock); | ||||
|                             Timeout, | ||||
|                             Clock); | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.Terminate) | ||||
|             { | ||||
| @@ -361,15 +332,15 @@ namespace Esyur.Net.HTTP | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.SystemReload) | ||||
|             { | ||||
|                 Trigger(ResourceTrigger.Terminate); | ||||
|                 Trigger(ResourceTrigger.Initialize); | ||||
|                 await Trigger(ResourceTrigger.Terminate); | ||||
|                 await Trigger(ResourceTrigger.Initialize); | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.SystemInitialized) | ||||
|             { | ||||
|                 Instance.Children<HTTPFilter>().Then(x => filters = x); | ||||
|                 filters = await Instance.Children<HTTPFilter>(); | ||||
|             } | ||||
|  | ||||
|             return new AsyncReply<bool>(true); | ||||
|             return true; | ||||
|  | ||||
|         } | ||||
|          | ||||
| @@ -380,7 +351,7 @@ namespace Esyur.Net.HTTP | ||||
|             sender.SetParent(this); | ||||
|  | ||||
|             //Console.WriteLine("IN: " + this.Connections.Count); | ||||
|             if (filters == null) | ||||
|             if (filters.Length == 0) | ||||
|             { | ||||
|                 sender.Close(); | ||||
|                 return; | ||||
|   | ||||
| @@ -36,8 +36,8 @@ namespace Esyur.Net.HTTP | ||||
| { | ||||
|     public class IIPoWS: HTTPFilter | ||||
|     { | ||||
|         [ResourceProperty] | ||||
|         public DistributedServer DistributedServer | ||||
|         [Attribute] | ||||
|         public DistributedServer Server | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
| @@ -48,7 +48,7 @@ namespace Esyur.Net.HTTP | ||||
|  | ||||
|             if (sender.IsWebsocketRequest()) | ||||
|             { | ||||
|                 if (DistributedServer == null) | ||||
|                 if (Server == null) | ||||
|                     return false; | ||||
|  | ||||
|                 var tcpSocket = sender.Unassign(); | ||||
| @@ -62,7 +62,7 @@ namespace Esyur.Net.HTTP | ||||
|  | ||||
|                 var iipConnection = new DistributedConnection(); | ||||
|  | ||||
|                 DistributedServer.AddConnection(iipConnection); | ||||
|                 Server.AddConnection(iipConnection); | ||||
|                 iipConnection.Assign(wsSocket); | ||||
|                 wsSocket.Begin(); | ||||
|  | ||||
|   | ||||
| @@ -801,13 +801,13 @@ namespace Esyur.Net.IIP | ||||
|          } | ||||
|  | ||||
|  | ||||
|         [ResourceAttribute] | ||||
|         [Attribute] | ||||
|         public string Username { get; set; } | ||||
|  | ||||
|         [ResourceAttribute] | ||||
|         [Attribute] | ||||
|         public string Password { get; set; } | ||||
|  | ||||
|         [ResourceAttribute] | ||||
|         [Attribute] | ||||
|         public string Domain { get; set; } | ||||
|         /// <summary> | ||||
|         /// Resource interface | ||||
|   | ||||
| @@ -39,48 +39,44 @@ namespace Esyur.Net.IIP | ||||
| { | ||||
|     public class DistributedServer : NetworkServer<DistributedConnection>, IResource | ||||
|     { | ||||
|  | ||||
|         //[Storable] | ||||
|         //[ResourceProperty] | ||||
|         public string ip | ||||
|         [Attribute] | ||||
|         public string IP | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         //[Storable] | ||||
|         //[ResourceProperty] | ||||
|         [Attribute] | ||||
|         public IMembership Membership | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Attribute] | ||||
|         public EntryPoint EntryPoint | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         //[Storable] | ||||
|         //[ResourceProperty] | ||||
|         public ushort port | ||||
|         [Attribute] | ||||
|         public ushort Port | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         //[Storable] | ||||
|         //[ResourceProperty] | ||||
|         public uint timeout | ||||
|         [Attribute] | ||||
|         public uint Timeout | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|          | ||||
|         //[Storable] | ||||
|         //[ResourceProperty] | ||||
|         public uint clock | ||||
|         | ||||
|         [Attribute] | ||||
|         public uint Clock | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
| @@ -99,12 +95,12 @@ namespace Esyur.Net.IIP | ||||
|             { | ||||
|                 TCPSocket listener; | ||||
|  | ||||
|                 if (ip != null) | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Parse(ip), port)); | ||||
|                 if (IP != null) | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Parse(IP), Port)); | ||||
|                 else | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Any, port)); | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Any, Port)); | ||||
|  | ||||
|                 Start(listener, timeout, clock); | ||||
|                 Start(listener, Timeout, Clock); | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.Terminate) | ||||
|             { | ||||
|   | ||||
| @@ -35,6 +35,6 @@ namespace Esyur.Net.IIP | ||||
|     { | ||||
|  | ||||
|         public abstract AsyncReply<IResource[]> Query(string path, DistributedConnection sender); | ||||
|         public abstract override bool Create(); | ||||
|         protected abstract override bool Create(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -31,18 +31,19 @@ using Esyur.Core; | ||||
| using Esyur.Net.Sockets; | ||||
| using Esyur.Resource; | ||||
| using System.Threading.Tasks; | ||||
| using System.Diagnostics; | ||||
|  | ||||
| namespace Esyur.Net | ||||
| { | ||||
|  | ||||
|     public abstract class NetworkServer<TConnection>: IDestructible where TConnection : NetworkConnection, new() | ||||
|     public abstract class NetworkServer<TConnection> : IDestructible where TConnection : NetworkConnection, new() | ||||
|     { | ||||
|         //private bool isRunning; | ||||
|         uint clock; | ||||
|         private ISocket listener; | ||||
|         private AutoList<TConnection, NetworkServer<TConnection>> connections; | ||||
|  | ||||
|         //private Thread thread; | ||||
|         private Thread thread; | ||||
|  | ||||
|         protected abstract void DataReceived(TConnection sender, NetworkBuffer data); | ||||
|         protected abstract void ClientConnected(TConnection sender); | ||||
| @@ -102,18 +103,18 @@ namespace Esyur.Net | ||||
|         } | ||||
|         */ | ||||
|  | ||||
|             /* | ||||
|         protected virtual void SessionModified(NetworkSession session, string key, object oldValue, object newValue) | ||||
|         { | ||||
|         /* | ||||
|     protected virtual void SessionModified(NetworkSession session, string key, object oldValue, object newValue) | ||||
|     { | ||||
|  | ||||
|         } | ||||
|     } | ||||
|  | ||||
|         protected virtual void SessionEnded(NetworkSession session) | ||||
|         { | ||||
|             Sessions.Remove(session.Id); | ||||
|             session.Destroy(); | ||||
|         } | ||||
|         */ | ||||
|     protected virtual void SessionEnded(NetworkSession session) | ||||
|     { | ||||
|         Sessions.Remove(session.Id); | ||||
|         session.Destroy(); | ||||
|     } | ||||
|     */ | ||||
|  | ||||
|         private void MinuteThread(object state) | ||||
|         { | ||||
| @@ -179,14 +180,21 @@ namespace Esyur.Net | ||||
|             listener = socket; | ||||
|  | ||||
|             // Start accepting | ||||
|             var r = listener.Accept(); | ||||
|             r.Then(NewConnection); | ||||
|             //var r = listener.Accept(); | ||||
|             //r.Then(NewConnection); | ||||
|             //r.timeout?.Dispose(); | ||||
|  | ||||
|             //var rt = listener.Accept().Then() | ||||
|             //thread = new Thread(new System.Threading.ThreadStart(ListenForConnections)); | ||||
|             thread = new Thread(new ThreadStart(() => | ||||
|             { | ||||
|                 while (true) | ||||
|                 { | ||||
|                     var s = listener.Accept(); | ||||
|                     NewConnection(s); | ||||
|                 } | ||||
|             })); | ||||
|  | ||||
|             //thread.Start(); | ||||
|             thread.Start(); | ||||
|  | ||||
|         } | ||||
|  | ||||
| @@ -221,7 +229,7 @@ namespace Esyur.Net | ||||
|                 // wait until the listener stops | ||||
|                 //while (isRunning) | ||||
|                 //{ | ||||
|                   //  Thread.Sleep(100); | ||||
|                 //  Thread.Sleep(100); | ||||
|                 //} | ||||
|  | ||||
|                 //Console.WriteLine("Listener stopped"); | ||||
| @@ -230,8 +238,8 @@ namespace Esyur.Net | ||||
|  | ||||
|                 //lock (connections.SyncRoot) | ||||
|                 //{ | ||||
|                     foreach (TConnection con in cons) | ||||
|                         con.Close(); | ||||
|                 foreach (TConnection con in cons) | ||||
|                     con.Close(); | ||||
|                 //} | ||||
|  | ||||
|                 //Console.WriteLine("Sockets Closed"); | ||||
| @@ -268,48 +276,35 @@ namespace Esyur.Net | ||||
|  | ||||
|         private void NewConnection(ISocket sock) | ||||
|         { | ||||
|  | ||||
|             try | ||||
|             { | ||||
|  | ||||
|                 /* | ||||
|                     if (listener.State == SocketState.Closed || listener.State == SocketState.Terminated) | ||||
|                     { | ||||
|                            Console.WriteLine("Listen socket break "); | ||||
|                            Console.WriteLine(listener.LocalEndPoint.Port); | ||||
|                            break; | ||||
|                     } | ||||
|                     */ | ||||
|  | ||||
|                     if (sock == null) | ||||
|                     { | ||||
|                         //Console.Write("sock == null"); | ||||
|                         return; | ||||
|                     } | ||||
|  | ||||
|                 //sock.ReceiveBufferSize = 102400; | ||||
|                 //sock.SendBufferSize = 102400; | ||||
|                  | ||||
|                 if (sock == null) | ||||
|                 { | ||||
|                     Console.Write("sock == null"); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|  | ||||
|                     TConnection c = new TConnection(); | ||||
|                     AddConnection(c); | ||||
|                 TConnection c = new TConnection(); | ||||
|                 AddConnection(c); | ||||
|  | ||||
|                     c.Assign(sock); | ||||
|                 c.Assign(sock); | ||||
|  | ||||
|                     try | ||||
|                     { | ||||
|                         ClientConnected(c); | ||||
|                     } | ||||
|                     catch | ||||
|                     { | ||||
|                        // something wrong with the child. | ||||
|                     } | ||||
|                 try | ||||
|                 { | ||||
|                     ClientConnected(c); | ||||
|                 } | ||||
|                 catch | ||||
|                 { | ||||
|                     // something wrong with the child. | ||||
|                 } | ||||
|  | ||||
|                  | ||||
|                 // Accept more | ||||
|                 listener.Accept().Then(NewConnection); | ||||
|                 //l.timeout?.Dispose(); | ||||
|  | ||||
|                     sock.Begin(); | ||||
|                 //listener.Accept().Then(NewConnection); | ||||
|                 sock.Begin(); | ||||
|  | ||||
|  | ||||
|             } | ||||
| @@ -332,7 +327,7 @@ namespace Esyur.Net | ||||
|                 //isRunning;  | ||||
|             } | ||||
|         } | ||||
|   | ||||
|  | ||||
|         public void OnDataReceived(NetworkConnection sender, NetworkBuffer data) | ||||
|         { | ||||
|             DataReceived((TConnection)sender, data); | ||||
| @@ -372,7 +367,7 @@ namespace Esyur.Net | ||||
|             GC.Collect(); | ||||
|         } | ||||
|  | ||||
|          | ||||
|  | ||||
|         public void Destroy() | ||||
|         { | ||||
|             Stop(); | ||||
|   | ||||
| @@ -58,7 +58,9 @@ namespace Esyur.Net.Sockets | ||||
|         AsyncReply<bool> Connect(string hostname, ushort port); | ||||
|         bool Begin(); | ||||
|         //ISocket Accept(); | ||||
|         AsyncReply<ISocket> Accept(); | ||||
|         AsyncReply<ISocket> AcceptAsync(); | ||||
|         ISocket Accept(); | ||||
|  | ||||
|         IPEndPoint RemoteEndPoint { get; } | ||||
|         IPEndPoint LocalEndPoint { get; } | ||||
|  | ||||
|   | ||||
| @@ -311,24 +311,26 @@ namespace Esyur.Net.Sockets | ||||
|             OnDestroy?.Invoke(this); | ||||
|         } | ||||
|  | ||||
|         public AsyncReply<ISocket> Accept() | ||||
|         public async AsyncReply<ISocket> AcceptAsync() | ||||
|         { | ||||
|             var reply = new AsyncReply<ISocket>(); | ||||
|             //var reply = new AsyncReply<ISocket>(); | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 sock.AcceptAsync().ContinueWith((x) => | ||||
|                 { | ||||
|                     try | ||||
|                     { | ||||
|                         reply.Trigger(new SSLSocket(x.Result, cert, true)); | ||||
|                     } | ||||
|                     catch | ||||
|                     { | ||||
|                         reply.Trigger(null); | ||||
|                     } | ||||
|                 return new SSLSocket(await sock.AcceptAsync(), cert, true); | ||||
|  | ||||
|                 }, null); | ||||
|                 //sock.AcceptAsync().ContinueWith((x) => | ||||
|                 //{ | ||||
|                 //    try | ||||
|                 //    { | ||||
|                 //        reply.Trigger(new SSLSocket(x.Result, cert, true)); | ||||
|                 //    } | ||||
|                 //    catch | ||||
|                 //    { | ||||
|                 //        reply.Trigger(null); | ||||
|                 //    } | ||||
|  | ||||
|                 //}, null); | ||||
|  | ||||
|             } | ||||
|             catch | ||||
| @@ -337,7 +339,7 @@ namespace Esyur.Net.Sockets | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return reply; | ||||
|             //return reply; | ||||
|         } | ||||
|  | ||||
|         public void Hold() | ||||
| @@ -354,5 +356,18 @@ namespace Esyur.Net.Sockets | ||||
|         { | ||||
|             throw new NotImplementedException(); | ||||
|         } | ||||
|  | ||||
|         public ISocket Accept() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 return new SSLSocket(sock.Accept(), cert, true); | ||||
|             } | ||||
|             catch | ||||
|             { | ||||
|                 state = SocketState.Terminated; | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -178,7 +178,9 @@ namespace Esyur.Net.Sockets | ||||
|                 //lock (receiveNetworkBuffer.SyncLock) | ||||
|                 // Console.WriteLine(e. + " " + e.BytesTransferred); | ||||
|  | ||||
|                 receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)e.BytesTransferred); | ||||
|                 var recCount = e.BytesTransferred > e.Count ? e.Count : e.BytesTransferred; | ||||
|  | ||||
|                 receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)recCount); | ||||
|  | ||||
|                 //Console.WriteLine("TC IN: " + (uint)e.BytesTransferred + " " + DC.ToHex(receiveBuffer, 0, (uint)e.BytesTransferred)); | ||||
|  | ||||
| @@ -365,7 +367,15 @@ namespace Esyur.Net.Sockets | ||||
|                 else | ||||
|                 { | ||||
|                     asyncSending = true; | ||||
|                     sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null); | ||||
|                     try | ||||
|                     { | ||||
|                         sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, null); | ||||
|                     } | ||||
|                     catch { | ||||
|                         asyncSending = false; | ||||
|                         state = SocketState.Terminated; | ||||
|                         Close(); | ||||
|                     } | ||||
|                     //sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent); | ||||
|                 } | ||||
|             } | ||||
| @@ -403,7 +413,7 @@ namespace Esyur.Net.Sockets | ||||
|                         } | ||||
|                         catch (Exception ex2) | ||||
|                         { | ||||
|                             Console.WriteLine("Level 2 {0}", ex2); | ||||
|                             state = SocketState.Terminated; | ||||
|                         } | ||||
|  | ||||
|                         Global.Log("TCPSocket", LogType.Error, ex.ToString()); | ||||
| @@ -427,33 +437,35 @@ namespace Esyur.Net.Sockets | ||||
|             OnDestroy?.Invoke(this); | ||||
|         } | ||||
|  | ||||
|         public AsyncReply<ISocket> Accept() | ||||
|         public ISocket Accept() | ||||
|         { | ||||
|             var reply = new AsyncReply<ISocket>(); | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 sock.AcceptAsync().ContinueWith((x) => | ||||
|                 { | ||||
|                     try | ||||
|                     { | ||||
|                         reply.Trigger(new TCPSocket(x.Result)); | ||||
|                     } | ||||
|                     catch | ||||
|                     { | ||||
|                         reply.Trigger(null); | ||||
|                     } | ||||
|                 }); | ||||
|                 var s = sock.Accept(); | ||||
|                 return new TCPSocket(s); | ||||
|             } | ||||
|             catch | ||||
|             { | ||||
|                 state = SocketState.Terminated; | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return reply; | ||||
|         } | ||||
|  | ||||
|         public async AsyncReply<ISocket> AcceptAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var s = await sock.AcceptAsync(); | ||||
|                 return new TCPSocket(s); | ||||
|             } | ||||
|             catch | ||||
|             { | ||||
|                 state = SocketState.Terminated; | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public void Hold() | ||||
|         { | ||||
|             held = true; | ||||
| @@ -494,12 +506,23 @@ namespace Esyur.Net.Sockets | ||||
|                 else | ||||
|                 { | ||||
|                     asyncSending = true; | ||||
|                     sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null); | ||||
|                     try | ||||
|                     { | ||||
|                         sock.BeginSend(msg, 0, msg.Length, SocketFlags.None, PacketSent, rt);// null); | ||||
|                     } | ||||
|                     catch | ||||
|                     { | ||||
|                         asyncSending = false; | ||||
|                         state = SocketState.Terminated; | ||||
|                         Close(); | ||||
|                     } | ||||
|                     //sock.SendAsync(new ArraySegment<byte>(msg), SocketFlags.None).ContinueWith(DataSent); | ||||
|                 } | ||||
|  | ||||
|                 return rt; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -63,13 +63,13 @@ namespace Esyur.Net.Sockets | ||||
|             get { return (IPEndPoint)sock.LocalEndPoint; } | ||||
|         } | ||||
|  | ||||
|    | ||||
|          | ||||
|     public IPEndPoint RemoteEndPoint | ||||
|     { | ||||
|         get { return sock.RemoteEndPoint; } | ||||
|     } | ||||
|      | ||||
|  | ||||
|  | ||||
|         public IPEndPoint RemoteEndPoint | ||||
|         { | ||||
|             get { return sock.RemoteEndPoint; } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public SocketState State | ||||
|         { | ||||
| @@ -79,7 +79,7 @@ namespace Esyur.Net.Sockets | ||||
|             } | ||||
|         } | ||||
|  | ||||
|   | ||||
|  | ||||
|         public WSSocket(ISocket socket) | ||||
|         { | ||||
|             pkt_send.FIN = true; | ||||
| @@ -103,7 +103,7 @@ namespace Esyur.Net.Sockets | ||||
|             if (processing) | ||||
|                 return; | ||||
|  | ||||
|             | ||||
|  | ||||
|             var msg = buffer.Read(); | ||||
|  | ||||
|             if (msg == null) | ||||
| @@ -161,7 +161,7 @@ namespace Esyur.Net.Sockets | ||||
|  | ||||
|                 if (offset == msg.Length) | ||||
|                 { | ||||
|                 //    Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available); | ||||
|                     //    Console.WriteLine("WS IN: " + receiveNetworkBuffer.Available); | ||||
|                     OnReceive?.Invoke(receiveNetworkBuffer); | ||||
|                     return; | ||||
|                 } | ||||
| @@ -200,14 +200,14 @@ namespace Esyur.Net.Sockets | ||||
|  | ||||
|         public void Send(WebsocketPacket packet) | ||||
|         { | ||||
|             lock(sendLock) | ||||
|             lock (sendLock) | ||||
|                 if (packet.Compose()) | ||||
|                     sock.Send(packet.Data); | ||||
|         } | ||||
|  | ||||
|         public void Send(byte[] message) | ||||
|         { | ||||
|             lock(sendLock) | ||||
|             lock (sendLock) | ||||
|             { | ||||
|                 if (held) | ||||
|                 { | ||||
| @@ -219,13 +219,16 @@ namespace Esyur.Net.Sockets | ||||
|                     //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) | ||||
| @@ -258,7 +261,7 @@ namespace Esyur.Net.Sockets | ||||
|             throw new NotImplementedException(); | ||||
|         } | ||||
|  | ||||
|   | ||||
|  | ||||
|         public bool Begin() | ||||
|         { | ||||
|             return sock.Begin(); | ||||
| @@ -275,20 +278,20 @@ namespace Esyur.Net.Sockets | ||||
|             OnDestroy?.Invoke(this); | ||||
|         } | ||||
|  | ||||
|         public AsyncReply<ISocket> Accept() | ||||
|         public AsyncReply<ISocket> AcceptAsync() | ||||
|         { | ||||
|             throw new NotImplementedException(); | ||||
|         } | ||||
|  | ||||
|         public void Hold() | ||||
|         { | ||||
|                //Console.WriteLine("WS Hold  "); | ||||
|             //Console.WriteLine("WS Hold  "); | ||||
|             held = true; | ||||
|         } | ||||
|  | ||||
|         public void Unhold() | ||||
|         { | ||||
|             lock(sendLock) | ||||
|             lock (sendLock) | ||||
|             { | ||||
|                 held = false; | ||||
|  | ||||
| @@ -305,7 +308,7 @@ namespace Esyur.Net.Sockets | ||||
|                 if (pkt_send.Compose()) | ||||
|                     sock.Send(pkt_send.Data); | ||||
|  | ||||
|                  | ||||
|  | ||||
|  | ||||
|             } | ||||
|         } | ||||
| @@ -314,5 +317,10 @@ namespace Esyur.Net.Sockets | ||||
|         { | ||||
|             throw new NotImplementedException(); | ||||
|         } | ||||
|  | ||||
|         public ISocket Accept() | ||||
|         { | ||||
|             throw new NotImplementedException(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -39,26 +39,26 @@ namespace Esyur.Net.TCP | ||||
|     public class TCPServer : NetworkServer<TCPConnection>, IResource | ||||
|     { | ||||
|  | ||||
|         [Storable] | ||||
|         string ip | ||||
|         [Attribute] | ||||
|         public string IP | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|         [Attribute] | ||||
|         public ushort Port | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|         [Storable] | ||||
|         ushort port | ||||
|         public uint Timeout | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|         [Storable] | ||||
|         uint timeout | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|         [Storable] | ||||
|         uint clock | ||||
|         [Attribute] | ||||
|         public uint Clock | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
| @@ -75,12 +75,12 @@ namespace Esyur.Net.TCP | ||||
|                 TCPSocket listener; | ||||
|  | ||||
|  | ||||
|                 if (ip != null) | ||||
|                     listener =new TCPSocket(new IPEndPoint(IPAddress.Parse(ip), port)); | ||||
|                 if (IP != null) | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Parse(IP), Port)); | ||||
|                 else | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Any, port)); | ||||
|                     listener = new TCPSocket(new IPEndPoint(IPAddress.Any, Port)); | ||||
|  | ||||
|                 Start(listener, timeout, clock); | ||||
|                 Start(listener, Timeout, Clock); | ||||
|  | ||||
|  | ||||
|             } | ||||
| @@ -108,9 +108,9 @@ namespace Esyur.Net.TCP | ||||
|             var msg = data.Read(); | ||||
|  | ||||
|             foreach (var filter in filters) | ||||
|             {  | ||||
|                     if (filter.Execute(msg, data, sender)) | ||||
|                         return; | ||||
|             { | ||||
|                 if (filter.Execute(msg, data, sender)) | ||||
|                     return; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -30,8 +30,8 @@ using System.Text; | ||||
| using System.Collections; | ||||
| using Esyur.Data; | ||||
| using Esyur.Misc; | ||||
| using Esyur.Engine; | ||||
| using Esyur.Resource; | ||||
| using Esyur.Core; | ||||
|  | ||||
| namespace Esyur.Net.UDP | ||||
| { | ||||
| @@ -43,8 +43,9 @@ namespace Esyur.Net.UDP | ||||
|     }*/ | ||||
|     public class UDPServer : IResource | ||||
|     { | ||||
|         Thread Receiver; | ||||
|         UdpClient Udp; | ||||
|         Thread receiver; | ||||
|         UdpClient udp; | ||||
|         UDPFilter[] filters = new UDPFilter[0]; | ||||
|  | ||||
|         public event DestroyedEvent OnDestroy; | ||||
|  | ||||
| @@ -54,48 +55,30 @@ namespace Esyur.Net.UDP | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         string ip | ||||
|         [Attribute] | ||||
|         string IP | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         [Storable] | ||||
|         ushort port | ||||
|         [Attribute] | ||||
|         ushort Port | ||||
|         { | ||||
|             get; | ||||
|             set; | ||||
|         } | ||||
|  | ||||
|         public bool Trigger(ResourceTrigger trigger) | ||||
|         { | ||||
|             if (trigger == ResourceTrigger.Initialize) | ||||
|             { | ||||
|                 var address = ip == null ? IPAddress.Any : IPAddress.Parse(ip); | ||||
|  | ||||
|                 Udp = new UdpClient(new IPEndPoint(address, (int)port)); | ||||
|                 Receiver = new Thread(Receiving); | ||||
|                 Receiver.Start(); | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.Terminate) | ||||
|             { | ||||
|                 if (Receiver != null) | ||||
|                     Receiver.Abort(); | ||||
|             } | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         private void Receiving() | ||||
|          private void Receiving() | ||||
|         { | ||||
|             IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0); | ||||
|              | ||||
|  | ||||
|  | ||||
|             while (true) | ||||
|             { | ||||
|                 byte[] b = Udp.Receive(ref ep); | ||||
|                 byte[] b = udp.Receive(ref ep); | ||||
|  | ||||
|                 foreach(var child in Instance.Children) | ||||
|                 foreach (var child in filters) | ||||
|                 { | ||||
|                     var f = child as UDPFilter; | ||||
|  | ||||
| @@ -119,7 +102,7 @@ namespace Esyur.Net.UDP | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 Udp.Send(Data, Count, EP); | ||||
|                 udp.Send(Data, Count, EP); | ||||
|                 return true; | ||||
|             } | ||||
|             catch | ||||
| @@ -131,7 +114,7 @@ namespace Esyur.Net.UDP | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 Udp.Send(Data, Data.Length, EP); | ||||
|                 udp.Send(Data, Data.Length, EP); | ||||
|                 return true; | ||||
|             } | ||||
|             catch | ||||
| @@ -143,7 +126,7 @@ namespace Esyur.Net.UDP | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 Udp.Send(Data, Count, Host, Port); | ||||
|                 udp.Send(Data, Count, Host, Port); | ||||
|                 return true; | ||||
|             } | ||||
|             catch | ||||
| @@ -155,7 +138,7 @@ namespace Esyur.Net.UDP | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 Udp.Send(Data, Data.Length, Host, Port); | ||||
|                 udp.Send(Data, Data.Length, Host, Port); | ||||
|                 return true; | ||||
|             } | ||||
|             catch | ||||
| @@ -167,7 +150,7 @@ namespace Esyur.Net.UDP | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 Udp.Send(Encoding.Default.GetBytes(Data), Data.Length, EP); | ||||
|                 udp.Send(Encoding.Default.GetBytes(Data), Data.Length, EP); | ||||
|                 return true; | ||||
|             } | ||||
|             catch | ||||
| @@ -179,7 +162,7 @@ namespace Esyur.Net.UDP | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 Udp.Send(Encoding.Default.GetBytes(Data), Data.Length, Host, Port); | ||||
|                 udp.Send(Encoding.Default.GetBytes(Data), Data.Length, Host, Port); | ||||
|                 return true; | ||||
|             } | ||||
|             catch | ||||
| @@ -190,8 +173,32 @@ namespace Esyur.Net.UDP | ||||
|  | ||||
|         public void Destroy() | ||||
|         { | ||||
|             Udp.Close(); | ||||
|             udp.Close(); | ||||
|             OnDestroy?.Invoke(this); | ||||
|         } | ||||
|  | ||||
|         async AsyncReply<bool> IResource.Trigger(ResourceTrigger trigger) | ||||
|         { | ||||
|             if (trigger == ResourceTrigger.Initialize) | ||||
|             { | ||||
|                 var address = IP == null ? IPAddress.Any : IPAddress.Parse(IP); | ||||
|  | ||||
|                 udp = new UdpClient(new IPEndPoint(address, Port)); | ||||
|  | ||||
|                 receiver = new Thread(Receiving); | ||||
|                 receiver.Start(); | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.Terminate) | ||||
|             { | ||||
|                 if (receiver != null) | ||||
|                     receiver.Abort(); | ||||
|             } | ||||
|             else if (trigger == ResourceTrigger.SystemInitialized) | ||||
|             { | ||||
|                 filters = await Instance.Children<UDPFilter>(); | ||||
|             } | ||||
|  | ||||
|             return true; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user