I have TCP server , what recieve big data without a break.
And I need to broadcast this stream to many clients.
UPDATE:
I need to broadcast video stream. Perhaps there are ready solutions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to do this asynchronously, then you can take advantage of the
System.Threading.Tasksnamespace.First, you’ll need a map of the
Streaminstances to theTaskthat can be waited on for completion:There’s a slight overhead in the above, in that there’s a wasted
Taskinstance that does nothing, but that price is small given the number ofTaskinstances and continuations you need to perform.From there, you would read the content from the stream and then write it out to each of the
Streaminstances asynchronously:And in the end, you can wait on all of the streams written to by calling:
There are a few things to note.
First, the copy of
bufferis needed because of the lambda that is passed toContinueWith; the lambda is a closure that would encapsulatebufferand because it is processing asynchronously, the contents are likely to change. Each continuance needs its own copy of the buffer to read.This is also why the call to
Stream.Writeuses theArray.Lengthproperty; otherwise, thereadvariable would have to be copied through each iteration of the loop.Additionally, it would be more ideal to be able to utilize the
BeginWrite/EndWritemethods on theStreamclass; because there is noContinueWithAsyncmethod which will take aTaskand continue with an asynchronous method, there is no benefit to calling the async versions of read.This is one of those cases where it might be better to call BeginWrite/EndWrite yourself (as well as
BeginRead/EndRead) in order to make the most of async operations; of course, this will be a bit more complex because you won’t have the encapsulation of the operation’s result thatTaskprovides, and you will have to take the same precautions with thebufferif you use anonymous methods/closures.