private static void AddMessage2(NetworkStream stream) { int reciepeId; const int BufferSize = 128; byte[] commandResponce = new byte[BufferSize]; stream.Read(commandResponce, 0, BufferSize); string email = Encoding.UTF8.GetString(commandResponce); commandResponce = new byte[BufferSize]; stream.Read(commandResponce, 0, BufferSize); string userEmail = Encoding.UTF8.GetString(commandResponce); const int MessageSize = 1000; commandResponce = new byte[MessageSize]; stream.Read(commandResponce, 0, MessageSize); string message = Encoding.UTF8.GetString(commandResponce); XmlSerializer xml = new XmlSerializer(typeof(Reciepe)); Reciepe reciepe = (Reciepe)xml.Deserialize(stream); int userId = DbHelper.IsUser(email); if (userId >= 0) { reciepeId = DbHelper.AddReciepe(new Data_access_layer.Reciepe() { ReciepeName = reciepe.ReciepeName, Ingredients = reciepe.Ingredients, PhotoPath = reciepe.PhotoPath, Instruction = reciepe.Instruction, TypeId = reciepe.ReciepeType.Id, CuisineId = reciepe.Cuisine.Id, CookingTime = reciepe.CookingTime, Rating = reciepe.Raiting, Calories = reciepe.Calories, }); int FromId = DbHelper.IsUser(userEmail); DbHelper.AddMessage(FromId, userId, reciepeId, message); Console.WriteLine("->New Message added"); } }
private static void AddMessage(NetworkStream stream) { int reciepeId; const int BufferSize = 128; byte[] commandResponce = new byte[BufferSize]; stream.Read(commandResponce, 0, BufferSize); string email = Encoding.UTF8.GetString(commandResponce); int userId = DbHelper.IsUser(email); stream.Write(Encoding.UTF8.GetBytes(userId.ToString()), 0, userId.ToString().Length); Thread.Sleep(100); if (userId >= 0) { const int BufferSize2 = 3000; //XmlSerializer xml = new XmlSerializer(typeof(Reciepe)); //Reciepe reciepe = (Reciepe)xml.Deserialize(stream); int countBytes = 0; string text = ""; int size = 0; commandResponce = new byte[BufferSize2]; do { countBytes = stream.Read(commandResponce, 0, 1000); text += Encoding.UTF8.GetString(commandResponce, 0, countBytes); } while (countBytes > 0); Console.WriteLine(text.Length); Thread.Sleep(100); Reciepe reciepe = JsonConvert.DeserializeObject <Reciepe>(text); reciepeId = DbHelper.AddReciepe(new Data_access_layer.Reciepe() { ReciepeName = reciepe.ReciepeName, Ingredients = reciepe.Ingredients, PhotoPath = reciepe.PhotoPath, Instruction = reciepe.Instruction, TypeId = reciepe.ReciepeType.Id, CuisineId = reciepe.Cuisine.Id, CookingTime = reciepe.CookingTime, Rating = reciepe.Raiting, Calories = reciepe.Calories, }); commandResponce = new byte[BufferSize]; stream.Read(commandResponce, 0, BufferSize); string FromEmail = (Encoding.UTF8.GetString(commandResponce)); int FromId = DbHelper.IsUser(FromEmail); commandResponce = new byte[1024]; Thread.Sleep(100); stream.Read(commandResponce, 0, 1024); string message = Encoding.UTF8.GetString(commandResponce); DbHelper.AddMessage(FromId, userId, reciepeId, message); Console.WriteLine("->New Message added"); } }