Ejemplo n.º 1
0
        private static void GMSBindAccept(IAsyncResult ar)
        {
            Socket _listen = (Socket)ar.AsyncState;

            try
            {
                Socket client = _listen.EndAccept(ar);

                if (client != null)
                {
                    int ClientID = client.GetHashCode();
                    Console.WriteLine("[W] GameManagerServer Connected");
                    GMS_Manager MyGMS = new GMS_Manager(ClientID, client);
                    GMS_Clients.Add(MyGMS);
                    int GMSClientIndex = GetClientIndex(ClientID);
                    GMS_Clients[GMSClientIndex]._socket.BeginReceive(GMS_Clients[GMSClientIndex].buffer, 0, GMS_Manager.BufferSize, SocketFlags.None, new AsyncCallback(GMSBind_receive), GMS_Clients[GMSClientIndex]);
                }
            }
            catch
            {
                return;
            }
            finally
            {
                _listen.BeginAccept(new AsyncCallback(GMSBindAccept), _listen);
            }
        }
Ejemplo n.º 2
0
        private static void GMSBind_receive(IAsyncResult ar)
        {
            byte[]      Data;
            GMS_Manager My_GMS = (GMS_Manager)ar.AsyncState;
            Socket      client = My_GMS._socket;


            if (client == null)
            {
                return;
            }
            if (!client.Connected)
            {
                return;
            }
            int BufferSize = AsyncSocket.getPendingByteCount(client);

            if (BufferSize > 1000)
            {
                return;
            }

            try
            {
                int BytesReceive = client.EndReceive(ar);
                if (BytesReceive > 0)
                {
                    Data = new byte[BytesReceive];
                    Array.Copy(My_GMS.buffer, Data, BytesReceive);
                    // process byte

                    HandleGMSReceive(My_GMS, Data);
                    My_GMS.buffer = new byte[GMS_Manager.BufferSize];
                }
                else
                {
                    DisconnectClientFromID(My_GMS.id);
                    //disconnect
                }
                client.BeginReceive(My_GMS.buffer, 0, GMS_Manager.BufferSize, SocketFlags.None, new AsyncCallback(GMSBind_receive), My_GMS);
            }
            catch (SocketException e)
            {
                if (e.ErrorCode == 10054)
                {
                    DisconnectClientFromID(My_GMS.id);
                    //disconnect
                    return;
                }
            }
            catch
            {
                DisconnectClientFromID(My_GMS.id);
                //disconnect
                return;
            }
        }
Ejemplo n.º 3
0
        private static void HandleGMSReceive(GMS_Manager MyGMS, byte[] data)
        {
            // struct packet gms
            // 2 bytes - tamanho
            // 1 byte - packet type
            // data
            //Console.WriteLine(data.Length);
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader br = new BinaryReader(ms, Encoding.UTF8))
                {
                    short  DataLen    = br.ReadInt16();
                    byte   packetType = br.ReadByte();
                    byte[] Data       = br.ReadBytes(DataLen);

                    ParsingGMSDataReceived(packetType, Data);

                    br.Close();
                }
                ms.Close();
            }
        }