【发布时间】:2012-08-01 03:47:48
【问题描述】:
我正在尝试将游戏内控制台连接到 WCF 界面,以便外部应用程序可以发送控制台命令并接收控制台输出。为此,我创建了以下服务合同:
public interface IConsoleNetworkCallbacks
{
[OperationContract(IsOneWay = true)]
void NewOutput(IEnumerable<string> text, string category);
}
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IConsoleNetworkCallbacks))]
public interface IConsoleInterface
{
[OperationContract]
void ProcessInput(string input);
[OperationContract]
void ChangeCategory(string category);
}
在我实现它的服务器上:
public class ConsoleNetworkInterface : IConsoleInterface, IDisposable
{
public ConsoleNetworkInterface()
{
ConsoleManager.Instance.RegisterOutputUpdateHandler(OutputHandler);
}
public void Dispose()
{
ConsoleManager.Instance.UnregisterOutputHandler(OutputHandler);
}
public void ProcessInput(string input)
{
ConsoleManager.Instance.ProcessInput(input);
}
public void ChangeCategory(string category)
{
ConsoleManager.Instance.UnregisterOutputHandler(OutputHandler);
ConsoleManager.Instance.RegisterOutputUpdateHandler(OutputHandler, category);
}
protected void OutputHandler(IEnumerable<string> text, string category)
{
var callbacks = OperationContext.Current.GetCallbackChannel<IConsoleNetworkCallbacks>();
callbacks.NewOutput(text, category);
}
}
在客户端我实现了回调:
public class Callbacks : IConsoleNetworkCallbacks
{
public void NewOutput(IEnumerable<string> text, string category)
{
MessageBox.Show(string.Format("{0} lines received for '{1}' category", text.Count(), category));
}
}
最后,我用以下类建立服务主机:
public class ConsoleServiceHost : IDisposable
{
protected ServiceHost _host;
public ConsoleServiceHost()
{
_host = new ServiceHost(typeof(ConsoleNetworkInterface), new Uri[] { new Uri("net.pipe://localhost") });
_host.AddServiceEndpoint(typeof(IConsoleInterface), new NetNamedPipeBinding(), "FrbConsolePipe");
_host.Open();
}
public void Dispose()
{
_host.Close();
}
}
并在我的客户端上使用以下代码建立连接:
protected Callbacks _callbacks;
protected IConsoleInterface _proxy;
protected void ConnectToConsoleServer()
{
_callbacks = new Callbacks();
var factory = new DuplexChannelFactory<IConsoleInterface>(_callbacks,
new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/FrbConsolePipe"));
_proxy = factory.CreateChannel();
_proxy.ProcessInput("Connected");
}
所以发生的情况是我的ConnectToConsoleServer() 被调用,然后它一直到_proxy.ProcessInput("Connected");。在我的游戏中(在服务器上),我立即看到由 ProcessInput 调用引起的输出,但客户端仍然在 _proxy.ProcessInput() 调用上停滞不前。
一分钟后,我的客户收到了 JIT TimeoutException然而,同时我的 MessageBox 消息出现了。
显然,不仅我的命令被立即发送,我的回调也被正确调用。那么为什么会出现超时异常呢?
注意:即使移除 MessageBox 调用,我仍然有这个问题,所以这不是 GUI 阻塞回调响应的问题。
【问题讨论】:
-
诊断说明了什么? msdn.microsoft.com/en-us/library/ms732023.aspx 当你用不阻塞的东西替换 MessageBox 时会发生什么?也许 MessageBox 隐藏了?