Ejemplo 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 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;
                    }
                }
            }
        }
Ejemplo n.º 2
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);

                    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;
                    }
                }
            }
        }