Created
March 8, 2014 21:14
-
-
Save ProdigySim/9438955 to your computer and use it in GitHub Desktop.
Trying to write an async wrapper for SteamKit's SteamClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using SteamKit2; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace SteamConnection | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| Task.WaitAll(DoSteamyStuff()); | |
| } | |
| static async Task AsyncMain(string[] args) | |
| { | |
| await DoSteamyStuff(); | |
| } | |
| private static async Task DoSteamyStuff() | |
| { | |
| SteamConnection myConnection = new SteamConnection(); | |
| Console.WriteLine("Attempting to connect to steam..."); | |
| var result = await myConnection.Connect(); | |
| Console.WriteLine("Steam Connection finished. Result: {0}", result.Result); | |
| } | |
| } | |
| /// <summary> | |
| /// Trying to provide an async wrapper for steamkit stuff | |
| /// </summary> | |
| public class SteamConnection : IDisposable | |
| { | |
| private SteamClient m_Client = new SteamClient(); | |
| private CallbackManager m_CbManager; | |
| public SteamConnection() | |
| { | |
| m_CbManager = new CallbackManager(m_Client); | |
| } | |
| public async Task<SteamClient.ConnectedCallback> Connect() | |
| { | |
| SteamClient.ConnectedCallback callbackResult = null; | |
| new Callback<SteamClient.ConnectedCallback>(cb => | |
| { | |
| callbackResult = cb; | |
| }, m_CbManager); | |
| m_Client.Connect(); | |
| await Task.Run(() => | |
| { | |
| while (callbackResult == null) | |
| { | |
| m_CbManager.RunWaitCallbacks(TimeSpan.FromSeconds(1)); | |
| } | |
| }); | |
| return callbackResult; | |
| } | |
| public SteamClient Client { get { return m_Client; } } | |
| public void Dispose() | |
| { | |
| m_Client.Disconnect(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment