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 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 bufferWriter = new DataStreamWriter(m_Buffer.Length, Unity.Collections.Allocator.Temp); bufferWriter.Write(m_Buffer, read); var reader = new DataStreamReader(bufferWriter, 0, read); var ctx = default(DataStreamReader.Context); var header = new SQPHeader(); header.FromStream(reader, ref ctx); SQPMessageType type = (SQPMessageType)header.Type; switch (type) { case SQPMessageType.ChallangeRequest: { if (!m_OutstandingTokens.ContainsKey(endpoint)) { uint token = GetNextToken(); //Debug.Log("token generated: " + token); var writer = new DataStreamWriter(m_Buffer.Length, Unity.Collections.Allocator.Temp); var rsp = new ChallangeResponse(); rsp.Header.ChallangeId = token; rsp.ToStream(ref writer); writer.CopyTo(0, writer.Length, ref m_Buffer); m_Socket.SendTo(m_Buffer, writer.Length, SocketFlags.None, endpoint); m_OutstandingTokens.Add(endpoint, token); } } break; case SQPMessageType.QueryRequest: { uint token; if (!m_OutstandingTokens.TryGetValue(endpoint, out token)) { //Debug.Log("Failed to find token!"); return; } m_OutstandingTokens.Remove(endpoint); ctx = default(DataStreamReader.Context); var req = new QueryRequest(); req.FromStream(reader, ref ctx); if ((SQPChunkType)req.RequestedChunks == SQPChunkType.ServerInfo) { var rsp = m_ServerInfo; var writer = new DataStreamWriter(m_Buffer.Length, Unity.Collections.Allocator.Temp); rsp.QueryHeader.Header.ChallangeId = token; rsp.ToStream(ref writer); writer.CopyTo(0, writer.Length, ref m_Buffer); m_Socket.SendTo(m_Buffer, writer.Length, SocketFlags.None, endpoint); } } break; default: break; } } } }
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) { // Transfer incoming data in m_Buffer into a DataStreamReader var writer = new DataStreamWriter(m_Buffer.Length, Unity.Collections.Allocator.Temp); writer.Write(m_Buffer, read); var reader = new DataStreamReader(writer, 0, read); var ctx = default(DataStreamReader.Context); var header = new SQPHeader(); header.FromStream(reader, ref ctx); foreach (var q in m_Queries) { if (q.m_Server == null || !endpoint.Equals(q.m_Server)) { continue; } switch (q.m_State) { case SQPClientState.Idle: // Just ignore if we get extra data break; case SQPClientState.WaitingForChallange: if ((SQPMessageType)header.Type == SQPMessageType.ChallangeResponse) { q.ChallangeId = header.ChallangeId; q.RTT = NetworkUtils.stopwatch.ElapsedMilliseconds - q.StartTime; // We restart timer so we can get an RTT that is an average between two measurements q.StartTime = NetworkUtils.stopwatch.ElapsedMilliseconds; SendServerInfoQuery(q); } break; case SQPClientState.WaitingForResponse: if ((SQPMessageType)header.Type == SQPMessageType.QueryResponse) { ctx = default(DataStreamReader.Context); q.m_ServerInfo.FromStream(reader, ref ctx); // We report the average of two measurements q.RTT = (q.RTT + (NetworkUtils.stopwatch.ElapsedMilliseconds - q.StartTime)) / 2; /* * GameDebug.Log(string.Format("ServerName: {0}, BuildId: {1}, Current Players: {2}, Max Players: {3}, GameType: {4}, Map: {5}, Port: {6}", * m_ServerInfo.ServerInfoData.ServerName, * m_ServerInfo.ServerInfoData.BuildId, * (ushort)m_ServerInfo.ServerInfoData.CurrentPlayers, * (ushort)m_ServerInfo.ServerInfoData.MaxPlayers, * m_ServerInfo.ServerInfoData.GameType, * m_ServerInfo.ServerInfoData.Map, * (ushort)m_ServerInfo.ServerInfoData.Port)); */ q.validResult = true; q.m_State = SQPClientState.Idle; } break; default: break; } } } } foreach (var q in m_Queries) { // Timeout if stuck in any state but idle for too long if (q.m_State != SQPClientState.Idle) { var now = NetworkUtils.stopwatch.ElapsedMilliseconds; if (now - q.StartTime > 3000) { q.m_State = SQPClientState.Idle; } } } }
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; } }
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); SQPMessageType type = (SQPMessageType)header.Type; switch (type) { case SQPMessageType.ChallangeRequest: { if (!m_OutstandingTokens.ContainsKey(endpoint)) { uint token = GetNextToken(); Debug.Log("token generated: " + token); var writer = new ByteOutputStream(m_Buffer); var rsp = new ChallangeResponse(); rsp.Header.ChallangeId = token; rsp.ToStream(ref writer); m_Socket.SendTo(m_Buffer, writer.GetBytePosition(), SocketFlags.None, endpoint); m_OutstandingTokens.Add(endpoint, token); } } break; case SQPMessageType.QueryRequest: { uint token; if (!m_OutstandingTokens.TryGetValue(endpoint, out token)) { Debug.Log("Failed to find token!"); return; } m_OutstandingTokens.Remove(endpoint); reader.Reset(); var req = new QueryRequest(); req.FromStream(ref reader); if ((SQPChunkType)req.RequestedChunks == SQPChunkType.ServerInfo) { var rsp = m_ServerInfo; var writer = new ByteOutputStream(m_Buffer); rsp.QueryHeader.Header.ChallangeId = token; rsp.ToStream(ref writer); m_Socket.SendTo(m_Buffer, writer.GetBytePosition(), SocketFlags.None, endpoint); } } break; default: break; } } } }