欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简单的游戏框架:网络方案

程序员文章站 2024-03-25 23:59:52
...

一个简单的游戏框架:网络方案

代码:https://github.com/HushengStudent/myGameFramework

1、C# socket编程:

使用socket,通过ip+端口,连接服务器和客户端,传递数据;

//client:
//创建Socket
_socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);

//连接服务器
_socket.BeginConnect(_ipAddress, _port, ConnectCallback, param);

//发送数据
_socket.BeginSend(buffer, offset, size, SocketFlags.None, SendCallback, param);

//server:
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 10000);
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(0);
serverSocket.BeginAccept(AcceptCallBack, serverSocket);


2、数据的序列化

数据序列化使用了Protobuff:

public static void Serialize<T>(Session session, MemoryStream destination, T packet) where T : Packet
        {
            byte[] idBytes = ConverterUtility.GetBytes(packet.GetPacketId());
            destination.Write(idBytes, 0, idBytes.Length);
            packet.Serialize(destination);
            ProtoRegister.ReturnPacket(packet);
        }

        public static Packet Deserialize(Session session, MemoryStream source, out object customErrorData)
        {
            customErrorData = null;
            long begin = source.Position;
            byte[] buffer = new byte[4];
            source.Read(buffer, 0, sizeof(int));
            int id = ConverterUtility.GetInt32(buffer);
            Packet packet = ProtoRegister.GetPacket(id);
            packet.DeSerialize(source);
            return packet;
        }


3、数据包设计

长度(int)+数据


4、协议设计

协议基类:

public abstract class Packet
    {
        //TODO:协议ID优化为UShort,减少数据;
        public abstract int GetPacketId();

        public abstract void Serialize(MemoryStream stream);
        public abstract void DeSerialize(MemoryStream stream);

        public abstract void Process();
    }

举个栗子,登录协议:

public class Packet_LoginRequest : Packet
{
    private LoginRequest data = new LoginRequest();

    public LoginRequest Data { get { return data; } }

    public override int GetPacketId()
    {
        return 10011;
    }

    public override void DeSerialize(MemoryStream stream)
    {
        data = ProtoBuf.Serializer.Deserialize<LoginRequest>(stream);
    }

    public override void Serialize(MemoryStream stream)
    {
        ProtoBuf.Serializer.Serialize<LoginRequest>(stream, data);
    }

    public override void Process()
    {
        Process_LoginRequest.Process(this);
    }
}

测试:

Packet_LoginRequest req = new Packet_LoginRequest();
        req.Data.id = 1001;
        req.Data.name = "HushengStudent";
        NetMgr.Instance.Send<Packet_LoginRequest>(req);
Packet_LoginResponse rsp = new Packet_LoginResponse();
            rsp.Data.id = 123;
            session.Send<Packet_LoginResponse>(rsp);
            Console.WriteLine(string.Format("send to client: {0} !",rsp.Data.id));

一个简单的游戏框架:网络方案

一个简单的游戏框架:网络方案