public ChannelHandler(IConfigurationRoot config, IrcSocket socket, string name) { _config = config; _socket = socket; _name = name; _random = new Random(); }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); config = builder.Build(); var server = new IrcServer() { Name = "Server", Hostname = config.GetConnectionString("Hostname"), Port = Convert.ToInt32(config.GetConnectionString("Port")), Ssl = config.GetConnectionString("SSL") == "True", IgnoreCertErrors = true, Username = config.GetConnectionString("Username"), Channels = config.GetConnectionString("Channels") }; _socket = new IrcSocket(server); _socket.Initialise(); _socket.Connect(); _socket.ChannelList.CollectionChanged += (s, e) => { new ChannelHandler(config, _socket, (e.NewItems[0] as Channel)?.Name).Init(); }; var mre = new ManualResetEvent(false); _socket.HandleDisconnect += irc => { mre.Set(); }; // The main thread can just wait on the wait handle, which basically puts it into a "sleep" state, and blocks it forever mre.WaitOne(); }
protected async override void OnBackgroundActivated(BackgroundActivatedEventArgs args) { var taskInstance = args.TaskInstance; var deferral = taskInstance.GetDeferral(); Debug.WriteLine("Attempting background execution: " + taskInstance.Task.Name); try { var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails; var socketInformation = details.SocketInformation; var servers = IrcUiHandler.Instance.connectedServers.Values; IrcSocket irc = null; foreach (var server in servers) { Debug.WriteLine("Irc server name " + server.Server.Name + " - Task name " + taskInstance.Task.Name); //if (server is IrcSocket && taskInstance.Task.Name == server.BackgroundTaskName) //{ // irc = server as IrcSocket; //} } if (irc == null) { Debug.WriteLine("Unable to get irc server: " + taskInstance.Task.Name); return; } Debug.WriteLine("Able to get irc server: " + taskInstance.Task.Name); switch (details.Reason) { case SocketActivityTriggerReason.SocketActivity: case SocketActivityTriggerReason.KeepAliveTimerExpired: var socket = socketInformation.StreamSocket; using (DataReader reader = new DataReader(socket.InputStream)) using (DataWriter writer = new DataWriter(socket.OutputStream)) { reader.InputStreamOptions = InputStreamOptions.Partial; await irc.ReadFromServer(reader, writer); } break; case SocketActivityTriggerReason.SocketClosed: // implement reconnecting break; default: break; } deferral.Complete(); } catch (Exception exception) { Debug.WriteLine(exception.Message); Debug.WriteLine(exception.StackTrace); deferral.Complete(); } }