异步套接字写 - 潜力发送操作弄混? (.NET套接字) [英] Async Socket Writes - Potential for send operations to get mixed up? (.NET Sockets)

查看:271
本文介绍了异步套接字写 - 潜力发送操作弄混? (.NET套接字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这已被问过,但我觉得这是一个很难的事来搜索:

Sorry if this has been asked before, but I find it a hard thing to search for:

如果我用 BeginSend()上的 .NET插座的,我大概在code与此类似什么样的行为(伪)code:

If I use BeginSend() on a .NET socket, what kind of behaviour should I expect in code similar to this (pseudo) code:

主要计划code:

Network.OutBound.SendData(messageOne);

Network.OutBound.SendData(messageTwo);

Network.OutBound类

public void SendData(byte[] buffer)
{
     connectedSocket.BeginSend(buffer,0,buffer.Length,SocketFlags.None,SendCompleteMethod);
}

private void SendComplete(arResult)
{
     int bytesSent;
     bytesSent = arResult.EndSend();

     if(bytesSent < arResult.state.buffer.Length)
     {
         //Not all data sent yet, recall BeginSend
         connectedSocket.BeginSend(buffer,bytesSent,buffer.Length - bytesSent,SocketFlags.None,SendCompleteMethod);
     }
     else
     {
         //All data sent successfully
     }
}

由于即时通讯使用 BeginSend(),当第一送出数据(MessageOne的)被调用时,它会立即返回第二送出数据(messageTwo)将被调用。 然而,考虑正在发送两个消息之一,并消息二大量数据的情况下,并用数据的局部量,并因此 BeginSend()的发送完成回顾发送剩余的数据。

As im using BeginSend(), when the first SendData(messageOne) is called, it will immediately return and the second SendData(messageTwo) will be called. However, consider a case where a large amount of data is being sent for both message one and message two, and the send completes with a partial amount of data and so BeginSend() is recalled to send the remaining data.

这会导致两个消息字节在传出插座混用吗? 它们会被运行在单独的线程池线程,将双方派出在同一个插座。我理解TCP套接字确保订单数据到达,但如何运行/套接字层知道,如果我的 BeginSend()是一个后续尝试完成发送MessageOne的,或开始messageTwo?就其而言,wouldnt数据被发送,并且可以在远程终端接收在相同的顺序,因为它已被写入到插座中(尽管这是在为混淆的可能性似乎是)

Would this cause the two messages' bytes in the outgoing socket to be mixed up? They would both be running in seperate threadpool threads and would both be sending on the same socket. I understand TCP sockets ensure data arrives in order, but how does the runtime/socket layer know if my BeginSend() is a subsequent attempt to finish sending messageOne, or the start of messageTwo? As far as it is concerned, wouldnt the data be sent and received on the remote end be in the same order as it was written to the socket in (even though this is where the potential for mix up seems to be)

如果多数民众赞成的情况下,什么是开始/结束发送,如果我能够连续访问它的意义呢?

If thats the case, the what is the point of Begin/End Send if I have to serialize access to it?

我应该有某种标志是被提出时,MessageOne的已经完全发送(即使花费数 BeginSend()试图发送所有的数据),所以主程序code可以等待它试图发送一个不同的消息之前完成?

Should I have some sort of flag that gets raised when messageOne has been fully sent (even if it took several BeginSend() attempts to send all the data) so the Main Program code can wait for it to complete before attempting to sending a different message?

我应该写某种传出队列包装?还是我失去了一些东西?

Should I write some sort of outgoing queue wrapper? Or am I missing something?

推荐答案

展望与反射,你会发现BeginSend最终退出从.NET包装成的Win32 API,使一个调用的 WSASend 的哪个似乎不提供任何形式的的async选项,那么据我可以看到它只会产生阻止那些.NET框架内设法调用是异步使用线程池。

Looking in with Reflector, you will see that BeginSend finally exits from the .NET wrappers into the Win32 API and makes a call to WSASend which doesn't appear to provide any form of ASync option, so as far as I can see it will only generate Blocking calls that are managed inside .NET framework to be Async using the threadpool.

在应用中,我写的网络我从来没有真正考虑过这个情况,因为我有块,如果你试图再次把它发送方法,previous发送已完成,或我面前总是阻止使用。发送仅因为它们通常在专用线程一个响应于接收到的消息。

In the applications I've written with networking I've never really considered this situation, because I have a sender method that blocks if you attempt to call it again, before the previous send has complete, or I have that always block using .Send Only because they are usually a response to a received message on a dedicated thread.

编辑:

有没有一点点额外的测试。

Did a little extra testing.

如果你开始2 beginsends一个小缓冲区中的每个然后它可能不会真正发挥作用,但如果你的缓冲区显著我的测试中我使用了400K缓存,那么你肯定是在为受伤的世界,你' LL希望只是例外,但它要打破。

If you start 2 beginsends with a small buffer each then it probably won't really make a difference but if your buffer is significant my tests I used a 400k buffer, then you are definately in for a world of hurt, you'll hopefully just exception, but it's gonna break.

这篇关于异步套接字写 - 潜力发送操作弄混? (.NET套接字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