I send a 7 byte TX command I am to receive a 7 byte RX response.
If possible, I'd like to chain the receive bytes task to send bytes task as this only seems logical.
I am aware that background workers are outdated and would like to modernize the code by utilizing the Task-Parallel-Library.
Can someone elucidate how to replace the backgroundWorker_DoWork event handler with an async task for the context described?
If you are always first sending bytes, and then receiving bytes, you could use something like this:
public static class NetworkStreamExtensions
{
public static async Task<byte[]> SendReceiveBytes(this NetworkStream self, byte[] toSend, int bytesToReceive, CancellationToken cancel)
{
await self.WriteAsync(toSend, 0, toSend.Length, cancel);
var receiveBuffer = new byte[bytesToReceive];
var receivedBytes = await self.ReadAsync(receiveBuffer, 0, receiveBuffer.Length, cancel);
if (receivedBytes != receiveBuffer.Length)
{
// Handle incorrect number of received bytes,
}
return receiveBuffer;
}
}
As this is using async/await it will use the current synchronization context to run the code. I.e. If called on the main thread, it will send and receive on the main thread. It will however permit other code to run between Writing and reading, since async methods does not block execution.
Related
I am working on refactoring code that uses the Bootstrap protocol to update the firmware of several nodes in a machine. The current code looks something like this (pseudo-code):
public void StartUpdate()
{
Sokcet bootpSocket = new Socket():
StateObject bootpState = new StateObject(bootpSocket);
BOOTPReceive(bootpState);
SendMagicPacket();
while (!IsError && !IsUpdateComplete)
{
//wait for BOOTP/Update to finish before returning to caller
}
}
private void BOOTPReceive(object state)
{
bOOTPSocket.BeginReceive(PACKET_DATA, 0, PACKET_DATA.Length, 0, OnBOOTPReceive, state);
}
SendMagicPacket()
{
//create and send magic packet
// this will tell the node to respond with a BOOTPPacket
}
private void OnBOOTPReceive(IAsyncResult result)
{
StateObject state = (StateObject) result.AsyncState;
Socket handler = state.workSocket;
int bytesRcvd = handler.EndReceive(result);
packet = PACKET_DATA;
if(isValidBOOTP(packet))
{
SendBOOTPResponse();
}
else{
BOOTPReceive(); //keep listening for valid bootp response
}
}
private void SendBOOTPResponse()
{
UdpClient udpClient = new UdpClient();
udpClient.BeginSend(packetData, packetData.Length, BROADCAST_IP, (int)UdpPort.BOOTP_CLIENT_PORT, OnBOOTPSend, udpClient);
}
private void OnBOOTPSend(IAsyncResult result)
{
UdpClient udpClient = (UdpClient)result.AsyncState;
int bytesSent = udpClient.EndSend(result);
udpClient.Close();
}
What I want to do is convert this to async-await but still require that I don't return back to the caller right away. How would I go about doing this? Is this possible to do? And would this be the right thing to do since await-async propagates all the way to the top?
Pseudo-code of what I think this would look like:
public void StartUpdate()
{
bool result = await SendMagicPacket();
bool IsError = await BOOTPCommunication(); //Handles all of the BOOTP recieve/sends
//don't return to caller until BOOTPCommunication is completed. How do i do this?
}
You need to wait for the two tasks try the following:
public async Task StartUpdate()
{
var resultTask = SendMagicPacket();
var isErrorTask = BOOTPCommunication(); //Handles all of the BOOTP recieve/sends
await Task.WhenAll(new[]{resultTask, isErrorTask});
//don't return to caller until BOOTPCommunication is completed. How do i do this?
}
//wait for BOOTP/Update to finish before returning to caller
You don't need any async IO at all because you want to wait until all operations are done. I assume you have copied some sample code. Most sample code uses async socket APIs.
Switch everything over to synchronous socket APIs and you're done.
If you want to keep this async for some reason you can indeed switch to await and untangle this code. The pseudo-code you posted looks like a good goal. It forces the surrounding method to be async Task, though.
You can deal with that by making all callers recursively async as well. If you don't need to conserve threads you could block on that task and have a mostly synchronous call chain. At that point you lose all async benefits, though.
Radin was on the right track, but I think what you want is something like this:
You need to wait for the two tasks try the following:
public async Task StartUpdate()
{
var resultTask = SendMagicPacket();
var isErrorTask = BOOTPCommunication(); //Handles all of the BOOTP recieve/sends
Task.WhenAll(new[]{resultTask, isErrorTask}).Wait(); //Wait() will block so that the method doesn't return to the caller until both of the asynchronous tasks complete.
}
What that allows is SendMagicPacket and BOOTPCommunication to both fire simultaneously, but to wait for BOTH to complete. Using that pattern you can fire of N events simultaneously, while using Wait() to wait for all to finish so that the method itself returns synchronously.
I am attempting to wrap up both the sending and receiving of data to a server using the async/await pattern. What I have created is an event driven socket framework that fires events whenever data is received from the server. However, I am having trouble trying to figure out how to await the thread, after the send, so that I do not return until the data from the server returns.
In the past I used a manual reset event to achieve this...
byte[] returnData;
public byte[] SendData(byte[] data){
socket.Send(data, 0, data.Length);
manualResetEvent.Wait();
return returnData;
}
public void OnDataReceived(byte[] serverData){
returnData = serverData;
manualResetEvent.Set();
}
This method simply pauses the caller thread until the reset event is triggered. This is no good as I want the async/await pattern so that the calling thread is free to do other work while waiting for the data from the server.
What I have now is....
byte[] returnData;
public async Task<byte[]> SendData(byte[] data){
await socket.SendAsync(data, 0, data.Length);
//await something that frees up this thread to do other work
return returnData;
}
public void OnDataReceived(byte[] serverData){
returnData = serverData;
//do something that pushes SendData back into the active context
}
I think I am doing somethign wrong. Any help would be appreciated!
I have found a suitable solution.
Instead of using a ManualResetEvent, I can use what is called a TaskCompletionSource<T>. This gives me the functionality I need to allow the thread pool to wait for some state change.
I trying to copy an incoming HTTP stream into another stream and use that instead.
When I use Request.Content.ReadAsStreamAsync().Result directly, everything works fine.
The problem that I am facing is how to "channel" the original stream thru another one.
I wrote the flowing method, but the problem is that it either blocks until all the stream data has arrived (when I use Wait()), or it returns immediately and exists without capturing any data.
private Task<Stream> GetAudioStream(RecordingSession recordingSession)
{
Task<Stream> task = Task<Stream>.Factory.StartNew(() =>
{
Request.Content.ReadAsStreamAsync().Result.CopyToAsync(recordingSession.AudioStream).Wait();
return recordingSession.AudioStream;
});
return task;
}
Perhaps, you tackle the problem from the wrong side. CopyToAsync gives you a complete copy of the stream, the Task returned by CopyToAsync is completed when all data has been copied.
If you don't need the whole complete copy of the stream, or don't want to wait for all data, then copy it manually yourself and process each chunk:
static async Task<Stream> CopyAndProcessAsync(RecordingSession recordingSession, CancellationToken token)
{
var srcStream = await Request.Content.ReadAsStreamAsync();
var dstStream = recordingSession.AudioStream;
var buffer = new byte[4096];
int bytesRead;
while ((bytesRead = await srcStream.ReadAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false)) != 0)
{
await dstStream.WriteAsync(buffer, 0, bytesRead, token).ConfigureAwait(false);
// do whatever you want with the chunk
PlaybackChunk(buffer);
}
return dstStream;
}
Do this wherever you'd otherwise be consuming the result of your GetAudioStream. It well may turn out that you don't need a copy at all, when you've processed each chunk.
I think you mean something like this:
private async Task<Stream> GetAudioStream(RecordingSession recordingSession)
{
var result = await Request.Content.ReadAsStreamAsync();
await result.CopyToAsync(recordingSession.AudioStream);
return recordingSession.AudioStream;
}
async methods return Tasks. Very rarely should a non-async method return a Task.
I want to create Socket message sending via TAP via async/await.
After reading this answer and this one - I decided to create a fully working sample :
So what have I tried :
I took the TAP extenstion methods from here (all ok) : and I test it in console cmd :
Reciever Code
public static class SocketExtensions
{
public static Task<int> ReceiveTaskAsync(this Socket socket, byte[] buffer, int offset, int count)
{
return Task.Factory.FromAsync<int>(
socket.BeginReceive(buffer, offset, count, SocketFlags.None, null, socket),
socket.EndReceive);
}
public static async Task<byte[]> ReceiveExactTaskAsync(this Socket socket, int len)
{
byte[] buf = new byte[len];
int totalRead = 0;
do{
int read = await ReceiveTaskAsync(socket, buf, totalRead, buf.Length - totalRead);
if (read <= 0) throw new SocketException();
totalRead += read;
}while (totalRead != buf.Length);
return buf;
}
public static Task ConnectTaskAsync(this Socket socket, string host, int port)
{
return Task.Factory.FromAsync(
socket.BeginConnect(host, port, null, null),
socket.EndConnect);
}
public static Task SendTaskAsync(this Socket socket, byte[] buffer)
{
return Task.Factory.FromAsync<int>(
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, socket),
socket.EndSend);
}
}
static void Main()
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ConnectTaskAsync("127.0.0.1", 443);
var buf1 = s.ReceiveExactTaskAsync(100); //read exactly 100 bytes
Console.Write(Encoding.UTF8.GetString(buf1.Result));
var buf2 = s.ReceiveExactTaskAsync(100); //read exactly 100 bytes
Console.Write(Encoding.UTF8.GetString(buf2.Result));
Console.ReadLine();
}
Sender Code :
// use same extension method class like above ....^
void Main()
{
Socket s = new Socket(SocketType.Stream , ProtocolType.Tcp);
s.ConnectTaskAsync( "127.0.0.1" , 443);
s.SendTaskAsync(Encoding.UTF8.GetBytes("hello"));
s.Close();
Console.ReadLine();
}
notice I removed the async from main since im testing it in console.
Question ,
According to link above , the code should work
However I'm getting no exception and it's just hangs on that line :
Console.Write(Encoding.UTF8.GetString(buf1.Result));
(First I run receiver , then I run sender)
What am I missing?
the problem comes from the "notice I removed the async from main since im testing it in console.".
You need to wait for the operation to complete before doing the next step. The code you used as an example pauses at each await for the operation to complete, your code just goes straight through.
You may be able to fix this by putting a .Wait() after each operation that would have had a await or by running this function inside a threadpool thread via Task.Run(, however I think it is better to know when you should use async and when you should not.
Async should be used when you have other work you could have the thread be doing, very commonly that "other work" will be things like processing UI messages on a WinForms project or accepting new connections on a ASP.NET site. In a console application there is no other work your program could be doing while it waits, so in that situation it would be more appropriate to use the synchronous version of the functions instead.
P.S. You made the comment after I posted "that's why I remove the async awaits and used Task.result", just so you know never ever1 combine code that uses await and code that blocks the synchronization contest (by using things like Task.Result or Task.Wait(), you will likely cause your code to deadlock and stop functioning!
It is not a issue for your current example because console applications do not have a synchronization context, but if you copied and pasted this code to something that did you could easily lock up your program.
1: Ok, you could combine await and blocking code but there are rules you need to follow, but if you know enough to dispute my what I am saying you know enough to safely do it. If you don't know how to safely do it just avoid doing it
since you do not wait for the threads to do their work and then call s.Close() the socket will be closed before any traffic is sent out.
You would have to either remove the s.Close() call or wait until the calls are complete, for instance via
Task connect = s.ConnectTaskAsync( "127.0.0.1" , 443);
connect.Wait(); // you have to connect before trying to send
Task sendData = s.SendTaskAsync(Encoding.UTF8.GetBytes("hello"));
sendData.Wait(); // wait for the data to be sent before calling s.Close()
s.Close();
or you could box it in a method and Wait for that method to complete. The end result is to not call Close before completing the previous calls.
void Main()
{
Socket s = new Socket(SocketType.Stream , ProtocolType.Tcp);
Task worker = DoWorkAsync(s);
worker.Wait();
s.Close();
Console.ReadLine();
}
private async Task DoWorkAsync(Socket s){
await s.ConnectTaskAsync( "127.0.0.1" , 443);
await s.SendTaskAsync(Encoding.UTF8.GetBytes("hello"));
}
I'm using a TcpClient in one of my Compact Framework 2.0 applications. I want to receive some information from a TCP server.
As the Compact Framework does not support the timeout mechanisms of the "large" framework, I'm trying to implement my own timeout-thing. Basically, I want to do the following:
IAsyncResult result = networkStream.BeginRead(buffer, 0, size, ..., networkStream);
if (!result.AsyncWaitHandle.WaitOne(5000, false))
// Handle timeout
private void ReceiveFinished(IAsyncResult ar)
{
NetworkStream stream = (NetworkStream)ar.AsyncState;
int numBytes = stream.EndRead(ar);
// SIGNAL IASYNCRESULT.ASYNCWAITHANDLE HERE ... HOW??
}
I'd like to call Set for the IAsyncResult.AsyncWaitHandle, but it doesn't have such a method and I don't know which implementation to cast it to.
How do I set the wait handle? Or is it automatically set by calling EndRead? The documentation suggests that I'd have to call Set myself...
Thanks for any help!
UPDATE
Seems that the wait handle is set automatically when calling EndRead - but it's not in the docs. Can somebody confirm this?
UPDATE 2
Wrote client.BeginRead in my sample code. Of course, BeginRead is called on the NetworkStream...
I think you have a misunderstanding about async IO with TCP.
To kick off async IO, call stream.BeginRead().
In the callback, you call EndRead on the stream.
You don't call BeginRead on the TcpClient, as your code shows. Your app doesn't ever signal the WaitHandle. The IO layer will invoke your callback when the waithandle is signalled, in other words when the async Read happens.
In your callback, normally you'd call BeginRead again, on the stream, if it's possible that you'll be receiving more data.
You can see a clear example in this answer.
Before starting the BeginRead/EndRead dance,
you may want to do an async Connect on the TcpClient - then you would use BeginConnect. But that's done just once. Alternatively, you might want a synchronous connect, in which case you just call TcpClient.Connect().
example code:
private class AsyncState
{
public NetworkStream ns;
public ManualResetEvent e;
public byte[] b;
}
public void Run()
{
NetworkStream networkStream = ...;
byte[] buffer = new byte[1024];
var completedEvent = new ManualResetEvent(false);
networkStream.BeginRead(buffer, 0, buffer.Length,
AsyncRead,
new AsyncState
{
b = buffer,
ns = networkStream,
e = completedEvent
});
// do other stuff here. ...
// finally, wait for the reading to complete
completedEvent.WaitOne();
}
private void AsyncRead(IAsyncResult ar)
{
AsyncState state = ar as AsyncState;
int n = state.ns.EndRead(ar);
if (n == 0)
{
// signal completion
state.e.Set();
return;
}
// state.buffer now contains the bytes read
// do something with it here...
// for example, dump it into a filesystem file.
// read again
state.ns.BeginRead(state.b, 0, state.b.Length, AsyncRead, state);
}