Esempio n. 1
0
        //start a async loop checking for internal messages and processing them. This includes internal connect negotiation and disconnect requests so runs outside "connected"
        private async Task InternalReceiveLoop()
        {
            Debug.Log("InternalReceiveLoop Start");

            uint     readPacketSize;
            CSteamID clientSteamID;

            try
            {
                while (!Offline)
                {
                    while (ReceiveInternal(out readPacketSize, out clientSteamID))
                    {
                        Debug.Log("InternalReceiveLoop - data");
                        if (readPacketSize != 1)
                        {
                            continue;
                        }
                        Debug.Log("InternalReceiveLoop - received " + receiveBufferInternal[0]);
                        switch (receiveBufferInternal[0])
                        {
                        //requesting to connect to us
                        case (byte)InternalMessages.CONNECT:
                            if (steamConnectionMap.Count >= maxConnections)
                            {
                                SendInternal(clientSteamID, disconnectMsgBuffer);
                                continue;
                                //too many connections, reject
                            }
                            SendInternal(clientSteamID, acceptConnectMsgBuffer);

                            int connectionId = nextConnectionID++;
                            steamConnectionMap.Add(clientSteamID, connectionId, SteamClient.ConnectionState.CONNECTED);
                            OnConnected?.Invoke(connectionId);
                            break;

                        //asking us to disconnect
                        case (byte)InternalMessages.DISCONNECT:
                            try
                            {
                                SteamClient steamClient = steamConnectionMap.fromSteamID[clientSteamID];
                                steamConnectionMap.Remove(steamClient);
                                OnDisconnected?.Invoke(steamClient.connectionID);
                                CloseP2PSessionWithUser(steamClient.steamID);
                            }
                            catch (KeyNotFoundException)
                            {
                                //we have no idea who this connection is
                                Debug.LogError("Trying to disconnect a client thats not known SteamID " + clientSteamID);
                            }

                            break;
                        }
                    }

                    //not got a message - wait a bit more
                    await Task.Delay(TimeSpan.FromSeconds(secondsBetweenPolls));
                }
            }
            catch (ObjectDisposedException) { }

            Debug.Log("InternalReceiveLoop Stop");
        }