Esempio n. 1
0
        public override async Task <NetworkConnection> Connect(NetworkConnectionConfig config)
        {
            ValidateSteamInitalized();
            if (config.LobbyInfo == null)
            {
                throw new ArgumentException("Cannot connect to Steam networking except through a lobby.");
            }
            var id = new CSteamID(config.LobbyInfo.Id);

            Debug.Log($"[Steam] Joining lobby: {config.LobbyInfo.Id}");
            var lobbyEnter = await SteamMatchmaking.JoinLobby(id).ToTask <LobbyEnter_t>();

            var responseCode = (EChatRoomEnterResponse)lobbyEnter.m_EChatRoomEnterResponse;

            if (responseCode != EChatRoomEnterResponse.k_EChatRoomEnterResponseSuccess)
            {
                throw new NetworkingException($"Could not join lobby {lobbyEnter.m_ulSteamIDLobby}. Error: {responseCode}.");
            }
            await SteamUtility.WaitFor <LobbyDataUpdate_t>();

            currentLobbyId = new CSteamID(lobbyEnter.m_ulSteamIDLobby);
            lobbyOwner     = SteamMatchmaking.GetLobbyOwner(currentLobbyId);
            // Send initial connection packet.
            Debug.Log($"[Steam] Sending initial connection packet to lobby owner {lobbyOwner}...");
            SendPacket(lobbyOwner, null, 0);
            var connection  = AddConnection(lobbyOwner);
            var connectTask = connection.ConnectTask;

            if (await Task.WhenAny(connectTask, Task.Delay(20000)) == connectTask)
            {
                Debug.Log("[Steam] Connected to host");
                return(connection);
            }
            else
            {
                throw new NetworkingException("Could not connect to lobby host. Timeout: 20 seconds passed.");
            }
        }
Esempio n. 2
0
        public override async Task Initialize(NetworkInterfaceConfiguration config)
        {
            ValidateSteamInitalized();
            await base.Initialize(config);

            Debug.Log($"[Steam] Steam User ID: {SteamUser.GetSteamID()}");
            callbackP2PSesssionRequest = Callback <P2PSessionRequest_t> .Create(OnP2PSessionRequest);

            callbackP2PConnectFail = Callback <P2PSessionConnectFail_t> .Create(OnP2PSessionConnectFail);

            callbackLobbyChatUpdate = Callback <LobbyChatUpdate_t> .Create(OnLobbyChatUpdate);

            // Allow use of Steam relay servers
            SteamNetworking.AllowP2PPacketRelay(true);

            if (config.Type != NetworkInterfaceType.Server)
            {
                return;
            }
            var type = config.ServerSteamLobbyType;
            var size = config.ServerSteamLobbyMaxSize;

            Debug.Log($"[Steam] Creating lobby");
            var result = await SteamMatchmaking.CreateLobby(type, size).ToTask <LobbyCreated_t>();

            Debug.Log($"[Steam] Creating lobby");

            if (SteamUtility.IsError(result.m_eResult))
            {
                throw SteamUtility.CreateError(result.m_eResult);
            }
            var lobbyEnter = await SteamUtility.WaitFor <LobbyEnter_t>();

            currentLobbyId = new CSteamID(lobbyEnter.m_ulSteamIDLobby);
            Debug.Log($"[Steam] Created server lobby ID: {currentLobbyId}");
            SetLobbyData(currentLobbyId);
        }