Kindly make this code better guys. I want a port refresher that uses less Cpu usage.
or can anyone help me make a code that will check if the port is disable ?
sample if the port is disabled the code will run to enable it automatically.
something like this.
Code:
static void Port_Refresher() { while (true) { { AuthServer.Disable(); AuthServer = null; AuthServer = new AsyncSocket(AuthPort); AuthServer.OnClientConnect += new Action<Interfaces.ISocketWrapper>(AuthServer_AnnounceNewConnection); AuthServer.OnClientReceive += new Action<byte[], Interfaces.ISocketWrapper>(AuthServer_AnnounceReceive); AuthServer.OnClientDisconnect += new Action<Interfaces.ISocketWrapper>(AuthServer_AnnounceDisconnection);//loads all GameServer.Disable(); GameServer = null; GameServer = new AsyncSocket(GamePort); GameServer.OnClientConnect += new Action<Interfaces.ISocketWrapper>(GameServer_AnnounceNewConnection); GameServer.OnClientReceive += new Action<byte[], Interfaces.ISocketWrapper>(GameServer_AnnounceReceive); GameServer.OnClientDisconnect += new Action<Interfaces.ISocketWrapper>(GameServer_AnnounceDisconnection); } Thread.Sleep(60000); } }
sample if the port is disabled the code will run to enable it automatically.
something like this.
Code:
if ((AuthServer == null) || (GameServer == null)) { AuthServer = new AsyncSocket(AuthPort); AuthServer.OnClientConnect += new Action<Interfaces.ISocketWrapper>(AuthServer_AnnounceNewConnection); AuthServer.OnClientReceive += new Action<byte[], Interfaces.ISocketWrapper>(AuthServer_AnnounceReceive); AuthServer.OnClientDisconnect += new Action<Interfaces.ISocketWrapper>(AuthServer_AnnounceDisconnection); GameServer = new AsyncSocket(GamePort); GameServer.OnClientConnect += new Action<Interfaces.ISocketWrapper>(GameServer_AnnounceNewConnection); GameServer.OnClientReceive += new Action<byte[], Interfaces.ISocketWrapper>(GameServer_AnnounceReceive); GameServer.OnClientDisconnect += new Action<Interfaces.ISocketWrapper>(GameServer_AnnounceDisconnection); break; }
Quote:
This is my AsyncSocket.cs
Code:
namespace Co2.Network.Sockets { using Co2; using System; using System.Net; using System.Net.Sockets; using System.Threading; using Co2.Interfaces; public class AsyncSocket { private int backlog; private int clientbuffersize = 0xffff; private WinSocket Connection = new WinSocket(); private bool enabled; public bool GameServer = false; public event Action<Interfaces.ISocketWrapper> OnClientConnect; public event Action<Interfaces.ISocketWrapper> OnClientDisconnect; public event Action<byte[], Interfaces.ISocketWrapper> OnClientReceive; private ushort port; public AsyncSocket(ushort port) { if (!this.enabled) { this.port = port; this.Connection.Bind(new IPEndPoint(IPAddress.Any, this.port)); this.Connection.Listen(100); this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null); this.enabled = true; } } private void AsyncConnect(IAsyncResult res) { AsyncSocketWrapper sender = null; try { sender = new AsyncSocketWrapper(); sender.Create(this.Connection.EndAccept(res)); if (sender.Socket == null) { this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null); return; } if (this.OnClientConnect != null) { this.OnClientConnect(sender); } sender.Socket.BeginReceive(sender.Buffer, 0, sender.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), sender); this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null); } catch (SocketException e) { Program.SaveException(e); Co2.Console.WriteLine(e); if (this.enabled) { this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null); } } catch (ObjectDisposedException e) { Program.SaveException(e); Co2.Console.WriteLine(e); } } private unsafe void AsyncReceive(IAsyncResult res) { bool was = false; try { SocketError error; AsyncSocketWrapper asyncState = (AsyncSocketWrapper)res.AsyncState; int RecvSize = asyncState.Socket.EndReceive(res, out error); if ((error == SocketError.Success) && (RecvSize > 0)) { was = true; byte[] buffer = new byte[RecvSize]; for (int i = 0; i < RecvSize; i++) { buffer[i] = asyncState.Buffer[i]; } if (this.OnClientReceive != null) { this.OnClientReceive(buffer, asyncState); } asyncState.Socket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), asyncState); } else { this.InvokeDisconnect(asyncState); } } catch (SocketException e) { Program.SaveException(e); Co2.Console.WriteLine(e); if (was) { AsyncSocketWrapper asyncState = (AsyncSocketWrapper)res.AsyncState; asyncState.Socket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), asyncState); } } catch (ObjectDisposedException e) { Program.SaveException(e); Co2.Console.WriteLine(e); } catch (Exception e) { Program.SaveException(e); } } public void Disable() { if (this.enabled) { this.Connection.Disable(); this.enabled = false; } } public void Enable() { if (!this.enabled) { this.Connection.Bind(new IPEndPoint(IPAddress.Any, this.port)); this.Connection.Listen(this.backlog); this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null); this.enabled = true; } } private void enabledCheck(string Variable) { if (this.enabled) { throw new Exception("Cannot modify " + Variable + " while socket is enabled."); } } public void InvokeDisconnect(AsyncSocketWrapper Client) { if (Client != null) { try { if (Client.Socket.Connected) { Client.Socket.Shutdown(SocketShutdown.Both); Client.Socket.Close(); if (this.OnClientDisconnect != null) { this.OnClientDisconnect(Client); } Client.Connector = null; Client = null; } else { if (this.OnClientDisconnect != null) { this.OnClientDisconnect(Client); } Client.Connector = null; Client = null; } } catch (ObjectDisposedException e) { Program.SaveException(e); Co2.Console.WriteLine(e); } } } public int Backlog { get { return this.backlog; } set { this.enabledCheck("Backlog"); this.backlog = value; } } public int ClientBufferSize { get { return this.clientbuffersize; } set { this.enabledCheck("ClientBufferSize"); this.clientbuffersize = value; } } public bool Enabled { get { return this.enabled; } } public ushort Port { get { return this.port; } set { this.enabledCheck("Port"); this.port = value; } } } }
Quote:
This is my Winsocket.cs
Code:
namespace Co2.Network.Sockets { using System; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Threading; public class WinSocket { private Socket Connection; public bool Disabled; private bool disconnected; public WinSocket() { this.disconnected = false; this.Disabled = false; this.Connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public WinSocket(Socket socket) { this.disconnected = false; this.Disabled = false; this.Connection = socket; } public Socket Accept() { if (!this.Disabled) { try { if (this.disconnected) { return null; } return this.Connection.Accept(); } catch { this.Disabled = true; } } return null; } public void BeginAccept(AsyncCallback async, object state) { if (!this.Disabled) { try { if (!this.disconnected) { this.Connection.BeginAccept(async, state); } } catch { this.Disabled = true; } } } public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags flag, AsyncCallback callback, object state) { if (!this.Disabled) { try { if (!this.disconnected && this.Connected) { this.Connection.BeginReceive(buffer, offset, size, flag, callback, state); } } catch { this.Disabled = true; } } } public void Bind(EndPoint point) { if (!this.disconnected) { this.Connection.Bind(point); } } public void Close() { } public void Disable() { try { this.Disabled = true; this.Connection.Close(); } catch { this.Disabled = true; } } public void Disconnect(bool reuse) { if (!this.Disabled) { try { if (!this.disconnected) { this.disconnected = true; } else { return; } Thread.Sleep(4); this.Connection.Disconnect(false); } catch { this.Disabled = true; } } } public Socket EndAccept(IAsyncResult async) { if (!this.Disabled) { try { if (this.disconnected) { return null; } return this.Connection.EndAccept(async); } catch { this.Disabled = true; } } return null; } public int EndReceive(IAsyncResult res, out SocketError err) { err = SocketError.Disconnecting; if (!this.Disabled) { try { if (!this.Usable) { return 0; } return this.Connection.EndReceive(res, out err); } catch { this.Disabled = true; } } return 0; } public void Listen(int backlog) { if (!this.disconnected) { this.Connection.Listen(backlog); } } public int Receive(byte[] buffer) { if (this.disconnected) { return 0; } return this.Connection.Receive(buffer); } public void Send(byte[] buffer) { if (!this.Disabled) { try { if (!this.disconnected) { this.Connection.Send(buffer); } } catch { this.Disabled = true; } } } public void Shutdown(SocketShutdown type) { this.Connection.Shutdown(type); } public bool Connected { get { if (!this.Disabled) { try { if (this.disconnected) { return false; } return this.Connection.Connected; } catch { this.Disabled = true; } } return false; } } public EndPoint RemoteEndPoint { get { if (this.Disabled) { return new IPEndPoint(1L, 1); } try { return this.Connection.RemoteEndPoint; } catch { this.Disabled = true; } return new IPEndPoint(1L, 1); } } private bool Usable { get { return !this.disconnected; } } } }