private static void WriteDataCallback(IAsyncResult result) { var instance = result.AsyncState as Client; if (instance.isDisposed) { return; } try { instance.stream.EndWrite(result); if (instance.done) { Program.RemoveClient(instance); } else { instance.timer.Change(SampleValues.GetNextSample(instance.timeDelay, instance.timeDeviation), Timeout.Infinite); } } catch (Exception e) { Program.LogError("Write exception: {0} - restarting", e.Message); Program.RemoveClient(instance); } }
/// <summary> /// Starts communicating with the chat service. /// </summary> public void Communicate() { var host = this.address.DnsSafeHost; var port = this.address.Port; try { Program.LogVerbose("Connecting to {0} on {1}", host, port); this.client = new TcpClient(host, port); this.stream = this.client.GetStream(); this.stream.BeginRead(this.inputData, 0, this.inputData.Length, Client.ReadDataCallback, this); this.timer = new Timer(new TimerCallback(Client.WriteData), this, SampleValues.GetNextSample(this.timeDelay, this.timeDeviation), Timeout.Infinite); } catch (Exception e) { Program.LogError("Connect exception: {0} - restarting", e.Message); Program.RemoveClient(this); } }