/// <summary> /// Sends a simulated event. /// </summary> /// <param name="serverConnection">The switch connection.</param> /// <param name="eventCode">The event code.</param> /// <param name="text">The content text.</param> /// <param name="args">Additional name/values to be added to the event.</param> private void SendEvent(SwitchConnection serverConnection, SwitchEventCode eventCode, string text, params NameValue[] args) { var properties = new ArgCollection(ArgCollectionType.Unconstrained); var sb = new StringBuilder(); byte[] data = Helper.ASCIIEncoding.GetBytes(text); byte[] content; properties["Event-Name"] = SwitchHelper.GetEventCodeString(eventCode); properties["Content-Type"] = "text"; properties["Content-Length"] = data.Length.ToString(); foreach (var pair in args) { properties[pair.Name] = pair.Value; } foreach (var key in properties) { sb.AppendFormat("{0}: {1}\n", key, Helper.UrlEncode(properties[key])); } sb.Append('\n'); content = Helper.Concat(Helper.ASCIIEncoding.GetBytes(sb.ToString()), data); properties = new ArgCollection(ArgCollectionType.Unconstrained); properties["Content-Type"] = "text/event-plain"; properties["Content-Length"] = content.Length.ToString(); sb.Clear(); foreach (var key in properties) { sb.AppendFormat("{0}: {1}\n", key, Helper.UrlEncode(properties[key])); } sb.Append('\n'); serverConnection.Send(Helper.Concat(Helper.ASCIIEncoding.GetBytes(sb.ToString()), content)); }