Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reduction

  • Loading branch information...
commit 218a5d99c93a4431446ee76eae765427f852bc49 1 parent 16dd971
@Strilanc authored
View
2  Bnet/Bnet Client Component.vb
@@ -38,7 +38,7 @@ Namespace Bnet
Dim ct = New CancellationTokenSource()
component._hooks.Add(DirectCast(New DelegatedDisposable(Sub() ct.Cancel()), IDisposable).AsTask())
client.IncludePacketHandlerAsync(Protocol.Packets.ServerToClient.ChatEvent,
- Function(pickle) component.OnReceivedChatEvent(pickle.Value),
+ Function(vals) component.OnReceivedChatEvent(vals),
ct.Token)
client.ChainEventualDisposalTo(component)
View
17 Bnet/Bnet Client.vb
@@ -169,11 +169,10 @@ Namespace Bnet
Dim ct As CancellationToken = Nothing
'Handled packets
IncludePacketHandlerAsync(Protocol.Packets.ServerToClient.Ping,
- Function(value) TrySendPacketAsync(Protocol.MakePing(salt:=value.Value)),
+ Function(salt) TrySendPacketAsync(Protocol.MakePing(salt)),
ct)
IncludePacketHandlerAsync(Protocol.Packets.ServerToClient.ChatEvent,
- Async Function(value)
- Dim vals = value.Value
+ Async Function(vals)
Dim eventId = vals.ItemAs(Of Protocol.ChatEventId)("event id")
Dim text = vals.ItemAs(Of String)("text")
If eventId = Protocol.ChatEventId.Channel Then
@@ -183,8 +182,7 @@ Namespace Bnet
End Function,
ct)
IncludePacketHandlerAsync(Protocol.Packets.ServerToClient.MessageBox,
- Function(value)
- Dim vals = value.Value
+ Function(vals)
Dim msg = "MESSAGE BOX FROM BNET: {0}: {1}".Frmt(vals.ItemAs(Of String)("caption"), vals.ItemAs(Of String)("text"))
Logger.Log(msg, LogMessageType.Problem)
Return CompletedTask()
@@ -230,12 +228,12 @@ Namespace Bnet
Return _state
End Function
- Public Async Sub IncludePacketHandlerAsync(Of T)(packetDefinition As Protocol.Packets.Definition(Of T),
+ Public Async Sub IncludePacketPickleHandlerAsync(Of T)(packetDefinition As Protocol.Packets.Definition(Of T),
handler As Func(Of IPickle(Of T), Task),
ct As CancellationToken)
Contract.Assume(packetDefinition IsNot Nothing)
Contract.Assume(handler IsNot Nothing)
- await inQueue
+ Await inQueue
Using walker = _manualPacketHandler.CreateWalker()
While Not ct.IsCancellationRequested
@@ -251,6 +249,11 @@ Namespace Bnet
End While
End Using
End Sub
+ Public Sub IncludePacketHandlerAsync(Of T)(packetDefinition As Protocol.Packets.Definition(Of T),
+ handler As Func(Of T, Task),
+ ct As CancellationToken)
+ IncludePacketPickleHandlerAsync(packetDefinition, Function(e As IPickle(Of T)) handler(e.Value), ct)
+ End Sub
Private Sub IncludeLogger(Of T)(packetDefinition As Protocol.Packets.Definition(Of T), ct As CancellationToken)
IncludePacketHandlerAsync(packetDefinition, Function() CompletedTask(), ct)
End Sub
View
80 Bnet/BnetClientControl.vb
@@ -7,14 +7,14 @@ Namespace Bnet
Private ReadOnly inQueue As CallQueue = MakeControlCallQueue(Me)
Private ReadOnly _component As Bnet.ClientComponent
Private ReadOnly _client As Bnet.Client
- Private ReadOnly _hooks As New List(Of IDisposable)
+ Private ReadOnly _life As New CancellationTokenSource()
Private numPrimaryStates As Integer
<ContractInvariantMethod()> Private Sub ObjectInvariant()
Contract.Invariant(inQueue IsNot Nothing)
Contract.Invariant(_component IsNot Nothing)
Contract.Invariant(_client IsNot Nothing)
- Contract.Invariant(_hooks IsNot Nothing)
+ Contract.Invariant(_life IsNot Nothing)
End Sub
Public Shared Async Function FromComponentAsync(component As Bnet.ClientComponent) As Task(Of BnetClientControl)
@@ -27,42 +27,32 @@ Namespace Bnet
Me._client = component.Client
Me._component = component
+
+ Init()
+ End Sub
+
+ Private Sub Init()
logClient.SetLogger(Me._client.Logger, "Client")
- Dim ct = New CancellationTokenSource()
- Me._hooks.Add(DirectCast(New DelegatedDisposable(Sub() ct.Cancel()), IDisposable).AsTask())
- Me._client.IncludePacketHandlerAsync(Packets.ServerToClient.ChatEvent,
- Function(pickle) inQueue.QueueAction(Sub() OnClientReceivedChatEvent(Me._client, pickle.Value)),
- ct.Token)
- Me._client.IncludePacketHandlerAsync(Packets.ServerToClient.QueryGamesList(Me._client.Clock),
- Function(pickle) inQueue.QueueAction(Sub() OnClientReceivedQueryGamesList(Me._client, pickle.Value)),
- ct.Token)
-
- inQueue.QueueAction(Async Sub()
- Dim state = Await Me._client.GetStateAsync
- OnClientStateChanged(Me._client, state, state)
- End Sub)
- Dim stateChangedHandler As Client.StateChangedEventHandler = Sub(sender, oldState, newState) inQueue.QueueAction(
- Sub() OnClientStateChanged(sender, oldState, newState))
- Dim advertisedHandler As Client.AdvertisedGameEventHandler = Sub(sender, gameDescription, [private], refreshed) inQueue.QueueAction(
- Sub() OnClientAdvertisedGame(sender, gameDescription, [private], refreshed))
- AddHandler Me._client.StateChanged, stateChangedHandler
- AddHandler Me._client.AdvertisedGame, advertisedHandler
- Me._hooks.Add(New DelegatedDisposable(Sub() RemoveHandler Me._client.StateChanged, stateChangedHandler))
- Me._hooks.Add(New DelegatedDisposable(Sub() RemoveHandler Me._client.AdvertisedGame, advertisedHandler))
+ _client.IncludePacketHandlerAsync(Packets.ServerToClient.ChatEvent, AddressOf OnClientReceivedChatEventAsync, _life.Token)
+ _client.IncludePacketHandlerAsync(Packets.ServerToClient.QueryGamesList(_client.Clock), AddressOf OnClientReceivedQueryGamesListAsync, _life.Token)
+
+ Call Async Sub()
+ Dim state = Await _client.GetStateAsync()
+ OnClientStateChangedAsync(_client, state, state)
+ End Sub
+ AddHandler Me._client.StateChanged, AddressOf OnClientStateChangedAsync
+ AddHandler Me._client.AdvertisedGame, AddressOf OnClientAdvertisedGameAsync
+ _life.Token.Register(Sub() RemoveHandler Me._client.StateChanged, AddressOf OnClientStateChangedAsync)
+ _life.Token.Register(Sub() RemoveHandler Me._client.AdvertisedGame, AddressOf OnClientAdvertisedGameAsync)
End Sub
- Public Function QueueDispose() As Task
- Return inQueue.QueueAction(Sub() Me.Dispose())
- End Function
- Private Sub BnetClientControl_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
- For Each hook In _hooks
- hook.Dispose()
- Next hook
+ Private Sub BnetClientControl_Disposed() Handles Me.Disposed
+ _life.Cancel()
End Sub
- Private Sub OnClientReceivedQueryGamesList(sender As Bnet.Client, value As QueryGamesListResponse)
- If sender IsNot _client Then Return
+ Private Async Function OnClientReceivedQueryGamesListAsync(value As QueryGamesListResponse) As Task
+ Await inQueue
While lstState.Items.Count > numPrimaryStates
lstState.Items.RemoveAt(lstState.Items.Count - 1)
End While
@@ -75,10 +65,10 @@ Namespace Bnet
lstState.Items.Add(game.GameStats.HostName)
lstState.Items.Add(game.GameStats.AdvertisedPath.ToString.Split("\"c).Last)
Next game
- End Sub
- Private Sub OnClientReceivedChatEvent(sender As Bnet.Client, vals As NamedValueMap)
+ End Function
+ Private Async Function OnClientReceivedChatEventAsync(vals As NamedValueMap) As Task
+ Await inQueue
If IsDisposed Then Return
- If sender IsNot Me._client Then Return
Dim id = vals.ItemAs(Of ChatEventId)("event id")
Dim user = vals.ItemAs(Of String)("username")
Dim text = vals.ItemAs(Of String)("text")
@@ -124,7 +114,7 @@ Namespace Bnet
Case ChatEventId.Emote
logClient.LogMessage("{0} {1}".Frmt(user, text), Color.DarkGray)
End Select
- End Sub
+ End Function
Private Async Sub txtTalk_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtTalk.KeyDown
If e.KeyCode <> Keys.Enter Then Return
@@ -144,10 +134,11 @@ Namespace Bnet
End Try
End Sub
- Private Sub OnClientStateChanged(sender As Bnet.Client,
- oldState As Bnet.ClientState,
- newState As Bnet.ClientState)
+ Private Async Sub OnClientStateChangedAsync(sender As Bnet.Client,
+ oldState As Bnet.ClientState,
+ newState As Bnet.ClientState)
Contract.Requires(sender IsNot Nothing)
+ Await inQueue
If IsDisposed Then Return
If sender IsNot _client Then Return
@@ -166,12 +157,13 @@ Namespace Bnet
lstState.BackColor = SystemColors.ButtonFace
End Select
End Sub
- Private Sub OnClientAdvertisedGame(sender As Bnet.Client,
- gameDescription As WC3.LocalGameDescription,
- [private] As Boolean,
- refreshed As Boolean)
+ Private Async Sub OnClientAdvertisedGameAsync(sender As Bnet.Client,
+ gameDescription As WC3.LocalGameDescription,
+ [private] As Boolean,
+ refreshed As Boolean)
Contract.Requires(sender IsNot Nothing)
Contract.Requires(gameDescription IsNot Nothing)
+ Await inQueue
If IsDisposed Then Return
If sender IsNot _client Then Return
@@ -189,7 +181,7 @@ Namespace Bnet
numPrimaryStates = lstState.Items.Count
End Sub
- Private Sub comClient_IssuedCommand(sender As CommandControl, argument As String) Handles comClient.IssuedCommand
+ Private Sub OnIssuedCommand(sender As CommandControl, argument As String) Handles comClient.IssuedCommand
Contract.Requires(argument IsNot Nothing)
Tinker.Components.UIInvokeCommand(_component, argument)
End Sub
View
4 Warden/Warden Socket.vb
@@ -31,7 +31,7 @@ Namespace Warden
End Sub
Public Async Function QueueRunAsync(ct As CancellationToken, callback As Action(Of IRist(Of Byte))) As Task
- Await inQueue.AwaitableEntrance()
+ Await inQueue
If ct.IsCancellationRequested Then Return
WritePacket(ClientPacket.MakeFullServiceConnect(_cookie, _seed))
Do
@@ -96,7 +96,7 @@ Namespace Warden
Protected Overrides Async Function PerformDispose(finalizing As Boolean) As Task
If finalizing Then Return
- Await inQueue.AwaitableEntrance()
+ Await inQueue
Call Async Sub() Await _socket.QueueDisconnect(expected:=True, reason:="Disposed")
End Function
Please sign in to comment.
Something went wrong with that request. Please try again.