c# - C#TCP异步BeginSend回调永远不会发生

标签 c# asynchronous tcp callback mono

我正在开发一款与C_统一的游戏,并使用TCP在服务器和客户机之间交换数据。
我正在使用异步调用进行连接,这似乎可以正常工作。然后,当服务器提示客户端提供客户端版本、客户端回复、服务器询问播放机名称、播放机回复时,会发生握手/身份验证,如果一切正常,则服务器通知客户端已被接受,否则连接将关闭。
大部分时间都是这样。然而,在大约20分之一的时间里,对于第一个消息(服务器->客户端版本请求,游戏开始后几秒钟内),第一个beginsend的异步回调永远不会被调用,从而停止了身份验证过程。客户机确实根据我已经准备好的日志接收消息,并通过调试进行验证。
服务器的调用是:

m_isSending = true;
m_socket.BeginSend(byteArray.buffer, 0, byteArray.arrayLen, SocketFlags.None, new AsyncCallback(EndAsyncWrite), byteArray);

我添加了m_issending以帮助更轻松地调试/跟踪是否发送消息以确认是否调用了回调。
EndAsyncWrite为:
protected void EndAsyncWrite(IAsyncResult iar)
{
        m_isSending = false;
        m_socket.EndSend(iar);
        ByteArray byteArray = (iar.AsyncState as ByteArray);
        //Add prev msg's ByteArray to await recycling
        lock (m_byteArraysAwaitingRecycle)
        {
            m_byteArraysAwaitingRecycle.Add(byteArray);
        }
}

在这20种情况中的1种情况下,即使在客户机收到并处理了消息之后,m issending仍然是真的。我已经在调试器中进行了深入的探讨,但是由于,我假设Unity使用Mono,因此不能进行过多的窥视。但是,我在套接字的WRITEQ中找到了消息(我假设它是用于写入的队列)。通常它是空的,所以我想知道它是否能够解释为什么不调用回调。
Watch Snippet唯一存在的条目是发送的消息和客户机接收的消息。
其他信息:Nagle是禁用的,Blocking设置为true。其他19/20的尝试似乎可以正常工作,没有变化。我目前正在以localhost作为目标进行测试。
所以我很困惑,因为我尽我所能做的一切都很好。为什么不打回电话?有什么想法吗?有什么建议吗?有办法解决这个问题吗?

最佳答案

在做了更多的测试之后,我得出结论,这是在unity编辑器中运行时对线程的一个bug/影响。我是这样得出这个结论的:
我注意到这个问题在第一次打开Unity项目并开始游戏时更经常出现。停止,然后开始,通常是有效的。它总是在2-3次尝试中工作,并且至少还要继续工作十几次。当然,这可能表明在我自己的代码中存在某种竞争条件或线程并发问题,因此我做了以下操作:1)在Unity中创建了一个非常精简的tcplistener/tcpclient项目,消除了对byte[]数组的任何缓存/回收或其他可能无意中影响异步或整体性能。2)我在编辑器中测试了这个新项目,并将其作为一个独立的构建来检查结果。
当然,这需要统一性,尽管它可能同样容易被用于.NET控制台应用程序进行进一步评估。这个项目由一个场景组成,一个游戏摄像机,下面三个脚本附在摄像机上。一旦连接到gameobject,就需要将tcpclient和tcpserver引用拖放到connectgui上。代码:
连接gui.cs

using UnityEngine;
using System.Collections;

public class ConnectGUI : MonoBehaviour {

public enum ConnectionState
{
    NotConnected,
    AttemptingConnect,
    Connected
}

public TCPClient client;
public TCPServer server;

// Use this for initialization
void Start () 
{
    client.connectState = ConnectionState.NotConnected;
}

// Update is called once per frame
void Update () {

}

void OnGUI()
{
    GUI.Label(new Rect(10, 10, Screen.width - 20, 20), client.connectState.ToString());

    if (client.connectState == ConnectionState.NotConnected)
    {
        if (GUI.Button(new Rect(Screen.width * 0.5f - 200, Screen.height * 0.5f - 40, 400, 80), "Connect"))
        {
            server.StartServer();
            System.Threading.Thread.Sleep(10);
            client.StartConnect(); 
        }
    }
}
}

