Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 14:30

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Async Socket Wrapper

Discussion on Async Socket Wrapper within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Async Socket Wrapper

So I was bored and thought I would code a socket wrapper. It's a class library, but full project is available for download + an example use for a socket server.

It contains both wrapper for server and client stuff, which means it can be used for either private servers as server-socket or for a proxy.

WinAsync - This class contains all the events.

Code:
    public delegate void WinEvent(WinClient wClient);
    public delegate void WinBufferEvent(WinClient wClient, WinBuffer Buffer);
    public delegate void WinExceptionEvent(Exception Error);

    public class WinAsync
    {
        public WinEvent OnConnection;
        public WinBufferEvent OnReceive;
        public WinEvent OnDisconnection;
        public WinExceptionEvent OnError;
    }
WinBuffer - This class contains the buffer, which is send through the sockets.

Code:
    public class WinBuffer
    {
        public byte[] Buffer;

        public void Modify<T>(T Value, int Offset)
        {
            #region UInt8
            if (typeof(T) == typeof(byte))
            {
                object t = Value;
                Buffer[Offset] = (byte)t;
            }
            #endregion
            #region UInt16
            else if (typeof(T) == typeof(ushort))
            {
                object t = Value;
                ushort value = (ushort)t;
                Buffer[Offset] = (byte)(value);
                Buffer[Offset + 1] = (byte)(value >> 8);
            }
            #endregion
            #region UInt32
            else if (typeof(T) == typeof(uint))
            {
                object t = Value;
                uint value = (uint)t;
                Buffer[Offset] = (byte)(value);
                Buffer[Offset + 1] = (byte)(value >> 8);
                Buffer[Offset + 2] = (byte)(value >> 16);
                Buffer[Offset + 3] = (byte)(value >> 24);
            }
            #endregion
            #region UInt64
            else if (typeof(T) == typeof(ulong))
            {
                object t = Value;
                ulong value = (ulong)t;
                Buffer[Offset] = (byte)(value);
                Buffer[Offset + 1] = (byte)(value >> 8);
                Buffer[Offset + 2] = (byte)(value >> 16);
                Buffer[Offset + 3] = (byte)(value >> 24);
                Buffer[Offset + 4] = (byte)(value >> 32);
                Buffer[Offset + 5] = (byte)(value >> 40);
                Buffer[Offset + 6] = (byte)(value >> 48);
                Buffer[Offset + 7] = (byte)(value >> 56);
            }
            #endregion
        }

        public string ToHex()
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in Buffer)
                sb.Append(b.ToString("X2"));
            return sb.ToString();
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in Buffer)
            {
                sb.Append(b);
                sb.Append(" ");
            }
            sb.Length -= 1;
            return sb.ToString();
        }

        public static implicit operator WinBuffer(byte[] buffer)
        {
            return new WinBuffer() { Buffer = buffer };
        }
        public static implicit operator byte[](WinBuffer buffer)
        {
            return buffer.Buffer;
        }
    }
WinClCon - This class contains the wrapper for a client-socket.

Code:
    public class WinClCon : WinSocWrapper
    {
        public WinClCon(int BufferSize)
            : base(BufferSize)
        {
        }

        private void BeginConnect()
        {
            base.Network.Socket.BeginConnect(base.Network, new AsyncCallback(Connect_Callback), new WinClient());
        }

        public void Connect_Callback(IAsyncResult result)
        {
            try
            {
                WinClient wClient = base.Get(result, true);
                wClient.Socket.EndConnect(result);

                base.AsyncEvents.OnConnection.Invoke(wClient);

                wClient.Socket.BeginReceive(wClient.Buffer, 0, wClient.BufferLen, 0, new AsyncCallback(Receive_Callback), wClient);
            }
            catch (Exception Error)
            {
                base.AsyncEvents.OnError.Invoke(Error);
            }
        }
        public void Receive_Callback(IAsyncResult result)
        {
            try
            {
                WinClient wClient = base.Get(result, false);
                int BufferLength = wClient.Socket.EndReceive(result);
                byte[] rBuffer = new byte[BufferLength];
                Buffer.BlockCopy(wClient.Buffer, 0, rBuffer, 0, BufferLength);

                base.AsyncEvents.OnReceive.Invoke(wClient, rBuffer);

                wClient.Socket.BeginReceive(wClient.Buffer, 0, wClient.BufferLen, 0, new AsyncCallback(Receive_Callback), wClient);
            }
            catch (Exception Error)
            {
                base.AsyncEvents.OnError.Invoke(Error);
            }
        }
    }
