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; } }
public SQPClient(IPEndPoint server) { m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_Socket.SetupAndBind(0); m_Server = server; m_State = new SQPClientState(); }
public void StartInfoQuery() { Debug.Assert(m_State == SQPClientState.Idle); StartTime = NetworkUtils.stopwatch.ElapsedMilliseconds; var writer = new ByteOutputStream(m_Buffer); var req = new ChallangeRequest(); req.ToStream(ref writer); m_Socket.SendTo(m_Buffer, writer.GetBytePosition(), SocketFlags.None, m_Server); m_State = SQPClientState.WaitingForChallange; }
public unsafe void StartInfoQuery() { Debug.Assert(m_State == SQPClientState.Idle); StartTime = m_Timer.ElapsedMilliseconds; var writer = new DataStreamWriter(BufferSize, Allocator.Temp); var req = new ChallangeRequest(); req.ToStream(ref writer); writer.CopyTo(0, writer.Length, ref m_Buffer); m_Socket.SendTo(m_Buffer, writer.Length, SocketFlags.None, m_Server); m_State = SQPClientState.WaitingForChallange; writer.Dispose(); }
void SendServerInfoQuery() { StartTime = NetworkUtils.stopwatch.ElapsedMilliseconds; var req = new QueryRequest(); req.Header.ChallangeId = ChallangeId; req.RequestedChunks = (byte)SQPChunkType.ServerInfo; var writer = new ByteOutputStream(m_Buffer); req.ToStream(ref writer); m_State = SQPClientState.WaitingForResponse; m_Socket.SendTo(m_Buffer, writer.GetBytePosition(), SocketFlags.None, m_Server); }
unsafe void SendServerInfoQuery() { StartTime = m_Timer.ElapsedMilliseconds; var req = new QueryRequest(); req.Header.ChallangeId = ChallangeId; req.RequestedChunks = (byte)SQPChunkType.ServerInfo; var writer = new DataStreamWriter(BufferSize, Allocator.Temp); req.ToStream(ref writer); m_State = SQPClientState.WaitingForResponse; writer.CopyTo(0, writer.Length, ref m_Buffer); m_Socket.SendTo(m_Buffer, writer.Length, SocketFlags.None, m_Server); writer.Dispose(); }
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; } }