一、前言
  《码神联盟》是一款为技术人做的开源情怀游戏,每一种编程语言都是一位英雄。客户端和服务端均使用C#开发,客户端使用Unity3D引擎,数据库使用MySQL。这个MOBA类游戏是笔者在学习时期和客户端美术策划的小伙伴一起做的游戏,笔者主要负责游戏服务端开发,客户端也参与了一部分,同时也是这个项目的发起和负责人。这次主要分享这款游戏的服务端相关的设计与实现,从整体的架构设计,到服务器网络通信底层的搭建,通信协议、模型定制,再到游戏逻辑的分层架构实现。同时这篇博客也沉淀了笔者在游戏公司实践五个月后对游戏架构与设计的重新审视与思考。

这款游戏自去年完成后笔者曾多次想写篇博客来分享,也曾多次停笔,只因总觉得灵感还不够积淀还不够思考还不够,现在终于可以跨过这一步和大家分享,希望可以带来的是干货与诚意满满。由于目前关于游戏服务端相关的介绍文章少之又少,而为数不多的几篇也都是站在游戏服务端发展历史和架构的角度上进行分享,很少涉及具体的实现,这篇文章我将尝试多从实现的层面上加以介绍,所附的代码均有详尽注释,篇幅较长,可以关注收藏后再看。学习时期做的项目可能无法达到工业级,参考了github上开源的C#网络框架,笔者在和小伙伴做这款游戏时农药还没有现在这般火。 : )

二、服务器架构

上图为这款游戏的服务器架构和主要逻辑流程图,笔者将游戏的代码实现分为三个主要模块:Protocol通信协议、NetFrame服务器网络通信底层的搭建以及LOLServer游戏的具体逻辑分层架构实现,下面将针对每个模块进行分别介绍。

三、通信协议
  

先从最简单也最基本的通信协议部分说起,我们可以看到这部分代码主要分为xxxProtocol、xxxDTO和xxxModel、以及xxxData四种类型,让我们来对它们的作用一探究竟。

1.Protocol协议
LOLServer\Protocol\Protocol.cs