WinClient - This class contains the socket-client, which the wrappers will use. It can either be the server or the client, depending on if it's a client or a server.

Code:
    public class WinClient
    {
        public System.Net.Sockets.Socket Socket;
        public WinBuffer Buffer;
        public int BufferLen
        {
            get
            {
                return Buffer.Buffer.Length;
            }
        }
        public string IP
        {
            get
            {
                try
                {
                    return (this.Socket.RemoteEndPoint as System.Net.IPEndPoint).Address.ToString();
                }
                catch { return "127.0.0.1"; }
            }
        }
        public bool Connected
        {
            get { return Socket.Connected; }
        }
        public void Disconnect()
        {
            Socket.Disconnect(false);
        }
        public void Send(WinBuffer Buffer)
        {
            byte[] sBuffer = new byte[Buffer.Buffer.Length];
            System.Buffer.BlockCopy(Buffer, 0, sBuffer, 0, Buffer.Buffer.Length);
            Socket.Send(sBuffer);
        }
        public void EndAccept(System.Net.Sockets.Socket socket, IAsyncResult result)
        {
            Socket = socket.EndAccept(result);
        }
        public int EndReceive(IAsyncResult result, out System.Net.Sockets.SocketError SE)
        {
            return Socket.EndReceive(result, out SE);
        }
    }
WinNetwork - This class contains network information and the global socket for each wrapper.

Code:
    public class WinNetwork
    {
        public System.Net.Sockets.Socket Socket;
        public string Address;
        public int Port;

        public void Bind()
        {
            Socket.Bind(this);
        }
        public void Connect()
        {
            Socket.Connect(this);
        }

        public static implicit operator System.Net.IPEndPoint(WinNetwork Network)
        {
            return new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Network.Address), Network.Port);
        }
    }
WinSocWrapper - This class contains the wrapper both the server and the client socket wrappers are using.

Code:
    public enum WinProtocol : byte
    {
        TCP = 0,
        UDP = 1
    }

    public class WinSocWrapper
    {
        public WinAsync AsyncEvents;
        public WinNetwork Network;
        public int BufferSize;

        public WinSocWrapper(int BufferSize)
        {
            this.BufferSize = BufferSize;
        }

        public void DisconnectSocket()
        {
            Network.Socket.Disconnect(false);
        }
        public void Listen(WinProtocol Protocol, int Backlog)
        {
            if (Protocol == WinProtocol.TCP)
            {
                Network.Socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                    System.Net.Sockets.SocketType.Stream,
                    System.Net.Sockets.ProtocolType.Tcp);
            }
            else
            {
                Network.Socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                    System.Net.Sockets.SocketType.Stream,
                    System.Net.Sockets.ProtocolType.Udp);
            }

            Network.Bind();
            Network.Socket.Listen(Backlog);
        }

        protected WinClient Get(IAsyncResult result, bool retnull)
        {
            try
            {
                return result.AsyncState as WinClient;
            }
            catch (Exception Error)
            {
                if (!retnull)
                    this.AsyncEvents.OnError.Invoke(Error);
                return null;
            }
        }
    }
WinSvCon - This class contains the wrapper for a server-socket.

