Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
InteractiveProcess.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/Queue.h"
7#include "Containers/UnrealString.h"
8#include "CoreTypes.h"
9#include "Delegates/Delegate.h"
10#include "HAL/PlatformProcess.h"
11#include "HAL/Runnable.h"
12#include "Logging/LogMacros.h"
13#include "Misc/DateTime.h"
14#include "Misc/Timespan.h"
15
16class FRunnableThread;
17
19
20/**
21* Declares a delegate that is executed when a interactive process completed.
22*
23* The first parameter is the process return code.
24*/
25DECLARE_DELEGATE_TwoParams(FOnInteractiveProcessCompleted, int32, bool);
26
27/**
28* Declares a delegate that is executed when a interactive process produces output.
29*
30* The first parameter is the produced output.
31*/
32DECLARE_DELEGATE_OneParam(FOnInteractiveProcessOutput, const FString&);
33
34/**
35* Implements an external process that can be interacted.
36*/
38 : public FRunnable
39{
40public:
41
42 /**
43 * Creates a new interactive process.
44 *
45 * @param InURL The URL of the executable to launch.
46 * @param InParams The command line parameters.
47 * @param InHidden Whether the window of the process should be hidden.
48 */
49 FInteractiveProcess(const FString& InURL, const FString& InParams, bool InHidden, bool LongTime = false);
50
51 /**
52 * Creates a new interactive process.
53 *
54 * @param InURL The URL of the executable to launch.
55 * @param InParams The command line parameters.
56 * @param InWorkingDir The URL of the working dir where the executable should launch.
57 * @param InHidden Whether the window of the process should be hidden.
58 */
59 FInteractiveProcess(const FString& InURL, const FString& InParams, const FString& InWorkingDir, bool InHidden, bool LongTime = false);
60
61 /** Destructor. */
63
64 /**
65 * Gets the duration of time that the task has been running.
66 *
67 * @return Time duration.
68 */
70
71 /**
72 * Gets the Process Handle. The instance can be invalid if the process was not created.
73 *
74 * @return The Process Handle
75 */
77 {
78 return ProcessHandle;
79 }
80
81 /**
82 * Checks whether the process is still running.
83 *
84 * @return true if the process is running, false otherwise.
85 */
86 bool IsRunning() const
87 {
88 return Thread != nullptr;
89 }
90
91 /**
92 * Launches the process
93 *
94 * @return True if succeed
95 */
96 bool Launch();
97
98 /**
99 * Returns a delegate that is executed when the process has been canceled.
100 *
101 * @return The delegate.
102 */
103 FSimpleDelegate& OnCanceled()
104 {
105 return CanceledDelegate;
106 }
107
108 /**
109 * Returns a delegate that is executed when the interactive process completed.
110 * Delegate won't be executed if process terminated without user wanting
111 *
112 * @return The delegate.
113 */
114 FOnInteractiveProcessCompleted& OnCompleted()
115 {
116 return CompletedDelegate;
117 }
118
119 /**
120 * Returns a delegate that is executed when a interactive process produces output.
121 *
122 * @return The delegate.
123 */
124 FOnInteractiveProcessOutput& OnOutput()
125 {
126 return OutputDelegate;
127 }
128
129 /**
130 * Sends the string message when process is ready
131 *
132 * @param Message to be sent
133 */
134 void SendWhenReady(const FString &Message);
135
136 /**
137 * Sends the data message when process is ready
138 *
139 * @param Data to be sent
140 */
141 void SendWhenReady(const TArray<uint8> &Data);
142
143 /**
144 * Returns the return code from the exited process
145 *
146 * @return Process return code
147 */
148 int GetReturnCode() const
149 {
150 return ReturnCode;
151 }
152
153 /**
154 * Cancels the process.
155 *
156 * @param InKillTree Whether to kill the entire process tree when canceling this process.
157 */
158 void Cancel(bool InKillTree = false)
159 {
160 bCanceling = true;
161 bKillTree = InKillTree;
162 }
163
164 // FRunnable interface
165
166 virtual bool Init() override
167 {
168 return true;
169 }
170
171 virtual uint32 Run() override;
172
173 virtual void Stop() override
174 {
175 Cancel();
176 }
177
178 virtual void Exit() override { }
179
180protected:
181
182 /**
183 * Processes the given output string.
184 *
185 * @param Output The output string to process.
186 */
187 void ProcessOutput(const FString& Output);
188
189 /**
190 * Takes the first message to be sent from MessagesToProcess, if there is one, and sends it to process
191 */
193
194private:
195 // Whether the process is being canceled. */
196 bool bCanceling : 1;
197
198 // Whether the window of the process should be hidden. */
199 bool bHidden : 1;
200
201 // Whether to kill the entire process tree when cancelling this process. */
202 bool bKillTree : 1;
203
204 // How many milliseconds should the process sleep */
206
207 // Holds the URL of the executable to launch. */
209
210 // Holds the command line parameters. */
212
213 // Holds the URL of the working dir for the process. */
215
216 // Holds the handle to the process. */
218
219 // Holds the read pipe of parent process. */
221
222 // Holds the write pipe of parent process. */
224
225 // Holds the read pipe of child process. Should not be used except for testing */
227
228 // Holds the write pipe of child process. Should not be used except for testing */
230
231 // Holds the thread object. */
233
234 // Holds the name of thread */
236
237 // Holds the return code. */
239
240 // Holds the time at which the process started. */
242
243 // Holds the time at which the process ended. */
245
246 // Holds string messages to be written to pipe when ready */
248
249 // Holds data messages to be written to pipe when ready */
251
252 // Holds a delegate that is executed when the process has been canceled. */
253 FSimpleDelegate CanceledDelegate;
254
255 // Holds a delegate that is executed when a interactive process completed. */
256 FOnInteractiveProcessCompleted CompletedDelegate;
257
258 // Holds a delegate that is executed when a interactive process produces output. */
259 FOnInteractiveProcessOutput OutputDelegate;
260};
#define DECLARE_DELEGATE_TwoParams(DelegateName, Param1Type, Param2Type)
#define DECLARE_DELEGATE_OneParam(DelegateName, Param1Type)
DECLARE_LOG_CATEGORY_EXTERN(LogChunkInstaller, Log, All)
FOnInteractiveProcessCompleted CompletedDelegate
FSimpleDelegate CanceledDelegate
virtual void Stop() override
void SendWhenReady(const FString &Message)
FOnInteractiveProcessOutput & OnOutput()
FInteractiveProcess(const FString &InURL, const FString &InParams, bool InHidden, bool LongTime=false)
virtual uint32 Run() override
FSimpleDelegate & OnCanceled()
TQueue< FString > StringMessagesToProcess
FProcHandle GetProcessHandle() const
FOnInteractiveProcessOutput OutputDelegate
FOnInteractiveProcessCompleted & OnCompleted()
void Cancel(bool InKillTree=false)
void SendWhenReady(const TArray< uint8 > &Data)
TQueue< TArray< uint8 > > DataMessagesToProcess
virtual bool Init() override
FInteractiveProcess(const FString &InURL, const FString &InParams, const FString &InWorkingDir, bool InHidden, bool LongTime=false)
void ProcessOutput(const FString &Output)
FTimespan GetDuration() const
virtual void Exit() override
FRunnableThread * Thread
void SendMessageToProcessIf()