public Decode(Socket wSock, byte[] buffer, SRClient net, object packetf)
        {
            try
            {
                ushort security;
                packet = packetf;
                if (buffer.Length != 0 || buffer.Length > 0)
                {
                    ms = new MemoryStream(buffer);
                    br = new BinaryReader(ms);

                    dataSize = br.ReadUInt16();

                    byte[] b = new byte[dataSize];
                    Array.Copy(buffer, 6, b, 0, dataSize);

                    BUFFER = b;
                    OPCODE = br.ReadUInt16();

                    security = br.ReadUInt16();
                }
                socket = wSock;
                NET = net;
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        public Decode(Socket wSock, byte[] buffer, SRClient net, object packetf)
        {
            try
            {
                ushort security;
                packet = packetf;
                if (buffer.Length != 0 || buffer.Length > 0)
                {
                    ms = new MemoryStream(buffer);
                    br = new BinaryReader(ms);

                    dataSize = br.ReadUInt16();

                    byte[] b = new byte[dataSize];
                    Array.Copy(buffer, 6, b, 0, dataSize);

                    BUFFER = b;
                    OPCODE = br.ReadUInt16();

                    security = br.ReadUInt16();
                }
                socket = wSock;
                NET    = net;
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
        }
        private void ClientConnect(IAsyncResult ar)
        {
            Socket handlingSocket = (Socket)ar.AsyncState;

            try
            {
                Socket wSocket = handlingSocket.EndAccept(ar);

                wSocket.DontFragment = false;

                //Console.WriteLine("client connected from {0}", wSocket.RemoteEndPoint);

                object   p      = null;
                SRClient Player = new SRClient();

                try
                {
                    OnConnect(ref p, Player);
                }
                catch (Exception ex)
                {
                    Log.Exception("[Server.ClientConnect.OnConnect] ", ex);
                }

                Player.Packets      = p;
                Player.clientSocket = wSocket;

                handlingSocket.BeginAccept(new AsyncCallback(ClientConnect), handlingSocket);

                try
                {
                    wSocket.Send(new byte[] { 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x01 });
                    wSocket.BeginReceive(Player.tmpbuf, 0, Player.tmpbuf.Length, SocketFlags.None, new AsyncCallback(Player.ReceiveData), wSocket);
                }
                catch (SocketException ex)
                {
                    if (ex.ErrorCode == 10054) // client did not wait for accept and gone
                    {
                        // ToDo: OnDisconnect
                    }
                }
                catch (Exception ex)
                {
                    Log.Exception("[Server.ClientConnect.Send+Receive]", ex);
                }
            }
            catch (ObjectDisposedException ex)
            {
                Log.Exception(ex);
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10054) // Accept failed because client is no longer trying to connect, thus start new Accept
                {
                    handlingSocket.BeginAccept(new AsyncCallback(ClientConnect), handlingSocket);
                }
                else
                {
                    Log.Exception("[Server.ClientConnect.SocketException]", ex);
                }
            }
            catch (Exception ex)
            {
                Log.Exception("[Server.ClientConnect.Exception]", ex);
            }
        }
 public void Send(byte[] buff, SRClient client)
 {
     for (byte b = 0; b <= MembersClient.Count - 1; b++)
     {
         if (MembersClient[b] != client)
             MembersClient[b].Send(buff);
     }
 }
 public PlayerMgr(SRClient s)
 {
     client = s;
 }
        private void ClientConnect(IAsyncResult ar)
        {
            Socket handlingSocket = (Socket)ar.AsyncState;
            try
            {
                Socket wSocket = handlingSocket.EndAccept(ar);

                wSocket.DontFragment = false;

                //Console.WriteLine("client connected from {0}", wSocket.RemoteEndPoint);

                object p = null;
                SRClient Player = new SRClient();

                try
                {
                    OnConnect(ref p, Player);
                }
                catch (Exception ex)
                {
                    Log.Exception("[Server.ClientConnect.OnConnect] ", ex);
                }

                Player.Packets = p;
                Player.clientSocket = wSocket;

                handlingSocket.BeginAccept(new AsyncCallback(ClientConnect), handlingSocket);

                try
                {
                    wSocket.Send(new byte[] { 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x01 });
                    wSocket.BeginReceive(Player.tmpbuf, 0, Player.tmpbuf.Length, SocketFlags.None, new AsyncCallback(Player.ReceiveData), wSocket);
                }
                catch (SocketException ex)
                {
                    if (ex.ErrorCode == 10054) // client did not wait for accept and gone
                    {
                        // ToDo: OnDisconnect
                    }
                }
                catch (Exception ex)
                {
                    Log.Exception("[Server.ClientConnect.Send+Receive]", ex);
                }
            }
            catch (ObjectDisposedException ex)
            {
                Log.Exception(ex);
            }
            catch (SocketException ex)
            {
                if (ex.ErrorCode == 10054) // Accept failed because client is no longer trying to connect, thus start new Accept
                {
                    handlingSocket.BeginAccept(new AsyncCallback(ClientConnect), handlingSocket);
                }
                else
                {
                    Log.Exception("[Server.ClientConnect.SocketException]", ex);
                }

            }
            catch (Exception ex)
            {
                Log.Exception("[Server.ClientConnect.Exception]", ex);
            }
        }
 public static void _OnClientConnect(ref object de, SRClient net)
 {
     de = new PlayerMgr(net);
 }