Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed May 21, 2024
1 parent 359452f commit 85946c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"args": [
"--project",
"${workspaceRoot}/tsconfig.json",
"${workspaceRoot}/examples/save_to_disk/mp4/av.ts"
"${workspaceRoot}/packages/ice/examples/turn_turn.ts"
],
"env": {
"DEBUG": "werift*"
Expand Down
28 changes: 21 additions & 7 deletions packages/ice/src/turn/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ class TurnClient implements Protocol {
message.messageClass === classes.ERROR
) {
const transaction = this.transactions[message.transactionIdHex];
if (transaction) transaction.responseReceived(message, addr);
if (transaction) {
transaction.responseReceived(message, addr);
}
} else if (message.messageClass === classes.REQUEST) {
this.onDatagramReceived(data, addr);
}
Expand Down Expand Up @@ -163,7 +165,12 @@ class TurnClient implements Protocol {
.setAttribute("LIFETIME", this.lifetime)
.setAttribute("REQUESTED-TRANSPORT", UDP_TRANSPORT);

const [response] = await this.requestWithRetry(request, this.server);
const [response] = await this.requestWithRetry(request, this.server).catch(
(e) => {
log("connect error", e);
throw e;
},
);
this.relayedAddress = response.getAttributeValue("XOR-RELAYED-ADDRESS");
this.mappedAddress = response.getAttributeValue("XOR-MAPPED-ADDRESS");
const exp = response.getAttributeValue("LIFETIME");
Expand Down Expand Up @@ -200,7 +207,9 @@ class TurnClient implements Protocol {
const request = new Message(methods.REFRESH, classes.REQUEST);
request.setAttribute("LIFETIME", exp);

await this.requestWithRetry(request, this.server);
await this.requestWithRetry(request, this.server).catch((e) => {
log("refresh error", e);
});
}
});

Expand Down Expand Up @@ -238,19 +247,23 @@ class TurnClient implements Protocol {
[message, address] = await this.request(request, addr);
} catch (error) {
if (error instanceof TransactionFailed == false) {
log("requestWithRetry error", error);
throw error;
}

// resolve dns address
this.server = error.addr;

const errorCode = error.response.getAttributeValue("ERROR-CODE");
const [errorCode] = error.response.getAttributeValue("ERROR-CODE");
const nonce = error.response.getAttributeValue("NONCE");
const realm = error.response.getAttributeValue("REALM");

if (
nonce &&
((errorCode === 401 && realm) || (errorCode === 438 && this.realm))
((errorCode === 401 && realm) || (errorCode === 438 && this.realm)) &&
nonce
) {
log("retry with nonce", errorCode);

this.nonce = nonce;
if (errorCode === 401) {
this.realm = realm;
Expand All @@ -260,8 +273,9 @@ class TurnClient implements Protocol {
this.realm!,
this.password,
);

request.transactionId = randomTransactionId();
[message, address] = await this.request(request, addr);
[message, address] = await this.request(request, this.server);
} else {
throw error;
}
Expand Down

0 comments on commit 85946c0

Please sign in to comment.