/// <summary> /// Sets up Socket ready to send data to endpoint as a client /// </summary> /// <param name="endPoint"></param> public void Connect(IEndPoint endPoint) { switch (_steamOptions.SteamMode) { case SteamModes.P2P: var steamEndPoint = (SteamEndpoint)endPoint; var steamIdentity = new SteamNetworkingIdentity(); steamIdentity.SetSteamID(steamEndPoint.Address); _steamSocketManager.HoHSteamNetConnection = SteamNetworkingSockets.ConnectP2P(ref steamIdentity, 0, 0, Array.Empty <SteamNetworkingConfigValue_t>()); _steamSocketManager.SteamConnections.Add(steamEndPoint, _steamSocketManager.HoHSteamNetConnection); break; case SteamModes.UDP: _endPoint = (SteamSocketFactory.SteamEndPointWrapper)endPoint; var address = new SteamNetworkingIPAddr(); address.SetIPv6(((IPEndPoint)_endPoint.inner).Address.GetAddressBytes(), (ushort)((IPEndPoint)_endPoint.inner).Port); _steamSocketManager.HoHSteamNetConnection = SteamNetworkingSockets.ConnectByIPAddress(ref address, 0, Array.Empty <SteamNetworkingConfigValue_t>()); _steamSocketManager.SteamConnections.Add(_endPoint, _steamSocketManager.HoHSteamNetConnection); break; default: Debug.LogWarning("Unknown steam mode. Please check if mode has been supported."); break; } }
private async void Connect(string host) { cancelToken = new CancellationTokenSource(); c_onConnectionChange = Callback <SteamNetConnectionStatusChangedCallback_t> .Create(OnConnectionStatusChanged); try { hostSteamID = new CSteamID(UInt64.Parse(host)); connectedComplete = new TaskCompletionSource <Task>(); OnConnected += SetConnectedComplete; SteamNetworkingIdentity smi = new SteamNetworkingIdentity(); smi.SetSteamID(hostSteamID); SteamNetworkingConfigValue_t[] options = new SteamNetworkingConfigValue_t[] { }; HostConnection = SteamNetworkingSockets.ConnectP2P(ref smi, 0, options.Length, options); Task connectedCompleteTask = connectedComplete.Task; Task timeOutTask = Task.Delay(ConnectionTimeout, cancelToken.Token); if (await Task.WhenAny(connectedCompleteTask, timeOutTask) != connectedCompleteTask) { if (cancelToken.IsCancellationRequested) { Debug.LogError($"The connection attempt was cancelled."); } else if (timeOutTask.IsCompleted) { Debug.LogError($"Connection to {host} timed out."); } OnConnected -= SetConnectedComplete; OnConnectionFailed(); } OnConnected -= SetConnectedComplete; } catch (FormatException) { Debug.LogError($"Connection string was not in the right format. Did you enter a SteamId?"); Error = true; OnConnectionFailed(); } catch (Exception ex) { Debug.LogError(ex.Message); Error = true; OnConnectionFailed(); } finally { if (Error) { Debug.LogError("Connection failed."); OnConnectionFailed(); } } }
/// <summary> /// Begins a p2p conenctiona attempt with the target. /// </summary> /// <param name="target"></param> private void AttemptConnection(CSteamID target) { SteamNetworkingIdentity id = new SteamNetworkingIdentity(); id.SetSteamID(target); Send(OpCode.AttemptP2PConnection, new IdentityPacket() { identity = SteamID.m_SteamID }, id); }