private TcpTalk GetTalker(string connectionId, TcpAddress destination) { lock (_talkers) { TcpTalk talker; if (!_talkers.TryGetValue(connectionId, out talker)) { talker = new TcpTalk(_port, destination); talker.HandleMessage(HandleMessage); _talkers[connectionId] = talker; } return talker; } }
private void HandleMessage(byte[] data, TcpTalk talker) { if (_handleMessage != null) { var message = Message.Deserialise(data); var connectionId = message.ConnectionId.GetString(); if (!string.IsNullOrEmpty(connectionId)) { if (!_talkers.ContainsKey(connectionId)) { _talkers[connectionId] = talker; } _handleMessage(message, talker.Destination); } } }
private void Send(TcpTalk talker, byte[] data) { var send = new Task(() => { lock (talker) { talker.SendData(data); } }); send.Start(); }
private void NewClientConnection(TcpClient client) { lock (_talkers) { var talker = new TcpTalk(client); talker.HandleMessage(HandleMessage); } }