public void UpdateAll(IEnumerable <User> users) { _serviceStorage.UpdateAll(users); MasterSlaveMessage <User> message = new MasterSlaveMessage <User> { Code = MessageCode.Update, Items = users }; SendMessages(MessageCode.Update, users); }
public void Update(User user) { _serviceStorage.Update(user); MasterSlaveMessage <User> message = new MasterSlaveMessage <User> { Code = MessageCode.Update, Items = new List <User> { user } }; SendMessages(MessageCode.Update, new List <User> { user }); }
private void SendMessages(MessageCode code, IEnumerable <User> users) { MasterSlaveMessage <User> message = new MasterSlaveMessage <User>() { Code = code, Items = users }; foreach (var ipEndPoint in _ipEndPoints) { SendMessage(message, ipEndPoint); } }
private void HandleRequest(MasterSlaveMessage <User> message) { switch (message.Code) { case MessageCode.Add: AddRange(message.Items); break; case MessageCode.Remove: RemoveAll(message.Items); break; case MessageCode.Update: UpdateAll(message.Items); break; case MessageCode.Load: Load(message.Items); break; } }
private void SendMessage(MasterSlaveMessage <User> message, IPEndPoint endPoint) { try { using (TcpClient client = new TcpClient()) { client.Connect(endPoint); using (NetworkStream stream = client.GetStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, message); } } } catch (SocketException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } }
private void Process(object client) { try { using (NetworkStream stream = ((TcpClient)client).GetStream()) { BinaryFormatter formatter = new BinaryFormatter(); MasterSlaveMessage <User> message = ((MasterSlaveMessage <User>)formatter.Deserialize(stream)); HandleRequest(message); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (!ReferenceEquals(client, null)) { ((TcpClient)client).Close(); } } }