Register for your free account! | Forgot your password?

You last visited: Today at 07:56

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

Advertisement



Sockets

Discussion on Sockets within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

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

Socket Client
PHP Code:
public class SocketClient
    
{
        public 
SocketClient(Socket socket)
        {
            
Socket socket
        }

        public 
Socket Socket;
        public 
string IP
        
{
            
get
            
{
                try
                {
                    return (
Socket.RemoteEndPoint as IPEndPoint).Address.ToString();
                }
                catch { return 
""; }
            }
        }
        public 
void Disconnect(bool reUse)
        {
            try
            {          
                
Socket.Close();
                
Socket.Disconnect(reUse);
            }
            catch (
Exception e)
            {
                
Server.ErrorHandler.Handle(e);
            }
        }

        public 
byte[] Buffer;
        public 
object Owner;
        public 
string AuthAccount;
        public 
string AuthPassword;

        public 
void EndAccept(Socket socketIAsyncResult result)
        {
            
Socket socket.EndAccept(result);
        }
        public 
bool Connected
        
{
            
get { return Socket.Connected; }
        }
        public 
int EndReceive(IAsyncResult resultout SocketError SE)
        {
            return 
Socket.EndReceive(resultout SE);
        }

        public 
ConquerCryption Cryption;

        public 
unsafe void Send(byte[] Packet)
        {
            try
            {
                if (
Socket.Connected)
                {
                    
byte[] Data = new byte[Packet.Length];
                    
fixed (bytesrc Packetdest Data)
                        
NativeImports.MSVCRT.MEMCPY(destsrcData.Length);
                    
Cryption.Encrypt(Data);
                    
Socket.Send(Data);
                }
            }
            catch (
Exception e)
            {
                
Server.ErrorHandler.Handle(e);
            }
        }
    } 
Socket Class:
PHP Code:
public unsafe class ConquerSockets
    
{
        
Socket socket;
        
int port;
        
IPEndPoint IPE;
        
SocketInvoke InvokeHandler;
        public 
ConquerSockets(int PortSocketInvoke _InvokeHandler)
        {
            
port Port;
            
InvokeHandler _InvokeHandler;
        }
        public 
void Listen()
        {
            
socket = new Socket(AddressFamily.InterNetworkSocketType.StreamProtocolType.Tcp);
            
IPE = new IPEndPoint(IPAddress.Parse(Kernel.ServerIP), port);
            
socket.Bind(IPE);
            
socket.Listen(100);
        }

        public 
void Accept()
        {
            
socket.BeginAccept(new AsyncCallback(Accept), new SocketClient(null));
        }
        
void Accept(IAsyncResult result)
        {
            try
            {
                
SocketClient Client result.AsyncState as SocketClient;
                
Client.Buffer = new byte[1024];

                if (!
Accepted(Clientresult))
                    
Accept();

                
InvokeHandler.Invoke(ClientConquerServerProject.Enums.SocketInvokeType.Connectionnull);
                
Accept();
                
Receive(Client);
            }
            catch { }
        }

        public 
void Receive(SocketClient Client)
        {
            
Client.Socket.BeginReceive(Client.Buffer01024SocketFlags.None, new AsyncCallback(Receive), Client);
        }
        
void Receive(IAsyncResult result)
        {
            try
            {
                
SocketClient Client result.AsyncState as SocketClient;
                try
                {

                    switch (
Client.Connected)
                    {
                        case 
true:
                            {
                                
SocketError error;
                                
int Len Client.EndReceive(resultout error);

                                switch (
error)
                                {
                                    case 
SocketError.Success:
                                        {
                                            switch (
Len)
                                            {
                                                case 
0:
                                                    
InvokeHandler.Invoke(ClientEnums.SocketInvokeType.Disconnectionnull);
                                                    break;

                                                default:
                                                    {
                                                       if (
Len 0)//Avoid negative.
                                                       
{
                                                        
byte[] Received = new byte[Len];
                                                        
MEMCPY(ReceivedClient.BufferLen);
                                                        
InvokeHandler.Invoke(ClientEnums.SocketInvokeType.ReceiveReceived);
                                                        
Receive(Client);
                                                        }
                                                       break;
                                                    }
                                            }
                                            break;
                                        }
                                }
                                break;
                            }

                        case 
false:
                            {
                                
InvokeHandler.Invoke(ClientEnums.SocketInvokeType.Disconnectionnull);
                                break;
                            }
                    }
                }
                catch
                {
                    
InvokeHandler.Invoke(ClientEnums.SocketInvokeType.Disconnectionnull);
                }
            }
            catch (
Exception e) { Server.ErrorHandler.Handle(e); }
        }

        
bool Accepted(SocketClient ClientIAsyncResult result)
        {
            try
            {
                
Client.EndAccept(socketresult);
                return 
true;
            }
            catch
            {
                return 
false;
            }
        }

        
void MEMCPY(byte[] Receivedbyte[] Bufferint Len)
        {
            
fixed (bytepointer Receivedbuffer Buffer)
                
MSVCRT.MEMCPY(pointerbufferLen);
        }
    } 