using System;
using System.Collections.Generic;
using System.Text;namespace GameProtocol
{public class Protocol{public const byte TYPE_LOGIN = 0;//登录模块public const byte TYPE_USER = 1;//用户模块public const byte TYPE_MATCH = 2;//战斗匹配模块public const byte TYPE_SELECT = 3;//战斗选人模块public const byte TYPE_FIGHT = 4;//战斗模块}
}

从上述的代码举例可以看到,在Protocol协议部分,我们主要是定义了一些常量用于模块通信,在这个部分分别定义了用户协议、登录协议、战斗匹配协议、战斗选人协议以及战斗协议。

2.DTO数据传输对象
  DTO即数据传输对象,表现层与应用层之间是通过数据传输对象(DTO)进行交互的,需要了解的是,数据传输对象DTO本身并不是业务对象。数据传输对象是根据UI的需求进行设计的,而不是根据领域对象进行设计的。比如,User领域对象可能会包含一些诸如name, level, exp, email等信息。但如果UI上不打算显示email的信息,那么UserDTO中也无需包含这个email的数据。

简单来说Model面向业务,我们是通过业务来定义Model的。而DTO是面向界面UI,是通过UI的需求来定义的。通过DTO我们实现了表现层与Model之间的解耦,表现层不引用Model,如果开发过程中我们的模型改变了,而界面没变,我们就只需要改Model而不需要去改表现层中的东西。

using System;
using System.Collections.Generic;
using System.Text;namespace GameProtocol.dto
{[Serializable]public class UserDTO{public int id;//玩家ID 唯一主键public string name;//玩家昵称public int level;//玩家等级public int exp;//玩家经验public int winCount;//胜利场次public int loseCount;//失败场次public int ranCount;//逃跑场次public int[] heroList;//玩家拥有的英雄列表public UserDTO() { }public UserDTO(string name, int id, int level, int win, int lose, int ran,int[] heroList){this.id = id;this.name = name;this.winCount = win;this.loseCount = lose;this.ranCount = ran;this.level = level;this.heroList = heroList;}}
}

3.Data属性配置表
  这部分的实现主要是为了将程序功能与属性配置分离,后面可以由策划来配置这部分内容,由导表工具自动生成配表,从而减轻程序的开发工作量,扩展游戏的功能。

using System;
using System.Collections.Generic;
using System.Text;namespace GameProtocol.constans
{/// <summary>/// 英雄属性配置表/// </summary>public class HeroData{public static readonly Dictionary<int, HeroDataModel> heroMap = new Dictionary<int, HeroDataModel>();/// <summary>/// 静态构造 初次访问的时候自动调用/// </summary>static HeroData() {create(1, "西嘉迦[C++]", 100, 20, 500, 300, 5, 2, 30, 10, 1, 0.5f, 200,200, 1, 2, 3, 4);create(2, "派森[Python]", 100, 20, 500, 300, 5, 2, 30, 10, 1, 0.5f, 200, 200, 1, 2, 3, 4);create(3, "扎瓦[Java]", 100, 20, 500, 300, 5, 2, 30, 10, 1, 0.5f, 200, 200, 6, 2, 3, 4);create(4, "琵欸赤貔[PHP]", 100, 20, 500, 300, 5, 2, 30, 10, 1, 0.5f, 200, 200, 3, 2, 3, 4);}/// <summary>/// 创建模型并添加进字典/// </summary>/// <param name="code"></param>/// <param name="name"></param>/// <param name="atkBase"></param>/// <param name="defBase"></param>/// <param name="hpBase"></param>/// <param name="mpBase"></param>/// <param name="atkArr"></param>/// <param name="defArr"></param>/// <param name="hpArr"></param>/// <param name="mpArr"></param>/// <param name="speed"></param>/// <param name="aSpeed"></param>/// <param name="range"></param>/// <param name="eyeRange"></param>/// <param name="skills"></param>private static void create(int code,string name,int  atkBase,int  defBase,int  hpBase,int  mpBase,int  atkArr,int  defArr,int  hpArr,int  mpArr,float speed,float aSpeed,float range,float eyeRange,params int[] skills) {HeroDataModel model = new HeroDataModel();model.code = code;model.name = name;model.atkBase = atkBase;model.defBase = defBase;model.hpBase = hpBase;model.mpBase = mpBase;model.atkArr = atkArr;model.defArr = defArr;model.hpArr = hpArr;model.mpArr = mpArr;model.speed = speed;model.aSpeed = aSpeed;model.range = range;model.eyeRange = eyeRange;model.skills = skills;heroMap.Add(code, model);}}public partial class HeroDataModel{public int code;//策划定义的唯一编号public string name;//英雄名称public int atkBase;//初始(基础)攻击力public int defBase;//初始防御public int hpBase;//初始血量public int mpBase;//初始蓝public int atkArr;//攻击成长public int defArr;//防御成长public int hpArr;//血量成长public int mpArr;//蓝成长public float speed;//移动速度public float aSpeed;//攻击速度public float range;//攻击距离public float eyeRange;//视野范围public int[] skills;//拥有技能}}

四、服务器通信底层搭建
  这部分为服务器的网络通信底层实现,也是游戏服务器的核心内容,下面将结合具体的代码以及代码注释一一介绍底层的实现,可能会涉及到一些C#的网络编程知识,对C#语言不熟悉没关系,笔者对C#的运用也仅仅停留在使用阶段,只需通过C#这门简单易懂的语言来窥探整个服务器通信底层搭建起来的过程,来到我们的NetFrame网络通信框架,这部分干货很多,我将用完整的代码和详尽的注释来阐明其意。

1.四层Socket模型

  将SocketModel分为了四个层级,分别为:

(1)type:一级协议 用于区分所属模块,如用户模块
  (2)area:二级协议 用于区分模块下的所属子模块,如用户模块的子模块为道具模块1、装备模块2、技能模块3等
  (3)command:三级协议 用于区分当前处理逻辑功能,如道具模块的逻辑功能有“使用(申请/结果),丢弃,获得”等,技能模块的逻辑功能有“学习,升级,遗忘”等;
  (4)message:消息体 当前需要处理的主体数据,如技能书

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NetFrame.auto
{public class SocketModel{/// <summary>/// 一级协议 用于区分所属模块/// </summary>public byte type {get;set;}/// <summary>/// 二级协议 用于区分 模块下所属子模块/// </summary>public int area { get; set; }/// <summary>/// 三级协议  用于区分当前处理逻辑功能/// </summary>public int command { get; set; }/// <summary>/// 消息体 当前需要处理的主体数据/// </summary>public object message { get; set; }public SocketModel() { }public SocketModel(byte t,int a,int c,object o) {this.type = t;this.area = a;this.command = c;this.message = o;}public T GetMessage<T>() {return (T)message;}}
}

同时封装了一个消息封装的方法,收到消息的处理流程如图所示:

2.对象序列化与反序列化为对象  
  序列化: 将数据结构或对象转换成二进制串的过程。

反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;namespace NetFrame
{public class SerializeUtil{/// <summary>/// 对象序列化/// </summary>/// <param name="value"></param>/// <returns></returns>public static byte[] encode(object value) {MemoryStream ms = new MemoryStream();//创建编码解码的内存流对象BinaryFormatter bw = new BinaryFormatter();//二进制流序列化对象//将obj对象序列化成二进制数据 写入到 内存流bw.Serialize(ms, value);byte[] result=new byte[ms.Length];//将流数据 拷贝到结果数组Buffer.BlockCopy(ms.GetBuffer(), 0, result, 0, (int)ms.Length);ms.Close();return result;}/// <summary>/// 反序列化为对象/// </summary>/// <param name="value"></param>/// <returns></returns>public static object decode(byte[] value) {MemoryStream ms = new MemoryStream(value);//创建编码解码的内存流对象 并将需要反序列化的数据写入其中BinaryFormatter bw = new BinaryFormatter();//二进制流序列化对象//将流数据反序列化为obj对象object result= bw.Deserialize(ms);ms.Close();return result;}}
}

3.消息体序列化与反序列化
  相应的,我们利用上面写好的序列化和反序列化方法将我们再Socket模型中定义的message消息体进行序列化与反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NetFrame.auto
{public class MessageEncoding{/// <summary>/// 消息体序列化/// </summary>/// <param name="value"></param>/// <returns></returns>public static byte[] encode(object value) {SocketModel model = value as SocketModel;ByteArray ba = new ByteArray();ba.write(model.type);ba.write(model.area);ba.write(model.command);//判断消息体是否为空  不为空则序列化后写入if (model.message != null){ba.write(SerializeUtil.encode(model.message));}byte[] result = ba.getBuff();ba.Close();return result;}/// <summary>/// 消息体反序列化/// </summary>/// <param name="value"></param>/// <returns></returns>public static object decode(byte[] value){ByteArray ba = new ByteArray(value);SocketModel model = new SocketModel();byte type;int area;int command;//从数据中读取 三层协议  读取数据顺序必须和写入顺序保持一致ba.read(out type);ba.read(out area);ba.read(out command);model.type = type;model.area = area;model.command = command;//判断读取完协议后 是否还有数据需要读取 是则说明有消息体 进行消息体读取if (ba.Readnable) {byte[] message;//将剩余数据全部读取出来ba.read(out message, ba.Length - ba.Position);//反序列化剩余数据为消息体model.message = SerializeUtil.decode(message);}ba.Close();return model;}}
}

4.将数据写入成二进制

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;namespace NetFrame
{/// <summary>/// 将数据写入成二进制/// </summary>public class ByteArray{MemoryStream ms = new MemoryStream();BinaryWriter bw;BinaryReader br;public void Close() {bw.Close();br.Close();ms.Close();}/// <summary>/// 支持传入初始数据的构造/// </summary>/// <param name="buff"></param>public ByteArray(byte[] buff) {ms = new MemoryStream(buff);bw = new BinaryWriter(ms);br = new BinaryReader(ms);}/// <summary>/// 获取当前数据 读取到的下标位置/// </summary>public int Position {get { return (int)ms.Position; }}/// <summary>/// 获取当前数据长度/// </summary>public int Length{get { return (int)ms.Length; }}/// <summary>/// 当前是否还有数据可以读取/// </summary>public bool Readnable{get { return ms.Length > ms.Position; }}/// <summary>/// 默认构造/// </summary>public ByteArray() {bw = new BinaryWriter(ms);br = new BinaryReader(ms);}public void write(int value) {bw.Write(value);}public void write(byte value){bw.Write(value);}public void write(bool value){bw.Write(value);}public void write(string value){bw.Write(value);}public void write(byte[] value){bw.Write(value);}public void write(double value){bw.Write(value);}public void write(float value){bw.Write(value);}public void write(long value){bw.Write(value);}public void read(out int value){value= br.ReadInt32();}public void read(out byte value){value = br.ReadByte();}public void read(out bool value){value = br.ReadBoolean();}public void read(out string value){value = br.ReadString();}public void read(out byte[] value,int length){value = br.ReadBytes(length);}public void read(out double value){value = br.ReadDouble();}public void read(out float value){value = br.ReadSingle();}public void read(out long value){value = br.ReadInt64();}public void reposition() {ms.Position = 0;}/// <summary>/// 获取数据/// </summary>/// <returns></returns>public byte[] getBuff(){byte[] result = new byte[ms.Length];Buffer.BlockCopy(ms.GetBuffer(), 0, result, 0, (int)ms.Length);return result;}}
}

5.粘包长度编码与解码
   粘包出现原因:在流传输中出现(UDP不会出现粘包,因为它有消息边界)
   1 发送端需要等缓冲区满才发送出去,造成粘包
   2 接收方不及时接收缓冲区的包,造成多个包接收

所以这里我们需要对粘包长度进行编码与解码,具体的代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NetFrame.auto
{public class LengthEncoding{/// <summary>/// 粘包长度编码/// </summary>/// <param name="buff"></param>/// <returns></returns>public static byte[] encode(byte[] buff) {MemoryStream ms = new MemoryStream();//创建内存流对象BinaryWriter sw = new BinaryWriter(ms);//写入二进制对象流//写入消息长度sw.Write(buff.Length);//写入消息体sw.Write(buff);byte[] result = new byte[ms.Length];Buffer.BlockCopy(ms.GetBuffer(), 0, result, 0, (int)ms.Length);sw.Close();ms.Close();return result;}/// <summary>/// 粘包长度解码/// </summary>/// <param name="cache"></param>/// <returns></returns>public static byte[] decode(ref List<byte> cache) {if (cache.Count < 4) return null;MemoryStream ms = new MemoryStream(cache.ToArray());//创建内存流对象,并将缓存数据写入进去BinaryReader br = new BinaryReader(ms);//二进制读取流int length = br.ReadInt32();//从缓存中读取int型消息体长度//如果消息体长度 大于缓存中数据长度 说明消息没有读取完 等待下次消息到达后再次处理if (length > ms.Length - ms.Position) {return null;}//读取正确长度的数据byte[] result = br.ReadBytes(length);//清空缓存cache.Clear();//将读取后的剩余数据写入缓存cache.AddRange(br.ReadBytes((int)(ms.Length - ms.Position)));br.Close();ms.Close();return result;}}
}

6.delegate委托声明
   delegate 是表示对具有特定参数列表和返回类型的方法的引用的类型。 在实例化委托时,可以将其实例与任何具有兼容签名和返回类型的方法相关联。通过委托实例调用方法。委托相当于将方法作为参数传递给其他方法,类似于 C++ 函数指针,但它们是类型安全的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NetFrame
{public delegate byte[] LengthEncode(byte[] value);public delegate byte[] LengthDecode(ref List<byte> value);public delegate byte[] encode(object value);public delegate object decode(byte[] value);
}

7.用户连接对象UserToken
SocketAsyncEventArgs介绍
  SocketAsyncEventArgs是微软提供的高性能异步Socket实现类,主要为高性能网络服务器应用程序而设计,主要是为了避免在在异步套接字 I/O 量非常大时发生重复的对象分配和同步。使用此类执行异步套接字操作的模式包含以下步骤:
  (1)分配一个新的 SocketAsyncEventArgs 上下文对象,或者从应用程序池中获取一个空闲的此类对象。
  (2)将该上下文对象的属性设置为要执行的操作(例如,完成回调方法、数据缓冲区、缓冲区偏移量以及要传输的最大数据量)。
  (3)调用适当的套接字方法 (xxxAsync) 以启动异步操作。
  (4)如果异步套接字方法 (xxxAsync) 返回 true,则在回调中查询上下文属性来获取完成状态。
  (5)如果异步套接字方法 (xxxAsync) 返回 false,则说明操作是同步完成的。可以查询上下文属性来获取操作结果。
  (6)将该上下文重用于另一个操作,将它放回到应用程序池中,或者将它丢弃。

SocketAsyncEventArgs.UserToken 属性
  获取或设置与此异步套接字操作关联的用户或应用程序对象。

命名空间: System.Net.Sockets

public object UserToken { get; set; }
  备注:

此属性可以由应用程序相关联的应用程序状态对象与 SocketAsyncEventArgs 对象。 首先,此属性是一种将状态传递到应用程序的事件处理程序(例如,异步操作完成方法)的应用程序的方法。

此属性用于所有异步套接字 (xxxAsync) 方法。

UserToken类的完整实现代码如下,可以结合代码注释加以理解:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;namespace NetFrame
{/// <summary>/// 用户连接信息对象/// </summary>public class UserToken{/// <summary>/// 用户连接/// </summary>public Socket conn;//用户异步接收网络数据对象public SocketAsyncEventArgs receiveSAEA;//用户异步发送网络数据对象public SocketAsyncEventArgs sendSAEA;public LengthEncode LE;public LengthDecode LD;public encode encode;public decode decode;public delegate void SendProcess(SocketAsyncEventArgs e);public SendProcess sendProcess;public delegate void CloseProcess(UserToken token, string error);public CloseProcess closeProcess;public AbsHandlerCenter center;List<byte> cache = new List<byte>();private bool isReading = false;private bool isWriting = false;Queue<byte[]> writeQueue = new Queue<byte[]>();public UserToken() {receiveSAEA = new SocketAsyncEventArgs();sendSAEA = new SocketAsyncEventArgs();receiveSAEA.UserToken = this;sendSAEA.UserToken = this;//设置接收对象的缓冲区大小receiveSAEA.SetBuffer(new byte[1024], 0, 1024);}//网络消息到达public void receive(byte[] buff) {//将消息写入缓存cache.AddRange(buff);if (!isReading){isReading = true;onData();}}//缓存中有数据处理void onData() {//解码消息存储对象byte[] buff = null;//当粘包解码器存在的时候 进行粘包处理if (LD != null){buff = LD(ref cache);//消息未接收全 退出数据处理 等待下次消息到达if (buff == null) { isReading = false; return; }}else {//缓存区中没有数据 直接跳出数据处理 等待下次消息到达if (cache.Count == 0) { isReading = false; return; }buff = cache.ToArray();cache.Clear();}//反序列化方法是否存在if (decode == null) { throw new Exception("message decode process is null"); }//进行消息反序列化object message = decode(buff);//TODO 通知应用层 有消息到达center.MessageReceive(this, message);//尾递归 防止在消息处理过程中 有其他消息到达而没有经过处理onData();}public void write(byte[] value) {if (conn == null) {//此连接已经断开了closeProcess(this, "调用已经断开的连接");return;}writeQueue.Enqueue(value);if (!isWriting) {isWriting = true;onWrite();}}public void onWrite() {//判断发送消息队列是否有消息if (writeQueue.Count == 0) { isWriting = false; return; }//取出第一条待发消息byte[] buff = writeQueue.Dequeue();//设置消息发送异步对象的发送数据缓冲区数据sendSAEA.SetBuffer(buff, 0, buff.Length);//开启异步发送bool result = conn.SendAsync(sendSAEA);//是否挂起if (!result) {sendProcess(sendSAEA);}}public void writed() {//与onData尾递归同理onWrite();}public void Close() {try{writeQueue.Clear();cache.Clear();isReading = false;isWriting = false;conn.Shutdown(SocketShutdown.Both);conn.Close();conn = null;}catch (Exception e) {Console.WriteLine(e.Message);}}}
}

8.连接池UserTokenPool

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NetFrame
{public class UserTokenPool{private Stack<UserToken> pool;public UserTokenPool(int max) {pool = new Stack<UserToken>(max);}/// <summary>/// 取出一个连接对象 --创建连接/// </summary>public UserToken pop() {return pool.Pop();}//插入一个连接对象---释放连接public void push(UserToken token) {if (token != null)pool.Push(token);}public int Size {get { return pool.Count; } }}
}

9.抽象处理中心AbsHandlerCenter
  在这里我们定义了客户端连接、收到客户端消息和客户端断开连接的抽象类,标记为抽象或包含在抽象类中的成员必须通过从抽象类派生的类来实现。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace NetFrame
{public abstract class AbsHandlerCenter{/// <summary>/// 客户端连接/// </summary>/// <param name="token">连接的客户端对象</param>public abstract void ClientConnect(UserToken token);/// <summary>/// 收到客户端消息/// </summary>/// <param name="token">发送消息的客户端对象</param>/// <param name="message">消息内容</param>public abstract void MessageReceive(UserToken token, object message);/// <summary>/// 客户端断开连接/// </summary>/// <param name="token">断开的客户端对象</param>/// <param name="error">断开的错误信息</param>public abstract void ClientClose(UserToken token, string error);}
}

10.HandlerCenter实现类
   接下来具体实现客户端连接、断开连接以及收到消息后的协议分发到具体的逻辑处理模块,代码如下:

using GameProtocol;
using LOLServer.logic;
using LOLServer.logic.fight;
using LOLServer.logic.login;
using LOLServer.logic.match;
using LOLServer.logic.select;
using LOLServer.logic.user;
using NetFrame;
using NetFrame.auto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LOLServer
{public class HandlerCenter:AbsHandlerCenter{HandlerInterface login;HandlerInterface user;HandlerInterface match;HandlerInterface select;HandlerInterface fight;public HandlerCenter() {login = new LoginHandler();user = new UserHandler();match = new MatchHandler();select = new SelectHandler();fight = new FightHandler();}public override void ClientClose(UserToken token, string error){Console.WriteLine("有客户端断开连接了");select.ClientClose(token, error);match.ClientClose(token, error);fight.ClientClose(token, error);//user的连接关闭方法 一定要放在逻辑处理单元后面//其他逻辑单元需要通过user绑定数据来进行内存清理 //如果先清除了绑定关系 其他模块无法获取角色数据会导致无法清理user.ClientClose(token, error);login.ClientClose(token, error);}public override void ClientConnect(UserToken token){Console.WriteLine("有客户端连接了");}public override void MessageReceive(UserToken token, object message){SocketModel model = message as SocketModel;switch (model.type) { case Protocol.TYPE_LOGIN:login.MessageReceive(token, model);break;case Protocol.TYPE_USER:user.MessageReceive(token, model);break;case Protocol.TYPE_MATCH:match.MessageReceive(token, model);break;case Protocol.TYPE_SELECT:select.MessageReceive(token, model);break;case Protocol.TYPE_FIGHT:fight.MessageReceive(token, model);break;default://未知模块  可能是客户端作弊了 无视break;}}}
}

11.启动服务器
启动服务器->监听IP(可选)->监听端口,服务器处理流程如下图:

让我们来具体看看代码实现,均给了详细的注释:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace NetFrame
{public class ServerStart{Socket server;//服务器socket监听对象int maxClient;//最大客户端连接数Semaphore acceptClients;UserTokenPool pool;public LengthEncode LE;public LengthDecode LD;public encode encode;public decode decode;/// <summary>/// 消息处理中心,由外部应用传入/// </summary>public AbsHandlerCenter center;/// <summary>/// 初始化通信监听/// </summary>/// <param name="port">监听端口</param>public ServerStart(int max) {//实例化监听对象server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//设定服务器最大连接人数maxClient = max;}public void Start(int port) {//创建连接池pool = new UserTokenPool(maxClient);//连接信号量acceptClients = new Semaphore(maxClient, maxClient);for (int i = 0; i < maxClient; i++){UserToken token = new UserToken();//初始化token信息               token.receiveSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Comleted);token.sendSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Comleted);token.LD = LD;token.LE = LE;token.encode = encode;token.decode = decode;token.sendProcess = ProcessSend;token.closeProcess = ClientClose;token.center = center;pool.push(token);}//监听当前服务器网卡所有可用IP地址的port端口// 外网IP  内网IP192.168.x.x 本机IP一个127.0.0.1try{server.Bind(new IPEndPoint(IPAddress.Any, port));//置于监听状态server.Listen(10);StartAccept(null);}catch (Exception e){Console.WriteLine(e.Message);}}/// <summary>/// 开始客户端连接监听/// </summary>public void StartAccept(SocketAsyncEventArgs e) {//如果当前传入为空  说明调用新的客户端连接监听事件 否则的话 移除当前客户端连接if (e == null){e = new SocketAsyncEventArgs();e.Completed += new EventHandler<SocketAsyncEventArgs>(Accept_Comleted);}else {e.AcceptSocket = null;}//信号量-1acceptClients.WaitOne();bool result= server.AcceptAsync(e);//判断异步事件是否挂起  没挂起说明立刻执行完成  直接处理事件 否则会在处理完成后触发Accept_Comleted事件if (!result) {ProcessAccept(e);}}public void ProcessAccept(SocketAsyncEventArgs e) {//从连接对象池取出连接对象 供新用户使用UserToken token = pool.pop();token.conn = e.AcceptSocket;//TODO 通知应用层 有客户端连接center.ClientConnect(token);//开启消息到达监听StartReceive(token);//释放当前异步对象StartAccept(e);}public void Accept_Comleted(object sender, SocketAsyncEventArgs e) {ProcessAccept(e);}public void StartReceive(UserToken token) {try{//用户连接对象 开启异步数据接收bool result = token.conn.ReceiveAsync(token.receiveSAEA);//异步事件是否挂起if (!result){ProcessReceive(token.receiveSAEA);}}catch (Exception e) {Console.WriteLine(e.Message);}}public void IO_Comleted(object sender, SocketAsyncEventArgs e){if (e.LastOperation == SocketAsyncOperation.Receive){ProcessReceive(e);}else {ProcessSend(e);}}public void ProcessReceive(SocketAsyncEventArgs e) {UserToken token= e.UserToken as UserToken;//判断网络消息接收是否成功if (token.receiveSAEA.BytesTransferred > 0 && token.receiveSAEA.SocketError == SocketError.Success){byte[] message = new byte[token.receiveSAEA.BytesTransferred];//将网络消息拷贝到自定义数组Buffer.BlockCopy(token.receiveSAEA.Buffer, 0, message, 0, token.receiveSAEA.BytesTransferred);//处理接收到的消息token.receive(message);StartReceive(token);}else {if (token.receiveSAEA.SocketError != SocketError.Success){ClientClose(token, token.receiveSAEA.SocketError.ToString());}else {ClientClose(token, "客户端主动断开连接");}}}public void ProcessSend(SocketAsyncEventArgs e) {UserToken token = e.UserToken as UserToken;if (e.SocketError != SocketError.Success){ClientClose(token, e.SocketError.ToString());}else { //消息发送成功,回调成功token.writed();}}/// <summary>/// 客户端断开连接/// </summary>/// <param name="token"> 断开连接的用户对象</param>/// <param name="error">断开连接的错误编码</param>public void ClientClose(UserToken token,string error) {if (token.conn != null) {lock (token) { //通知应用层面 客户端断开连接了center.ClientClose(token, error);token.Close();//加回一个信号量,供其它用户使用pool.push(token);acceptClients.Release();                   }}}}
}

至此,服务器的通信底层已经搭建完毕,可以进一步进行具体的游戏逻辑玩法开发了。

五、游戏服务端逻辑分层实现

  逻辑处理主要分层架构如下:

(1)logic逻辑层:逻辑处理模块,异步的逻辑处理,登录、用户处理、匹配、选人、战斗的主要逻辑都在这里,Moba类游戏是典型的房间服务器架构,AbsOnceHandler用于单体消息发送的处理,AbsMulitHandler用于群发;

AbsOnceHandler代码如下:

using LOLServer.biz;
using LOLServer.dao.model;
using NetFrame;
using NetFrame.auto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LOLServer.logic
{public class AbsOnceHandler{public IUserBiz userBiz = BizFactory.userBiz;private byte type;private int area;public void SetArea(int area) {this.area = area;}public virtual int GetArea() {return area;}public void SetType(byte type){this.type = type;}public new virtual byte GetType(){return type;}/// <summary>/// 通过连接对象获取用户/// </summary>/// <param name="token"></param>/// <returns></returns>public USER getUser(UserToken token){return userBiz.get(token);}/// <summary>/// 通过ID获取用户/// </summary>/// <param name="token"></param>/// <returns></returns>public USER getUser(int id){return userBiz.get(id);}/// <summary>/// 通过连接对象 获取用户ID/// </summary>/// <param name="token"></param>/// <returns></returns>public int getUserId(UserToken token){USER user = getUser(token);if(user==null)return -1;return user.id;}/// <summary>/// 通过用户ID获取连接/// </summary>/// <param name="id"></param>/// <returns></returns>public UserToken getToken(int id) {return userBiz.getToken(id);}#region 通过连接对象发送public void write(UserToken token,int command) {write(token, command, null);}public void write(UserToken token, int command,object message){write(token,GetArea(), command, message);}public void write(UserToken token,int area, int command, object message){write(token,GetType(), GetArea(), command, message);}public void write(UserToken token,byte type, int area, int command, object message){byte[] value = MessageEncoding.encode(CreateSocketModel(type,area,command,message));value = LengthEncoding.encode(value);token.write(value);}#endregion#region 通过ID发送public void write(int id, int command){write(id, command, null);}public void write(int id, int command, object message){write(id, GetArea(), command, message);}public void write(int id, int area, int command, object message){write(id, GetType(), area, command, message);}public void write(int id, byte type, int area, int command, object message){UserToken token= getToken(id);if(token==null)return;write(token, type, area, command, message);}public void writeToUsers(int[] users, byte type, int area, int command, object message) {byte[] value = MessageEncoding.encode(CreateSocketModel(type, area, command, message));value = LengthEncoding.encode(value);foreach (int item in users){UserToken token = userBiz.getToken(item);if (token == null) continue;byte[] bs = new byte[value.Length];Array.Copy(value, 0, bs, 0, value.Length);token.write(bs);}}#endregionpublic SocketModel CreateSocketModel(byte type, int area, int command, object message){return new SocketModel(type, area, command, message);}}
}

AbsMulitHandler继承自AbsOnceHandler,实现代码如下:

using NetFrame;
using NetFrame.auto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LOLServer.logic
{public class AbsMulitHandler:AbsOnceHandler{public List<UserToken> list = new List<UserToken>();/// <summary>/// 用户进入当前子模块/// </summary>/// <param name="token"></param>/// <returns></returns>public bool enter(UserToken token) {if (list.Contains(token)) {return false;}list.Add(token);return true;}/// <summary>/// 用户是否在此子模块/// </summary>/// <param name="token"></param>/// <returns></returns>public bool isEntered(UserToken token) {return list.Contains(token);}/// <summary>/// 用户离开当前子模块/// </summary>/// <param name="token"></param>/// <returns></returns>public bool leave(UserToken token) {if (list.Contains(token)) {list.Remove(token);return true;}return false;}#region 消息群发APIpublic void brocast(int command, object message,UserToken exToken=null) {brocast(GetArea(), command, message, exToken);}public void brocast(int area, int command, object message, UserToken exToken = null){brocast(GetType(), area, command, message, exToken);}public void brocast(byte type, int area, int command, object message, UserToken exToken = null){byte[] value = MessageEncoding.encode(CreateSocketModel(type, area, command, message));value = LengthEncoding.encode(value);foreach (UserToken item in list){if (item != exToken){byte[] bs = new byte[value.Length];Array.Copy(value, 0, bs, 0, value.Length);item.write(bs);}}}#endregion}
}

(2)biz事务层:事务处理,保证数据安全的逻辑处理,如账号、用户信息相关的处理,impl是相关的实现类;
  (3)cache缓存层:读取数据库中的内容放在内存中,加快访问速度;
  (4)dao数据层:服务器和数据库之间的中间件;
  (5)工具类:一些实用的工具类放在这里,如定时任务列表,用来实现游戏中的刷怪,buff等;

逻辑处理流程如下:

六、优化思路
  思考了一些优化思路,自文章发布后也收到了许多来自朋友圈或留言评论中大神们给出的优化思路,大多数建议都质量很高,极具参考价值和学习意义,大概这就是开源的魅力所在吧。现在把这些思路整理出来分享给大家:

(1)在原有架构基础上,可以进一步考虑下:协议的自动化生成,托管内存的gc消耗控制,更小的网络延迟和更大的网络并发;

(2)如果用上异步消息机制和Nosql 单服承载人数或许还能够上升一些,目前Nosql中MongoDB在游戏服务端中有较多应用,Redis是笔者个人很喜欢的一个开源Nosql数据库,也有一些游戏项目已经在尝试集成;

(3).net 自带的二进制序列化性能偏差,文章中代码里数据接收发送时的内存拷贝次数偏多,序列化可以尝试Google开源的protobuf,目前很多线上游戏都在应用;

(4)用.net framework其实就把服务器绑定到windows上了,同时mono性能堪忧,如果非要用c#的话,可以尝试.net core + docker ,网络库可以libuv ,这个方案不管是从扩展还是性能监控管理上都比windows要优秀许多,业界的游戏服务器也确实大多在Linux上部署;

(5)收发消息部分太复杂,使用现成的RPC框架性能、安全性会更好。

谈一款MOBA类游戏的服务端架构设计相关推荐

  1. 谈一款MOBA类游戏《码神联盟》的服务端架构设计与实现 (转载)

    原文链接 一.前言 <码神联盟>是一款为技术人做的开源情怀游戏,每一种编程语言都是一位英雄.客户端和服务端均使用C#开发,客户端使用,数据库使用MySQL.这个MOBA类游戏是笔者在学习时 ...

  2. 谈一款MOBA类游戏《码神联盟》的服务端架构设计与实现(更新优化思路)

    一.前言 <码神联盟>是一款为技术人做的开源情怀游戏,每一种编程语言都是一位英雄.客户端和服务端均使用C#开发,客户端使用Unity3D引擎,数据库使用MySQL.这个MOBA类游戏是笔者 ...

  3. 【网单服务端】团队索尼克赛车PC端赛车类游戏单机服务端

    [网单服务端]团队索尼克赛车PC端赛车类游戏单机服务端,应该有人喜欢这类游戏 下载链接:https://pan.baidu.com/s/1ds_xFq1Rd1_xC4515BRGXw  提取码:soh ...

  4. 各类游戏对应服务端架构

    卡牌.跑酷等弱交互服务端 卡牌跑酷类因为交互弱,玩家和玩家之间不需要实时面对面PK,打一下对方的离线数据,计算下排行榜,买卖下道具即可,所以实现往往使用简单的 HTTP服务器: 登录时可以使用非对称加 ...

  5. 游戏系统开发笔记(六)——服务端架构设计

    . http://blog.csdn.net/mooke/article/details/8913051 上回写了写服务端的分层结构,分层是比较宏观上的东西,至于层次间具体的交互方式还得通过各个模块的 ...

  6. 基于滴滴云的棋牌游戏服务端架构设计

    现在小团队开发的棋牌游戏有很多,棋牌行业的相互攻击是非常普遍的现象,同行之间往往会采取 DDOS.CC 等攻击的手段来打击对手,这是目前棋牌运营商们面临的比较严峻的一个问题,那么在设计棋牌游戏服务端架 ...

  7. 中小型手机棋牌网络游戏服务端架构设计(带源码)

    承接自己<中小型棋牌类网络游戏服务端架构>博文,用Golang实现基础架构逻辑后,准备再次谈谈我的想法. 已实现的逻辑与前文描述有几点不同: Gateway更名为Proxy,DBProxy ...

  8. 【大会】海量高清视频服务端架构设计的变与不变

    随着4K甚至更高分辨率的视频日渐普及,海量视频并发,对服务端的编码.转码的处理能力提出了前所未有的要求,传统的软件编码加速未必能够满足极端场景的要求,包括ASIC.GPU和FPGA方案你方唱罢我登场, ...

  9. 服务端架构设计及功能说明-续1

    上一篇文章我们讲述了一个基本的服务器架构图,并描述了这个架构图中每个服务器的功能.不知道看了上一篇文章的朋友有没有发现这个服务器架构图的问题.今天我们就来分析一下这个服务器架构中是否存在问题和不足的地 ...

最新文章

  1. DataPipeline联合Confluent Kafka Meetup上海站
  2. 干掉MessageBox,自定义弹出框JMessbox (WindowsPhone)
  3. 《Objective-c》-(@property和@synsthesize)
  4. JavaScriptSerializer序列化与反序列化--备忘
  5. 重温SQL——行转列,列转行(转:http://www.cnblogs.com/kerrycode/archive/2010/07/28/1786547.html)...
  6. python线性拟合、不确定性
  7. 南宁公交有两个应用付费通道,互不通用
  8. python pytorch tenser 索引 slice 切片
  9. 用计算机做设计,做平面设计一般电脑可以吗
  10. python拉格朗日插值法_Python 实现拉格朗日插值
  11. (黎活明老师讲学)Android学习(一)---从网络获取图片
  12. 云安全技术有什么特点?云安全包含哪些方面?
  13. 免费的ftp服务器 linux,免费ftp服务器,3款免费ftp服务器推荐
  14. ACM中的整数K拆分 (有条件限制 无条件限制 插板法 URAL-1036 HDU-6397)
  15. 1024程序员节:向改变世界的程序员致敬
  16. 谷歌浏览器linux,windows下载
  17. 2.4G NRF24L01无线模块总结
  18. Linux学习笔记(二十三) -- QT的安装和卸载
  19. libcurl入门之相关接口函数curl_easy_setopt
  20. python培训浦东

热门文章

  1. Lumerical学习2-使用脚本构建基础仿真结构
  2. python解释器的提示符号是什么_python解释器的提示符有什么用
  3. A+C双口快充协议IC:PD3.0,QC3.0,QC2.0,AFC,FCP,BC1.2,Apple2.4A
  4. Vue.js学习的第一天
  5. 【Pytorch】残差神经网络(Residual Networks)
  6. 状态值函数和动作值函数
  7. 在Windows10系统下安装SQL Server2000
  8. 如何在线上给证件打上水印
  9. jquery,bootstrap实现的用户名片信息提示
  10. 【炫酷战机Win7热门主题】