@@ -25,7 +25,7 @@ var CHUNK_SIZE = 1024 * 15;
2525// the 16MB for Chrome 37+ and "100 messages" of previous versions mentioned):
2626// https://code.google.com/p/webrtc/issues/detail?id=2866
2727// CONSIDER: make it 0. There is no size in the spec.
28- var PC_QUEUE_LIMIT = 1024 * 250 ;
28+ export var PC_QUEUE_LIMIT = 1024 * 250 ;
2929// Javascript has trouble representing integers larger than 2^53. So we simply
3030// don't support trying to send array's bigger than that.
3131var MAX_MESSAGE_SIZE = Math . pow ( 2 , 53 ) ;
@@ -70,9 +70,9 @@ export interface DataChannel {
7070 // cleared. There can only be one listener at a time. Pass null to unset.
7171 setOverflowListener ( listener :( overflow :boolean ) => void ) : void ;
7272
73- // Closes this data channel.
73+ // Closes this data channel once all outgoing messages have been sent .
7474 // A channel cannot be re-opened once this has been called.
75- close ( ) : void ;
75+ close ( ) : Promise < void > ;
7676
7777 toString ( ) : string ;
7878}
@@ -109,6 +109,13 @@ export class DataChannelClass implements DataChannel {
109109 public onceOpened :Promise < void > ;
110110 public onceClosed :Promise < void > ;
111111
112+ // True iff close() has been called.
113+ private draining_ = false ;
114+ private fulfillDrained_ :( ) => void ;
115+ private onceDrained_ = new Promise ( ( F , R ) => {
116+ this . fulfillDrained_ = F ;
117+ } ) ;
118+
112119 private opennedSuccessfully_ :boolean ;
113120 private rejectOpened_ :( e :Error ) => void ;
114121
@@ -119,10 +126,8 @@ export class DataChannelClass implements DataChannel {
119126
120127 // |rtcDataChannel_| is the freedom rtc data channel.
121128 // |label_| is the rtcDataChannel_.getLabel() result
122- // |id| is the Freedom GUID for the underlying browser object.
123129 constructor ( private rtcDataChannel_ :freedom_RTCDataChannel . RTCDataChannel ,
124- private label_ :string ,
125- id :string ) {
130+ private label_ :string ) {
126131 this . dataFromPeerQueue = new handler . Queue < Data , void > ( ) ;
127132 this . toPeerDataQueue_ = new handler . Queue < Data , void > ( ) ;
128133 this . toPeerDataBytes_ = 0 ;
@@ -153,7 +158,7 @@ export class DataChannelClass implements DataChannel {
153158 } ) ;
154159 this . onceOpened . then ( ( ) => {
155160 this . opennedSuccessfully_ = true ;
156- this . toPeerDataQueue_ . setNextHandler ( this . handleSendDataToPeer_ ) ;
161+ this . conjestionControlSendHandler ( ) ;
157162 } ) ;
158163 this . onceClosed . then ( ( ) => {
159164 if ( ! this . opennedSuccessfully_ ) {
@@ -164,6 +169,10 @@ export class DataChannelClass implements DataChannel {
164169 }
165170 this . opennedSuccessfully_ = false ;
166171 } ) ;
172+ this . onceDrained_ . then ( ( ) => {
173+ log . debug ( 'all messages sent, closing' ) ;
174+ this . rtcDataChannel_ . close ( ) ;
175+ } ) ;
167176 }
168177
169178
@@ -303,6 +312,9 @@ export class DataChannelClass implements DataChannel {
303312 } else {
304313 if ( this . toPeerDataQueue_ . getLength ( ) === 0 ) {
305314 this . setOverflow_ ( false ) ;
315+ if ( this . draining_ ) {
316+ this . fulfillDrained_ ( ) ;
317+ }
306318 }
307319 // This processes one block from the queue, which (in Chrome) is
308320 // expected to be no larger than 4 KB. We will then go through the
@@ -316,8 +328,17 @@ export class DataChannelClass implements DataChannel {
316328 } ) ;
317329 }
318330
319- public close = ( ) : void => {
320- this . rtcDataChannel_ . close ( ) ;
331+ public close = ( ) : Promise < void > => {
332+ log . debug ( 'close requested (%1 messages to send)' ,
333+ this . toPeerDataQueue_ . getLength ( ) ) ;
334+
335+ this . draining_ = true ;
336+
337+ if ( this . toPeerDataQueue_ . getLength ( ) === 0 ) {
338+ this . fulfillDrained_ ( ) ;
339+ }
340+
341+ return this . onceClosed ;
321342 }
322343
323344 public getBrowserBufferedAmount = ( ) : Promise < number > => {
@@ -343,15 +364,19 @@ export class DataChannelClass implements DataChannel {
343364 }
344365} // class DataChannelClass
345366
346-
347- // Static, async constructor that returns a DataChannel ready for use.
348- // |id| is the GUID generated by Freedom to identify the underlying
349- // browser object.
367+ // Static constructor which constructs a core.rtcdatachannel instance
368+ // given a core.rtcdatachannel GUID.
350369export function createFromFreedomId ( id :string ) : Promise < DataChannel > {
351- var rtcDataChannel = freedom [ 'core.rtcdatachannel' ] ( id ) ;
370+ return createFromRtcDataChannel ( freedom [ 'core.rtcdatachannel' ] ( id ) ) ;
371+ }
372+
373+ // Static constructor which constructs a core.rtcdatachannel instance
374+ // given a core.rtcdatachannel instance.
375+ export function createFromRtcDataChannel (
376+ rtcDataChannel :freedom_RTCDataChannel . RTCDataChannel ) : Promise < DataChannel > {
352377 return rtcDataChannel . setBinaryType ( 'arraybuffer' ) . then ( ( ) => {
353378 return rtcDataChannel . getLabel ( ) . then ( ( label :string ) => {
354- return new DataChannelClass ( rtcDataChannel , label , id ) ;
379+ return new DataChannelClass ( rtcDataChannel , label ) ;
355380 } ) ;
356381 } ) ;
357382}
0 commit comments