Socket Events:
PHP Code:
    public delegate void Connection(SocketClient Client);
    public 
delegate void ReceivePacket(SocketClient Clientbyte[] Packet);
    public 
delegate void Disconnection(SocketClient Client);

    public class 
SocketInvoke
    
{
        
Connection con;
        
ReceivePacket rec;
        
Disconnection disc;

        public 
SocketInvoke(Connection connectionReceivePacket receiveDisconnection disconnection)
        {
            
con connection;
            
rec receive;
            
disc disconnection;
        }

        
/// <summary>
        /// Invoking socket events.
        /// </summary>
        /// <param name="Client">The socket client.</param>
        /// <param name="invoke">The socket event invoke type.</param>
        /// <param name="Packet">The packet to invoke for receive. Can be null for connection & disconnection</param>
        
public void Invoke(SocketClient ClientEnums.SocketInvokeType invokebyte[] Packet)
        {
            switch (
invoke)
            {
                case 
ConquerServerProject.Enums.SocketInvokeType.Connection:
                    {
                        if (
con != null)
                            
con.Invoke(Client);
                        break;
                    }

                case 
ConquerServerProject.Enums.SocketInvokeType.Receive:
                    {
                        if (
rec != null)
                            
rec.Invoke(ClientPacket);
                        break;
                    }

                case 
ConquerServerProject.Enums.SocketInvokeType.Disconnection:
                    {
                        if (
disc != null)
                            
disc.Invoke(Client);
                        break;
                    }
            }
        }
    } 
Network Process:
PHP Code:
public class NetworkProcess_Auth
    
{
        public static 
void ProcessConnection(SocketClient Client)
        {
        }
        public static 
void ProcessPacket(SocketClient Clientbyte[] Packet)
        {
        }

        public static 
void ProcessDisconnection(SocketClient Client)
        { 
        }
    }
public class 
NetworkProcess_Game
    
{
        public static 
void ProcessConnection(SocketClient Client)
        {
        }
        public static 
void ProcessPacket(SocketClient Clientbyte[] Packet)
        {
        }

        public static 
void ProcessDisconnection(SocketClient Client)
        { 
        }
    } 
Useage:
PHP Code:
            #region Sockets
            
ConquerSockets authsocket = new ConquerSockets(Kernel.AuthServerPort, new SocketInvoke(
                new 
Connection(Network.NetworkProcess_Auth.ProcessConnection),
                new 
ReceivePacket(Network.NetworkProcess_Auth.ProcessPacket),
                new 
Disconnection(Network.NetworkProcess_Auth.ProcessDisconnection)));
            
authsocket.Listen();
            
authsocket.Accept();

            
ConquerSockets gamesocket = new ConquerSockets(Kernel.GameServerPort, new SocketInvoke(
                new 
Connection(Network.NetworkProcess_Game.ProcessConnection),
                new 
ReceivePacket(Network.NetworkProcess_Game.ProcessPacket),
                new 
Disconnection(Network.NetworkProcess_Game.ProcessDisconnection)));
            
gamesocket.Listen();
            
gamesocket.Accept();
            
#endregion 
Native Import:
PHP Code:
    public unsafe class MSVCRT
    
{
        [
DllImport("msvcrt.dll"EntryPoint "memcpy"CallingConvention CallingConvention.CdeclSetLastError false)]
        public static 
extern unsafe voidMEMCPY(voiddestvoidsrcint size);
    } 
Enumerator:
PHP Code:
    public enum SocketInvokeType
    
