private void processSalaamClient(SalaamClient salaamClient, string protocolMessage, bool isFromLocal) { bool contains = salaamClientDateTimes.Contains(new SalaamClientDateTime(salaamClient)); if (contains) { int index = salaamClientDateTimes.IndexOf(new SalaamClientDateTime(salaamClient)); salaamClientDateTimes[index].Time = DateTime.Now; if (string.IsNullOrEmpty(protocolMessage)) { if (salaamClient.Message != salaamClientDateTimes[index].Client.Message) { salaamClientDateTimes[index].Client.SetMessage(salaamClient.Message); if (ClientMessageChanged != null) { ClientMessageChanged(this, new SalaamClientEventArgs(salaamClientDateTimes[index].Client, isFromLocal)); } } } else { if (protocolMessage.ToUpper() == "EOS") { if (ClientDisappeared != null) { ClientDisappeared(this, new SalaamClientEventArgs(salaamClientDateTimes[index].Client, isFromLocal)); salaamClientDateTimes.Remove(salaamClientDateTimes[index]); } } } } else { if (string.IsNullOrEmpty(protocolMessage)) { salaamClientDateTimes.Add(new SalaamClientDateTime(salaamClient, DateTime.Now)); int index = salaamClientDateTimes.Count - 1; if (ClientAppeared != null) { ClientAppeared(this, new SalaamClientEventArgs(salaamClientDateTimes[index].Client, isFromLocal)); } } else { if (protocolMessage.ToUpper() == "EOS") { return; } } } }
public SalaamClientDateTime(SalaamClient client, DateTime time = new DateTime()) { Client = client; Time = time; }
private void ProcessClientData(byte[] dataBytes, IPEndPoint clientIPEndPoint) { const string messageRegex = "^Salaam:(?<Base64>(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?)$"; const string messageDataRegex = @"^(?<Length>\d+);(?<HostName>.*?);(?<ServiceType>.*?);(?<Name>.*?);(?<Port>\d+);(?<Message>.*);(<(?<ProtocolMessage>[A-Z][A-Z0-9]{2,3})>)?$"; string dataString = Encoding.UTF8.GetString(dataBytes); if (Regex.IsMatch(dataString, messageRegex)) { try { string messageData = Encoding.UTF8.GetString( Convert.FromBase64String(Regex.Match(dataString, messageRegex).Groups["Base64"].Value)); if (Regex.IsMatch(messageData, messageDataRegex)) { try { Match dataMatch = Regex.Match(messageData, messageDataRegex); int length = int.Parse(dataMatch.Groups["Length"].Value); length += length.ToString().Length + 1; if (messageData.Length == length) { string hostName = dataMatch.Groups["HostName"].Value; string serviceType = dataMatch.Groups["ServiceType"].Value; string name = dataMatch.Groups["Name"].Value; int port = int.Parse(dataMatch.Groups["Port"].Value); string message = dataMatch.Groups["Message"].Value; IPAddress ipAddress = clientIPEndPoint.Address; string protocolMessage = ""; if (dataMatch.Groups["ProtocolMessage"].Success) { protocolMessage = dataMatch.Groups["ProtocolMessage"].Value; } bool isFromLocal = false; if (hostName.ToLower() == currentHostName.ToLower() && (currentIPAddresses.Contains(ipAddress) || IPAddress.IsLoopback(ipAddress))) { isFromLocal = true; if (!ReceiveFromLocalMachine) { return; } } if (ServiceType == "*" || serviceType.ToLower() == ServiceType.ToLower()) { SalaamClient salaamClient = new SalaamClient(ipAddress, hostName, serviceType, name, port, message); processSalaamClient(salaamClient, protocolMessage, isFromLocal); } else { return; } } else { return; } } catch { } } } catch { } } }
/// <summary> /// Initializes a new instance of the <see cref="SalaamClientEventArgs"/> class. /// </summary> /// <param name="client">The client.</param> /// <param name="isFromLocalMachine">if set to <c>true</c> this instance is from the local machine.</param> public SalaamClientEventArgs(SalaamClient client, bool isFromLocalMachine) { Client = client; IsFromLocalMachine = isFromLocalMachine; }