// Обработка сокетов private void ManageClients() { lock (mutex) { foreach (TcpClient client in clients) { NetworkStream stream = client.GetStream(); if (stream.DataAvailable) { byte[] buf = new byte[4096]; stream.Read(buf, 0, 4096); Packet data = (Packet)MyFormatter.Deserialize(buf); switch (data.Command) { case CommandType.Message: Console.WriteLine(data.GetMessage()); break; case CommandType.Collection: break; case CommandType.Choice: int choice = data.getChoice(); vManager.Vote(choice); break; case CommandType.Stats: Console.WriteLine("StatRequest"); Packet pack = new Packet(); pack.Command = CommandType.Stats; pack.Data = (Object)vManager.currentVoteBox; SendPacket(client, pack); break; default: break; } } // На всякий случай спим, чтобы не напрягать процессор Thread.Sleep(100); } } }
private void Loop() { while (!isExit) { if (stream.DataAvailable) { byte[] buf = new byte[4096]; try { stream.Read(buf, 0, 4096); } catch (Exception ex) { isExit = true; Console.WriteLine(ex.Message); } Packet pack = (Packet)MyFormatter.Deserialize(buf); switch (pack.Command) { case CommandType.Message: Console.WriteLine(pack.GetMessage()); break; case CommandType.Collection: VoteBox voteBox = pack.GetVoteBox(); PrintVoteBox(voteBox); TakeChoice(voteBox); RequestStats(); break; //вприципе тоже самое что и collection case CommandType.Stats: PrintStats(pack.Data); isExit = true; break; default: break; } } Thread.Sleep(10); } }
public void ClassANullSerializtionTest() { ClassA a2; a1.ClassB = null; a1.ClassC = null; using (FileStream writeStream = new FileStream("testf2.csv", FileMode.Create)) { myFormatter.Serialize(writeStream, a1); } using (FileStream readStream = new FileStream("testf2.csv", FileMode.Open)) { a2 = (ClassA)myFormatter.Deserialize(readStream); } Assert.AreEqual(a1.Name, a2.Name); Assert.AreEqual(a1.Num, a2.Num); Assert.AreEqual(a1.Date, a2.Date); Assert.AreSame(a2.ClassB, null); Assert.AreSame(a2.ClassC, null); }