2using System.Collections.Generic;
4using System.Threading.Tasks;
22 private readonly DateTime created = DateTime.Now;
23 private readonly DateTime expires;
25 private readonly
string[] privileges;
26 private readonly
string userVariable;
27 private readonly
string resource;
28 private readonly
string snifferId;
29 private readonly
bool feedbackCheck;
30 private string[] tabIds =
null;
31 private DateTime tabIdTimestamp = DateTime.MinValue;
46 string UserVariable, params
string[] Privileges)
49 this.expires = DateTime.Now.Add(MaxLife);
50 this.comLayer = ComLayer;
52 this.resource = PageResource;
55 this.userVariable = UserVariable;
56 this.privileges = Privileges;
59 if (this.feedbackCheck)
60 this.outgoing =
new Cache<string, bool>(
int.MaxValue, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1),
true);
79 private Task Process(DateTime Timestamp,
string Message,
string Function)
81 if (Timestamp >= this.expires)
84 return this.Push(Timestamp, Message, Function,
true);
87 private async Task Push(DateTime Timestamp,
string Message,
string Function,
bool CloseIfNoTabs)
91 DateTime Now = DateTime.Now;
93 if ((Now - this.tabIdTimestamp).TotalSeconds > 2 || this.tabIds is
null || this.tabIds.Length == 0)
96 this.tabIdTimestamp = Now;
99 if (this.feedbackCheck && Message.StartsWith(
"{") && Message.EndsWith(
"}"))
104 if (Parsed is IDictionary<string, object> Obj &&
105 Obj.TryGetValue(
"data", out
object Temp) &&
106 Temp is IDictionary<string, object> Obj2 &&
107 Obj2.TryGetValue(
"timestamp", out
object Timestamp2) &&
108 Obj2.TryGetValue(
"message", out
object Message2) &&
109 (
this.outgoing?.ContainsKey(
this.ToJson(Timestamp2, Message2)) ??
true))
120 string Data = this.ToJson(
XML.
Encode(Timestamp), Message);
122 this.outgoing?.
Add(Data,
true);
124 int Tabs = await ClientEvents.PushEvent(this.tabIds, Function, Data,
true, this.userVariable, this.privileges);
126 if (CloseIfNoTabs && Tabs <= 0 && (Now - this.created).TotalSeconds >= 5)
135 private string ToJson(
object Timestamp,
object Message)
137 return JSON.
Encode(
new KeyValuePair<string, object>[]
139 new KeyValuePair<string, object>(
"timestamp", Timestamp),
140 new KeyValuePair<string, object>(
"message", Message)
144 private async Task Close()
146 await this.Push(DateTime.Now,
"Sniffer closed.",
"Information",
false);
147 this.comLayer.Remove(
this);
151 private Task Process(DateTime Timestamp,
byte[] Data,
string Function)
153 if (Timestamp >= this.expires)
156 return this.Push(Timestamp, this.HexOutput(Data), Function,
true);
159 private string HexOutput(
byte[] Data)
161 switch (this.binaryPresentationMethod)
164 StringBuilder sb =
new StringBuilder();
168 foreach (
byte b
in Data)
177 sb.Append(b.ToString(
"X2"));
182 return sb.ToString();
185 return Convert.ToBase64String(Data);
189 return "<" + Data.Length.ToString() +
" bytes>";
200 return this.Process(Timestamp,
Error,
"Error");
208 public override Task
Exception(DateTime Timestamp,
string Exception)
210 return this.Process(Timestamp, Exception,
"Exception");
218 public override Task
Information(DateTime Timestamp,
string Comment)
220 return this.Process(Timestamp, Comment,
"Information");
230 return this.Process(Timestamp, Data,
"Rx");
240 return this.Process(Timestamp, Text,
"Rx");
250 return this.Process(Timestamp, Data,
"Tx");
260 return this.Process(Timestamp, Text,
"Tx");
270 return this.Process(Timestamp,
Warning,
"Warning");
Helps with common JSON-related tasks.
static object Parse(string Json)
Parses a JSON string.
static string Encode(string s)
Encodes a string for inclusion in JSON.
Helps with common XML-related tasks.
static string Encode(string s)
Encodes a string for use in XML.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
static string[] GetTabIDsForLocation(string Location)
Gets the Tab IDs of all tabs that display a particular resource.
Sending sniffer events to the corresponding web page(s).
override Task ReceiveBinary(DateTime Timestamp, byte[] Data)
Called when binary data has been received.
override Task Information(DateTime Timestamp, string Comment)
Called to inform the viewer of something.
string SnifferId
Sniffer ID
override Task Error(DateTime Timestamp, string Error)
Called to inform the viewer of an error state.
override Task Warning(DateTime Timestamp, string Warning)
Called to inform the viewer of a warning state.
override Task TransmitText(DateTime Timestamp, string Text)
Called when text has been transmitted.
override Task Exception(DateTime Timestamp, string Exception)
Called to inform the viewer of an exception state.
WebSniffer(string SnifferId, string PageResource, TimeSpan MaxLife, BinaryPresentationMethod BinaryPresentationMethod, ICommunicationLayer ComLayer, string UserVariable, params string[] Privileges)
Sending sniffer events to the corresponding web page(s).
virtual void Dispose()
IDisposable.Dispose
override Task ReceiveText(DateTime Timestamp, string Text)
Called when text has been received.
override Task TransmitBinary(DateTime Timestamp, byte[] Data)
Called when binary data has been transmitted.
Implements an HTTP server.
Abstract base class for sniffers. Implements default method overloads.
Implements an in-memory cache.
void Dispose()
IDisposable.Dispose
void Add(KeyType Key, ValueType Value)
Adds an item to the cache.
Interface for observable classes implementing communication protocols.
BinaryPresentationMethod
How binary data is to be presented.