tcpclient.cs公司
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;

public class TCPClient : MonoBehaviour {

public ConnectGUI.ConnectionState connectState;
Socket m_clientSocket;
byte[] m_readBuffer;

void Start()
{
    connectState = ConnectGUI.ConnectionState.NotConnected;
    m_readBuffer = new byte[1024];
}

public void StartConnect()
{
    m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    try
    {
       System.IAsyncResult result = m_clientSocket.BeginConnect("127.0.0.1", 10000, EndConnect, null);

       bool connectSuccess = result.AsyncWaitHandle.WaitOne(System.TimeSpan.FromSeconds(10));

       if (!connectSuccess)
       {
           m_clientSocket.Close();
           Debug.LogError(string.Format("Client unable to connect. Failed"));
       }
    }
    catch (System.Exception ex)
    {
        Debug.LogError(string.Format("Client exception on beginconnect: {0}", ex.Message));
    }

    connectState = ConnectGUI.ConnectionState.AttemptingConnect;
}

void EndConnect(System.IAsyncResult iar)
{
    m_clientSocket.EndConnect(iar);

    m_clientSocket.NoDelay = true;

    connectState = ConnectGUI.ConnectionState.Connected;

    BeginReceiveData();

    Debug.Log("Client connected");
}

void OnDestroy()
{
    if (m_clientSocket != null)
    {
        m_clientSocket.Close();
        m_clientSocket = null;
    }
}

void BeginReceiveData()
{
    m_clientSocket.BeginReceive(m_readBuffer, 0, m_readBuffer.Length, SocketFlags.None, EndReceiveData, null);
}

void EndReceiveData(System.IAsyncResult iar)
{
    int numBytesReceived = m_clientSocket.EndReceive(iar);

    ProcessData(numBytesReceived);

    BeginReceiveData();
}

void ProcessData(int numBytesRecv)
{
    string temp = TCPServer.CompileBytesIntoString(m_readBuffer, numBytesRecv);

    Debug.Log(string.Format("Client recv: '{0}'", temp));

    byte[] replyMsg = new byte[m_readBuffer.Length];
    System.Buffer.BlockCopy(m_readBuffer, 0, replyMsg, 0, numBytesRecv);

    //Increment first byte and send it back
    replyMsg[0] = (byte)((int)replyMsg[0] + 1);

    SendReply(replyMsg, numBytesRecv);
}

void SendReply(byte[] msgArray, int len)
{
    string temp = TCPServer.CompileBytesIntoString(msgArray, len);

    Debug.Log(string.Format("Client sending: len: {1} '{0}'", temp, len));

    m_clientSocket.BeginSend(msgArray, 0, len, SocketFlags.None, EndSend, msgArray);
}

void EndSend(System.IAsyncResult iar)
{
    m_clientSocket.EndSend(iar);

    byte[] msg = (iar.AsyncState as byte[]);

    string temp = TCPServer.CompileBytesIntoString(msg, msg.Length);

    Debug.Log(string.Format("Client sent: '{0}'", temp));

    System.Array.Clear(msg, 0, msg.Length);
    msg = null;
}
}

tcpserver.cs.服务器
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;