Code:
    public class WinSvCon : WinSocWrapper
    {
        public WinSvCon(int BufferSize)
            : base(BufferSize)
        {
        }

        public void BeginAccept()
        {
            base.Network.Socket.BeginAccept(new AsyncCallback(Accept_Callback), new WinClient());
        }

        public void BeginReceive(WinClient client)
        {
            client.Socket.BeginReceive(client.Buffer, 0, base.BufferSize, System.Net.Sockets.SocketFlags.None, new AsyncCallback(Receive_Callback), client);
        }

        public void Accept_Callback(IAsyncResult result)
        {
            try
            {
                WinClient wClient = base.Get(result, true);
                wClient.Buffer = new byte[base.BufferSize];

                bool accepted;
                if ((accepted = Accepted(wClient, result)) == true)
                    AsyncEvents.OnConnection.Invoke(wClient);

                BeginAccept();

                if (accepted)
                    BeginReceive(wClient);
            }
            catch { }
        }
        private bool Accepted(WinClient wClient, IAsyncResult result)
        {
            try
            {
                wClient.EndAccept(base.Network.Socket, result);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Receive_Callback(IAsyncResult result)
        {
            WinClient wClient = base.Get(result, false);
            if (wClient != null)
            {
                try
                {
                    if (wClient.Connected)
                    {
                        System.Net.Sockets.SocketError error;
                        int BufferLength = wClient.EndReceive(result, out error);

                        if (error == System.Net.Sockets.SocketError.Success)
                        {
                            if (BufferLength > 0)
                            {
                                byte[] rBuffer = new byte[BufferLength];
                                Buffer.BlockCopy(wClient.Buffer, 0, rBuffer, 0, wClient.BufferLen);
                                base.AsyncEvents.OnReceive.Invoke(wClient, rBuffer);
                                BeginReceive(wClient);
                                return;
                            }
                        }
                    }

                    base.AsyncEvents.OnDisconnection.Invoke(wClient);
                }
                catch
                {
                    base.AsyncEvents.OnDisconnection.Invoke(wClient);
                }
            }
            else
            {
                base.AsyncEvents.OnDisconnection.Invoke(wClient);
            }
        }
    }
Server Example:
Code:
    class Program
    {
        static void Main(string[] args)
        {
            WinSvCon socket = new WinSvCon(1024);//Creating a new socket with the default buffersize as 1024

            socket.AsyncEvents = new WinAsync();//Creating a new asyncevent class
            /* Setting the events */
            socket.AsyncEvents.OnConnection = new WinEvent(NewConnection);
            socket.AsyncEvents.OnReceive = new WinBufferEvent(NewReceive);
            socket.AsyncEvents.OnDisconnection = new WinEvent(NewDisconnection);
            socket.AsyncEvents.OnError = new WinExceptionEvent(NewError);

            socket.Network = new WinNetwork();//Creating a new network
            socket.Network.Address = "127.0.0.1";//The network address
            socket.Network.Port = 4444;//The network port

            socket.Listen(WinProtocol.TCP, 100);//Starting to listen connections from a tcp protocol with the backlog 100
            socket.BeginAccept();//Beginning to accept connections

            Console.WriteLine("Server has started...");

            while (true)
                Console.ReadLine();
        }

        static void NewConnection(WinClient wClient)
        {
            Console.WriteLine("New connection from {0}.", wClient.IP);
        }
        static void NewReceive(WinClient wClient, WinBuffer Buffer)
        {
            Console.WriteLine("New data from {0}. Length: {1}.", wClient.IP, Buffer.Buffer.Length);
            wClient.Send(Buffer);
            Console.WriteLine("Send data back to client...");
        }
        static void NewDisconnection(WinClient wClient)
        {
            if (wClient != null)
            {
                Console.WriteLine("New disconnection from {0}.", wClient.IP);
            }
            else
            {
                Console.WriteLine("New disconnection from unknown client.");
            }
        }
        static void NewError(Exception Error)
        {
            Console.WriteLine(Error.ToString());
        }
    }


Ref:

BaussHacker is offline  
Thanks
1 User
Old 10/24/2011, 11:23   #2


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Should be noted that despite the heavy use of WinSoc, which i interpreted to mean WinSock, it infact is entirely managed and never touches WinSock.
Korvacs is offline  
Old 10/24/2011, 11:26   #3
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Korvacs View Post
Should be noted that despite the heavy use of WinSoc, which i interpreted to mean WinSock, it infact is entirely managed and never touches WinSock.
Just my namespace.
BaussHacker is offline  
Old 10/24/2011, 11:27   #4
 
elite*gold: 20
Join Date: Aug 2007
Posts: 1,749
Received Thanks: 2,199
Looks nice, I don't quite understand the whole
Code:
public void Modify<T>(T Value, int Offset)
though. Why not just make more overloaded methods which take different types of input, instead of checking the type of the parameter on every call?

Code:
public void Modify(byte value, int Offset)
public void Modify(uint value, int Offset)
public void Modify(ushort value, int Offset)

..etc
IAmHawtness is offline  
Thanks
1 User
Old 10/24/2011, 11:29   #5
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by IAmHawtness View Post
Looks nice, I don't quite understand the whole
Code:
public void Modify<T>(T Value, int Offset)
though. Why not just make more overloaded methods which take different types of input, instead of checking the type of the parameter on every call?

...
I actually don't know :P But either way, it's an open source now, so if people want to make changes and improvements they can go ahead.
BaussHacker is offline  
Old 10/24/2011, 13:05   #6
 
Chalkie's Avatar
 
elite*gold: 0
Join Date: Nov 2008
Posts: 288
Received Thanks: 197
hw i usa thas aimpoot?
Chalkie is offline  
Old 10/24/2011, 13:16   #7
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Chalkie View Post
hw i usa thas aimpoot?
Code:
void impoot(bool festblade)
{
blayer noob = getblyerframscren();


while (inscren(noob))
{
if (festblade)
useskell(1045, noob);
else
useskell(1046, noob);
}

}
BaussHacker is offline  
Thanks
2 Users
Old 10/24/2011, 17:49   #8
 
nTL3fTy's Avatar
 
elite*gold: 0
Join Date: Jun 2005
Posts: 692
Received Thanks: 353
Quote:
Originally Posted by BaussHacker View Post
Code:
public void Modify<T>(T Value, int Offset)
For this method, instead of using all those typeof calls, you can switch on Type.GetTypeCode(typeof(T)), which returns a TypeCode enum.

Code:
switch (Type.GetTypeCode(typeof(T))
{
    case TypeCode.Boolean: ...
    case TypeCode.Byte: ...
}
You can also use a mixture of the two:
Code:
if (typeof(T).IsEnum) { ... }

switch (Type.GetTypeCode(typeof(T))
{
    ...
}
nTL3fTy is offline  
Thanks
2 Users
Old 11/11/2011, 08:18   #9
 
elite*gold: 0
Join Date: Jan 2007
Posts: 118
Received Thanks: 20
I think my socket wrapper in my siggy is simpler than this one.
xmen01235 is offline  
Reply


Similar Threads Similar Threads
My Socket Wrapper With Multi Threading and Events Similar to Classic Winsock
10/25/2010 - CO2 Programming - 9 Replies
Hi guys to those who are planning to make their own proxy and want to have a socket wrapper which is similar to classic winsock then this class below is the one you are looking. I created a multi threading socket of System.Net.Sockets. I also created the events similar to classic winsock such as OnConnectionRequest, OnDisconnection, OnConnect, OnDataArrival, OnSendComplete and OnError. Imports System.Net.Sockets Imports System.Net Imports System.Text Imports System.Threading
au3 wrapper
12/23/2008 - Guild Wars - 11 Replies
hi ganz dumme fragen^^ wo gibst den neuen au3 wrapper zum download bei google habe ich ncihts gefunden oder ich bin einfach zu blöd zum richtig googlen kann mir wer weiter helfen??
Empty wrapper help
01/18/2008 - WoW PServer Exploits, Hacks & Tools - 1 Replies
I play on wowfusion, the only wrapping paper i can get is the empty wrapper. But everytime i try to wrap something i get : Wrapped item can't be wrap. What can i do? I tryed a lot of item and i always get this same message. I tryed slot switching , other bags and ... Any help would be appreciated. Thx



All times are GMT +1. The time now is 14:32.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.