Skip to content

Commit

Permalink
Improve connection initialisation stability
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Aug 20, 2024
1 parent d707bea commit 74ab504
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public int receive(byte[] buf, int off, int len, int waitMillis) throws IOExcept
public void send(byte[] buf, int off, int len) throws IOException {
System.out.println("send! " + new String(buf, off, len));
// System.out.println("send! " + bytesToHex(buf));
socket.send(new DatagramPacket(buf, off, len, component.getDefaultCandidate().getTransportAddress()));
socket.send(new DatagramPacket(buf, off, len));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.ice4j.ice.Component;
import org.ice4j.ice.IceMediaStream;
import org.ice4j.ice.IceProcessingState;
import org.ice4j.ice.LocalCandidate;
import org.ice4j.ice.RemoteCandidate;
import org.ice4j.ice.harvest.StunCandidateHarvester;
import org.ice4j.ice.harvest.TurnCandidateHarvester;
Expand Down Expand Up @@ -179,7 +180,6 @@ private void handleConnectRequest(BigInteger from, String sessionId, String mess
}
}
}
agent.startConnectivityEstablishment();

component = agent.createComponent(stream, 5000, 5000, 6000);

Expand Down Expand Up @@ -288,16 +288,18 @@ public void onRawStream(SCTPStream sctpStream) {
System.out.println(json);
send(json);

component.getLocalCandidates().forEach(candidate -> {
int i = 0;
for (LocalCandidate candidate : component.getLocalCandidates()) {
var jsonAdd = Constants.GSON.toJson(new WsToMessage(
1, from, "CANDIDATEADD " + sessionId + " " + candidate.toString() + " generation 0 ufrag " + agent.getLocalUfrag() + " network-id " + candidate.getFoundation()
1, from, "CANDIDATEADD " + sessionId + " " + candidate.toString() + " generation 0 ufrag " + agent.getLocalUfrag() + " network-id " + i + " network-cost 0"
));
i++;
System.out.println(jsonAdd);
send(jsonAdd);
});
}

agent.addStateChangeListener(evt -> {
System.out.println("state change! " + evt + " " + evt.getPropertyName());
System.out.println("state change! " + evt);
if ("IceProcessingState".equals(evt.getPropertyName()) && IceProcessingState.COMPLETED.equals(evt.getNewValue())) {
transport.init(component);
try {
Expand Down Expand Up @@ -340,14 +342,18 @@ private String fingerprintFor(byte[] input) {
return fp.toString();
}

int candidateCount = 0;
private void handleCandidateAdd(String sessionId, String message) throws UnknownHostException {
// agent.candidate
// activeSessions.get(sessionId).addCandidate(message);
component.addUpdateRemoteCandidates(parseCandidate(message, component.getParentStream()));
component.updateRemoteCandidates();
}

candidateCount++;

if (candidateCount == 4) {
component.updateRemoteCandidates();
agent.startConnectivityEstablishment();
}
}

public static RemoteCandidate parseCandidate(String value, IceMediaStream stream) {
StringTokenizer tokenizer = new StringTokenizer(value);
Expand Down Expand Up @@ -398,7 +404,6 @@ private void initialize(JsonObject message) {
var turnAuthServers = message.getAsJsonArray("TurnAuthServers");

agent = new Agent();
agent.setTrickling(true);

for (JsonElement authServerElement : turnAuthServers) {
var authServer = authServerElement.getAsJsonObject();
Expand Down

0 comments on commit 74ab504

Please sign in to comment.