Skip to content

Commit

Permalink
PeerConnectionのCreate/Closeでエラーが発生しても処理を継続するように変更 (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: Takuya Okita <takuya10@gmail.com>
  • Loading branch information
kiyohome and sasuke9410 committed Dec 15, 2023
1 parent 092011a commit aebd0e1
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Runtime/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class Host
/// <summary>
/// Id.
/// </summary>
public string Id { get;}
public string Id { get; }

/// <summary>
/// Name.
/// </summary>
public string Name { get;}
public string Name { get; }

/// <summary>
/// Creates a new host.
Expand Down
17 changes: 15 additions & 2 deletions Runtime/NativePeerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private static void LogIceServers(RTCConfiguration pcConfig)
{
if (pcConfig.iceServers is null)
{
// Not covered by testing due to defensive implementation
Logger.LogDebug("Ice server: None");
}
else
Expand Down Expand Up @@ -371,10 +372,22 @@ private void CreatePc(string id, bool isOffer)
}
};

pcCreateHooks.ForEach(hook => hook.Invoke(id, isOffer, pc));
pcCreateHooks.ForEach(hook => HandleHook(nameof(CreatePc), () => hook.Invoke(id, isOffer, pc)));
pcDict.Add(id, pc);
}

private static void HandleHook(string name, Action hook)
{
try
{
hook.Invoke();
}
catch (Exception e)
{
Logger.LogError($"Error has occured at {name}", e);
}
}

private async UniTask SendSdpByCompleteOrTimeoutAsync(string to, RTCPeerConnection pc)
{
var isTimeout = false;
Expand Down Expand Up @@ -402,7 +415,7 @@ private void ClosePc(string from)
from,
pc =>
{
pcCloseHooks.ForEach(hook => hook.Invoke(from));
pcCloseHooks.ForEach(hook => HandleHook(nameof(ClosePc), () => hook.Invoke(from)));
pc.Close();
pcDict.Remove(from);
});
Expand Down
4 changes: 4 additions & 0 deletions Samples~/MVS/ClientControl/ClientControlScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ protected override void Configure(IContainerBuilder builder)
builder.RegisterComponent(peerClient);

#if !UNITY_WEBGL || UNITY_EDITOR
NativeFailureClient.NativeFailureHook(peerClient as NativePeerClient);
builder.RegisterComponent<DataChannelClient>(new NativeDataChannelClient(peerClient as NativePeerClient));
NativeFailureClient.NativeFailureHook(peerClient as NativePeerClient);
#else
WebGLFailureClient.FailureConnect();
builder.RegisterComponent<DataChannelClient>(new WebGLDataChannelClient());
WebGLFailureClient.FailureConnect();
#endif

builder.RegisterEntryPoint<ClientControlPresenter>();
Expand Down
13 changes: 13 additions & 0 deletions Samples~/MVS/ClientControl/NativeFailureClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if !UNITY_WEBGL || UNITY_EDITOR
namespace Extreal.Integration.P2P.WebRTC.MVS.ClientControl
{
public class NativeFailureClient
{
public static void NativeFailureHook(NativePeerClient peerClient)
{
peerClient.AddPcCreateHook((id, isOffer, pc) => throw new System.Exception("PeerClient Create Error Test"));
peerClient.AddPcCloseHook(id => throw new System.Exception("PeerClient Close Error Test"));
}
}
}
#endif
3 changes: 3 additions & 0 deletions Samples~/MVS/ClientControl/NativeFailureClient.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Samples~/MVS/ClientControl/WebGLFailureClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if UNITY_WEBGL && !UNITY_EDITOR
using Extreal.Integration.Web.Common;

namespace Extreal.Integration.P2P.WebRTC.MVS.ClientControl
{
public class WebGLFailureClient
{
public static void FailureConnect() => WebGLHelper.CallAction("failure");
}
}
#endif
3 changes: 3 additions & 0 deletions Samples~/MVS/ClientControl/WebGLFailureClient.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Samples~/MVS/WebGLScripts~/src/FailureClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PeerClientProvider } from "@extreal-dev/extreal.integration.p2p.webrtc";

class FailureClient {
private static createPc = (id: string, isOffer: boolean, pc: RTCPeerConnection) => {
throw new Error("CreatePeerClient Error Test");
};

private static closePc = (id: string) => {
throw new Error("ClosePeerClient Error Test");
};

static failureHook(getPeerClient: PeerClientProvider) {
getPeerClient().addPcCreateHook(FailureClient.createPc)
getPeerClient().addPcCloseHook(FailureClient.closePc);
}
}

export { FailureClient };
3 changes: 3 additions & 0 deletions Samples~/MVS/WebGLScripts~/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { addAction } from "@extreal-dev/extreal.integration.web.common";
import { PeerAdapter } from "@extreal-dev/extreal.integration.p2p.webrtc";
import { DataChannelClient } from "./DataChannelClient";
import { FailureClient } from "./FailureClient";


const peerAdapter = new PeerAdapter();
peerAdapter.adapt();

let dataChannelClient: DataChannelClient;
addAction("start", () => dataChannelClient = new DataChannelClient(peerAdapter.getPeerClient));
addAction("clear", () => dataChannelClient.clear);
addAction("failure", () => FailureClient.failureHook(peerAdapter.getPeerClient));
22 changes: 16 additions & 6 deletions WebScripts~/src/PeerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ class PeerClient {
this.socket = null;
};

private handleHook = async (hook: Function) => {
try {
if (isAsync(hook)){
await hook()
} else {
hook()
}
} catch (e) {
console.error(e);
}
}

private createPcAsync = async (id: string, isOffer: boolean) => {
if (this.pcMap.has(id)) {
return;
Expand Down Expand Up @@ -288,11 +300,7 @@ class PeerClient {
};

for (const hook of this.pcCreateHooks) {
if (isAsync(hook)) {
await hook(id, isOffer, pc);
} else {
hook(id, isOffer, pc);
}
await this.handleHook(() => hook(id, isOffer, pc));
}
this.pcMap.set(id, pc);
};
Expand All @@ -315,7 +323,9 @@ class PeerClient {

private closePc = (from: string) => {
this.handlePc("closePc", from, (pc: RTCPeerConnection) => {
this.pcCloseHooks.forEach((hook) => hook(from));
this.pcCloseHooks.forEach((hook) => {
this.handleHook(() => hook(from));
});
pc.close();
this.pcMap.delete(from);
});
Expand Down
8 changes: 4 additions & 4 deletions WebScripts~/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@extreal-dev/extreal.integration.web.common@^0.0.9":
version "0.0.9"
resolved "https://registry.yarnpkg.com/@extreal-dev/extreal.integration.web.common/-/extreal.integration.web.common-0.0.9.tgz#10e5674e744e6d835bbc6f197fbf14790eae608e"
integrity sha512-HVtXEX7ukxv265SKcHfk1y1FefeWlQrotF7YnJn/gyPw9qp9hTjZ/qVXr0byk8hIC0pXrFYOux/sqgAbrAupiw==
"@extreal-dev/extreal.integration.web.common@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@extreal-dev/extreal.integration.web.common/-/extreal.integration.web.common-1.0.0.tgz#3113ecaf48e44cf6712d18fc6b40991b4cd82d3a"
integrity sha512-aH5A5ClpaKVUFE1DbvYVy4L7J81wo+oQmBJJmc6geR5CQTa7942CUYW11jnPp5hCq1QXG+jz/yVpU26p00I+Qw==

"@jridgewell/gen-mapping@^0.3.0":
version "0.3.3"
Expand Down

0 comments on commit aebd0e1

Please sign in to comment.