bitconverter.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C02
{
public unsafe class BitConverter
{
public static ulong ToUInt64(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((ulong*)(Buffer + offset));
}
}
public static uint ToUInt32(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((uint*)(Buffer + offset));
}
}
public static int ToInt32(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((int*)(Buffer + offset));
}
}
public static ulong ReadUlong(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((ulong*)(Buffer + offset));
}
}
public static uint ReadUint(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((uint*)(Buffer + offset));
}
}
public static ushort ReadUshort(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((ushort*)(Buffer + offset));
}
}
public static ushort ToUInt16(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((ushort*)(Buffer + offset));
}
}
public static short ToInt16(byte[] buffer, int offset)
{
fixed (byte* Buffer = buffer)
{
return *((short*)(Buffer + offset));
}
}
}
}
AssyncSocket.cs
Code:
namespace C02.Network.Sockets
{
using C02;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using C02.Interfaces;
public class AsyncSocket
{
private int backlog;
private int clientbuffersize = 0xffff;
private WinSocket Connection = new WinSocket();
private bool enabled;
public bool GameServer = false;
private Action<ISocketWrapper> OnClientConnects;
private Action<ISocketWrapper> OnClientDisconnects;
private Action<byte[], ISocketWrapper> OnClientReceives;
private ushort port;
public event Action<ISocketWrapper> OnClientConnect
{
add
{
Action<ISocketWrapper> action2;
Action<ISocketWrapper> onClientConnect = this.OnClientConnects;
do
{
action2 = onClientConnect;
Action<ISocketWrapper> action3 = (Action<ISocketWrapper>)Delegate.Combine(action2, value);
onClientConnect = Interlocked.CompareExchange<Action<ISocketWrapper>>(ref this.OnClientConnects, action3, action2);
}
while (onClientConnect != action2);
}
remove
{
Action<ISocketWrapper> action2;
Action<ISocketWrapper> onClientConnect = this.OnClientConnects;
do
{
action2 = onClientConnect;
Action<ISocketWrapper> action3 = (Action<ISocketWrapper>)Delegate.Remove(action2, value);
onClientConnect = Interlocked.CompareExchange<Action<ISocketWrapper>>(ref this.OnClientConnects, action3, action2);
}
while (onClientConnect != action2);
}
}
public event Action<ISocketWrapper> OnClientDisconnect
{
add
{
Action<ISocketWrapper> action2;
Action<ISocketWrapper> onClientDisconnect = this.OnClientDisconnects;
do
{
action2 = onClientDisconnect;
Action<ISocketWrapper> action3 = (Action<ISocketWrapper>)Delegate.Combine(action2, value);
onClientDisconnect = Interlocked.CompareExchange<Action<ISocketWrapper>>(ref this.OnClientDisconnects, action3, action2);
}
while (onClientDisconnect != action2);
}
remove
{
Action<ISocketWrapper> action2;
Action<ISocketWrapper> onClientDisconnect = this.OnClientDisconnects;
do
{
action2 = onClientDisconnect;
Action<ISocketWrapper> action3 = (Action<ISocketWrapper>)Delegate.Remove(action2, value);
onClientDisconnect = Interlocked.CompareExchange<Action<ISocketWrapper>>(ref this.OnClientDisconnects, action3, action2);
}
while (onClientDisconnect != action2);
}
}
public event Action<byte[], ISocketWrapper> OnClientReceive
{
add
{
Action<byte[], ISocketWrapper> action2;
Action<byte[], ISocketWrapper> onClientReceive = this.OnClientReceives;
do
{
action2 = onClientReceive;
Action<byte[], ISocketWrapper> action3 = (Action<byte[], ISocketWrapper>)Delegate.Combine(action2, value);
onClientReceive = Interlocked.CompareExchange<Action<byte[], ISocketWrapper>>(ref this.OnClientReceives, action3, action2);
}
while (onClientReceive != action2);
}
remove
{
Action<byte[], ISocketWrapper> action2;
Action<byte[], ISocketWrapper> onClientReceive = this.OnClientReceives;
do
{
action2 = onClientReceive;
Action<byte[], ISocketWrapper> action3 = (Action<byte[], ISocketWrapper>)Delegate.Remove(action2, value);
onClientReceive = Interlocked.CompareExchange<Action<byte[], ISocketWrapper>>(ref this.OnClientReceives, action3, action2);
}
while (onClientReceive != action2);
}
}
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 wrapper = null;
try
{
string iPAddress = "";
wrapper = new AsyncSocketWrapper();
wrapper.Create(this.Connection.EndAccept(res));
iPAddress = wrapper.Socket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0].ToString();
if (!BruteforceProtection.IsBanned(iPAddress))
{
BruteforceProtection.AddWatch(iPAddress);
if (wrapper.Socket == null)
{
this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null);
}
else
{
if (this.OnClientConnects != null)
{
this.OnClientConnects(wrapper);
}
wrapper.Socket.BeginReceive(wrapper.Buffer, 0, wrapper.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), wrapper);
this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null);
}
}
}
catch (SocketException exception)
{
Program.SaveException(exception);
C02.Console.WriteLine(exception);
if (this.enabled)
{
this.Connection.BeginAccept(new AsyncCallback(this.AsyncConnect), null);
}
}
catch (ObjectDisposedException exception2)
{
Program.SaveException(exception2);
C02.Console.WriteLine(exception2);
}
}
private void AsyncReceive(IAsyncResult res)
{
AsyncSocketWrapper asyncState;
bool flag = false;
try
{
SocketError error;
asyncState = (AsyncSocketWrapper)res.AsyncState;
int num = asyncState.Socket.EndReceive(res, out error);
if ((error == SocketError.Success) && (num > 0))
{
flag = true;
byte[] buffer = new byte[num];
for (int i = 0; i < num; i++)
{
buffer[i] = asyncState.Buffer[i];
}
if (this.OnClientReceives != null)
{
this.OnClientReceives(buffer, asyncState);
}
asyncState.Socket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), asyncState);
}
else
{
this.InvokeDisconnect(asyncState);
}
}
catch (SocketException exception)
{
Program.SaveException(exception);
C02.Console.WriteLine(exception);
}
catch (ObjectDisposedException exception2)
{
Program.SaveException(exception2);
C02.Console.WriteLine(exception2);
}
catch (Exception exception3)
{
Program.SaveException(exception3);
if (flag)
{
asyncState = (AsyncSocketWrapper)res.AsyncState;
asyncState.Socket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.AsyncReceive), asyncState);
}
}
}
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.OnClientDisconnects != null)
{
this.OnClientDisconnects(Client);
}
Client.Connector = null;
Client = null;
}
else
{
if (this.OnClientDisconnects != null)
{
this.OnClientDisconnects(Client);
}
Client.Connector = null;
Client = null;
}
}
catch (ObjectDisposedException exception)
{
Program.SaveException(exception);
C02.Console.WriteLine(exception);
}
}
}
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;
}
}
}
}