UnrealVoxta 0.1.1
 
Loading...
Searching...
No Matches
HubConnection.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020-2022 Frozen Storm Interactive, Yoann Potinet
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#pragma once
26
27#include "CallbackManager.h"
28#include "CoreMinimal.h"
29#include "IHubConnection.h"
30#include "IHubProtocol.h"
31#include "Tickable.h"
32
33class FConnection;
34
40class FHubConnection : public IHubConnection, FTickableGameObject
41{
42public:
43 static constexpr float PingTimer = 10.0f;
44
51 FHubConnection(const FString& InUrl, const TMap<FString, FString>& InHeaders);
52
57 virtual ~FHubConnection();
58
59#pragma region IHubConnection overrides
60public:
65 virtual void Start() override;
66
71 virtual void Stop() override;
72
78 FORCEINLINE virtual FOnHubConnectedEvent& OnConnected() override
79 {
80 return OnHubConnectedEvent;
81 }
82
88 FORCEINLINE virtual FOnHubConnectionErrorEvent& OnConnectionError() override
89 {
90 return OnHubConnectionErrorEvent;
91 }
92
98 FORCEINLINE virtual FHubConnectionClosedEvent& OnClosed() override
99 {
100 return OnHubConnectionClosedEvent;
101 }
102
109 virtual FOnMethodInvocation& On(const FString& EventName) override;
110
118 virtual FOnMethodCompletion& Invoke(const FString& EventName, const TArray<FSignalRValue>& InArguments = TArray<FSignalRValue>()) override;
119
126 virtual void Send(const FString& InEventName, const TArray<FSignalRValue>& InArguments = TArray<FSignalRValue>()) override;
127#pragma endregion IHubConnection overrides
128
129#pragma region FTickableGameObject overrides
130public:
136 virtual void Tick(float DeltaTime) override;
137
143 TStatId GetStatId() const override;
144
150 virtual ETickableTickType GetTickableTickType() const override
151 {
152 return ETickableTickType::Always;
153 }
154
160 virtual bool IsTickable() const override
161 {
162 return true;
163 }
164
170 virtual bool IsTickableInEditor() const override
171 {
172 return true;
173 }
174
180 virtual bool IsTickableWhenPaused() const override
181 {
182 return true;
183 }
184#pragma endregion FTickableGameObject overrides
185
186protected:
187 void ProcessMessage(const FString& InMessageStr);
188
189private:
190 enum class EConnectionState
191 {
193 Connected,
194 Disconnecting,
195 Disconnected,
196 };
197 EConnectionState ConnectionState;
198
199 void OnConnectionStarted();
200 void OnConnectionFailed();
201 void OnConnectionError(const FString& /* Error */);
202 void OnConnectionClosed(int32 StatusCode, const FString& Reason, bool bWasClean);
203
204 void TryReconnectIfNeeded();
205
206 void Ping();
207 void InvokeHubMethod(const FString& MethodName, const TArray<FSignalRValue>& InArguments, FName CallbackId);
208
209 FString Host;
210
211 TSharedPtr<IHubProtocol> HubProtocol;
212 TSharedPtr<FConnection> Connection;
213 TMap<FString, FOnMethodInvocation> InvocationHandlers;
214 FCriticalSection InvocationHandlersGuard;
215 FCallbackManager CallbackManager;
216
217 bool bHandshakeReceived = false;
218
219 float TickTimeCounter = 0;
220
221 TArray<FString> WaitingCalls;
222
223 FOnHubConnectedEvent OnHubConnectedEvent;
224 FOnHubConnectionErrorEvent OnHubConnectionErrorEvent;
225 FHubConnectionClosedEvent OnHubConnectionClosedEvent;
226
227 void SendCloseMessage();
228
229 bool bReceivedCloseMessage = false;
230 bool bShouldReconnect = false;
231};
@ Connecting
Definition VoxtaMicrophoneState.h:16
Represents a connection to a SignalR server.
Definition Connection.h:37
virtual bool IsTickableWhenPaused() const override
Determines if this object is tickable when the game is paused.
Definition HubConnection.h:180
virtual void Start() override
Starts the hub connection.
Definition HubConnection.cpp:68
virtual FORCEINLINE FHubConnectionClosedEvent & OnClosed() override
Gets the event that is triggered when the hub connection is closed.
Definition HubConnection.h:98
virtual ETickableTickType GetTickableTickType() const override
Gets the tickable tick type for this object.
Definition HubConnection.h:150
virtual ~FHubConnection()
Destructor for the hub connection.
Definition HubConnection.cpp:50
void ProcessMessage(const FString &InMessageStr)
Definition HubConnection.cpp:144
virtual void Send(const FString &InEventName, const TArray< FSignalRValue > &InArguments=TArray< FSignalRValue >()) override
Sends a hub method invocation with the specified arguments without waiting for a result.
Definition HubConnection.cpp:124
virtual bool IsTickableInEditor() const override
Determines if this object is tickable in the editor.
Definition HubConnection.h:170
FHubConnection(const FString &InUrl, const TMap< FString, FString > &InHeaders)
Creates a new connection to a SignalR hub.
Definition HubConnection.cpp:35
virtual void Tick(float DeltaTime) override
Ticks the hub connection, used for periodic tasks such as sending pings.
Definition HubConnection.cpp:129
virtual FORCEINLINE FOnHubConnectionErrorEvent & OnConnectionError() override
Gets the event that is triggered when a hub connection error occurs.
Definition HubConnection.h:88
virtual FOnMethodInvocation & On(const FString &EventName) override
Registers a handler for a hub method invocation.
Definition HubConnection.cpp:94
TStatId GetStatId() const override
Gets the stat ID for this tickable object.
Definition HubConnection.cpp:139
virtual void Stop() override
Stops the hub connection.
Definition HubConnection.cpp:79
virtual FOnMethodCompletion & Invoke(const FString &EventName, const TArray< FSignalRValue > &InArguments=TArray< FSignalRValue >()) override
Invokes a hub method with the specified arguments and waits for the result.
Definition HubConnection.cpp:117
static constexpr float PingTimer
Definition HubConnection.h:43
virtual FORCEINLINE FOnHubConnectedEvent & OnConnected() override
Gets the event that is triggered when the hub connection is established.
Definition HubConnection.h:78
virtual bool IsTickable() const override
Determines if this object is tickable.
Definition HubConnection.h:160
Interface for a SignalR hub connection that enables real-time communication with a SignalR server.
Definition IHubConnection.h:110