Esempio n. 1
0
        private bool RecvInfo()
        {
            int recv = 0;

            byte[] buffer = new byte[1400];

            try
            {
                recv = m_Socket.SendTo(QueryMsg.InfoQuery, m_serverEndPoint);
                recv = m_Socket.ReceiveFrom(buffer, ref m_remoteEP);
            }
            catch (Exception)
            {
                return(false);
            }

            //string requestBuffer = Encoding.UTF8.GetString(buffer).Substring(7);

            if ((byte)MsgHeader.A2S_INFO != buffer[4])
            {
                return(false);
            }


            m_rqstInfo.protocolVersion = buffer[5]; // Protocol Version

            int idx = 6;

            m_rqstInfo.serverName = MainApp.BufferToString(buffer, ref idx);
            if (String.IsNullOrEmpty(m_rqstInfo.serverName))
            {
                return(false);
            }

            m_rqstInfo.map = MainApp.BufferToString(buffer, ref idx);
            if (String.IsNullOrEmpty(m_rqstInfo.map))
            {
                return(false);
            }

            m_rqstInfo.gameDirectory = MainApp.BufferToString(buffer, ref idx);
            if (String.IsNullOrEmpty(m_rqstInfo.gameDirectory))
            {
                return(false);
            }

            m_rqstInfo.gameName = MainApp.BufferToString(buffer, ref idx);
            if (String.IsNullOrEmpty(m_rqstInfo.gameName))
            {
                return(false);
            }

            byte[] idBuffer = new byte[2];
            Array.Copy(buffer, idx, idBuffer, 0, 2); // idx ~ endIdx - 1
            m_rqstInfo.appID = (short)(idBuffer[0] + (idBuffer[1] << 8));
            idx += 2;

            m_rqstInfo.players    = buffer[idx];
            m_rqstInfo.maxPlayers = buffer[idx + 1];
            m_rqstInfo.bots       = buffer[idx + 2];
            m_rqstInfo.serverType = buffer[idx + 3];
            m_rqstInfo.os         = buffer[idx + 4];
            m_rqstInfo.visibility = buffer[idx + 5];
            m_rqstInfo.vac        = buffer[idx + 6];

            idx += 7;

            m_rqstInfo.gameVersion = MainApp.BufferToString(buffer, ref idx);
            if (String.IsNullOrEmpty(m_rqstInfo.gameVersion))
            {
                return(false);
            }

            m_NStat = NetworkStatus.Online;
            return(true);
        }
Esempio n. 2
0
        private void RecvPlayer()
        {
            int recv = 0;

            byte[] buffer = new byte[1400];

            try
            {
                recv = m_Socket.SendTo(QueryMsg.PlayerQuery, m_serverEndPoint);
                recv = m_Socket.ReceiveFrom(buffer, ref m_remoteEP);
            }
            catch (Exception)
            {
                return;
            }

            if ((byte)MsgHeader.A2S_CHALLENGE != buffer[4])
            {
                m_Socket.Close();
                InitSocket();
                return;
            }

            byte[] challengeQuery = { 0xFF, 0xFF, 0xFF, 0xFF, 0x55, buffer[5], buffer[6], buffer[7], buffer[8] };
            buffer = new byte[1400];
            try
            {
                recv = m_Socket.SendTo(challengeQuery, m_serverEndPoint);
                recv = m_Socket.ReceiveFrom(buffer, ref m_remoteEP);
            }
            catch (Exception)
            {
                return;
            }

            if ((byte)MsgHeader.A2S_PLAYER != buffer[4])
            {
                m_Socket.Close();
                InitSocket();
                return;
            }

            foreach (RQST_PLAYER exstPlayer in m_rqstPlayerList)
            {
                exstPlayer.isCheck = false;
            }

            int idx = 5;

            int numPlayer = buffer[idx]; // player cnt

            ++idx;

            for (int i = 0; i < numPlayer; ++i)
            {
                RQST_PLAYER player = new RQST_PLAYER();
                ++idx;

                player.playerName = MainApp.BufferToString(buffer, ref idx);
                if (String.IsNullOrEmpty(player.playerName))
                {
                    continue;
                }

                byte[] scoreBuffer = new byte[4];
                Array.Copy(buffer, idx, scoreBuffer, 0, 4);
                player.score = (int)(scoreBuffer[0] + (scoreBuffer[1] << 8) + (scoreBuffer[2] << 16) + (scoreBuffer[3] << 24));
                idx         += 4;

                byte[] timeBuffer = new byte[4];
                Array.Copy(buffer, idx, timeBuffer, 0, 4);
                player.time = BitConverter.ToSingle(timeBuffer, 0);
                idx        += 4;

                int exstPlayerIdx = -1;
                for (int j = 0; j < m_rqstPlayerList.Count; ++j)
                {
                    if (m_rqstPlayerList[j].playerName == player.playerName)
                    {
                        exstPlayerIdx = j;
                        break;
                    }
                }

                if (-1 == exstPlayerIdx)
                {
                    player.isCheck = true;
                    m_rqstPlayerList.Add(player);
                }
                else
                {
                    m_rqstPlayerList[exstPlayerIdx].playerName = player.playerName;
                    m_rqstPlayerList[exstPlayerIdx].score      = player.score;
                    m_rqstPlayerList[exstPlayerIdx].time       = player.time;
                    m_rqstPlayerList[exstPlayerIdx].isCheck    = true;
                }
            }

            for (int i = 0; i < m_rqstPlayerList.Count;)
            {
                if (m_rqstPlayerList[i].isCheck == false)
                {
                    m_rqstPlayerList.RemoveAt(i);
                }
                else
                {
                    ++i;
                }
            }
        }