Monday, September 29, 2008

tee-sharp a.k.a. CopyStream

How many times do you have one stream, but you actually want to write the data to multiple places simultaneously[0]? Well, now you can[1]!

I took an hour and spun up this awesome asynchronous beast of a stream splitter. There is an optimisation that could be applied to it: Reads can be performed at the same time as writes. I figured that for a 1.0 implementation, this was good enough. If anyone wants to try their hand at making the read perform in parallel with the writes, feel free. Patches are welcome ;)

[0] http://tirania.org/blog/archive/2008/Sep-24.html
[1] http://www.monsoon-project.org/jaws/data/files/TeeStream.cs

EDIT: There's also a 'deliberate' bug there. 10 points to the first person to spot it and bonus 10 points if you can fix it with less than 5 lines of extra code.

8 comments:

Amber said...

hmmm it could be benficial to pass something other than null for state in line 106?

No time to look any further right now
(on an AIX job here)

Alan said...

It'd be a waste of time. That is a synchronous function, so it won't return until all the copying has been completed.

When calling async methods, you typically use the 'state' variable to store an object so you can figure out which of your async calls has completed. This is irrelevant with sync methods. This is also why 'callback' is null aswell.

Jonathan Pryor said...

1. TeeStream isn't a great name, as it's not actually a Stream subclass.

2. Would you be at all interested in adding this to Mono.Rocks? I think it could be munged into a set of extension methods, e.g.

static class StreamRocks {
public static void WriteTo (this Stream self, params Stream[] destinations);
public static void WriteTo (this Stream self, IEnumerable<Stream> destinations);
}

Though I'm at a loss for what the API for the async version should look like...

Amber said...

(a) frankly I don't fully grok all the async going on
(b) I don't have the time to work with the code to see how it works

So from my visual inspection only, wouldn't BeginCopy finally end up calling EndWrite somehow? In 174/176 this would result in an error since that asyncresult seems to be set to null?

I'm having the sneaking suspicion that multiple *different* async-result instances are being used.

I'd venture that if my understanding of this code is so far off that I'm askinga silly question here, perhaps this means that better variable naming is in order to distinguish *what* result is being passed/used for *what* purpose.

I must admit, there is much beauty in this aync beast, but (as I have learned in my Perl days) beauty should never come with obfuscation :)

Alan said...

Well, the bug in the code is that it's possible for the copyResult to be 'completed' more than once, meaning the callback could be invoked more than once, which is bad. When you work with Async code all the time, you notice these things pretty quickly ;)

The thing to note in the code is that the 'copyResult' is like the *global* IAsyncResult. This will only get signalled when the entire operation has been completed.

The idea is:
1) Read a chunk of data asynchronously
2) When that read is finished, begin writing to all destinations at the same time. Store the 'IAsyncResults' from these writes in a list.
3) As each write finishes, remove its IAsyncResult from the list. When the list is empty, all the writes have finished.
4) Go to step 1 unless all data has been read. Once all data is read, complete the 'copyResult'.

Anonymous said...

Have you seen http://msdn.microsoft.com/en-us/magazine/cc163290.aspx and http://msdn.microsoft.com/en-us/magazine/cc337900.aspx ".NET Matters: Stream Pipeline" and ".NET Matters
Asynchronous Stream Processing" respectively. It's a while since I read those articles but I think they're in the same ballpark.

Andy

Anonymous said...

免費視訊,煙火,煙火工廠,視訊,正妹視訊,真愛密碼,真愛密碼,煙火秀,煙火圖片,衣蝶,衣蝶,AV女優,AV女優,
煙火批發,小泉彩,小泉彩,情趣用品,鞭炮,live119論壇,蜂炮,視訊

梁爵 said...

2020.05.01北部一名酒店工作酒店女公關私下接酒店S(性交易)月初被確診武漢肺炎,導致全國舞廳跟酒店,4月9日起遭勒令無限期停止營業,至今將近三個星期了,還不見酒店上班復工曙光,不少八大行業員工因為這波停業生計受損,就有任職台中知名酒店PT酒店小姐不滿中央停業兩套標準,打破沉默,站出來爭工作權。酒店工作人員陳小姐說:「台北的(酒店打工)女公關確診,可是其他店都沒事啊,叫我們停業的標準我也覺得很可笑。」北部酒店女公關一人確診,全國同業被停業,沒收入快三個星期,陳小姐再也忍不住開砲。陳小姐表示:「八大的停業標準我不服氣,按摩業跟三溫暖都可以(營業),磐石艦的軍官很像很愛去量販店,量販店消毒過後都可以營業,為什麼我們就是一個(女公關)離我們這麼遠的地方,就叫我們(酒店)全部停業。」酒店舞廳被停業,被衝擊的不只舞小姐們,像陳小姐就在台中知名酒店內負責音響設備維修超過18年,當年SARS疫情也沒這麼慘,現在卻被無限期停業,全公司所有人都快完蛋。謀職碰壁,生活快過不下去,陳小姐寫了這4張陳情書打算全公司連署,向中央請命。

Hit Counter