Exemplo n.º 1
0
        public void Update()
        {
            if (m_Socket.Poll(0, SelectMode.SelectRead))
            {
                int read = m_Socket.ReceiveFrom(m_Buffer, m_Buffer.Length, SocketFlags.None, ref endpoint);
                if (read > 0)
                {
                    var reader = new ByteInputStream(m_Buffer);
                    var header = new SQPHeader();
                    header.FromStream(ref reader);

                    switch (m_State)
                    {
                    case SQPClientState.Idle:
                        break;

                    case SQPClientState.WaitingForChallange:
                        if ((SQPMessageType)header.Type == SQPMessageType.ChallangeResponse)
                        {
                            if (endpoint.Equals(m_Server))
                            {
                                ChallangeId = header.ChallangeId;
                                SendServerInfoQuery();
                            }
                        }
                        break;

                    case SQPClientState.WaitingForResponse:
                        if ((SQPMessageType)header.Type == SQPMessageType.QueryResponse)
                        {
                            reader.Reset();
                            var rsp = new SQP.ServerInfo();
                            rsp.FromStream(ref reader);
                            Debug.Log(string.Format("ServerName: {0}, BuildId: {1}, Current Players: {2}, Max Players: {3}, GameType: {4}, Map: {5}, Port: {6}",
                                                    rsp.ServerInfoData.ServerName,
                                                    rsp.ServerInfoData.BuildId,
                                                    (ushort)rsp.ServerInfoData.CurrentPlayers,
                                                    (ushort)rsp.ServerInfoData.MaxPlayers,
                                                    rsp.ServerInfoData.GameType,
                                                    rsp.ServerInfoData.Map,
                                                    (ushort)rsp.ServerInfoData.Port));
                            m_State   = SQPClientState.Success;
                            StartTime = NetworkUtils.stopwatch.ElapsedMilliseconds;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            var now = NetworkUtils.stopwatch.ElapsedMilliseconds;

            if (now - StartTime > 1000000)
            {
                Debug.Log("Failed");
                m_State = SQPClientState.Failure;
            }
        }
Exemplo n.º 2
0
        public unsafe void Update()
        {
            if (m_Socket.Poll(0, SelectMode.SelectRead))
            {
                int read = m_Socket.ReceiveFrom(m_Buffer, BufferSize, SocketFlags.None, ref endpoint);
                if (read > 0)
                {
                    var buffer = new DataStreamWriter(BufferSize, Allocator.Temp);
                    buffer.Write(m_Buffer, read);
                    var reader    = new DataStreamReader(buffer, 0, read);
                    var readerCtx = default(DataStreamReader.Context);
                    var header    = new SQPHeader();
                    header.FromStream(reader, ref readerCtx);

                    switch (m_State)
                    {
                    case SQPClientState.Idle:
                        break;

                    case SQPClientState.WaitingForChallange:
                        if ((SQPMessageType)header.Type == SQPMessageType.ChallangeResponse)
                        {
                            if (endpoint.Equals(m_Server))
                            {
                                ChallangeId = header.ChallangeId;
                                SendServerInfoQuery();
                            }
                        }
                        break;

                    case SQPClientState.WaitingForResponse:
                        if ((SQPMessageType)header.Type == SQPMessageType.QueryResponse)
                        {
                            readerCtx = default(DataStreamReader.Context);
                            var rsp = new SQP.ServerInfo();
                            rsp.FromStream(reader, ref readerCtx);
                            Debug.Log(string.Format("ServerName: {0}, BuildId: {1}, Current Players: {2}, Max Players: {3}, GameType: {4}, Map: {5}, Port: {6}",
                                                    rsp.ServerInfoData.ServerName,
                                                    rsp.ServerInfoData.BuildId,
                                                    (ushort)rsp.ServerInfoData.CurrentPlayers,
                                                    (ushort)rsp.ServerInfoData.MaxPlayers,
                                                    rsp.ServerInfoData.GameType,
                                                    rsp.ServerInfoData.Map,
                                                    (ushort)rsp.ServerInfoData.Port));
                            m_State   = SQPClientState.Idle;
                            StartTime = m_Timer.ElapsedMilliseconds;
                        }
                        break;

                    default:
                        break;
                    }
                    buffer.Dispose();
                }
            }
            var now = m_Timer.ElapsedMilliseconds;

            if (now - StartTime > 1000000)
            {
                Debug.Log("Failed");
                m_State = SQPClientState.Failure;
            }
        }