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.
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)
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. 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;
st开发者_如何转开发ring 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; 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. 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.
I have used my own communication protocol over TCP 2 years ago, about the base packet class:
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. 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.
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.
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.
protobuf might be what you want.
Here's the .NET port: protobuf-net
精彩评论