This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
intitial stream logic #5
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,163 @@ | ||
import { Stream } from '@libp2p/interface-connection'; | ||
import { StreamStat } from '@libp2p/interface-connection'; | ||
import { logger } from '@libp2p/logger'; | ||
// import { logger } from '@libp2p/logger'; | ||
import { Source } from 'it-stream-types'; | ||
import { Sink } from 'it-stream-types'; | ||
import { pushable, Pushable } from 'it-pushable'; | ||
import defer, { DeferredPromise } from 'p-defer'; | ||
import merge from 'it-merge'; | ||
|
||
const log = logger('libp2p:webrtc:connection'); | ||
// const log = logger('libp2p:webrtc:connection'); | ||
|
||
type StreamInitOpts = { | ||
channel: RTCDataChannel; | ||
metadata?: Record<string, any>; | ||
stat: StreamStat; | ||
}; | ||
|
||
export class WebRTCStream implements Stream { | ||
constructor() { | ||
this.id = 'TODO'; | ||
this.stat = { | ||
direction: 'outbound', | ||
timeline: { | ||
open: 0, | ||
close: 0, | ||
/** | ||
* Unique identifier for a stream | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Stats about this stream | ||
*/ | ||
stat: StreamStat; | ||
|
||
/** | ||
* User defined stream metadata | ||
*/ | ||
metadata: Record<string, any>; | ||
private readonly channel: RTCDataChannel; | ||
|
||
source: Source<Uint8Array> = pushable(); | ||
sink: Sink<Uint8Array, Promise<void>>; | ||
|
||
// promises | ||
opened: DeferredPromise<void> = defer(); | ||
closeWritePromise: DeferredPromise<void> = defer(); | ||
writeClosed: boolean = false; | ||
readClosed: boolean = false; | ||
closed: boolean = false; | ||
|
||
constructor(opts: StreamInitOpts) { | ||
this.channel = opts.channel; | ||
this.id = this.channel.label; | ||
|
||
this.stat = opts.stat; | ||
switch (this.channel.readyState) { | ||
case 'open': | ||
this.opened.resolve(); | ||
break; | ||
case 'closed': | ||
case 'closing': | ||
this.closed = true; | ||
if (!this.stat.timeline.close) { | ||
this.stat.timeline.close = new Date().getTime(); | ||
} | ||
this.opened.resolve(); | ||
break; | ||
} | ||
|
||
this.metadata = opts.metadata ?? {}; | ||
|
||
// closable sink | ||
this.sink = this._sinkFn; | ||
|
||
// handle RTCDataChannel events | ||
this.channel.onopen = (_evt) => { | ||
this.stat.timeline.open = new Date().getTime(); | ||
this.opened.resolve(); | ||
}; | ||
|
||
this.channel.onmessage = ({ data }) => { | ||
if (this.readClosed || this.closed) { | ||
return; | ||
} | ||
(this.source as Pushable<Uint8Array>).push(data); | ||
}; | ||
|
||
this.channel.onclose = (_evt) => { | ||
this.close(); | ||
}; | ||
|
||
this.channel.onerror = (evt) => { | ||
let err = (evt as RTCErrorEvent).error; | ||
this.abort(err); | ||
}; | ||
} | ||
|
||
private async _sinkFn(src: Source<Uint8Array>): Promise<void> { | ||
await this.opened.promise; | ||
if (closed || this.writeClosed) { | ||
return; | ||
} | ||
|
||
let self = this; | ||
let closeWriteIterable = { | ||
async *[Symbol.asyncIterator]() { | ||
await self.closeWritePromise.promise; | ||
yield new Uint8Array(0); | ||
}, | ||
}; | ||
this.metadata = {}; | ||
this.sink = (x) => new Promise((res, rej) => {}); //TODO | ||
if (this.dataChannel) { | ||
log('TODO', this.dataChannel.id); | ||
|
||
for await (const buf of merge(closeWriteIterable, src)) { | ||
if (closed || this.writeClosed) { | ||
break; | ||
} | ||
this.channel.send(buf); | ||
} | ||
} | ||
|
||
/** | ||
* Close a stream for reading and writing | ||
*/ | ||
close(): void {} | ||
close(): void { | ||
if (this.closed) { | ||
return; | ||
} | ||
this.stat.timeline.close = new Date().getTime(); | ||
this.closed = true; | ||
this.closeRead(); | ||
ckousik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.closeWrite(); | ||
this.channel.close(); | ||
} | ||
|
||
/** | ||
* Close a stream for reading only | ||
*/ | ||
closeRead(): void {} | ||
closeRead(): void { | ||
this.readClosed = true; | ||
(this.source as Pushable<Uint8Array>).end(); | ||
if (this.readClosed && this.writeClosed) { | ||
this.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Close a stream for writing only | ||
*/ | ||
closeWrite(): void {} | ||
closeWrite(): void { | ||
this.writeClosed = true; | ||
this.closeWritePromise.resolve(); | ||
if (this.readClosed && this.writeClosed) { | ||
this.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Call when a local error occurs, should close the stream for reading and writing | ||
*/ | ||
abort(err: Error): void {} | ||
abort(err: Error): void { | ||
this.close(); | ||
} | ||
|
||
/** | ||
* Call when a remote error occurs, should close the stream for reading and writing | ||
*/ | ||
reset(): void {} | ||
|
||
/** | ||
* Unique identifier for a stream | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Stats about this stream | ||
*/ | ||
stat: StreamStat; | ||
|
||
/** | ||
* User defined stream metadata | ||
*/ | ||
metadata: Record<string, any>; | ||
|
||
source: Source<Uint8Array> = process.stdin; //TODO | ||
sink: Sink<Uint8Array, Promise<void>>; | ||
|
||
private dataChannel?: RTCDataChannel; | ||
reset(): void { | ||
this.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this, and also some other methods, alter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will add that in a future commit. |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need/want any logging?
I suppose that makes sense. The Stream's methods could be called very frequently.