// Done! private static void CommandTesterHelper(string s, EventHandler <MessageEventArgs> call) { // Vamos enviar isto! IList <AsyncDataEventArgs> data = FragmentAssyncDataCollection(ConvertToAssyncDataCollection(RandomSplit(s))); AsyncStreamReader ar = CreateAsyncStreamReader(); MessengerReader mr = new MessengerReader(ar); bool done = false; mr.MessageReceived += (o, e) => { call.Invoke(o, e); done = true; }; // Send data! for (int i = 0; i < data.Count; ++i) { ar.Invoke(data[i]); } int time = Environment.TickCount; while (done == false) { Thread.Sleep(20); if (Environment.TickCount - time > 2000) { throw new AssertionException("Could not complete operation on time"); } } }
// Main workload public MessengerSocket(string hostname, int port, int timeout) { this.Timeout = timeout; this.tcpClient = new TcpClient(hostname, port); this.writer = new StreamWriter(this.tcpClient.GetStream(), this.encoding); this.reader = new MessengerReader(this.tcpClient.GetStream()); this.reader.MessageReceived += this.MessageReceived; this.reader.StartListening(); if (Debugger.IsAttached) { //Console.OutputEncoding = encoding; } }