Skip to content

Commit

Permalink
Merge branch 'Tencent:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoChenFx authored Nov 21, 2024
2 parents 8408b61 + 283f45b commit 2706e2f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unity_build_plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ on:
- unity/native_src/**
- unity/cli/**
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
- unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.h
- unreal/Puerts/Source/JsEnv/Private/PromiseRejectCallback.hpp
- unreal/Puerts/ThirdParty/**
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unity_unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- unity/cli/**
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.cpp
- unreal/Puerts/Source/JsEnv/Private/V8InspectorImpl.h
- unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp
- unreal/Puerts/Source/JsEnv/Private/PromiseRejectCallback.hpp
- .github/workflows/unity_unittest.yml

Expand Down
16 changes: 8 additions & 8 deletions unity/Assets/core/upm/Runtime/Resources/puerts/websocketpp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class WebSocket extends EventTarget {
this._cleanup();
this._addPendingEvent({type:'close', code:code, reason: reason});
},
() => {
this._fail();
(err) => {
this._fail(err);
});

this._readyState = WebSocket.CONNECTING;
Expand All @@ -89,20 +89,20 @@ class WebSocket extends EventTarget {
send(data) {
if (this._readyState !== WebSocket.OPEN) {
//throw new Error(`WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`);
this.dispatchEvent({type:'error'}); //dispatchEvent immediately
this.dispatchEvent({type:'error', data: `WebSocket is not open: readyState ${this._readyState} (${readyStates[this._readyState]})`}); //dispatchEvent immediately
return;
}
try {
this._raw.send(data);
} catch {
this._fail();
} catch (e) {
this._fail(e.message);
}
}

_fail() {
this._addPendingEvent({type:'error'});
_fail(err) {
this._addPendingEvent({type:'error', data: err});
this._cleanup();
this._addPendingEvent({type:'close', code:1006, reason: ""});
this._addPendingEvent({type:'close', code:1006, reason: err});
}

_cleanup() {
Expand Down
23 changes: 23 additions & 0 deletions unity/test/Src/Cases/WebsocketTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public async Task SendAsync(string message)
await _webSocket.SendAsync(new ArraySegment<byte>(responseBuffer), WebSocketMessageType.Text, true, CancellationToken.None);
Console.WriteLine($"Sent message to client: {message}");
}

public async Task SendAsync(byte[] buffer)
{
if (buffer == null || buffer.Length == 0)
{
throw new ArgumentException("Buffer cannot be null or empty.", nameof(buffer));
}

await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, true, CancellationToken.None);
Console.WriteLine($"Sent binary data to client: {BitConverter.ToString(buffer)}");
}

public async Task CloseAsync()
{
Expand Down Expand Up @@ -104,6 +115,9 @@ public async Task SmokeTest()
con.addEventListener('message', (ev) => {
console.log(`on message: ${ev.data}`);
global.webSocketMessage = ev.data;
if (ev.data instanceof ArrayBuffer) {
global.webSocketMessage = Array.from(new Uint8Array(ev.data)).map(byte => byte.toString()).join(',');
}
//con.close();
});
con.addEventListener('close', (ev) => {
Expand All @@ -129,6 +143,15 @@ public async Task SmokeTest()
Assert.AreEqual(res, "puerts websocket");

waitJsEnv();

byte[] buffer = new byte[] {0,0,0,46,14,0,34,8,128,32,16,1,24,2,34,15,87,90,82,89,45,49,56,57,57,54,57,50,56,56,48,40,6,48,0,56,0,72,0,88,0,0,2,8,0,15};
await wss.SendAsync(buffer);

waitJsEnv();

res = jsEnv.Eval<string>("global.webSocketMessage");

Assert.AreEqual("0,0,0,46,14,0,34,8,128,32,16,1,24,2,34,15,87,90,82,89,45,49,56,57,57,54,57,50,56,56,48,40,6,48,0,56,0,72,0,88,0,0,2,8,0,15", res);

jsEnv.Eval(@"
con._raw.send = () => {throw new Error()};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ private static void UnLuaDefaultParamCollectorUbtPluginExporter(IUhtExportFactor

paramDefaultValueMetas.Generate();

#if UE_5_5_OR_LATER
bool bHasGameRuntime = (from module in factory.Session.Modules where module.Module.ModuleType == UHTModuleType.GameRuntime select module).Any();
#else
bool bHasGameRuntime = (from package in factory.Session.Packages where package.Module.ModuleType == UHTModuleType.GameRuntime select package).Any();
#endif
paramDefaultValueMetas.OutputIfNeeded(bHasGameRuntime);
}

Expand Down Expand Up @@ -61,9 +65,15 @@ private void Generate()
{
GeneratedContentBuilder.Append("// Generated By C# UbtPlugin\r\n");

var packages = (from package in Factory.Session.Packages
#if UE_5_5_OR_LATER
var packages = (from module in Factory.Session.Modules
where (module.Module.ModuleType == UHTModuleType.EngineRuntime || module.Module.ModuleType == UHTModuleType.GameRuntime)
select module.ScriptPackage);
#else
var packages = (from package in Factory.Session.Packages
where (package.Module.ModuleType == UHTModuleType.EngineRuntime || package.Module.ModuleType == UHTModuleType.GameRuntime)
select package);
#endif

var classes = packages
.SelectMany(TraverseTree)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
<Import Project="$(EngineDir)\Source\Programs\Shared\UnrealEngine.csproj.props" />

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>


<EngineVersion >$([System.Text.RegularExpressions.Regex]::Match($(EngineDir), "\d+.\d+"))</EngineVersion >
<TargetFramework Condition="$([MSBuild]::VersionGreaterThanOrEquals($(EngineVersion), '5.5'))">net8.0</TargetFramework>
<TargetFramework Condition="$([MSBuild]::VersionLessThan($(EngineVersion), '5.5'))">net6.0</TargetFramework>

<Configuration Condition=" '$(Configuration)' == '' ">Development</Configuration>
<OutputType>Library</OutputType>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down
20 changes: 4 additions & 16 deletions unreal/Puerts/Source/JsEnv/Private/WebSocketImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ class DataTransfer
DataLength = BS->ByteLength();
return BS->Data();
#endif
#endif
}

static v8::Local<v8::ArrayBuffer> NewArrayBuffer(v8::Local<v8::Context> Context, void* Data, size_t DataLength)
{
#if defined(HAS_ARRAYBUFFER_NEW_WITHOUT_STL)
return v8::ArrayBuffer_New_Without_Stl(Context->GetIsolate(), Data, DataLength);
#else
#if USING_IN_UNREAL_ENGINE
return v8::ArrayBuffer::New(Context->GetIsolate(), Data, DataLength);
#else
auto Backing = v8::ArrayBuffer::NewBackingStore(Data, DataLength, v8::BackingStore::EmptyDeleter, nullptr);
return v8::ArrayBuffer::New(Context->GetIsolate(), std::move(Backing));
#endif
#endif
}
};
Expand Down Expand Up @@ -358,8 +344,10 @@ void V8WebSocketClientImpl::OnMessage(wspp_connection_hdl InHandle, wspp_message
}
else if (InMessage->get_opcode() == websocketpp::frame::opcode::BINARY)
{
args[0] = DataTransfer::NewArrayBuffer(
GContext.Get(Isolate), (void*) InMessage->get_payload().c_str(), InMessage->get_payload().size());
v8::Local<v8::ArrayBuffer> Ab = v8::ArrayBuffer::New(Isolate, InMessage->get_payload().size());
void* Buff = DataTransfer::GetArrayBufferData(Ab);
::memcpy(Buff, InMessage->get_payload().data(), InMessage->get_payload().size());
args[0] = Ab;
}
else
{
Expand Down
46 changes: 45 additions & 1 deletion unreal/Puerts/Source/JsEnv/Public/StaticCall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

void SetArgument(typename ArgumentType<T>::type InArg)
{
Arg = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
}
Expand Down Expand Up @@ -418,6 +423,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

void SetArgument(typename ArgumentType<T>::type InArg)
{
Arg = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
if (&Buf != &(Arg.get()))
Expand Down Expand Up @@ -452,6 +462,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

void SetArgument(typename ArgumentType<T>::type InArg)
{
Arg = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
}
Expand All @@ -476,6 +491,15 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

using BuffType = typename std::remove_const<typename std::remove_reference<T>::type>::type;
BuffType Buf;

void SetArgument(BuffType InArg)
{
Buf = InArg;
Arg = Buf;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
}
Expand All @@ -501,6 +525,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

void SetArgument(typename ArgumentType<T>::type InArg)
{
Arg = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
API::UpdateRefValue(context, holder, API::template Converter<typename std::decay<T>::type>::toScript(context, Arg));
Expand All @@ -527,6 +556,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg ? Arg : &Buf;
}

void SetArgument(BuffType InArg)
{
Buf = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
API::UpdateRefValue(context, holder, API::template Converter<BuffType>::toScript(context, Buf));
Expand All @@ -551,6 +585,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

void SetArgument(T InArg)
{
Arg = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
}
Expand All @@ -574,6 +613,11 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
return Arg;
}

void SetArgument(typename ArgumentType<T>::type InArg)
{
Arg = InArg;
}

void SetRef(typename API::ContextType context, typename API::ValueType holder)
{
}
Expand Down Expand Up @@ -615,7 +659,7 @@ struct FuncCallHelper<API, std::pair<Ret, std::tuple<Args...>>, CheckArguments,
{
if (argCount <= Pos)
{
std::get<Pos>(cppArgHolders).Arg = defaultValue;
std::get<Pos>(cppArgHolders).SetArgument(defaultValue);
}
DefaultValueSetter<0, Pos + 1, FullArgs...>::Set(cppArgHolders, argCount, rest...);
}
Expand Down

0 comments on commit 2706e2f

Please sign in to comment.