Skip to content

Commit

Permalink
improve buildRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Jan 3, 2025
1 parent 95dbd1e commit 93690eb
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions packages/ice/src/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ export class Connection implements IceConnection {
this.queryConsentHandle = undefined;
});

const { localUsername, remoteUsername, iceControlling } = this;

// """
// Periodically check consent (RFC 7675).
// """
Expand All @@ -613,7 +615,12 @@ export class Connection implements IceConnection {
if (!pair) {
break;
}
const request = this.buildRequest(false);
const request = this.buildRequest({
nominate: false,
localUsername,
remoteUsername,
iceControlling,
});
try {
const [msg, addr] = await pair.protocol.request(
request,
Expand Down Expand Up @@ -875,10 +882,6 @@ export class Connection implements IceConnection {
log("check start", pair.toJSON());

pair.updateState(CandidatePairState.IN_PROGRESS);

const nominate = this.iceControlling && !this.remoteIsLite;
const request = this.buildRequest(nominate);

const result: { response?: Message; addr?: Address } = {};
const {
remotePassword,
Expand All @@ -887,6 +890,15 @@ export class Connection implements IceConnection {
localPassword,
generation,
} = this;

const nominate = this.iceControlling && !this.remoteIsLite;
const request = this.buildRequest({
nominate,
localUsername,
remoteUsername,
iceControlling: this.iceControlling,
});

try {
const [response, addr] = await pair.protocol.request(
request,
Expand Down Expand Up @@ -958,7 +970,12 @@ export class Connection implements IceConnection {
} else if (this.iceControlling && !this.nominating) {
// # perform regular nomination
this.nominating = true;
const request = this.buildRequest(true);
const request = this.buildRequest({
nominate: true,
localUsername,
remoteUsername,
iceControlling: this.iceControlling,
});
try {
await pair.protocol.request(
request,
Expand Down Expand Up @@ -1084,13 +1101,23 @@ export class Connection implements IceConnection {
}
};

private buildRequest(nominate: boolean) {
const txUsername = `${this.remoteUsername}:${this.localUsername}`;
private buildRequest({
nominate,
remoteUsername,
localUsername,
iceControlling,
}: {
nominate: boolean;
remoteUsername: string;
localUsername: string;
iceControlling: boolean;
}) {
const txUsername = `${remoteUsername}:${localUsername}`;
const request = new Message(methods.BINDING, classes.REQUEST);
request
.setAttribute("USERNAME", txUsername)
.setAttribute("PRIORITY", candidatePriority("prflx"));
if (this.iceControlling) {
if (iceControlling) {
request.setAttribute("ICE-CONTROLLING", this.tieBreaker);
if (nominate) {
request.setAttribute("USE-CANDIDATE", null);
Expand Down

0 comments on commit 93690eb

Please sign in to comment.