【发布时间】:2009-11-11 00:38:55
【问题描述】:
我最近一直在用C#做异步发送,就在几天前,当我遇到需要发送多个位的数据时,我开始怀疑发送的数据是否按顺序发送。
所以我的问题是,当您在 C# 中异步发送数据时,它是否会在发送数据时相应地发送数据?
例如。 我发送一个包含 10 个字节的数据数组,在我发送它之后,我发送一个包含 5 个字节的数组。这是否意味着包含 5 个字节的数组将首先到达远程客户端(因为 5 个字节的处理时间很可能少于 10 个)?
这是代码(C#):
/// <summary>
/// Sends the welcome screen to the character.
/// </summary>
/// <param name="character">The character requesting the welcome screen.</param>
public void SendWelcomeScreen(Character character)
{
SendWindowPane(character, 549);
SendInterface(character, 1, 549, 2, 378);
SendInterface(character, 1, 549, 3, 15);
SendString(character, "Welcome to " + GameEngine.World.Name + ".", 378, 115);
// TODO: last ip
SendString(character, "You are connected from: " + character.Session.Connection.IPAddress, 378, 116);
SendString(character, "0", 378, 39);
SendString(character, "0", 378, 96);
SendString(character, "0 unread messages", 378, 37);
SendString(character, GameEngine.World.Name + " staff will NEVER email you. We use the Message Centre on the website instead.", 378, 38);
SendString(character, "0 days of member credit", 378, 94);
SendString(character, "You have 0 days of member credit remaining. Please click here to extend your credit.", 378, 93);
SendString(character, "You do not have a bank pin. Please visit a bank if you would like one.", 378, 62);
SendString(character, "You have not yet set any recovery questions.", 378, 56);
SendString(character, "Message of the Week", 15, 0);
SendString(character, "Remember to keep your account secure: set a bank PIN, set recover questions and NEVER give away your password.", 15, 4);
}
/// <summary>
/// Sends a window pane to the character's client.
/// </summary>
/// <param name="character">The character to send window pane to.</param>
/// <param name="pane">The pane id.</param>
public void SendWindowPane(Character character, short pane)
{
character.Session.SendPacket(
new PacketBuilder(239)
.AppendShort(pane)
.AppendByteA(0));
}
/// <summary>
/// Sends an interface to the character's client.
/// </summary>
/// <param name="character">The character to send interface to.</param>
/// <param name="showId">The show ids.</param>
/// <param name="windowId">The window id.</param>
/// <param name="interfaceId">The interface id.</param>
/// <param name="childId">The child id.</param>
public void SendInterface(Character character, byte showId, short windowId, short interfaceId, short childId)
{
character.Session.SendPacket(
new PacketBuilder(93)
.AppendShort(childId)
.AppendByteA(showId)
.AppendShort(windowId)
.AppendShort(interfaceId));
}
/// <summary>
/// Sends a piece of text to the character's client.
/// </summary>
/// <param name="character">The character to send the text to.</param>
/// <param name="text">The string to be displayed.</param>
/// <param name="interfaceId">The interface id of which we place this text on.</param>
/// <param name="childId">The child id of which we place this text on.</param>
public void SendString(Character character, string text, short interfaceId, short childId)
{
int stringSize = text.Length + 5;
character.Session.SendPacket(
new PacketBuilder(179)
.AppendByte((byte)(stringSize / 256))
.AppendByte((byte)(stringSize % 256))
.AppendString(text)
.AppendShort(childId)
.AppendShort(interfaceId));
}
【问题讨论】:
-
“发送”用什么方式?举个例子怎么样?
-
简短回答:这取决于。我们可能需要看一些代码才能更具体地回答。
-
好的,我已经添加了一些代码,请看一下:)
-
你可能在使用 WCF 吗?
-
我很确定这没有必要成为社区维基。只是一个想法。
标签: c# networking asynchronous