UnrealVoxta 0.1.1
 
Loading...
Searching...
No Matches
IHubConnection.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 "CoreMinimal.h"
28#include "SignalRValue.h"
29
34{
36public:
38
40 {
41 bError = OtherValue.bError;
42 ErrorMessage = OtherValue.ErrorMessage;
43 }
44
45 FSignalRInvokeResult(FSignalRInvokeResult&& OtherValue) noexcept : FSignalRValue(MoveTemp(OtherValue))
46 {
47 bError = MoveTemp(OtherValue.bError);
48 ErrorMessage = MoveTemp(OtherValue.ErrorMessage);
49 }
50
52 {
53 FSignalRValue::operator=(OtherValue);
54 bError = OtherValue.bError;
55 ErrorMessage = OtherValue.ErrorMessage;
56 return *this;
57 }
58
60 {
61 FSignalRValue::operator=(MoveTemp(OtherValue));
62 bError = MoveTemp(OtherValue.bError);
63 ErrorMessage = MoveTemp(OtherValue.ErrorMessage);
64 return *this;
65 }
66
72 FORCEINLINE bool HasError() const
73 {
74 return bError;
75 }
76
78 FORCEINLINE const FString& GetErrorMessage() const
79 {
80 return ErrorMessage;
81 }
82
90 FORCEINLINE static FSignalRInvokeResult Error(const FString& ErrorMessage)
91 {
93 Result.bError = true;
94 Result.ErrorMessage = ErrorMessage;
95 return Result;
96 }
97
98private:
99 FSignalRInvokeResult() = default;
100
101 bool bError = false;
102 FString ErrorMessage;
103};
104
109class IHubConnection : public TSharedFromThis<IHubConnection>
110{
111public:
115 virtual void Start() = 0;
116
120 virtual void Stop() = 0;
121
125 DECLARE_EVENT(IHubConnection, FOnHubConnectedEvent);
126
132 virtual FOnHubConnectedEvent& OnConnected() = 0;
133
137 DECLARE_EVENT_OneParam(IHubConnection, FOnHubConnectionErrorEvent, const FString& /* Error */);
138
144 virtual FOnHubConnectionErrorEvent& OnConnectionError() = 0;
145
150 DECLARE_EVENT(IHubConnection, FHubConnectionClosedEvent);
151
157 virtual FHubConnectionClosedEvent& OnClosed() = 0;
158
159 DECLARE_DELEGATE_OneParam(FOnMethodInvocation, const TArray<FSignalRValue>&);
160
168 virtual FOnMethodInvocation& On(const FString& EventName) = 0;
169
171
180 virtual FOnMethodCompletion& Invoke(const FString& EventName, const TArray<FSignalRValue>& InArguments = TArray<FSignalRValue>()) = 0;
181
192 template <typename... ArgTypes>
193 FORCEINLINE FOnMethodCompletion& Invoke(const FString& EventName, ArgTypes... Arguments)
194 {
195 static_assert(TAnd<TIsConstructible<FSignalRValue, ArgTypes>...>::Value, "Invalid argument type passed to IHubConnection::Invoke");
196 return Invoke(EventName, TArray<FSignalRValue> { MoveTemp(Arguments)... });
197 }
198
205 virtual void Send(const FString& EventName, const TArray<FSignalRValue>& InArguments = TArray<FSignalRValue>()) = 0;
206
213 template <typename... ArgTypes>
214 FORCEINLINE void Send(const FString& EventName, ArgTypes... Arguments)
215 {
216 static_assert(TAnd<TIsConstructible<FSignalRValue, ArgTypes>...>::Value, "Invalid argument type passed to IHubConnection::Send");
217 Send(EventName, TArray<FSignalRValue> { MoveTemp(Arguments)... });
218 }
219
220protected:
221 virtual ~IHubConnection();
222};
223
224typedef TSharedPtr<IHubConnection> IHubConnectionPtr;
TSharedPtr< IHubConnection > IHubConnectionPtr
Definition IHubConnection.h:224
Represents the result of a SignalR method invocation, containing either a value or error information.
Definition IHubConnection.h:34
FORCEINLINE bool HasError() const
Checks if the invoke result contains an error.
Definition IHubConnection.h:72
FORCEINLINE const FString & GetErrorMessage() const
Definition IHubConnection.h:78
FSignalRInvokeResult(const FSignalRValue &Value)
Definition IHubConnection.h:37
FSignalRInvokeResult(FSignalRInvokeResult &&OtherValue) noexcept
Definition IHubConnection.h:45
static FORCEINLINE FSignalRInvokeResult Error(const FString &ErrorMessage)
Creates an error result with the specified message.
Definition IHubConnection.h:90
FSignalRInvokeResult & operator=(const FSignalRInvokeResult &OtherValue)
Definition IHubConnection.h:51
FSignalRInvokeResult & operator=(FSignalRInvokeResult &&OtherValue) noexcept
Definition IHubConnection.h:59
friend struct FSignalRInvokeResultWrapper
Definition IHubConnection.h:35
FSignalRInvokeResult(const FSignalRInvokeResult &OtherValue)
Definition IHubConnection.h:39
FSignalRValue()
Create an object representing a EValueType::Null value.
Definition SignalRValue.h:53
FSignalRValue & operator=(const FSignalRValue &OtherValue)
Copies an existing value.
Definition SignalRValue.h:267
Interface for a SignalR hub connection that enables real-time communication with a SignalR server.
Definition IHubConnection.h:110
virtual FOnHubConnectedEvent & OnConnected()=0
Gets the event that is triggered when the connection is successfully established.
virtual FHubConnectionClosedEvent & OnClosed()=0
Gets the event that is triggered when the connection is closed.
FORCEINLINE void Send(const FString &EventName, ArgTypes... Arguments)
Templated version of Send that converts arguments to FSignalRValue automatically.
Definition IHubConnection.h:214
virtual void Start()=0
Starts the connection to the SignalR hub.
virtual void Send(const FString &EventName, const TArray< FSignalRValue > &InArguments=TArray< FSignalRValue >())=0
Sends a message to a hub method on the server without waiting for a response.
virtual ~IHubConnection()
Definition IHubConnection.cpp:27
DECLARE_EVENT(IHubConnection, FOnHubConnectedEvent)
Delegate called when a connection has been established successfully.
DECLARE_EVENT_OneParam(IHubConnection, FOnHubConnectionErrorEvent, const FString &)
Delegate called when a connection could not be established.
virtual FOnMethodCompletion & Invoke(const FString &EventName, const TArray< FSignalRValue > &InArguments=TArray< FSignalRValue >())=0
Invokes a hub method on the server with the specified arguments and waits for a response.
virtual FOnMethodInvocation & On(const FString &EventName)=0
Registers a callback for a specific hub method invocation from the server.
virtual void Stop()=0
Stops the connection to the SignalR hub.
DECLARE_DELEGATE_OneParam(FOnMethodCompletion, const FSignalRInvokeResult &)
FORCEINLINE FOnMethodCompletion & Invoke(const FString &EventName, ArgTypes... Arguments)
Templated version of Invoke that converts arguments to FSignalRValue automatically.
Definition IHubConnection.h:193
DECLARE_DELEGATE_OneParam(FOnMethodInvocation, const TArray< FSignalRValue > &)
virtual FOnHubConnectionErrorEvent & OnConnectionError()=0
Gets the event that is triggered when a connection error occurs.
DECLARE_EVENT(IHubConnection, FHubConnectionClosedEvent)
Delegate called when a web socket connection has been closed.