简体   繁体   English

C#包结构系统

[英]C# packet structure system

I am working on a packet system (UDP or TCP, either way is fine) but I came to the conclusion that my current (TCP) system is really awefull. 我正在开发一个数据包系统(UDP或TCP,无论哪种方式都可以),但得出的结论是我当前的(TCP)系统确实很棒。

Basicly, I'm sending a packet that looks like this: 基本上,我发送的数据包如下所示:

string packet = "LOGIN_AUTH;TRUE"
IPAddress target = IPAddress.Parse(endPoint);
// Send packet

When I recieve the packet in my client, I use this to determine the packet type: 当我在客户端中收到数据包时,将使用它来确定数据包类型:

string[] splitPacket = packet.Split(';');

if (splitPacket[0] == "LOGIN_AUTH" && splitPacket[1] == "TRUE")
   authLogin(packet); //Does stuff like loading the next screen

But I'm sure there must be a better way to do this, because of this thread: 但是我敢肯定,由于这个线程,必须有更好的方法来做到这一点:

http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/79660898-aeee-4c7b-8cba-6da55767daa1/ (post 2) http://social.msdn.microsoft.com/forums/zh-CN/netfxnetcom/thread/79660898-aeee-4c7b-8cba-6da55767daa1/ (第2页)

I'm just wondering if someone could give me a push in the right direction on what I should to categorize the packets in a way that's more easy on the eye. 我只是想知道是否有人可以按照我认为更容易的方式在正确方向上推动我对数据包进行分类。

Thank you in advance. 先感谢您。

Edit: I did some research, but I cant find a solution to my problem in WCF. 编辑:我做了一些研究,但我找不到在WCF中问题的解决方案。 So once again, what is the best way to create a packet structure, and how do I utilize it? 再次,创建数据包结构的最佳方法是什么?如何利用它?

I don't want to recreate a protocol, just something like this: 我不想重新创建协议,就像这样:

public string sepChar = ";"
public static struct loginPacket
{
    string type;
    string username;
    string password;
}

public void reqLogin()
{
    lock (_locker)      //Need to make it thread-safe in this case
    {
         loginPacket.type = "login";
         loginPacket.username = "username";
         loginPacket.password = "password;

         sendPacket(loginPacket);
    }
}

public void sendPacket(string packet)
{
    // Script to send packet using the struct information
}

I hope that's detailed enough. 我希望这足够详细。

I would recommend WCF; 我会推荐WCF; if not now, decide to learn it in the future. 如果不是现在,请决定将来学习它。 It has a huge learning curve because it covers not only what you're trying to do, but so many other things. 它具有巨大的学习曲线,因为它不仅涵盖了您要尝试做的事情,还涵盖了许多其他事情。

If you just want a quick-and-dirty solution, then you could use binary serialization. 如果您只想使用一种简单快捷的解决方案,则可以使用二进制序列化。 To take this approach, you'll need to define your message types in a dll shared between client and server. 要采用这种方法,您需要在客户端和服务器之间共享的dll中定义消息类型。 Then, remember to use a form of message framing so that the messages don't get munged up in transit. 然后,请记住使用一种消息框架 ,以使消息在传输过程中不会被蒙混。

Things get more complex when you consider versioning. 考虑版本控制时,事情变得更加复杂。 Eventually you'll want to use WCF. 最终,您将要使用WCF。

I have used my own communication protocol over TCP 2 years ago, about the base packet class: 2年前,我已经在TCP上使用了自己的通信协议,用于基本数据包类:

public enum PacketType {Login, Hello, FooBar}
public class Packet
{
    public long Size;
    public PacketType PacketType;
}
public class LoginPacket : Packet
{
    public string Login;
    public string Password;
}

I do not agree with Size property... because you will only know packet size AFTER it was serialized with binaryFormater. 我不同意Size属性...,因为只有在使用binaryFormater对其进行序列化之后,您才知道数据包的大小。 I have used other method, first i serialized packet object and then before putting this byte[] in network stream i wrote BitConverter.GetBytes(SerializedPacketMemoryStream.Length) before each packet bytes. 我使用了其他方法,首先序列化了数据包对象,然后在将此byte []放入网络流中之前,在每个数据包字节之前写了BitConverter.GetBytes(SerializedPacketMemoryStream.Length)。
2nd thing to consider, is that you need to create temp buffer for received bytes[] and wait until you receive full packet and only then desirialize it. 要考虑的第二件事是,您需要为接收到的bytes []创建临时缓冲区,并等待直到收到完整的数据包,然后对其进行反序列化。
I can send you my client\\server code part, it's not very clean i wrote it when i just started learning C#, but it is 100% stable, tested for 3 years in production environment. 我可以将您的客户端\\服务器代码部分发送给您,这不是很干净,我刚开始学习C#时就写了它,但是它是100%稳定的,已经在生产环境中进行了3年的测试。

protobuf might be what you want. protobuf可能就是您想要的。

Here's the .NET port: protobuf-net .NET端口: protobuf-net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM