Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update #14

Merged
merged 3 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Examples/Chat/Chat.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
51 changes: 21 additions & 30 deletions Examples/Chat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,33 @@ private static async Task Demo()
// Create an instance of the ActionScheduler. This will
// allow us to execute chat actions like: posting messages,
// kicking users, moving messages, etc.
using (var actionScheduler = new ActionScheduler(auth, roomUrl))
{
// Create an instance of the RoomWatcher class. Here we
// specify (via the type parameter) what WebSocket implementation
// we'd like to use. This class allows you to subscribe to chat events.
using (var roomWatcher = new RoomWatcher<DefaultWebSocket>(auth, roomUrl))
{
// Subscribe to the UserMentioned event.
roomWatcher.AddUserMentionedEventHandler(async m =>
{
await actionScheduler.CreateReplyAsync("hello!", m.MessageId);

/// Do stuff ...
});
using var actionScheduler = new ActionScheduler(auth, roomUrl);
// Create an instance of the RoomWatcher class. Here we
// specify (via the type parameter) what WebSocket implementation
// we'd like to use. This class allows you to subscribe to chat events.
using var roomWatcher = new RoomWatcher<DefaultWebSocket>(auth, roomUrl);
// Subscribe to the UserMentioned event.
_ = roomWatcher.AddUserMentionedEventHandler(async m => await actionScheduler.CreateReplyAsync("hello!", m.MessageId));

// Besides being able to subscribe to the default events,
// you can also create (and listen to) your own. Your class must
// implement the ChatEventDataProcessor class, you can also
// optionally implement IChatEventHandler or IChatEventHandler<T>.
var customEventHanlder = new AllData();
// Besides being able to subscribe to the default events,
// you can also create (and listen to) your own. Your class must
// implement the ChatEventDataProcessor class, you can also
// optionally implement IChatEventHandler or IChatEventHandler<T>.
var customEventHanlder = new AllData();

// Add a very basic handler.
customEventHanlder.OnEvent += data => Console.WriteLine(data);
// Add a very basic handler.
customEventHanlder.OnEvent += data => Console.WriteLine(data);

// Add our custom event handler so we can
// begin processing the incoming event data.
roomWatcher.EventRouter.AddProcessor(customEventHanlder);
// Add our custom event handler so we can
// begin processing the incoming event data.
roomWatcher.EventRouter.AddProcessor(customEventHanlder);

// Post a simple message.
var messageId = await actionScheduler.CreateMessageAsync("Hello world.");
// Post a simple message.
var messageId = await actionScheduler.CreateMessageAsync("Hello world.");

while (Console.ReadKey(true).Key != ConsoleKey.Q)
{
while (Console.ReadKey(true).Key != ConsoleKey.Q)
{

}
}
}
}
}
18 changes: 9 additions & 9 deletions SharpExchange/Api/2.2/ApiRequestScheduler.MasterScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static MasterSheduler()
{
reqs = new Queue<QueuedRequest>();

Task.Run(() => QueueProcessorLoop());
_ = Task.Run(() => QueueProcessorLoop());
}


Expand All @@ -47,7 +47,7 @@ public static void Dispose()
dispose = true;

reqs.Clear();
queueMre.Set();
_ = queueMre.Set();
queueMre.Dispose();
}

Expand All @@ -60,14 +60,14 @@ public static string Schedule(string endpoint)
{
result = x;

mre.Set();
_ = mre.Set();
});

reqs.Enqueue(new QueuedRequest(callback, endpoint));

queueMre.Set();
_ = queueMre.Set();

mre.WaitOne(Timeout);
_ = mre.WaitOne(Timeout);

return result;
}
Expand All @@ -82,8 +82,8 @@ private static void QueueProcessorLoop()
{
if (reqs.Count == 0)
{
queueMre.Reset();
queueMre.WaitOne();
_ = queueMre.Reset();
_ = queueMre.WaitOne();

if (dispose)
{
Expand All @@ -94,8 +94,8 @@ private static void QueueProcessorLoop()
var req = reqs.Dequeue();
var json = HttpRequest.GetAsync(req.Url).Result;

req.Callback.InvokeAsync(json);
mre.WaitOne(waitTime);
_ = req.Callback.InvokeAsync(json);
_ = mre.WaitOne(waitTime);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions SharpExchange/Api/2.2/ApiRequestScheduler.MethodSheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MethodSheduler()
{
reqs = new Queue<QueuedRequest>();

Task.Run(() => QueueProcessorLoop());
_ = Task.Run(() => QueueProcessorLoop());
}

~MethodSheduler()
Expand All @@ -53,7 +53,7 @@ public void Dispose()
dispose = true;

reqs.Clear();
queueMre.Set();
_ = queueMre.Set();
queueMre.Dispose();

GC.SuppressFinalize(this);
Expand All @@ -75,14 +75,14 @@ public async Task<Result<T>> ScheduleAsync<T>(string fullUrl)
//TODO: Log this somewhere.
}

mre.Set();
_ = mre.Set();
});

reqs.Enqueue(new QueuedRequest(callback, fullUrl));

queueMre.Set();
_ = queueMre.Set();

await Task.Run(() => mre.WaitOne(Timeout));
_ = await Task.Run(() => mre.WaitOne(Timeout));

return result;
}
Expand All @@ -97,8 +97,8 @@ private void QueueProcessorLoop()
{
if (reqs.Count == 0)
{
queueMre.Reset();
queueMre.WaitOne();
_ = queueMre.Reset();
_ = queueMre.WaitOne();

if (dispose)
{
Expand All @@ -109,7 +109,7 @@ private void QueueProcessorLoop()
var req = reqs.Dequeue();
var json = MasterSheduler.Schedule(req.Url);

req.Callback.InvokeAsync(json);
_ = req.Callback.InvokeAsync(json);

var backoff = 0;

Expand All @@ -124,7 +124,7 @@ private void QueueProcessorLoop()

if (backoff != 0)
{
mre.WaitOne(backoff * 1000);
_ = mre.WaitOne(backoff * 1000);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions SharpExchange/Api/2.2/ApiRequestScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private static string GetEndpointId(string url)
{
if (string.IsNullOrEmpty(s) || (s.Any(char.IsDigit) && s != "2.2")) continue;

id.Append(s);
id.Append('/');
_ = id.Append(s);
_ = id.Append('/');
}

return id.ToString();
Expand Down
6 changes: 3 additions & 3 deletions SharpExchange/Auth/FKeyAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using SharpExchange.Net;

namespace SharpExchange.Auth
Expand All @@ -22,7 +22,7 @@ public static async Task<string> GetAsync(string url, CookieManager cMan = null)
}

var html = await HttpRequest.GetAsync(url, cMan);
var dom = await new HtmlParser().ParseAsync(html);
var dom = await new HtmlParser().ParseDocumentAsync(html);
var fkey = Get(dom);

cache[url] = fkey;
Expand Down
16 changes: 8 additions & 8 deletions SharpExchange/Chat/Actions/ActionScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SharpExchange.Chat.Actions
{
public class ActionScheduler : IDisposable
{
private IAuthenticationProvider auth;
private readonly IAuthenticationProvider auth;
private readonly ManualResetEvent queueMre;
private readonly Queue<ChatAction> actionQueue;
private bool dispose;
Expand All @@ -36,7 +36,7 @@ public ActionScheduler(IAuthenticationProvider authProvider, string roomUrl)
Host = host.GetChatHost();
RoomId = id;

Task.Run(new Action(ProcessQueue));
_ = Task.Run(new Action(ProcessQueue));
}

public ActionScheduler(IAuthenticationProvider authProvider, string host, int roomId)
Expand All @@ -56,7 +56,7 @@ public ActionScheduler(IAuthenticationProvider authProvider, string host, int ro
Host = host.GetChatHost();
RoomId = roomId;

Task.Run(new Action(ProcessQueue));
_ = Task.Run(new Action(ProcessQueue));
}

~ActionScheduler()
Expand All @@ -71,7 +71,7 @@ public void Dispose()
if (dispose) return;
dispose = true;

queueMre.Set();
_ = queueMre.Set();
queueMre.Dispose();
actionQueue.Clear();

Expand All @@ -93,14 +93,14 @@ public async Task<T> ScheduleActionAsync<T>(ChatAction act, TimeSpan timeout)
act.CallBack = new Action<object>(x =>
{
data = x;
wait.Set();
_ = wait.Set();
});

actionQueue.Enqueue(act);

queueMre.Set();
_ = queueMre.Set();

await Task.Run(() => wait.WaitOne(timeout));
_ = await Task.Run(() => wait.WaitOne(timeout));

return (T)data;
}
Expand All @@ -113,7 +113,7 @@ private void ProcessQueue()
{
if (actionQueue.Count == 0)
{
queueMre.Reset();
_ = queueMre.Reset();
}

if (queueMre.WaitOne() && dispose)
Expand Down
4 changes: 2 additions & 2 deletions SharpExchange/Chat/Events/EventRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SharpExchange.Chat.Events
{
public sealed class EventRouter : IDisposable
{
private List<ChatEventDataProcessor> processors;
private readonly List<ChatEventDataProcessor> processors;
private bool dispose;

public int RoomId { get; private set; }
Expand Down Expand Up @@ -99,7 +99,7 @@ private void HandleNewMessage(string json)
continue;
}

Task.Run(() => processor.ProcessEventData(ev));
_ = Task.Run(() => processor.ProcessEventData(ev));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions SharpExchange/Chat/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using SharpExchange.Net;

namespace SharpExchange.Chat
Expand Down Expand Up @@ -77,7 +77,7 @@ public Message(string host, int messageId, IAuthenticationProvider auth = null)

var endpoint = $"https://{Host}/messages/{Id}/history";
var html = HttpRequest.GetAsync(endpoint, cMan).Result;
var dom = new HtmlParser().Parse(html);
var dom = new HtmlParser().ParseDocument(html);

RoomId = GetRoomId(dom);
Stars = GetStars(dom);
Expand Down Expand Up @@ -236,7 +236,7 @@ private MessageStates GetStates(IHtmlDocument dom)

for (var i = 0; i < childCount; i++)
{
content.RemoveChild(content.Children[0]);
_ = content.RemoveChild(content.Children[0]);
}

var isPuregd = content.TextContent.Trim() == "(older data no longer available)";
Expand Down
8 changes: 4 additions & 4 deletions SharpExchange/Chat/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Linq;
using System.Threading.Tasks;
using AngleSharp.Dom;
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using SharpExchange.Net;

namespace SharpExchange.Chat
Expand Down Expand Up @@ -71,7 +71,7 @@ public Room(string host, int roomId, IAuthenticationProvider auth = null)
throw new Exception($"Unable to find room {roomId} on {host}.");
}

var dom = new HtmlParser().Parse(result.Body);
var dom = new HtmlParser().ParseDocument(result.Body);

Host = host;
Id = roomId;
Expand Down Expand Up @@ -295,7 +295,7 @@ private User[] GetUsers(IHtmlCollection<IElement> userElements)
?.Value
.Split()[0];

int.TryParse(msgCountStr ?? "0", out var msgCount);
_ = int.TryParse(msgCountStr ?? "0", out var msgCount);

users[i] = new User
{
Expand Down
Loading