{
        
Connection,
        
Receive,
        
Disconnection
    

Figure out how to use it yourself.

Help:


BaussHacker is offline  
Thanks
3 Users
Old 07/20/2011, 23:32   #2


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Your receive method needs completely redesigning....
Korvacs is offline  
Thanks
1 User
Old 07/20/2011, 23:53   #3
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Korvacs View Post
Your receive method needs completely redesigning....
I know, that's why I left this as it was. but you could at least point to what's wrong etc.
BaussHacker is offline  
Old 07/21/2011, 00:03   #4
 
12tails's Avatar
 
elite*gold: 0
Join Date: Apr 2009
Posts: 782
Received Thanks: 458
its a great start for who wants to learn at last.... good job ;D
12tails is offline  
Thanks
1 User
Old 07/21/2011, 00:08   #5
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by 12tails View Post
its a great start for who wants to learn at last.... good job ;D
Yea and thank you.
BaussHacker is offline  
Old 07/21/2011, 11:32   #6


 
KraHen's Avatar
 
elite*gold: 0
Join Date: Jul 2006
Posts: 2,216
Received Thanks: 794
First of all the switches don`t make any sense.
KraHen is offline  
Old 07/21/2011, 11:39   #7


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Quote:
Originally Posted by BaussHacker View Post
I know, that's why I left this as it was. but you could at least point to what's wrong etc.
2 try/catch statements in the receive function where checks would do, and switches where if statements will do to name the most obvious.
Korvacs is offline  
Thanks
1 User
Old 07/21/2011, 12:22   #8
 
Mr_PoP's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 759
Received Thanks: 285
why do you use unsafe anyways!? your not using C/C++ lol, there is no point in using unsafe in C# unless you really needs it 0.0
Mr_PoP is offline  
Old 07/21/2011, 12:35   #9


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Quote:
Originally Posted by Y u k i View Post
Every bit counts. Thats why therer is unsafe code and switches in it. Acctually its faster that way
The compiler will decide if a switch or if/else try is faster at compile time, and in this case i can almost guarantee that an if/else will be the fastest method because there aren't enough values to justify a switch.
Korvacs is offline  
Thanks
1 User
Old 07/21/2011, 12:48   #10
 
Mr_PoP's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 759
Received Thanks: 285
unsafe code it's too messy in c#, and if you are using it because you think you're getting better performance ,in reality, while it might be better, it's not noticeably better, it's a hell of a lot worse to maintain though.

and don't use memcpy on managed memory :P , anyways it's seems OK but try to avoid any unsafe code in you're C# source
Mr_PoP is offline  
Old 07/21/2011, 18:58   #11


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Quote:
Originally Posted by Y u k i View Post
Its not like i dont belive you, but where do you get your informations from? Valid ressoruces would help me learn indeed.
Research, the internet, msdn, google, stackoverflow, just search what it is you want to read up on and perform tests for yourself to confirm what you have read.
Korvacs is offline  
Old 07/21/2011, 19:32   #12
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Korvacs View Post
Research, the internet, msdn, google, stackoverflow, just search what it is you want to read up on and perform tests for yourself to confirm what you have read.
Code project is also good, but Stackoverflow is my favorite. It has a lot good things.
BaussHacker is offline  
Old 07/22/2011, 02:08   #13
 
hady's Avatar
 
elite*gold: 0
Join Date: Dec 2006
Posts: 72
Received Thanks: 9
thank you for your release
hady is offline  
Old 07/26/2011, 12:36   #14
 
elite*gold: 0
Join Date: Jul 2011
Posts: 1
Received Thanks: 0
plz can any one told me haw to use it ?
kinggame201 is offline  
Old 07/26/2011, 19:14   #15
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by kinggame201 View Post
plz can any one told me haw to use it ?
You put it in your source. If you don't know how to use it, then you will probably not be able to use it.
BaussHacker is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
Sockets first to log in after SM
03/14/2007 - Conquer Online 2 - 13 Replies
If i was the first to log in after SM will the 1st met be the socket? or is there a certian amount i would have to spam? Thanks :) cheers i have quit this game for a while but I am back now. :P
Sockets in CO?
03/16/2006 - Conquer Online 2 - 14 Replies
I was reading through the socket thread on the other forum and such... I know a few people who are 100 pct on making 1 socket and around 90 pct on the 2nd socket... So I am wondering if there is a way to make it happen without a chance of failure? I know that the Conquer Online Beta Client (the old old one) you can make sockets without worry on anything with that client... Does anyone know where to get it or know someone who'd upload it to a server? Or just knows a fool proof way to...
Sockets
03/01/2006 - Conquer Online 2 - 35 Replies
Hi there everyone, just like to post what i know about socket making in TC. I don't post much unless I know what i am talking about. OK I tried pretty much every theory that there is arround about making a socket, I spammed 400 mets, I followed the xx:33 theory for over a month and had no success. But then out of know where I get 2 sockets in one week and another 1 this week by following a simple procedure, ok here it is: 1. Get 20 mets (If 20 mets can't get a socket, I don't think...
HELP WITH SOCKETS!
07/06/2005 - Conquer Online 2 - 6 Replies
Hi again, i was thinking about quiting the game but i dicided to try to make sockets in every thing so i sold all my equip(made 60mil) bought low lvl supers/unique/elite and dbs.IM a trojan :D. my boots,necky and attack ring was unique LOW lvl and i gave it all dbs to get super and trojan armor lvl 15 was elite and i made it super and i bought a low lvl super cornet. Today i gave my ring 65 mets and ring reach lvl 80 by doing so no socket :cry: . .any one no the best way i and get sockets.PLZ...



All times are GMT +1. The time now is 07:57.


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.