using System;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Rohan.Gateway
{
public class Player
{
private PlayerSocket _mapSocket;
private PlayerSocket _clientSocket;
public int _characterId = -1;
public int _characterType = -1;
public string _characterName = "";
public int _playerId = -1;
public string _dumpFolder = "";
public int _packetIndex;
public PlayerSocket mapSocket
{
get { return this._mapSocket; }
}
public PlayerSocket clientSocket
{
get { return this._clientSocket; }
}
public bool isConnected
{
get { return this._mapSocket.isConnected && this._clientSocket.isConnected; }
}
public Player(Socket clientSocket)
{
this._clientSocket = new PlayerSocket(clientSocket, this, PlayerSocketType.Client);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
socket.Connect(TCPListenerEx.remoteAddress, 22100);
this._mapSocket = new PlayerSocket(socket, this, PlayerSocketType.MapServer);
this.ValidateConnection();
}
public bool ValidateConnection()
{
if (!this.isConnected)
{
this.Disconnect();
return false;
}
return true;
}
public void Disconnect()
{
this._mapSocket.Disconnect();
this._clientSocket.Disconnect();
}
public void ReceiveClientData()
{
if (this.ValidateConnection())
{
this._clientSocket.ReceiveAsync();
}
}
public void DispatchClientData()
{
if (this.ValidateConnection())
{
this._mapSocket.SendAsync(this._clientSocket.reader);
}
}
public void ReceiveMapserverData()
{
if (this.ValidateConnection())
{
this._mapSocket.ReceiveAsync();
}
}
public void DispatchMapserverData()
{
if (this.ValidateConnection())
{
this._clientSocket.SendAsync(this._mapSocket.reader);
}
}
}
}
在下面的行中,我收到了此警告
因为没有等待这个电话
public void ReceiveClientData()
{
if (this.ValidateConnection())
{
this._clientSocket.ReceiveAsync();
}
}
public void DispatchClientData()
{
if (this.ValidateConnection())
{
this._mapSocket.SendAsync(this._clientSocket.reader);
}
}
public void ReceiveMapserverData()
{
if (this.ValidateConnection())
{
this._mapSocket.ReceiveAsync();
}
}
public void DispatchMapserverData()
{
if (this.ValidateConnection())
{
this._clientSocket.SendAsync(this._mapSocket.reader);
}
}
为什么我收到此错误?
答案 0 :(得分:1)
回应评论:
您是否尝试过将async添加到签名中,然后使用await来调用方法
您回答:
是的,我已经做到了
但是你发布的代码都没有显示出来。事实上,这是纠正此警告消息的正确方式。
如果没有一个好的Minimal, Complete, and Verifiable example能够清楚地显示所有相关类型(包括PlayerSocket
类型以及如何调用这些方法),则无法确定如何修复代码。您的选择包括:
#pragma warning
指令Task
方法返回的...Async()
对象分配给局部变量来隐式抑制警告。 E.g:
public void ReceiveClientData()
{
if (this.ValidateConnection())
{
var _ = this._clientSocket.ReceiveAsync();
}
}
public void ReceiveClientData()
{
if (this.ValidateConnection())
{
this._clientSocket.ReceiveAsync().Wait();
}
}
await
异步操作。坦率地说,这是迄今为止最好的选择,但却是最复杂的选择。主要优点是您可以获得异步操作的好处(即代码不会阻止等待操作完成),但您仍然可以观察到可能发生的任何异常。主要缺点是您必须将每个方法从void
更改为async Task
,然后对每个方法的调用方进行相同的更改。这些调用者的每个调用者,依此类推,直到你到达调用栈的顶部为止。就个人而言,我强烈推荐第三种或(特别是)第四种选择。它们是唯一允许您添加try
/ catch
以观察异常的选项。在某些情况下,第一个和第二个选项可能有意义,但我不会说执行套接字I / O将是其中一个场景。
有许多现有的Stack Overflow问题和答案可以更详细地介绍如何处理这类问题。请确定您要使用哪种方法,进行一些研究,如果您有特定的问题,请发布包含良好代码示例的问题,以便提供适当的答案。