Beispiel #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;
            }
        }
Beispiel #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)
                {
                    // 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;
                    }
                }
            }
        }
        private TransportProvider<EndPoint> IdentifyClient(EndPoint remoteEndPoint)
        {
            IPEndPoint remoteIPEndPoint = remoteEndPoint as IPEndPoint;
            IPEndPoint clientIPEndPoint;

            foreach (TransportProvider<EndPoint> client in m_clientInfoLookup.Values.Select(clientInfo => clientInfo.Client))
            {
                clientIPEndPoint = client.Provider as IPEndPoint;

                switch (m_clientIdentificationMode)
                {
                    case ClientIdentificationMode.IP:
                        if ((object)remoteIPEndPoint != null && (object)clientIPEndPoint != null)
                        {
                            if (remoteIPEndPoint.Address.Equals(clientIPEndPoint.Address))
                                return client;
                        }

                        break;

                    case ClientIdentificationMode.Port:
                        if ((object)remoteIPEndPoint != null && (object)clientIPEndPoint != null)
                        {
                            if (remoteIPEndPoint.Port == clientIPEndPoint.Port)
                                return client;
                        }

                        break;

                    case ClientIdentificationMode.EndPoint:
                        if (remoteEndPoint.Equals(client.Provider))
                            return client;

                        break;
                }
            }

            return null;
        }
Beispiel #4
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;
            }
        }
 private int getClientsPosition(EndPoint remoteEndPoint)
 {
     for (int i = 0; i < clientList.Count; i++)
     {
         if (remoteEndPoint.Equals(clientList[i].IPAddress))
         {
             return i;
         }
     }
     return -1;
 }
Beispiel #6
0
        public static bool doConnect(Socket fd, EndPoint addr)
        {
            repeatConnect:
            try
            {
                //
                // Even though we are on the client side, the call to Bind()
                // is necessary to work around a .NET bug: if a socket is
                // connected non-blocking, the LocalEndPoint and RemoteEndPoint
                // properties are null. The call to Bind() fixes this.
                //
                IPAddress any = fd.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any;
                fd.Bind(new IPEndPoint(any, 0));
                IAsyncResult result = fd.BeginConnect(addr, null, null);
                if(!result.CompletedSynchronously)
                {
                    return false;
                }
                fd.EndConnect(result);
            }
            catch(SocketException ex)
            {
                if(interrupted(ex))
                {
                    goto repeatConnect;
                }

                closeSocketNoThrow(fd);

                if(connectionRefused(ex))
                {
                    throw new Ice.ConnectionRefusedException(ex);
                }
                else
                {
                    throw new Ice.ConnectFailedException(ex);
                }
            }

            //
            // On Windows, we need to set the socket's blocking status again
            // after the asynchronous connect. Seems like a bug in .NET.
            //
            setBlock(fd, fd.Blocking);

            if(AssemblyUtil.platform_ == AssemblyUtil.Platform.NonWindows)
            {
                //
                // Prevent self connect (self connect happens on Linux when a client tries to connect to
                // a server which was just deactivated if the client socket re-uses the same ephemeral
                // port as the server).
                //
                if(addr.Equals(getLocalAddress(fd)))
                {
                    throw new Ice.ConnectionRefusedException();
                }
            }

            return true;
        }