public class TCPServer : MonoBehaviour 
{
public enum TestMessageOrder
{
    NotConnected,
    Connected,
    SendFirstMessage,
    ReceiveFirstMessageReply,
    SendSecondMessage,
    ReceiveSecondMessageReply,
    SendThirdMessage,
    ReceiveThirdMessageReply,
    Error,
    Done
}

protected TcpListener m_tcpListener;
protected Socket m_testClientSocket;
protected byte[] m_readBuffer;
[SerializeField]
protected TestMessageOrder m_testClientState;

public void StartServer()
{
    m_tcpListener = new TcpListener(IPAddress.Any, 10000);
    m_tcpListener.Start();

    StartListeningForConnections();
}

void StartListeningForConnections()
{
    m_tcpListener.BeginAcceptSocket(AcceptNewSocket, m_tcpListener);
    Debug.Log("SERVER ACCEPTING NEW CLIENTS");
}

void AcceptNewSocket(System.IAsyncResult iar)
{
    m_testClientSocket = null;
    m_testClientState = TestMessageOrder.NotConnected;
    m_readBuffer = new byte[1024];

    try
    {
        m_testClientSocket = m_tcpListener.EndAcceptSocket(iar);
    }
    catch (System.Exception ex)
    {
        //Debug.LogError(string.Format("Exception on new socket: {0}", ex.Message));
    }

    m_testClientSocket.NoDelay = true;
    m_testClientState = TestMessageOrder.Connected;

    BeginReceiveData();
    SendTestData();

    StartListeningForConnections();
}

void SendTestData()
{
    Debug.Log(string.Format("Server: Client state: {0}", m_testClientState));

    switch (m_testClientState)
    {
        case TestMessageOrder.Connected:
            SendMessageOne();
            break;

        //case TestMessageOrder.SendFirstMessage:
            //break;

        case TestMessageOrder.ReceiveFirstMessageReply:
            SendMessageTwo();
            break;

        //case TestMessageOrder.SendSecondMessage:
            //break;

        case TestMessageOrder.ReceiveSecondMessageReply:
            SendMessageTwo();
            break;

        case TestMessageOrder.SendThirdMessage:
            break;

        case TestMessageOrder.ReceiveThirdMessageReply:
            m_testClientState = TestMessageOrder.Done;
            Debug.Log("ALL DONE");
            break;

        case TestMessageOrder.Done:
            break;

        default:
            Debug.LogError("Server shouldn't be here");
            break;
    }
}

void SendMessageOne()
{
    m_testClientState = TestMessageOrder.SendFirstMessage;
    byte[] newMsg = new byte[] { 1, 100, 101, 102, 103, 104 };

    SendMessage(newMsg);
}

void SendMessageTwo()
{
    m_testClientState = TestMessageOrder.SendSecondMessage;
    byte[] newMsg = new byte[] { 3, 100, 101, 102, 103, 104, 105, 106 };

    SendMessage(newMsg);
}

void SendMessageThree()
{
    m_testClientState = TestMessageOrder.SendThirdMessage;
    byte[] newMsg = new byte[] { 5, 100, 101, 102, 103, 104, 105, 106, 107, 108 };

    SendMessage(newMsg);
}

void SendMessage(byte[] msg)
{
    string temp = TCPServer.CompileBytesIntoString(msg);

    Debug.Log(string.Format("Server sending: '{0}'", temp));

    m_testClientSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None, EndSend, msg);
}

void EndSend(System.IAsyncResult iar)
{
    m_testClientSocket.EndSend(iar);

    byte[] msgSent = (iar.AsyncState as byte[]);
    string temp = CompileBytesIntoString(msgSent);

    Debug.Log(string.Format("Server sent: '{0}'", temp));
}

void BeginReceiveData()
{
    m_testClientSocket.BeginReceive(m_readBuffer, 0, m_readBuffer.Length, SocketFlags.None, EndReceiveData, null);
}

void EndReceiveData(System.IAsyncResult iar)
{
    int numBytesReceived = m_testClientSocket.EndReceive(iar);

    ProcessData(numBytesReceived);

    BeginReceiveData();
}

void ProcessData(int numBytesRecv)
{
    string temp = TCPServer.CompileBytesIntoString(m_readBuffer, numBytesRecv);

    Debug.Log(string.Format("Server recv: '{0}'", temp));

    byte firstByte = m_readBuffer[0];

    switch (firstByte)
    {
        case 1:
            Debug.LogError(string.Format("Server should not receive first byte of 1"));
            m_testClientState = TestMessageOrder.Error;
            break;

        case 2:
            m_testClientState = TestMessageOrder.ReceiveSecondMessageReply;
            break;

        case 3:
            Debug.LogError(string.Format("Server should not receive first byte of 3"));
            m_testClientState = TestMessageOrder.Error;
            break;

        case 4:
            m_testClientState = TestMessageOrder.ReceiveThirdMessageReply;
            break;

        case 5:
            Debug.LogError(string.Format("Server should not receive first byte of 5"));
            m_testClientState = TestMessageOrder.Error;
            break;

        default:
            Debug.LogError(string.Format("Server should not receive first byte of {0}", firstByte));
            m_testClientState = TestMessageOrder.Error;
            break;
    }

    SendTestData();
}

