كود:
namespace ElSaher.Network.Sockets
{
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using ElSaher;
public delegate void NetworkClientConnection(ClientWrapper client);
public delegate void NetworkClientReceive(Byte[] buffer, Int32 length, ClientWrapper client);
public class ServerSocket
{
public Socket Socket { get; private set; }
public IPEndPoint LocalEndPoint { get; private set; }
public String Name { get; private set; }
public NetworkClientConnection OnConnect;
public NetworkClientReceive OnReceive;
public NetworkClientConnection OnDisconnect;
public BruteForceAttackProtection AttackProtector;
public Int32 ClientBufferSize;
public ServerSocket(String name, UInt32 maximum, UInt32 banTime)
{
Name = name;
AttackProtector = new BruteForceAttackProtection(maximum, banTime);
}
/*
The Set IP Protection Level m*ethod enables restricting an a IPv6 or IP socket to listen on a specified scope, such as addresses with the same link local or site local prefix. This socket option enables applications to place access restrictions on IPv6 or IP sockets. Such restrictions enable an application running on a private LAN to simply and robustly harden itself against external attacks. This socket option can also be used to remove access restrictions if the level parameter is set to Unrestricted. This socket option widens or narrows the scope of a listening socket, enabling unrestricted access from public and private users when appropriate, or restricting access only to the same site, as required.
This socket option has defined protection levels specified in the IPProtectionLevel enumeration.
The SetIPProtectionLevel m*ethod is used to enable or disable Network Address Traversal (NAT) for a Socket instance. NAT traversal may be provided using Teredo, 6to4, or an ISATAP tunnel.
When the level parameter is set to EdgeRestricted, or Restricted, this explicitly disables NAT traversal for a Socket instance.
When the level parameter is set to EdgeRestricted, this may allow NAT traversal for a Socket depending on firewall rules in place on the system.
*/
public void Prepare(int port, IPProtectionLevel protectionLevel)
{
LocalEndPoint = new IPEndPoint(IPAddress.Any, port);
Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket.Bind(LocalEndPoint);
Socket.SetIPProtectionLevel(protectionLevel);
Socket.NoDelay = true;
Socket.Listen((int)SocketOptionName.MaxConnections);
}
public void BeginAccept()
{
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false);
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
Socket.BeginAccept(Accept, null);
}
private void Accept(IAsyncResult result)
{
Socket clientSocket;
try
{
clientSocket = Socket.EndAccept(result);
}
catch (SocketException)
{
BeginAccept();
return;
}
if (AttackProtector.Authenticate(clientSocket))
{
clientSocket.ReceiveBufferSize = ClientBufferSize;
var client = new ClientWrapper(this, clientSocket, ClientBufferSize);
InvokeOnConnect(client);
client.BeginReceive();
}
else
clientSocket.Disconnect(false);
BeginAccept();
}
public void InvokeOnConnect(ClientWrapper client)
{
if (OnConnect != null) OnConnect(client);
}
public void InvokeOnReceive(Byte[] buffer, Int32 length, ClientWrapper client)
{
if (OnReceive != null) OnReceive(buffer, length, client);
}
public void InvokeOnDisconnect(ClientWrapper client)
{
if (!client.IsAlive) return;
// client.IsAlive = false;
if (OnDisconnect != null) OnDisconnect(client);
}
}
}
كود:
namespace ElSaher.Network.Sockets
{
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using ElSaher;
using System.Net;
public class ClientWrapper
{
public Socket Socket { get; private set; }
public ServerSocket Server { get; private set; }
public IPEndPoint RemoteEndPoint { get; private set; }
private readonly byte[] _buffer;
public object Owner;
public Boolean IsAlive { get { return Socket.Connected; } }
public String IP
{
get
{
if (Socket != null)
return (Socket.RemoteEndPoint as IPEndPoint).Address.ToString();
else
return null;
}
}
public ClientWrapper(ServerSocket server, Socket socket, Int32 bufferLength)
{
//IsAlive = true;
Server = server;
Socket = socket;
_buffer = new byte[bufferLength];
RemoteEndPoint = (IPEndPoint)Socket.RemoteEndPoint;
Socket.NoDelay = true;
}
public void BeginReceive()
{
try
{
Socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(Receive), null);
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
private void Receive(IAsyncResult result)
{
if (Socket != null)
{
try
{
SocketError error;
Int32 length = Socket.EndReceive(result, out error);
if (IsAlive && error == SocketError.Success)
{
if (length > 0)
{
try
{
Server.InvokeOnReceive(_buffer, length, this);
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
}
finally
{
BeginReceive();
}
}
else
{
Server.InvokeOnDisconnect(this);
}
}
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
}
public void Send(byte[] packet)
{
if (IsAlive)
{
try
{
Socket.BeginSend(packet, 0, packet.Length, SocketFlags.None, new AsyncCallback(EndSend), null);
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
}
private void EndSend(IAsyncResult result)
{
try
{
Socket.EndSend(result);
}
catch (SocketException)
{
Server.InvokeOnDisconnect(this);
}
}
public void Disconnect()
{
try
{
Socket.Disconnect(false);
}
catch (SocketException)
{
}
Server.InvokeOnDisconnect(this);
}
public override string ToString()
{
return RemoteEndPoint.ToString();
}
}
}