|
المشاركات 1,993 |
+التقييم 1.00 |
تاريخ التسجيل Jun 2019 |
الاقامة |
نظام التشغيل ويندوز 0 |
رقم العضوية 279 |
namespace ahmedfathy.Network.Sockets
{
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using ahmedfathy;
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 method 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 method 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);
}
}
}
تاني حاجا
كلاس
ClientWrapper.cs
هتسمح كل الي فيه بردو وتحوط دا بدلهم
namespace ahmedfathy.Network.Sockets
{
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using ahmedfathy;
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();
}
public bool Alive { get; set; }
}
}
الذين يشاهدون محتوى الموضوع الآن : 1 ( الأعضاء 0 والزوار 1) | |
|
الموضوع | كاتب الموضوع | المنتدى | مشاركات | آخر مشاركة |
سوكت جديد لتظبيت البنج | ElSaher | تطوير سيرفرات كونكر | 1 | 2020-04-13 05:47 PM |
سوكت جديد لتخفيف البنج | ElSaher | تطوير سيرفرات كونكر | 1 | 2020-02-21 08:33 PM |
سلفر [Empire-War] بعد التعديل | ElSaher | اعلانات السيرفيرات الشخصية | 0 | 2019-09-08 04:52 AM |
سوكت كونكر لتخفيف البنج | محمد ياسر | تطوير سيرفرات كونكر | 4 | 2019-08-12 03:38 PM |
محتاج سوكت يظبط البنج | youssefabdelmanam | مشكلات السيرفيرات كونكر الشخصيه | 4 | 2019-07-08 05:05 PM |