about summary refs log tree commit diff
path: root/assets/src/network/transfer/transfer.ts
diff options
context:
space:
mode:
Diffstat (limited to 'assets/src/network/transfer/transfer.ts')
-rw-r--r--assets/src/network/transfer/transfer.ts98
1 files changed, 7 insertions, 91 deletions
diff --git a/assets/src/network/transfer/transfer.ts b/assets/src/network/transfer/transfer.ts
index 25511d5..976d113 100644
--- a/assets/src/network/transfer/transfer.ts
+++ b/assets/src/network/transfer/transfer.ts
@@ -1,7 +1,5 @@
-import { on, onWithToken, send } from "../channel/connection";
 import type { UnregisterHandler } from "../channel/messages/handler";
 import type {
-    ShareAcceptedMessage,
     RequestIceCandidateMessage,
     ShareIceCandidateMessage,
 } from "../channel/messages/messages";
@@ -29,77 +27,17 @@ const servers = {
     iceCandidatePoolSize: 10,
 };
 
-export async function offer(request_token: string): Promise<Transfer> {
-    const transfer = createTransfer(TransferType.OFFER);
-
-    const offer = await transfer.pc.createOffer();
-    transfer.pc.setLocalDescription(offer);
-
-    transfer.pc.onicecandidate = event => {
-        const candidate = event.candidate;
-        if (event.candidate !== null) {
-            send("ice_candidate", { candidate, token: request_token });
-        }
-    };
-
-    send("accept_request", {
-        token: request_token,
-        sdp: offer.sdp,
-        type: offer.type,
-    });
-
-    onWithToken(
-        "share_accepted",
-        request_token,
-        (message: ShareAcceptedMessage, unregister) =>
-            onShareAccepted(transfer, message, unregister)
-    );
-
-    return transfer;
-}
-
-export async function answer(
-    offer: RTCSessionDescriptionInit
-): Promise<Transfer> {
-    const transfer = createTransfer(TransferType.ANSWER);
-
-    const offerDescription = new RTCSessionDescription(offer);
-    transfer.pc.setRemoteDescription(offerDescription);
-
-    const answer = await transfer.pc.createAnswer();
-    transfer.pc.setLocalDescription(answer);
-
-    transfer.pc.onicecandidate = event => {
-        const candidate = event.candidate;
-        if (event.candidate !== null) {
-            send("ice_candidate", { candidate });
-        }
-    };
-
-    const unregisterIce = on(
-        "ice_candidate",
-        (message: RequestIceCandidateMessage) =>
-            onIncomingIceCandidate(transfer, message)
-    );
-
-    unregisterIceOnComplete(transfer, unregisterIce);
-
-    send("accept_share", {
-        sdp: answer.sdp,
-        type: answer.type,
-    });
-
-    return transfer;
-}
-
-function createTransfer(type: TransferType): Transfer {
+export function createTransfer(
+    type: TransferType,
+    onChannel: (channel: RTCDataChannel) => void
+): Transfer {
     const pc = new RTCPeerConnection(servers);
     const channel = pc.createDataChannel("channel", {
         negotiated: true,
         id: 0,
     });
 
-    // TODO: Send data after channel was opened.
+    channel.onopen = () => onChannel(channel);
 
     return {
         pc,
@@ -108,36 +46,14 @@ function createTransfer(type: TransferType): Transfer {
     };
 }
 
-function onShareAccepted(
-    transfer: Transfer,
-    message: ShareAcceptedMessage,
-    unregister: UnregisterHandler
-) {
-    const token = message.token;
-
-    const answerDescription = new RTCSessionDescription(message);
-    transfer.pc.setRemoteDescription(answerDescription);
-
-    const unregisterIce = onWithToken(
-        "ice_candidate",
-        token,
-        (message: RequestIceCandidateMessage) =>
-            onIncomingIceCandidate(transfer, message)
-    );
-
-    unregisterIceOnComplete(transfer, unregisterIce);
-
-    unregister();
-}
-
-function onIncomingIceCandidate(
+export function onIncomingIceCandidate(
     transfer: Transfer,
     message: ShareIceCandidateMessage | RequestIceCandidateMessage
 ) {
     transfer.pc.addIceCandidate(message.candidate);
 }
 
-function unregisterIceOnComplete(
+export function unregisterIceOnComplete(
     transfer: Transfer,
     unregister: UnregisterHandler
 ) {