void OnDestroy()
{

    if (m_testClientSocket != null)
    {
        m_testClientSocket.Close();
        m_testClientSocket = null;
    }

    if (m_tcpListener != null)
    {
        m_tcpListener.Stop();
        m_tcpListener = null;
    }
}

public static string CompileBytesIntoString(byte[] msg, int len = -1)
{
    string temp = "";

    int count = len;

    if (count < 1)
    {
        count = msg.Length;
    }

    for (int i = 0; i < count; i++)
    {
        temp += string.Format("{0} ", msg[i]);
    }

    return temp;
}
}

它所做的是start是一个tcplistener,并开始接受异步连接套接字。然后创建一个客户端套接字,并将其作为TCP套接字进行连接(在127.0.0.1的端口10000上)。它转换nagle的算法,服务器发送第一条消息。客户机接收消息,将第一个字节从1->2递增,并返回原始消息。然后,服务器接收该消息,并发送另一条以3开头的消息。客户端接收,递增3->4,并回送消息的其余部分。然后,服务器接收到该消息,并发送以5开头的第三条也是最后一条消息。客户端返回5->6并发送回消息。一旦发生这种情况,服务器将打印“全部完成”。服务器和客户机都应该打印以记录各种消息内容(由于线程的性质,不总是以相同的顺序)。
如果出于某种原因“全部完成”没有打印出来,那么实验就失败了。
在UnityEditor中运行它,在第一次运行时失败10/10,在打开编辑器后立即运行。随后的运行尝试导致了第二次和第三次尝试的混合成功。在第四次尝试中,我没有记录到失败。
然后我把这个项目编译成一个独立的程序,并重复同样的尝试次数。由于它依赖于日志中的“all done”,output.log被检查为“all done”,并且每次都被找到。
所以,除非我误解了结果,否则unity编辑器或其底层的mono版本中都存在一个问题,这个问题是由于线程的混乱导致tcp异步读/写在某些容量上失败。然而,在独立的构建中,不管是什么东西,谢天谢地,至少在windows上的测试允许的范围内,似乎都不是问题。
我完全承认测试是有限的,每个测试只有40次,但是结果明显不同,尽管我太懒了,无法计算实际的重要性。我很困惑,仍然有点担心这可能是我自己的有缺陷的实现,因为类似的事情并不普遍;但是Unity自己的网络主要依赖于RPC调用,并且大多数中间件完全接受一个专用的基于UDP的网络选项。
如果有一些基本的缺陷,请让我知道,否则我希望这可能有助于一些迷失的灵魂(因为我已经有近两个星期了),因为在这个主题上几乎没有或没有可搜索的结果。这一切都是在Unity4.6.1f1中完成的,但是在目前的Unity5测试版(不确定当前的测试版号)中,一个朋友也测试了它。
就我个人而言,虽然这是非常烦人的,但我觉得我可以忽略这一点,因为作为一个只有编辑才能解决的问题,几乎不可能影响到实际玩家的编译版本。一旦构建经常发生,就需要对其进行大量测试。

关于c# - C#TCP异步BeginSend回调永远不会发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28296771/

相关文章:

java - 在本地测试 TCP/IP 连接

c# - 如何确定集合中最早/最晚的 DateTime?

c# - 如何在离线模式下提供唯一 ID?

c# - 如何使用循环创建 CAML 查询

swift - 在 NSURLSession 中等待下载任务完成

swift - 在异步网络请求中捕获 self weak 或 unowned

linux - 关于拥塞下tcp行为的几个问题

c# - 如何在 Xamarin.iOS 中从 URL 加载图像

ios - 从 Swift 函数中的异步调用返回数据

c++ - 在 C/C++ 中通过 TCP 套接字发送十六进制