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 socket, IAsyncResult result) { Socket = socket.EndAccept(result); } public bool Connected { get { return Socket.Connected; } } public int EndReceive(IAsyncResult result, out SocketError SE) { return Socket.EndReceive(result, out SE); }
public ConquerCryption Cryption;
public unsafe void Send(byte[] Packet) { try { if (Socket.Connected) { byte[] Data = new byte[Packet.Length]; fixed (byte* src = Packet, dest = Data) NativeImports.MSVCRT.MEMCPY(dest, src, Data.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 Port, SocketInvoke _InvokeHandler) { port = Port; InvokeHandler = _InvokeHandler; } public void Listen() { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.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];
public delegate void Connection(SocketClient Client); public delegate void ReceivePacket(SocketClient Client, byte[] Packet); public delegate void Disconnection(SocketClient Client);
public class SocketInvoke { Connection con; ReceivePacket rec; Disconnection disc;
public SocketInvoke(Connection connection, ReceivePacket receive, Disconnection 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 Client, Enums.SocketInvokeType invoke, byte[] 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(Client, Packet); 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 Client, byte[] Packet) { }
public static void ProcessDisconnection(SocketClient Client) { } } public class NetworkProcess_Game { public static void ProcessConnection(SocketClient Client) { } public static void ProcessPacket(SocketClient Client, byte[] 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.Cdecl, SetLastError = false)] public static extern unsafe void* MEMCPY(void* dest, void* src, int size); }
Enumerator:
PHP Code:
public enum SocketInvokeType { Connection, Receive, Disconnection }
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.
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
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.
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.
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...